From 40481f759177cfe4173b1c27aac237157f8d3a9c Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 19 Dec 2024 12:19:18 +0530 Subject: [PATCH] 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 },