diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 13ce06af..ba563b9c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2938,26 +2938,22 @@ exports.motorAction = async (req, reply) => { 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 currentTime = new Date(); + console.log(`🚀 Checking Motor ${motorId} | Threshold Time: ${thresholdTime} | Current Time: ${currentTime}`); - console.log(`🚀 Checking Motor ${motorId} | Threshold Time: ${thresholdTime} | Current Time: ${new Date()}`); - console.log(`🔍 Water Level: ${currentWaterLevel} | Percentage: ${currentWaterPercentage} | Low Threshold: ${lowWaterThreshold}`); - - // Check if stop condition is met - if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) { - console.log(`🛑 Motor ${motorId} threshold reached. Stopping motor.`); + // If threshold time has passed, force stop the motor + if (currentTime >= thresholdTime) { + console.log(`🛑 Motor ${motorId} threshold time exceeded. Stopping motor.`); - // Stop interval before proceeding + // Stop the interval before proceeding if (motorIntervals[motorId]) { clearInterval(motorIntervals[motorId]); delete motorIntervals[motorId]; console.log(`✅ Interval for motorId: ${motorId} successfully deleted.`); } - // Now update the tank data and publish stop status - const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); + // Update the tank data and publish stop status + const formattedTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { @@ -2966,15 +2962,18 @@ exports.motorAction = async (req, reply) => { "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, "connections.inputConnections.$.manual_threshold_percentage": null, - "connections.inputConnections.$.stopTime": currentTime, + "connections.inputConnections.$.stopTime": formattedTime, } } ); console.log(`🚨 Motor ${motorId} stop condition met. Sending stop command.`); this.publishMotorStopStatus(motorId, "1"); + + return; // Ensure the function exits } - }, 30000); // Every 30 seconds + }, 10000); // Check every 10 seconds instead of 30 + } }