From 777d1f045ddd1cd8627af77d957272f30dfed9bd Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 19 Mar 2025 16:34:38 +0530 Subject: [PATCH] changes --- src/controllers/tanksController.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 1a54cb38..32e1177d 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2837,6 +2837,7 @@ async function calculateTotalPumpedWater(customerId, motorId, start_instance_id) +const stoppedMotors = new Set(); // To track motors that have already been stopped exports.motorAction = async (req, reply) => { try { @@ -2936,23 +2937,31 @@ exports.motorAction = async (req, reply) => { console.log("New Threshold Time:", thresholdTime); console.log("Current Time:", new Date()); - motorIntervals[motorId] = setInterval(async () => { const currentTime = new Date(); console.log(`🚀 Checking Motor ${motorId} | Threshold Time: ${thresholdTime} | Current Time: ${currentTime}`); - // If threshold time has passed, force stop the motor + // If threshold time has passed, stop the motor if (currentTime >= thresholdTime) { console.log(`🛑 Motor ${motorId} threshold time exceeded. Stopping motor.`); - // Stop the interval before proceeding + // Check if motor has already been stopped + if (stoppedMotors.has(motorId)) { + console.log(`⚠️ Motor ${motorId} already stopped. Skipping duplicate stop.`); + return; + } + + // Mark motor as stopped to prevent duplicate execution + stoppedMotors.add(motorId); + + // Ensure the interval is cleared before proceeding if (motorIntervals[motorId]) { clearInterval(motorIntervals[motorId]); delete motorIntervals[motorId]; console.log(`✅ Interval for motorId: ${motorId} successfully deleted.`); } - // Update the tank data and publish stop status + // Update tank data const formattedTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, @@ -2967,13 +2976,14 @@ exports.motorAction = async (req, reply) => { } ); - console.log(`🚨 Motor ${motorId} stop condition met. Sending stop command.`); this.publishMotorStopStatus(motorId, "1"); - return; // Ensure the function exits + return; // Ensure function exits after stopping } - }, 10000); // Check every 10 seconds instead of 30 + }, 10000); // Check every 10 seconds + + }