From cd3a547fe20e8430ff58918a522779fe0e6839e1 Mon Sep 17 00:00:00 2001 From: varun Date: Tue, 16 May 2023 05:02:10 -0400 Subject: [PATCH] made changes in tanks start and stop --- src/controllers/tanksController.js | 47 +++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 5dff432e..b07d2d5c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -219,22 +219,25 @@ exports.getTanklevels = async (req, reply) => { } }; - - +const intervals = {}; +let sum_oh_count = 0 +let bore_sump_count = 0 exports.motorAction = async (req, reply) => { try { //let start_time,stop_time + const customerId = req.params.customerId; const action = req.body.action const receiver_tank = req.body.to 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 = {}; + const supplier_tank = req.body.from const supplier_tank_type = (req.body.from_type).toLowerCase() const receiver_type = (req.body.to_type).toLowerCase() console.log(supplier_tank) + const interval_variable = supplier_tank+receiver_tank if(action === "start"){ const start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) @@ -259,14 +262,15 @@ exports.motorAction = async (req, reply) => { await overheadTank.save(); } - + sum_oh_count++; + console.log(sum_oh_count) // await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); 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) - intervals[receiver_tank] = setInterval(async function () { + intervals[interval_variable] = setInterval(async function () { // Calculate new water levels const supplier_tank_info2 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); @@ -276,8 +280,9 @@ exports.motorAction = async (req, reply) => { console.log((newSupplierWaterLevel/supplier_capacity)*100) // 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(intervals[receiver_tank]); // Clear the interval for this tank - delete intervals[receiver_tank]; + clearInterval(intervals[interval_variable]); // Clear the interval for this tank + delete intervals[interval_variable]; + sum_oh_count--; await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -359,6 +364,7 @@ exports.motorAction = async (req, reply) => { if(supplier_tank_type==="bore" && receiver_type === "sump"){ const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) // console.log(receiver_capacity,"0",receiver_tank_info.tankName) + bore_sump_count++; await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); const sumpTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = sumpTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -371,7 +377,7 @@ exports.motorAction = async (req, reply) => { let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) // console.log(receiver_waterlevel,"1") - intervals[receiver_tank] = setInterval(async function () { + intervals[interval_variable] = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); //console.log(rcvr_info) @@ -382,8 +388,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(intervals[receiver_tank]); // Clear the interval for this tank - delete intervals[receiver_tank]; + clearInterval(intervals[interval_variable]); // Clear the interval for this tank + delete intervals[interval_variable]; + bore_sump_count--; const sumpTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = sumpTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -449,6 +456,23 @@ exports.motorAction = async (req, reply) => { else if (action === "stop") { const stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + clearInterval(intervals[interval_variable]); // Clear the interval for this tank + delete intervals[interval_variable]; + if (supplier_tank_type === "sump"){ + sum_oh_count--; + if (sum_oh_count===0){ + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); + + } + } + if (supplier_tank_type === "bore"){ + bore_sump_count--; + if (bore_sump_count===0){ + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); + + } + } + const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -458,7 +482,8 @@ exports.motorAction = async (req, reply) => { } // console.log(stop_time) // clearInterval(intervalId); - await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); + + // await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); reply.send({ status_code: 200, "stop time": stop_time}); } else {