From 248aa469b8876c0657e1c1ef6298c129fbea9ebf Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 21 Mar 2025 12:20:21 +0530 Subject: [PATCH 1/2] changes --- src/controllers/tanksController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 8840dc97..be0065fe 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -7237,4 +7237,4 @@ exports.notificationTiming = async (req, reply) => { await User.updateOne({ customerId }, { notificationPreference }); return reply.send({ message: "Preference updated successfully" }); -} \ No newline at end of file +} From 731a172ea9f000110de09702f243e2abb2e19c56 Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 21 Mar 2025 13:08:27 +0530 Subject: [PATCH 2/2] changes --- src/controllers/tanksController.js | 140 +++++++++++++---------------- 1 file changed, 63 insertions(+), 77 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 902efd6d..6769e7ea 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2900,7 +2900,7 @@ exports.motorAction = async (req, reply) => { let motorStopStatus = action === "start" ? "2" : "1"; const blockName = req.body.from || "Unknown Block"; const tankName = req.body.to || "Unknown Tank"; - const stopTime = req.body.stopTime + if (action === "start") { if (motorIntervals[motorId]) { @@ -2983,45 +2983,7 @@ exports.motorAction = async (req, reply) => { console.log(thresholdTime,"thresholdTime") console.log("motor stopping because it entered this condition") // Emit the threshold time notification - try { - console.log("motor stopping because it entered this condition") - - const tank = await Tank.findOne( - { customerId, "connections.inputConnections.motor_id": motorId }, - { "connections.inputConnections.$": 1 } // Fetch only relevant motor connection - ); - - if (tank && tank.connections.inputConnections[0].motor_stop_status === "1") { - console.log("⚠️ Motor already stopped. Skipping notification."); - } else { - console.log("🚀 Sending threshold time notification..."); - - eventEmitter.emit( - "sendThresholdTimeNotification", - customerId, - fcmToken, - manual_threshold_time, - motorId, - tankName, - blockName - ); - } - // eventEmitter.emit( - // "sendThresholdTimeNotification", - // customerId, - // fcmToken, - // manual_threshold_time, - // motorId, - // tankName, - // blockName - // ); - - reply.code(200).send({ message: "Motor stopped successfully." }); - } catch (error) { - console.error("Error in handleMotorStop:", error); - reply.code(500).send({ error: "Internal Server Error" }); - } - + // eventEmitter.emit( // "sendThresholdTimeNotification", // customerId, @@ -3032,32 +2994,32 @@ exports.motorAction = async (req, reply) => { // blockName // ); - // const notificationKey = `${customerId}_${motorId}_threshold`; + const notificationKey = `${customerId}_${motorId}_threshold`; - // // Check if the notification has already been sent - // if (!notificationTracker.get(notificationKey)) { - // console.log("Sending threshold time notification..."); + // 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..."); - // } + 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( @@ -3238,19 +3200,6 @@ exports.motorAction = async (req, reply) => { } else if (action === "stop") { await stopMotor(motorId, customerId, start_instance_id); - try { - - - const totalWaterPumped = await calculateTotalPumpedWater(customerId, motorId, start_instance_id); - - eventEmitter.emit("motorStop", customerId, fcmToken, tankName, blockName, stopTime, "Mobile APP", totalWaterPumped, typeOfWater, motorId, - loggedInUser.phone,); - - reply.code(200).send({ message: "Motor stopped successfully." }); - } catch (error) { - console.error("Error in handleMotorStop:", error); - reply.code(500).send({ error: "Internal Server Error" }); - } this.publishMotorStopStatus(motorId, motorStopStatus); reply.code(200).send({ message: "Motor stopped successfully." }); } @@ -3260,6 +3209,43 @@ exports.motorAction = async (req, reply) => { }; +async function stopMotor(motorId, customerId, start_instance_id) { + 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.$.stopTime": currentTime, + "connections.inputConnections.$.threshold_type": null, + "connections.inputConnections.$.manual_threshold_time": null, + "connections.inputConnections.$.manual_threshold_percentage": null + }} + ); + + if (motorIntervals[motorId]) { + clearInterval(motorIntervals[motorId]); + delete motorIntervals[motorId]; + } + + eventEmitter.emit("motorStop", customerId, [], "", "", currentTime, "Mobile APP", 0, "", motorId, ""); + + const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id }); + if (motorData) { + const startTime = moment(motorData.startTime, 'DD-MMM-YYYY - HH:mm'); + const runtime = moment.duration(moment(currentTime, 'DD-MMM-YYYY - HH:mm').diff(startTime)).asSeconds(); + + const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }); + const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel.replace(/,/g, ''), 10); + const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel.replace(/,/g, ''), 10); + + await MotorData.updateOne( + { customerId, motor_id: motorId, start_instance_id }, + { $set: { stopTime: currentTime, receiverfinalwaterlevel: receiverFinalWaterLevel.toString(), quantity_delivered: quantityDelivered.toString(), runtime: runtime } } + ); + } +} + + async function stopMotor(motorId, customerId, start_instance_id) { const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); await Tank.updateOne(