diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 3f2f89d1..0f55c36f 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -283,6 +283,7 @@ exports.motorAction = async (req, reply) => { const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}); const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10) const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10) + const intervals = {}; if(action === "start"){ @@ -309,7 +310,7 @@ exports.motorAction = async (req, reply) => { let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) console.log(supplier_waterlevel) let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) - intervalId = setInterval(async function () { + intervals[receiver_tank] = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); const newWaterLevel = receiver_waterlevel + 350//Math.floor(supplier_waterlevel * 0.1); @@ -318,10 +319,11 @@ exports.motorAction = async (req, reply) => { // Check if updating should stop if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0") { - clearInterval(intervalId) + clearInterval(intervals[receiver_tank]); // Clear the interval for this tank + delete intervals[receiver_tank]; await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); - console.log("end"); + console.log("end for"+receiver_tank); } else { // Update water levels in database supplier_waterlevel = newSupplierWaterLevel; @@ -392,7 +394,7 @@ exports.motorAction = async (req, reply) => { let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) console.log(receiver_waterlevel,"1") - intervalId = setInterval(async function () { + intervals[receiver_tank] = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); console.log(rcvr_info) @@ -403,8 +405,9 @@ exports.motorAction = async (req, reply) => { if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); - clearInterval(intervalId) - console.log("end"); + clearInterval(intervals[receiver_tank]); // Clear the interval for this tank + delete intervals[receiver_tank]; + console.log("end for" + receiver_tank); } else { // Update water levels in database