|  |  |  | @ -1309,59 +1309,80 @@ admin.initializeApp({ | 
			
		
	
		
			
				
					|  |  |  |  |   credential: admin.credential.cert(serviceAccount), | 
			
		
	
		
			
				
					|  |  |  |  | }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | const sendPushNotification = async (registrationToken, title, body) => { | 
			
		
	
		
			
				
					|  |  |  |  | const sendNotification = async (fcmTokens, title, body) => { | 
			
		
	
		
			
				
					|  |  |  |  |   if (!Array.isArray(fcmTokens) || fcmTokens.length === 0) { | 
			
		
	
		
			
				
					|  |  |  |  |     console.error('No FCM tokens provided.'); | 
			
		
	
		
			
				
					|  |  |  |  |     return; | 
			
		
	
		
			
				
					|  |  |  |  |   } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   const message = { | 
			
		
	
		
			
				
					|  |  |  |  |     tokens: fcmTokens,  | 
			
		
	
		
			
				
					|  |  |  |  |     notification: { | 
			
		
	
		
			
				
					|  |  |  |  |       title: title, | 
			
		
	
		
			
				
					|  |  |  |  |       body: body, | 
			
		
	
		
			
				
					|  |  |  |  |     }, | 
			
		
	
		
			
				
					|  |  |  |  |     data: { | 
			
		
	
		
			
				
					|  |  |  |  |       title: title, | 
			
		
	
		
			
				
					|  |  |  |  |       body: body, | 
			
		
	
		
			
				
					|  |  |  |  |     }, | 
			
		
	
		
			
				
					|  |  |  |  |   }; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   const options = { | 
			
		
	
		
			
				
					|  |  |  |  |     priority: "high", | 
			
		
	
		
			
				
					|  |  |  |  |     timeToLive: 60 * 60 * 24,  | 
			
		
	
		
			
				
					|  |  |  |  |   }; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   try { | 
			
		
	
		
			
				
					|  |  |  |  |     const response = await admin.messaging().sendToDevice(registrationToken, message, options); | 
			
		
	
		
			
				
					|  |  |  |  |     console.log('FCM response:', response); // Log the FCM response
 | 
			
		
	
		
			
				
					|  |  |  |  |     return response; // Return the FCM response object
 | 
			
		
	
		
			
				
					|  |  |  |  |     const response = await admin.messaging().sendMulticast(message); | 
			
		
	
		
			
				
					|  |  |  |  |     console.log('Notification sent successfully:', response); | 
			
		
	
		
			
				
					|  |  |  |  |   } catch (error) { | 
			
		
	
		
			
				
					|  |  |  |  |     console.error('FCM error:', error); | 
			
		
	
		
			
				
					|  |  |  |  |     throw error; // Throw the error to handle it further up the call stack
 | 
			
		
	
		
			
				
					|  |  |  |  |     console.error('Error sending notification:', error); | 
			
		
	
		
			
				
					|  |  |  |  |   } | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | // const sendPushNotification = async (registrationToken, title, body) => {
 | 
			
		
	
		
			
				
					|  |  |  |  | //   const message = {
 | 
			
		
	
		
			
				
					|  |  |  |  | //     notification: {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       title: title,
 | 
			
		
	
		
			
				
					|  |  |  |  | //       body: body,
 | 
			
		
	
		
			
				
					|  |  |  |  | //     },
 | 
			
		
	
		
			
				
					|  |  |  |  | //     data: {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       title: title,
 | 
			
		
	
		
			
				
					|  |  |  |  | //       body: body,
 | 
			
		
	
		
			
				
					|  |  |  |  | //     },
 | 
			
		
	
		
			
				
					|  |  |  |  | //   };
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //   const options = {
 | 
			
		
	
		
			
				
					|  |  |  |  | //     priority: "high",
 | 
			
		
	
		
			
				
					|  |  |  |  | //     timeToLive: 60 * 60 * 24, 
 | 
			
		
	
		
			
				
					|  |  |  |  | //   };
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //   try {
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const response = await admin.messaging().sendToDevice(registrationToken, message, options);
 | 
			
		
	
		
			
				
					|  |  |  |  | //     console.log('FCM response:', response); // Log the FCM response
 | 
			
		
	
		
			
				
					|  |  |  |  | //     return response; // Return the FCM response object
 | 
			
		
	
		
			
				
					|  |  |  |  | //   } catch (error) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //     console.error('FCM error:', error);
 | 
			
		
	
		
			
				
					|  |  |  |  | //     throw error; // Throw the error to handle it further up the call stack
 | 
			
		
	
		
			
				
					|  |  |  |  | //   }
 | 
			
		
	
		
			
				
					|  |  |  |  | // };
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | exports.motorAction = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |   try { | 
			
		
	
		
			
				
					|  |  |  |  |     const customerId = req.params.customerId; | 
			
		
	
		
			
				
					|  |  |  |  |     const action = req.body.action; | 
			
		
	
		
			
				
					|  |  |  |  |     const motorId = req.body.motor_id; | 
			
		
	
		
			
				
					|  |  |  |  |     const start_instance_id = req.body.start_instance_id | 
			
		
	
		
			
				
					|  |  |  |  |     console.log(req.body.startTime) | 
			
		
	
		
			
				
					|  |  |  |  |     // Ensure motor_id is provided
 | 
			
		
	
		
			
				
					|  |  |  |  |    // const fcmToken = req.body.fcmToken; // Ensure this is provided in the request
 | 
			
		
	
		
			
				
					|  |  |  |  |     const start_instance_id = req.body.start_instance_id; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (!motorId) { | 
			
		
	
		
			
				
					|  |  |  |  |       throw new Error("Motor ID is required."); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // Determine the motor stop status based on the action
 | 
			
		
	
		
			
				
					|  |  |  |  |     const users = await User.find({ customerId: customerId }); | 
			
		
	
		
			
				
					|  |  |  |  |     const fcmToken = users.map(user => user.fcmId).filter(fcmId => fcmId); | 
			
		
	
		
			
				
					|  |  |  |  |     console.log(fcmToken) | 
			
		
	
		
			
				
					|  |  |  |  |     let motorStopStatus; | 
			
		
	
		
			
				
					|  |  |  |  |     if (action === "start") { | 
			
		
	
		
			
				
					|  |  |  |  |       motorStopStatus = "2"; // If action is start, set stop status to "2"
 | 
			
		
	
		
			
				
					|  |  |  |  |       motorStopStatus = "2"; | 
			
		
	
		
			
				
					|  |  |  |  |     } else if (action === "stop") { | 
			
		
	
		
			
				
					|  |  |  |  |       motorStopStatus = "1"; // If action is stop, set stop status to "1"
 | 
			
		
	
		
			
				
					|  |  |  |  |       motorStopStatus = "1"; | 
			
		
	
		
			
				
					|  |  |  |  |     } else { | 
			
		
	
		
			
				
					|  |  |  |  |       throw new Error("Invalid action provided."); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // Update the motor stop status immediately if action is stop
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (action === "stop") { | 
			
		
	
		
			
				
					|  |  |  |  |       // Update the motor stop status and other fields
 | 
			
		
	
		
			
				
					|  |  |  |  |       await Tank.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |         { customerId, "connections.inputConnections.motor_id": motorId }, | 
			
		
	
		
			
				
					|  |  |  |  |         {  | 
			
		
	
	
		
			
				
					|  |  |  | @ -1375,14 +1396,16 @@ exports.motorAction = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |       ); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       // Send immediate response to the client
 | 
			
		
	
		
			
				
					|  |  |  |  |       reply.code(200).send({ message: "Motor stopped successfully." }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       // Perform stop operations in the background
 | 
			
		
	
		
			
				
					|  |  |  |  |       // Send stop notification
 | 
			
		
	
		
			
				
					|  |  |  |  |       if (fcmToken) { | 
			
		
	
		
			
				
					|  |  |  |  |         await sendNotification(fcmToken, 'Motor Stopped', `Motor ${motorId} has been stopped.`); | 
			
		
	
		
			
				
					|  |  |  |  |       } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       (async () => { | 
			
		
	
		
			
				
					|  |  |  |  |         await delay(300000); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         // Update the existing motor data entry with stop details
 | 
			
		
	
		
			
				
					|  |  |  |  |         const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); | 
			
		
	
		
			
				
					|  |  |  |  |         if (motorData) { | 
			
		
	
		
			
				
					|  |  |  |  |           const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }); | 
			
		
	
	
		
			
				
					|  |  |  | @ -1405,67 +1428,42 @@ exports.motorAction = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |               } | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |           ); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |           // Send high water level notification
 | 
			
		
	
		
			
				
					|  |  |  |  |           if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 >= 90) { | 
			
		
	
		
			
				
					|  |  |  |  |             if (fcmToken) { | 
			
		
	
		
			
				
					|  |  |  |  |               await sendNotification(fcmToken, 'High Water Level Alert', `Water level in tank ${motorData.receiverTank} has reached 90%.`); | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |           } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |       })(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       // Return here to ensure the rest of the code is not executed for the stop action
 | 
			
		
	
		
			
				
					|  |  |  |  |       return; | 
			
		
	
		
			
				
					|  |  |  |  |     } else { | 
			
		
	
		
			
				
					|  |  |  |  |       // Update the motor stop status to "2" for start action
 | 
			
		
	
		
			
				
					|  |  |  |  |       await Tank.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |         { customerId, "connections.inputConnections.motor_id": motorId }, | 
			
		
	
		
			
				
					|  |  |  |  |         { $set: { "connections.inputConnections.$.motor_stop_status": "2" } } | 
			
		
	
		
			
				
					|  |  |  |  |       ); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       // Send start notification
 | 
			
		
	
		
			
				
					|  |  |  |  |       if (fcmToken) { | 
			
		
	
		
			
				
					|  |  |  |  |         await sendNotification(fcmToken, 'Motor Started', `Motor ${motorId} has been started.`); | 
			
		
	
		
			
				
					|  |  |  |  |       } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // Check threshold settings if action is start
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (action === "start") { | 
			
		
	
		
			
				
					|  |  |  |  |       if (req.body.threshold_type === "time") { | 
			
		
	
		
			
				
					|  |  |  |  |         // If threshold type is time, update threshold time
 | 
			
		
	
		
			
				
					|  |  |  |  |         // await Tank.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  |         //   { customerId, "connections.inputConnections.motor_id": motorId },
 | 
			
		
	
		
			
				
					|  |  |  |  |         //   { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } }
 | 
			
		
	
		
			
				
					|  |  |  |  |         // );
 | 
			
		
	
		
			
				
					|  |  |  |  |         const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         const newMotorData = new MotorData({ | 
			
		
	
		
			
				
					|  |  |  |  |           customerId: customerId, | 
			
		
	
		
			
				
					|  |  |  |  |           motor_id: motorId, | 
			
		
	
		
			
				
					|  |  |  |  |           start_instance_id: start_instance_id, | 
			
		
	
		
			
				
					|  |  |  |  |           supplierTank: req.body.from, | 
			
		
	
		
			
				
					|  |  |  |  |           receiverTank: req.body.to, | 
			
		
	
		
			
				
					|  |  |  |  |           supplier_type: req.body.from_type, | 
			
		
	
		
			
				
					|  |  |  |  |           receiver_type: req.body.to_type, | 
			
		
	
		
			
				
					|  |  |  |  |           startTime: req.body.startTime, | 
			
		
	
		
			
				
					|  |  |  |  |           receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10) | 
			
		
	
		
			
				
					|  |  |  |  |         }); | 
			
		
	
		
			
				
					|  |  |  |  |         await newMotorData.save(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         for await (const tank of Tank.find({  "connections.inputConnections.motor_id": motorId })) { | 
			
		
	
		
			
				
					|  |  |  |  |           const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); | 
			
		
	
		
			
				
					|  |  |  |  |           if (index !== -1) { | 
			
		
	
		
			
				
					|  |  |  |  |             await Tank.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |               { customerId, "connections.inputConnections.motor_id": motorId }, | 
			
		
	
		
			
				
					|  |  |  |  |               { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime,[`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } } | 
			
		
	
		
			
				
					|  |  |  |  |             ); | 
			
		
	
		
			
				
					|  |  |  |  |           } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |          | 
			
		
	
		
			
				
					|  |  |  |  |         // (Existing code for time-based thresholds)
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         // Start monitoring water level based on threshold time
 | 
			
		
	
		
			
				
					|  |  |  |  |         const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); | 
			
		
	
		
			
				
					|  |  |  |  |         const intervalId = setInterval(async () => { | 
			
		
	
		
			
				
					|  |  |  |  |           const splr_tank_info3 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); | 
			
		
	
		
			
				
					|  |  |  |  |           const splr_tank_info3_waterlevel = parseInt(splr_tank_info3.waterlevel, 10); | 
			
		
	
		
			
				
					|  |  |  |  |           //console.log(splr_tank_info3_waterlevel,"splr_tank_info3_waterlevel")
 | 
			
		
	
		
			
				
					|  |  |  |  |           const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity.replace(/,/g, ''), 10); | 
			
		
	
		
			
				
					|  |  |  |  |          // const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  |          // console.log(splr_tank_info3.capacity,splr_tank_info3_capacity,"splr_tank_info3_capacity")
 | 
			
		
	
		
			
				
					|  |  |  |  |           const splr_tank_info3_percentage = (splr_tank_info3_waterlevel / splr_tank_info3_capacity) * 100; | 
			
		
	
		
			
				
					|  |  |  |  |          // console.log(splr_tank_info3_percentage, "percentage for less than 20");
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |           if (new Date() >= thresholdTime || splr_tank_info3_percentage <= 20) { | 
			
		
	
		
			
				
					|  |  |  |  |             console.log(splr_tank_info3_percentage,) | 
			
		
	
		
			
				
					|  |  |  |  |             await Tank.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |               { customerId, "connections.inputConnections.motor_id": motorId }, | 
			
		
	
		
			
				
					|  |  |  |  |               { | 
			
		
	
	
		
			
				
					|  |  |  | @ -1487,8 +1485,11 @@ exports.motorAction = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |               const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); | 
			
		
	
		
			
				
					|  |  |  |  |               const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); | 
			
		
	
		
			
				
					|  |  |  |  |               const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10); | 
			
		
	
		
			
				
					|  |  |  |  |               const totalwaterpumped = quantityDelivered + water_pumped_till_now | 
			
		
	
		
			
				
					|  |  |  |  |               await  Tank.findOneAndUpdate({customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase()}, { $set: { total_water_added_from_midnight: totalwaterpumped } }) | 
			
		
	
		
			
				
					|  |  |  |  |               const totalwaterpumped = quantityDelivered + water_pumped_till_now; | 
			
		
	
		
			
				
					|  |  |  |  |               await Tank.findOneAndUpdate( | 
			
		
	
		
			
				
					|  |  |  |  |                 { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }, | 
			
		
	
		
			
				
					|  |  |  |  |                 { $set: { total_water_added_from_midnight: totalwaterpumped } } | 
			
		
	
		
			
				
					|  |  |  |  |               ); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |               const stopTime = formatDate(new Date()); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -1496,88 +1497,43 @@ exports.motorAction = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |                 { customerId, motor_id: motorId, start_instance_id: start_instance_id }, | 
			
		
	
		
			
				
					|  |  |  |  |                 { | 
			
		
	
		
			
				
					|  |  |  |  |                   $set: { | 
			
		
	
		
			
				
					|  |  |  |  |                     stopTime:stopTime, | 
			
		
	
		
			
				
					|  |  |  |  |                     stopTime: stopTime, | 
			
		
	
		
			
				
					|  |  |  |  |                     receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), | 
			
		
	
		
			
				
					|  |  |  |  |                     quantity_delivered: quantityDelivered.toString() | 
			
		
	
		
			
				
					|  |  |  |  |                   } | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |               ); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |               // Send low water level notification
 | 
			
		
	
		
			
				
					|  |  |  |  |               if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) { | 
			
		
	
		
			
				
					|  |  |  |  |                 if (fcmToken) { | 
			
		
	
		
			
				
					|  |  |  |  |                   await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`); | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |               } | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |           } | 
			
		
	
		
			
				
					|  |  |  |  |         }, 60000); | 
			
		
	
		
			
				
					|  |  |  |  |       } else if (req.body.threshold_type === "litres") { | 
			
		
	
		
			
				
					|  |  |  |  |         console.log("entered litres") | 
			
		
	
		
			
				
					|  |  |  |  |         const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         const newMotorData = new MotorData({ | 
			
		
	
		
			
				
					|  |  |  |  |           customerId: customerId, | 
			
		
	
		
			
				
					|  |  |  |  |           motor_id: motorId, | 
			
		
	
		
			
				
					|  |  |  |  |           start_instance_id: start_instance_id, | 
			
		
	
		
			
				
					|  |  |  |  |           supplierTank: req.body.from, | 
			
		
	
		
			
				
					|  |  |  |  |           receiverTank: req.body.to, | 
			
		
	
		
			
				
					|  |  |  |  |           supplier_type: req.body.from_type, | 
			
		
	
		
			
				
					|  |  |  |  |           receiver_type: req.body.to_type, | 
			
		
	
		
			
				
					|  |  |  |  |           startTime: req.body.startTime, | 
			
		
	
		
			
				
					|  |  |  |  |           receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10) | 
			
		
	
		
			
				
					|  |  |  |  |         }); | 
			
		
	
		
			
				
					|  |  |  |  |         await newMotorData.save(); | 
			
		
	
		
			
				
					|  |  |  |  |         // If threshold type is percentage, calculate percentage threshold
 | 
			
		
	
		
			
				
					|  |  |  |  |         const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); | 
			
		
	
		
			
				
					|  |  |  |  |         const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); | 
			
		
	
		
			
				
					|  |  |  |  |         if (!receiver_tank_info) { | 
			
		
	
		
			
				
					|  |  |  |  |           throw new Error("Receiver tank not found."); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         if (!supplier_tank_info) { | 
			
		
	
		
			
				
					|  |  |  |  |           throw new Error("Supplierr tank not found."); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         const supplier_capacity =  parseInt(supplier_tank_info.capacity, 10); | 
			
		
	
		
			
				
					|  |  |  |  |         const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10);  | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         const capacity = parseInt(receiver_tank_info.capacity, 10); | 
			
		
	
		
			
				
					|  |  |  |  |         const waterLevel = parseInt(receiver_tank_info.waterlevel, 10);  | 
			
		
	
		
			
				
					|  |  |  |  |         const desired_percentage = parseInt(req.body.manual_threshold_litres.replace(/,/g, ''), 10); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         console.log(desired_percentage) | 
			
		
	
		
			
				
					|  |  |  |  |         const threshold_water_level = waterLevel+desired_percentage; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         const supplier_threshold = supplier_waterLevel-desired_percentage | 
			
		
	
		
			
				
					|  |  |  |  |         console.log(supplier_threshold,"supplier_threshold") | 
			
		
	
		
			
				
					|  |  |  |  |         // (Existing code for litres-based thresholds)
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         for await (const tank of Tank.find({  "connections.inputConnections.motor_id": motorId })) { | 
			
		
	
		
			
				
					|  |  |  |  |           const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); | 
			
		
	
		
			
				
					|  |  |  |  |           if (index !== -1) { | 
			
		
	
		
			
				
					|  |  |  |  |             await Tank.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |               { customerId, "connections.inputConnections.motor_id": motorId }, | 
			
		
	
		
			
				
					|  |  |  |  |               { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } | 
			
		
	
		
			
				
					|  |  |  |  |             ); | 
			
		
	
		
			
				
					|  |  |  |  |           } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |          | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         // Update water level threshold
 | 
			
		
	
		
			
				
					|  |  |  |  |         | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         // Start monitoring water level based on threshold percentage
 | 
			
		
	
		
			
				
					|  |  |  |  |         // Start monitoring water level based on litres
 | 
			
		
	
		
			
				
					|  |  |  |  |         const intervalId = setInterval(async () => { | 
			
		
	
		
			
				
					|  |  |  |  |           // Check if water level has reached the threshold percentage
 | 
			
		
	
		
			
				
					|  |  |  |  |           const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); | 
			
		
	
		
			
				
					|  |  |  |  |           const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10); | 
			
		
	
		
			
				
					|  |  |  |  |           if (current_water_level <= supplier_threshold) { | 
			
		
	
		
			
				
					|  |  |  |  |             // Stop the motor pump
 | 
			
		
	
		
			
				
					|  |  |  |  |             await Tank.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |               { customerId, "connections.inputConnections.motor_id": motorId }, | 
			
		
	
		
			
				
					|  |  |  |  |               {  | 
			
		
	
		
			
				
					|  |  |  |  |                 $set: {  | 
			
		
	
		
			
				
					|  |  |  |  |                   "connections.inputConnections.$.motor_stop_status": "1", | 
			
		
	
		
			
				
					|  |  |  |  |                  | 
			
		
	
		
			
				
					|  |  |  |  |                   "connections.inputConnections.$.threshold_type": null, | 
			
		
	
		
			
				
					|  |  |  |  |                   "connections.inputConnections.$.manual_threshold_time": null, | 
			
		
	
		
			
				
					|  |  |  |  |                   "connections.inputConnections.$.manual_threshold_percentage": null | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |               } | 
			
		
	
		
			
				
					|  |  |  |  |             ); | 
			
		
	
		
			
				
					|  |  |  |  |             clearInterval(intervalId); // Stop monitoring water level
 | 
			
		
	
		
			
				
					|  |  |  |  |             clearInterval(intervalId); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             await delay(300000); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); | 
			
		
	
	
		
			
				
					|  |  |  | @ -1586,34 +1542,315 @@ exports.motorAction = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |               const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10); | 
			
		
	
		
			
				
					|  |  |  |  |               const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |               const stopTime = formatDate(new Date()); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |               await MotorData.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |                 { customerId, motor_id: motorId, start_instance_id: start_instance_id }, | 
			
		
	
		
			
				
					|  |  |  |  |                 { | 
			
		
	
		
			
				
					|  |  |  |  |                   $set: { | 
			
		
	
		
			
				
					|  |  |  |  |                     stopTime:stopTime, | 
			
		
	
		
			
				
					|  |  |  |  |                     stopTime: stopTime, | 
			
		
	
		
			
				
					|  |  |  |  |                     receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), | 
			
		
	
		
			
				
					|  |  |  |  |                     quantity_delivered: quantityDelivered.toString() | 
			
		
	
		
			
				
					|  |  |  |  |                   } | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |               ); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |               // Send low water level notification
 | 
			
		
	
		
			
				
					|  |  |  |  |               if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) { | 
			
		
	
		
			
				
					|  |  |  |  |                 if (fcmToken) { | 
			
		
	
		
			
				
					|  |  |  |  |                   await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`); | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |               } | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |           } | 
			
		
	
		
			
				
					|  |  |  |  |         }, 20000); // Check water level every minute
 | 
			
		
	
		
			
				
					|  |  |  |  |         }, 20000); | 
			
		
	
		
			
				
					|  |  |  |  |       } | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // Respond with success message
 | 
			
		
	
		
			
				
					|  |  |  |  |     reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` }); | 
			
		
	
		
			
				
					|  |  |  |  |   } catch (err) { | 
			
		
	
		
			
				
					|  |  |  |  |     // Handle errors
 | 
			
		
	
		
			
				
					|  |  |  |  |     throw boom.boomify(err); | 
			
		
	
		
			
				
					|  |  |  |  |   } | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | // exports.motorAction = async (req, reply) => {
 | 
			
		
	
		
			
				
					|  |  |  |  | //   try {
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const customerId = req.params.customerId;
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const action = req.body.action;
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const motorId = req.body.motor_id;
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const start_instance_id = req.body.start_instance_id
 | 
			
		
	
		
			
				
					|  |  |  |  | //     console.log(req.body.startTime)
 | 
			
		
	
		
			
				
					|  |  |  |  | //     // Ensure motor_id is provided
 | 
			
		
	
		
			
				
					|  |  |  |  | //     if (!motorId) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       throw new Error("Motor ID is required.");
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     // Determine the motor stop status based on the action
 | 
			
		
	
		
			
				
					|  |  |  |  | //     let motorStopStatus;
 | 
			
		
	
		
			
				
					|  |  |  |  | //     if (action === "start") {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       motorStopStatus = "2"; // If action is start, set stop status to "2"
 | 
			
		
	
		
			
				
					|  |  |  |  | //     } else if (action === "stop") {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       motorStopStatus = "1"; // If action is stop, set stop status to "1"
 | 
			
		
	
		
			
				
					|  |  |  |  | //     } else {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       throw new Error("Invalid action provided.");
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     // Update the motor stop status immediately if action is stop
 | 
			
		
	
		
			
				
					|  |  |  |  | //     if (action === "stop") {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       // Update the motor stop status and other fields
 | 
			
		
	
		
			
				
					|  |  |  |  | //       await Tank.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //         { customerId, "connections.inputConnections.motor_id": motorId },
 | 
			
		
	
		
			
				
					|  |  |  |  | //         { 
 | 
			
		
	
		
			
				
					|  |  |  |  | //           $set: { 
 | 
			
		
	
		
			
				
					|  |  |  |  | //             "connections.inputConnections.$.motor_stop_status": "1",
 | 
			
		
	
		
			
				
					|  |  |  |  | //             "connections.inputConnections.$.stopTime": req.body.stopTime,
 | 
			
		
	
		
			
				
					|  |  |  |  | //             "connections.inputConnections.$.threshold_type": null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //             "connections.inputConnections.$.manual_threshold_time": null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //             "connections.inputConnections.$.manual_threshold_percentage": null
 | 
			
		
	
		
			
				
					|  |  |  |  | //           }
 | 
			
		
	
		
			
				
					|  |  |  |  | //         }
 | 
			
		
	
		
			
				
					|  |  |  |  | //       );
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //       // Send immediate response to the client
 | 
			
		
	
		
			
				
					|  |  |  |  | //       reply.code(200).send({ message: "Motor stopped successfully." });
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //       // Perform stop operations in the background
 | 
			
		
	
		
			
				
					|  |  |  |  | //       (async () => {
 | 
			
		
	
		
			
				
					|  |  |  |  | //         await delay(300000);
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         // Update the existing motor data entry with stop details
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
 | 
			
		
	
		
			
				
					|  |  |  |  | //         if (motorData) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const totalwaterpumped = quantityDelivered + water_pumped_till_now;
 | 
			
		
	
		
			
				
					|  |  |  |  | //           await Tank.findOneAndUpdate(
 | 
			
		
	
		
			
				
					|  |  |  |  | //             { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() },
 | 
			
		
	
		
			
				
					|  |  |  |  | //             { $set: { total_water_added_from_midnight: totalwaterpumped } }
 | 
			
		
	
		
			
				
					|  |  |  |  | //           );
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //           await MotorData.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //             { customerId, motor_id: motorId, start_instance_id: start_instance_id },
 | 
			
		
	
		
			
				
					|  |  |  |  | //             { 
 | 
			
		
	
		
			
				
					|  |  |  |  | //               $set: { 
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 stopTime: req.body.stopTime,
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 quantity_delivered: quantityDelivered.toString()
 | 
			
		
	
		
			
				
					|  |  |  |  | //               }
 | 
			
		
	
		
			
				
					|  |  |  |  | //             }
 | 
			
		
	
		
			
				
					|  |  |  |  | //           );
 | 
			
		
	
		
			
				
					|  |  |  |  | //         }
 | 
			
		
	
		
			
				
					|  |  |  |  | //       })();
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //       // Return here to ensure the rest of the code is not executed for the stop action
 | 
			
		
	
		
			
				
					|  |  |  |  | //       return;
 | 
			
		
	
		
			
				
					|  |  |  |  | //     } else {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       // Update the motor stop status to "2" for start action
 | 
			
		
	
		
			
				
					|  |  |  |  | //       await Tank.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //         { customerId, "connections.inputConnections.motor_id": motorId },
 | 
			
		
	
		
			
				
					|  |  |  |  | //         { $set: { "connections.inputConnections.$.motor_stop_status": "2" } }
 | 
			
		
	
		
			
				
					|  |  |  |  | //       );
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     // Check threshold settings if action is start
 | 
			
		
	
		
			
				
					|  |  |  |  | //     if (action === "start") {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       if (req.body.threshold_type === "time") {
 | 
			
		
	
		
			
				
					|  |  |  |  | //         // If threshold type is time, update threshold time
 | 
			
		
	
		
			
				
					|  |  |  |  | //         // await Tank.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //         //   { customerId, "connections.inputConnections.motor_id": motorId },
 | 
			
		
	
		
			
				
					|  |  |  |  | //         //   { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } }
 | 
			
		
	
		
			
				
					|  |  |  |  | //         // );
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const newMotorData = new MotorData({
 | 
			
		
	
		
			
				
					|  |  |  |  | //           customerId: customerId,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           motor_id: motorId,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           start_instance_id: start_instance_id,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           supplierTank: req.body.from,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           receiverTank: req.body.to,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           supplier_type: req.body.from_type,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           receiver_type: req.body.to_type,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           startTime: req.body.startTime,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10)
 | 
			
		
	
		
			
				
					|  |  |  |  | //         });
 | 
			
		
	
		
			
				
					|  |  |  |  | //         await newMotorData.save();
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         for await (const tank of Tank.find({  "connections.inputConnections.motor_id": motorId })) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
 | 
			
		
	
		
			
				
					|  |  |  |  | //           if (index !== -1) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //             await Tank.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //               { customerId, "connections.inputConnections.motor_id": motorId },
 | 
			
		
	
		
			
				
					|  |  |  |  | //               { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime,[`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } }
 | 
			
		
	
		
			
				
					|  |  |  |  | //             );
 | 
			
		
	
		
			
				
					|  |  |  |  | //           }
 | 
			
		
	
		
			
				
					|  |  |  |  | //         }
 | 
			
		
	
		
			
				
					|  |  |  |  |          | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         // Start monitoring water level based on threshold time
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate();
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const intervalId = setInterval(async () => {
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const splr_tank_info3 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const splr_tank_info3_waterlevel = parseInt(splr_tank_info3.waterlevel, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //           //console.log(splr_tank_info3_waterlevel,"splr_tank_info3_waterlevel")
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity.replace(/,/g, ''), 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //          // const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //          // console.log(splr_tank_info3.capacity,splr_tank_info3_capacity,"splr_tank_info3_capacity")
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const splr_tank_info3_percentage = (splr_tank_info3_waterlevel / splr_tank_info3_capacity) * 100;
 | 
			
		
	
		
			
				
					|  |  |  |  | //          // console.log(splr_tank_info3_percentage, "percentage for less than 20");
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //           if (new Date() >= thresholdTime || splr_tank_info3_percentage <= 20) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //             console.log(splr_tank_info3_percentage,)
 | 
			
		
	
		
			
				
					|  |  |  |  | //             await Tank.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //               { customerId, "connections.inputConnections.motor_id": motorId },
 | 
			
		
	
		
			
				
					|  |  |  |  | //               {
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 $set: {
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   "connections.inputConnections.$.motor_stop_status": "1",
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   "connections.inputConnections.$.threshold_type": null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   "connections.inputConnections.$.manual_threshold_time": null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   "connections.inputConnections.$.manual_threshold_percentage": null
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 }
 | 
			
		
	
		
			
				
					|  |  |  |  | //               }
 | 
			
		
	
		
			
				
					|  |  |  |  | //             );
 | 
			
		
	
		
			
				
					|  |  |  |  | //             clearInterval(intervalId);
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //             await delay(300000);
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //             const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
 | 
			
		
	
		
			
				
					|  |  |  |  | //             if (motorData) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const totalwaterpumped = quantityDelivered + water_pumped_till_now
 | 
			
		
	
		
			
				
					|  |  |  |  | //               await  Tank.findOneAndUpdate({customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase()}, { $set: { total_water_added_from_midnight: totalwaterpumped } })
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const stopTime = formatDate(new Date());
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //               await MotorData.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 { customerId, motor_id: motorId, start_instance_id: start_instance_id },
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 {
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   $set: {
 | 
			
		
	
		
			
				
					|  |  |  |  | //                     stopTime:stopTime,
 | 
			
		
	
		
			
				
					|  |  |  |  | //                     receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
 | 
			
		
	
		
			
				
					|  |  |  |  | //                     quantity_delivered: quantityDelivered.toString()
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   }
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 }
 | 
			
		
	
		
			
				
					|  |  |  |  | //               );
 | 
			
		
	
		
			
				
					|  |  |  |  | //             }
 | 
			
		
	
		
			
				
					|  |  |  |  | //           }
 | 
			
		
	
		
			
				
					|  |  |  |  | //         }, 60000);
 | 
			
		
	
		
			
				
					|  |  |  |  | //       } else if (req.body.threshold_type === "litres") {
 | 
			
		
	
		
			
				
					|  |  |  |  | //         console.log("entered litres")
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const newMotorData = new MotorData({
 | 
			
		
	
		
			
				
					|  |  |  |  | //           customerId: customerId,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           motor_id: motorId,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           start_instance_id: start_instance_id,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           supplierTank: req.body.from,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           receiverTank: req.body.to,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           supplier_type: req.body.from_type,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           receiver_type: req.body.to_type,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           startTime: req.body.startTime,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10)
 | 
			
		
	
		
			
				
					|  |  |  |  | //         });
 | 
			
		
	
		
			
				
					|  |  |  |  | //         await newMotorData.save();
 | 
			
		
	
		
			
				
					|  |  |  |  | //         // If threshold type is percentage, calculate percentage threshold
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | //         if (!receiver_tank_info) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //           throw new Error("Receiver tank not found.");
 | 
			
		
	
		
			
				
					|  |  |  |  | //         }
 | 
			
		
	
		
			
				
					|  |  |  |  | //         if (!supplier_tank_info) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //           throw new Error("Supplierr tank not found.");
 | 
			
		
	
		
			
				
					|  |  |  |  | //         }
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const supplier_capacity =  parseInt(supplier_tank_info.capacity, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10); 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const capacity = parseInt(receiver_tank_info.capacity, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const waterLevel = parseInt(receiver_tank_info.waterlevel, 10); 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const desired_percentage = parseInt(req.body.manual_threshold_litres.replace(/,/g, ''), 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         console.log(desired_percentage)
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const threshold_water_level = waterLevel+desired_percentage;
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const supplier_threshold = supplier_waterLevel-desired_percentage
 | 
			
		
	
		
			
				
					|  |  |  |  | //         console.log(supplier_threshold,"supplier_threshold")
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         for await (const tank of Tank.find({  "connections.inputConnections.motor_id": motorId })) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
 | 
			
		
	
		
			
				
					|  |  |  |  | //           if (index !== -1) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //             await Tank.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //               { customerId, "connections.inputConnections.motor_id": motorId },
 | 
			
		
	
		
			
				
					|  |  |  |  | //               { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } }
 | 
			
		
	
		
			
				
					|  |  |  |  | //             );
 | 
			
		
	
		
			
				
					|  |  |  |  | //           }
 | 
			
		
	
		
			
				
					|  |  |  |  | //         }
 | 
			
		
	
		
			
				
					|  |  |  |  |          | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         // Update water level threshold
 | 
			
		
	
		
			
				
					|  |  |  |  |         | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //         // Start monitoring water level based on threshold percentage
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const intervalId = setInterval(async () => {
 | 
			
		
	
		
			
				
					|  |  |  |  | //           // Check if water level has reached the threshold percentage
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | //           const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //           if (current_water_level <= supplier_threshold) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //             // Stop the motor pump
 | 
			
		
	
		
			
				
					|  |  |  |  | //             await Tank.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //               { customerId, "connections.inputConnections.motor_id": motorId },
 | 
			
		
	
		
			
				
					|  |  |  |  | //               { 
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 $set: { 
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   "connections.inputConnections.$.motor_stop_status": "1",
 | 
			
		
	
		
			
				
					|  |  |  |  |                  | 
			
		
	
		
			
				
					|  |  |  |  | //                   "connections.inputConnections.$.threshold_type": null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   "connections.inputConnections.$.manual_threshold_time": null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   "connections.inputConnections.$.manual_threshold_percentage": null
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 }
 | 
			
		
	
		
			
				
					|  |  |  |  | //               }
 | 
			
		
	
		
			
				
					|  |  |  |  | //             );
 | 
			
		
	
		
			
				
					|  |  |  |  | //             clearInterval(intervalId); // Stop monitoring water level
 | 
			
		
	
		
			
				
					|  |  |  |  | //             await delay(300000);
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //             const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
 | 
			
		
	
		
			
				
					|  |  |  |  | //             if (motorData) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //               const stopTime = formatDate(new Date());
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //               await MotorData.updateOne(
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 { customerId, motor_id: motorId, start_instance_id: start_instance_id },
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 {
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   $set: {
 | 
			
		
	
		
			
				
					|  |  |  |  | //                     stopTime:stopTime,
 | 
			
		
	
		
			
				
					|  |  |  |  | //                     receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
 | 
			
		
	
		
			
				
					|  |  |  |  | //                     quantity_delivered: quantityDelivered.toString()
 | 
			
		
	
		
			
				
					|  |  |  |  | //                   }
 | 
			
		
	
		
			
				
					|  |  |  |  | //                 }
 | 
			
		
	
		
			
				
					|  |  |  |  | //               );
 | 
			
		
	
		
			
				
					|  |  |  |  | //             }
 | 
			
		
	
		
			
				
					|  |  |  |  | //           }
 | 
			
		
	
		
			
				
					|  |  |  |  | //         }, 20000); // Check water level every minute
 | 
			
		
	
		
			
				
					|  |  |  |  | //       }
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     // Respond with success message
 | 
			
		
	
		
			
				
					|  |  |  |  | //     reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` });
 | 
			
		
	
		
			
				
					|  |  |  |  | //   } catch (err) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //     // Handle errors
 | 
			
		
	
		
			
				
					|  |  |  |  | //     throw boom.boomify(err);
 | 
			
		
	
		
			
				
					|  |  |  |  | //   }
 | 
			
		
	
		
			
				
					|  |  |  |  | // };
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | // exports.motorAction = async (req, reply) => {
 | 
			
		
	
	
		
			
				
					|  |  |  | 
 |