master^2
Varun 7 months ago
parent 340ded5935
commit 777d1f045d

@ -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) => { exports.motorAction = async (req, reply) => {
try { try {
@ -2936,23 +2937,31 @@ exports.motorAction = async (req, reply) => {
console.log("New Threshold Time:", thresholdTime); console.log("New Threshold Time:", thresholdTime);
console.log("Current Time:", new Date()); console.log("Current Time:", new Date());
motorIntervals[motorId] = setInterval(async () => { motorIntervals[motorId] = setInterval(async () => {
const currentTime = new Date(); 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: ${currentTime}`);
// If threshold time has passed, force stop the motor // If threshold time has passed, stop the motor
if (currentTime >= thresholdTime) { if (currentTime >= thresholdTime) {
console.log(`🛑 Motor ${motorId} threshold time exceeded. Stopping motor.`); 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]) { if (motorIntervals[motorId]) {
clearInterval(motorIntervals[motorId]); clearInterval(motorIntervals[motorId]);
delete motorIntervals[motorId]; delete motorIntervals[motorId];
console.log(`✅ Interval for motorId: ${motorId} successfully deleted.`); 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'); const formattedTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
await Tank.updateOne( await Tank.updateOne(
{ customerId, "connections.inputConnections.motor_id": motorId }, { 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.`); console.log(`🚨 Motor ${motorId} stop condition met. Sending stop command.`);
this.publishMotorStopStatus(motorId, "1"); 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
} }

Loading…
Cancel
Save