From daaf44494d470bbfbaa791e615a75cbfbf36c8c6 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 2 Mar 2023 05:29:33 -0500 Subject: [PATCH] creating start and stop,made changes in update tank levels --- src/controllers/tanksController.js | 153 ++++++++++++++++------------- 1 file changed, 86 insertions(+), 67 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index fc75521b..9a5ee63e 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -141,8 +141,49 @@ exports.getTank = async (req, reply) => { // } //}; - exports.updateTanklevels = async (req, reply) => { + try { + const customerId = req.params.customerId; + const tanks = await Tank.find({ customerId }); + + const intervals = {}; + + for (const tank of tanks) { + const tankId = tank._id; + let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); + let waterLevel = capacity - 100; // initial water level + + const intervalId = setInterval(async function () { + const newWaterLevel = Math.floor(waterLevel / 1.5); + + const result = await Tank.findOneAndUpdate( + { _id: tankId }, + { $set: { waterlevel: newWaterLevel } } + ); + + console.log(result); + + if (newWaterLevel === 0) { + clearInterval(intervals[tankId]); + console.log(`Stopped updating tank with ID ${tankId}`); + return; + } + + waterLevel = newWaterLevel; + }, 2000); + + intervals[tankId] = intervalId; + } + + return { message: 'Water level updates started' }; + } catch (err) { + throw boom.boomify(err); + } +}; + + + +exports.updateTanklevels1 = async (req, reply) => { try { const customerId = req.params.customerId; const tanks = await Tank.find({ customerId }); @@ -151,6 +192,7 @@ exports.updateTanklevels = async (req, reply) => { for (const tank of tanks) { const tankName = tank.tankName; + const tank_type = tank.tankLocation let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); let waterLevel = capacity - 100; // initial water level @@ -158,7 +200,7 @@ exports.updateTanklevels = async (req, reply) => { const newWaterLevel = Math.floor(waterLevel / 1.5); const result = await Tank.findOneAndUpdate( - { customerId, tankName }, + { customerId, tankName, }, { $set: { waterlevel: newWaterLevel } } ); @@ -222,68 +264,6 @@ const changingfrom_tankwaterlevel = async (customerId,initial_update,supplier_ta -exports.motorAction1 = async (req, reply) => { - try { - const action = req.body.action - let from, to_capacity, sump_capacity, update_level, intervalId; - - if (action === "start") { - const customerId = req.params.customerId; - const to = req.body.to - const from_type = req.body.from_type - - if (from_type === "sump") { - from = req.body.from - const [tankInfo, fromSump] = await Promise.all([ - Tank.findOne({customerId, tankName: to}), - Tank.findOne({customerId, tankName: from}) - ]); - - to_capacity = parseInt(tankInfo.capacity.replace(/,/g, ''), 10); - sump_capacity = parseInt(fromSump.capacity.replace(/,/g, ''), 10); - update_level = sump_capacity - 200 - console.log(to_capacity,sump_capacity,update_level) - var b_id = await changingfrom_tankwaterlevel(customerId,update_level,fromSump) - //await Tank.findOneAndUpdate({customerId, tankName: from}, { $set: { waterlevel: update_level } }) - - intervalId = setInterval(async function () { - let sumpWaterLevel = fromSump.waterlevel - let toWaterLevel = tankInfo.waterlevel - console.log( Math.floor(sumpWaterLevel * 0.10),"0") - console.log(toWaterLevel,"1") - - //const [sumpWaterLevel, toWaterLevel] = await Promise.all([ - - // tankInfo.waterlevel - //]); - - const newWaterLevel =parseInt(toWaterLevel.replace(/,/g, ''), 10) + Math.floor(sumpWaterLevel * 0.1); - const newSumpWaterLevel = sumpWaterLevel - Math.floor(sumpWaterLevel * 0.1) - console.log(newWaterLevel,"3") - console.log(newSumpWaterLevel,"4") - - await Promise.all([ - Tank.findOneAndUpdate({customerId, tankName: to}, { $set: { waterlevel: newWaterLevel } }), - Tank.findOneAndUpdate({customerId, tankName: from}, { $set: { waterlevel: newSumpWaterLevel } }) - ]); - }, 2000); - } - } else if (action === "stop") { - clearInterval(intervalId); - } else { - throw new Error("Invalid action"); - } - - return { message: 'Water level updates started' }; - } catch (err) { - throw new Error(`Failed to start/stop water level updates: ${err.message}`); - - }; -}; - - - - exports.motorAction = async (req, reply) => { try { const customerId = req.params.customerId; @@ -299,6 +279,7 @@ exports.motorAction = async (req, reply) => { if(supplier_tank_type==="sump"){ const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); initial_update = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)-200; + await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) @@ -306,25 +287,63 @@ exports.motorAction = async (req, reply) => { // Calculate new water levels const newWaterLevel = receiver_waterlevel + Math.floor(supplier_waterlevel * 0.1); const newSupplierWaterLevel = supplier_waterlevel - Math.floor(supplier_waterlevel * 0.1); + const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10) // Check if updating should stop - if (newSupplierWaterLevel <= 0 || newWaterLevel >= receiver_capacity) { - clearInterval(intervalId); + if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95) { + clearInterval(intervalId) + console.log("end"); } else { // Update water levels in database supplier_waterlevel = newSupplierWaterLevel; receiver_waterlevel = newWaterLevel; + console.log((newSupplierWaterLevel/supplier_capacity)*100) + console.log((newWaterLevel/receiver_capacity)*100) await Promise.all([ Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }), Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } }) ]); } }, 2000); + + + } + if(supplier_tank_type==="bore"){ + const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); + initial_update = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10)-200; + await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); + let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) + let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) + intervalId = setInterval(async function () { + const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10) + // Calculate new water levels + const newWaterLevel = receiver_waterlevel + Math.floor(supplier_waterlevel * 0.1); + const newSupplierWaterLevel = Math.min(supplier_capacity, supplier_waterlevel + Math.floor(supplier_waterlevel * 0.15)); + + // Check if updating should stop + if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95) { + clearInterval(intervalId) + console.log("end"); + } else { + // Update water levels in database + supplier_waterlevel = newSupplierWaterLevel; + receiver_waterlevel = newWaterLevel; + console.log((newSupplierWaterLevel/supplier_capacity)*100) + console.log((newWaterLevel/receiver_capacity)*100) + await Promise.all([ + Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }), + Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } }) + ]); + } + }, 2000); + } - } else if (action === "stop") { + } + + else if (action === "stop") { clearInterval(intervalId); } else { throw new Error("Invalid action");