From 1ed3061dfac63327e13d4156ccc54939b164d941 Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 19 Dec 2024 11:58:35 +0530 Subject: [PATCH 1/2] reverted changes in tanks controller --- src/controllers/tanksController.js | 32 ++++-------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index ffc8c515..b7aaf48d 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2105,7 +2105,6 @@ else if (currentWaterLevel >= highWaterThreshold) { this.publishMotorStopStatus(motorId, motorStopStatus); for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); - const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); if (index !== -1) { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, @@ -2114,43 +2113,20 @@ else if (currentWaterLevel >= highWaterThreshold) { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.threshold_type`]: "time", [`connections.inputConnections.${index}.startTime`]: req.body.startTime, - [`connections.inputConnections.${index}.start_instance_id`]: start_instance_id, - [`connections.inputConnections.${index}.manual_stop_threshold_time`]: req.body.manual_stop_threshold_time, + [`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } } ); } } - + const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); const intervalId = 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 receiverTank1 = await Tank.findOne({ - customerId, - tankName: req.body.to, - tankLocation: req.body.to_type.toLowerCase() - }); - - // Ensure the tank exists - if (!receiverTank1) { - throw new Error('Receiver tank not found'); - } - - // Extract the manual_stop_threshold_time from the appropriate input connection - let stop_time = null; - if (receiverTank1.connections && receiverTank1.connections.inputConnections) { - const inputConnection = receiverTank1.connections.inputConnections.find(connection => connection.motor_id === req.body.motorId); - - if (inputConnection && inputConnection.manual_stop_threshold_time) { - stop_time = inputConnection.manual_stop_threshold_time; - } else { - console.log('No matching input connection or manual_stop_threshold_time found'); - } - } - - if (new Date() >= stop_time || currentWaterPercentage <= lowWaterThreshold) { + + if (new Date() >= thresholdTime || currentWaterPercentage <= lowWaterThreshold) { console.log("motor stopping because it entered this condition") await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, From 40481f759177cfe4173b1c27aac237157f8d3a9c Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 19 Dec 2024 12:19:18 +0530 Subject: [PATCH 2/2] changes --- src/controllers/tanksController.js | 21 ++++++++++++++------- src/models/tanks.js | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b7aaf48d..1c7ea167 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1908,7 +1908,7 @@ const stat_stop_intervals = {}; // throw boom.boomify(err); // } // }; - +const motorIntervals = {}; exports.motorAction = async (req, reply) => { try { const customerId = req.params.customerId; @@ -2040,7 +2040,12 @@ else if (currentWaterLevel >= highWaterThreshold) { } } ); + if (motorIntervals[motorId]) { + clearInterval(motorIntervals[motorId]); // Clear the interval + delete motorIntervals[motorId]; // Remove the interval from the object + } this.publishMotorStopStatus(motorId, motorStopStatus); + // Send immediate response to the client reply.code(200).send({ message: "Motor stopped successfully." }); @@ -2120,8 +2125,8 @@ else if (currentWaterLevel >= highWaterThreshold) { } } - const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); - const intervalId = setInterval(async () => { + const thresholdTime = new Date(new Date().getTime() + req.body.manual_threshold_time * 60000); + 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; @@ -2141,8 +2146,9 @@ else if (currentWaterLevel >= highWaterThreshold) { } ); emitWithTimestamp('lowWaterLevel', fcmToken); // Emit low water level notification - - clearInterval(intervalId); + clearInterval(motorIntervals[motorId]); // Clear the interval when stopping + delete motorIntervals[motorId]; + this.publishMotorStopStatus(motorId, "1"); await delay(300000); const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); @@ -2232,7 +2238,7 @@ else if (currentWaterLevel >= highWaterThreshold) { // Start monitoring water level based on threshold percentage - const intervalId = setInterval(async () => { + motorIntervals[motorId] = setInterval(async () => { // Check if water level has reached the threshold percentage const supplier_tank_info1 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); const current_water_level = parseInt(supplier_tank_info1.waterlevel, 10); @@ -2250,7 +2256,8 @@ else if (currentWaterLevel >= highWaterThreshold) { } } ); - clearInterval(intervalId); // Stop monitoring water level + clearInterval(motorIntervals[motorId]); // Clear the interval when stopping + delete motorIntervals[motorId];// Stop monitoring water level await delay(300000); const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id }); diff --git a/src/models/tanks.js b/src/models/tanks.js index fd6232f7..d6274101 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -70,7 +70,7 @@ const tanksSchema = new mongoose.Schema({ water_level: { type: String, default: null }, manual_threshold_percentage: { type: String, default: "90" }, manual_threshold_time: { type: String, default: null }, - manual_stop_threshold_time: { type: String, default: null }, + stop_threshold_time: { type: String, default: null }, threshold_type: { type: String, default: "percentage" }, startTime: { type: String, default: null },