From e4cde7ab31a509bb4d9a0435d679ae3c2b59a54a Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 19 Mar 2025 15:46:02 +0530 Subject: [PATCH] changed --- src/controllers/tanksController.js | 139 +++++++---------------------- 1 file changed, 31 insertions(+), 108 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 97955bfc..13ce06af 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2937,122 +2937,45 @@ exports.motorAction = async (req, reply) => { console.log("Current Time:", new Date()); - motorIntervals[motorId] = setInterval(async () => { + motorIntervals[motorId] = setInterval(async () => { const supplierTank = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); const currentWaterLevel = parseInt(supplierTank.waterlevel, 10); const currentWaterPercentage = (currentWaterLevel / parseInt(supplierTank.capacity.replace(/,/g, ''), 10)) * 100; - const notificationTracker = new Map(); - if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) { - console.log(new Date(),"new date") - console.log(thresholdTime,"thresholdTime") - console.log("motor stopping because it entered this condition") - // Emit the threshold time notification - - // eventEmitter.emit( - // "sendThresholdTimeNotification", - // customerId, - // fcmToken, - // manual_threshold_time, - // motorId, - // tankName, - // blockName - // ); - - // const notificationKey = `${customerId}_${motorId}_threshold`; - - // // Check if the notification has already been sent - // if (!notificationTracker.get(notificationKey)) { - // console.log("Sending threshold time notification..."); - - // eventEmitter.emit( - // "sendThresholdTimeNotification", - // customerId, - // fcmToken, - // manual_threshold_time, - // motorId, - // tankName, - // blockName - // ); - - // // Mark notification as sent - // notificationTracker.set(notificationKey, true); - - // // Optionally, reset the flag after some time (e.g., 24 hours) - // setTimeout(() => { - // notificationTracker.delete(notificationKey); - // }, 24 * 60 * 60 * 1000); // Reset after 24 hours - // } else { - // console.log("Notification already sent, skipping..."); - // } - - const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); - 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, - "connections.inputConnections.$.stopTime": currentTime, - } - } - ); + + console.log(`🚀 Checking Motor ${motorId} | Threshold Time: ${thresholdTime} | Current Time: ${new Date()}`); + console.log(`🔍 Water Level: ${currentWaterLevel} | Percentage: ${currentWaterPercentage} | Low Threshold: ${lowWaterThreshold}`); - if (motorIntervals[motorId]) { - console.log(`🛑 Clearing interval for motorId: ${motorId}`); - clearInterval(motorIntervals[motorId]); - delete motorIntervals[motorId]; - - // Confirm deletion - if (!motorIntervals[motorId]) { - console.log(`✅ Interval for motorId: ${motorId} successfully deleted.`); - } else { - console.error(`❌ Failed to delete interval for motorId: ${motorId}`); + // Check if stop condition is met + if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) { + console.log(`🛑 Motor ${motorId} threshold reached. Stopping motor.`); + + // Stop interval before proceeding + if (motorIntervals[motorId]) { + clearInterval(motorIntervals[motorId]); + delete motorIntervals[motorId]; + console.log(`✅ Interval for motorId: ${motorId} successfully deleted.`); } - } - - - this.publishMotorStopStatus(motorId, "1"); - console.log(start_instance_id,"start_instance_id",customerId,"customerId",motorId,"motorId") - - const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); - console.log(motorData,"motorData") - if (motorData) { - console.log("got into if") - 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: currentTime, - receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), - quantity_delivered: quantityDelivered.toString() + + // Now update the tank data and publish stop status + const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); + 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, + "connections.inputConnections.$.stopTime": currentTime, + } } - } ); - } + + console.log(`🚨 Motor ${motorId} stop condition met. Sending stop command.`); + this.publishMotorStopStatus(motorId, "1"); } - - // Check for high water level and send notification - // if (currentWaterPercentage >= highWaterThreshold) { - // // eventEmitter.emit('sendHighWaterNotification', fcmToken, receiverTank); - // await checkWaterLevelsAndNotify(customerId, tankName, supplierTank.tankLocation, fcmToken); - - // } - - }, 30000); // Check every minute + }, 30000); // Every 30 seconds + } } else if (req.body.threshold_type === "litres") {