From a2bab73b6480f2c298fbf5cdee5188ece8981bc0 Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 2 Sep 2024 11:56:33 +0530 Subject: [PATCH] made changes in motoraction --- src/controllers/tanksController.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index df3ac654..f961d237 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1359,6 +1359,8 @@ const sendNotification = async (fcmTokens, title, body) => { // }; + +const stat_stop_intervals = {}; exports.motorAction = async (req, reply) => { try { const customerId = req.params.customerId; @@ -1395,6 +1397,11 @@ exports.motorAction = async (req, reply) => { } } ); + if (intervals[motorId]) { + clearInterval(stat_stop_intervals[motorId]); + delete stat_stop_intervals[motorId]; + console.log("interval cleared") + } reply.code(200).send({ message: "Motor stopped successfully." }); @@ -1480,7 +1487,8 @@ exports.motorAction = async (req, reply) => { // Start monitoring water level based on threshold time const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate(); - const intervalId = setInterval(async () => { + stat_stop_intervals[motorId] = setInterval(async () => { + console.log(stat_stop_intervals) const splr_tank_info3 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); const splr_tank_info3_waterlevel = parseInt(splr_tank_info3.waterlevel, 10); const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity.replace(/,/g, ''), 10); @@ -1498,7 +1506,9 @@ exports.motorAction = async (req, reply) => { } } ); - clearInterval(intervalId); + console.log(stat_stop_intervals[motorId]) + clearInterval(stat_stop_intervals[motorId]); + delete stat_stop_intervals[motorId]; // Send notification after motor stops due to time threshold if (fcmToken) { @@ -1589,7 +1599,7 @@ exports.motorAction = async (req, reply) => { } // Start monitoring water level based on litres - const intervalId = setInterval(async () => { + stat_stop_intervals[motorId] = setInterval(async () => { 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); if (current_water_level <= supplier_threshold) { @@ -1604,7 +1614,8 @@ exports.motorAction = async (req, reply) => { } } ); - clearInterval(intervalId); + clearInterval(stat_stop_intervals[motorId]); + delete stat_stop_intervals[motorId]; await delay(300000);