From 25369ffb7dad228a459a5bee0e09ec64074ccff7 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 06:41:19 -0400 Subject: [PATCH] start and stop --- src/controllers/tanksController.js | 36 +++++++++++++----------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 90529cbd..21bbad45 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -250,7 +250,6 @@ exports.getTanklevels = async (req, reply) => { - exports.motorAction = async (req, reply) => { try { //let start_time,stop_time @@ -263,14 +262,12 @@ exports.motorAction = async (req, reply) => { if(action === "start"){ - start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + // start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) - const supplier_tank = req.body.from - const supplier_tank_type = (req.body.from_type).toLowerCase() - const receiver_type = (req.body.to_type).toLowerCase() + supplier_tank = req.body.from + supplier_tank_type = (req.body.from_type).toLowerCase() + receiver_type = (req.body.to_type).toLowerCase() console.log(supplier_tank) - - if(supplier_tank_type==="sump" && receiver_type === "overhead"){ @@ -278,7 +275,7 @@ exports.motorAction = async (req, reply) => { const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); console.log(supplier_tank_info1) - const initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; + initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } }); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); @@ -363,7 +360,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) + console.log(receiver_capacity,"0") await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) @@ -371,11 +368,9 @@ exports.motorAction = async (req, reply) => { intervalId = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); - console.log(rcvr_info) console.log(rcvr_info.motor_status) const newWaterLevel = receiver_waterlevel+450; - console.log(newWaterLevel,"2",receiver_tank_info.tankName) - + console.log(newWaterLevel,"2") // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { @@ -387,7 +382,7 @@ exports.motorAction = async (req, reply) => { receiver_waterlevel = newWaterLevel; - console.log((newWaterLevel/receiver_capacity)*100,"4",receiver_tank_info.tankName) + console.log((newWaterLevel/receiver_capacity)*100,"4") await Promise.all([ Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }), ]); @@ -426,25 +421,24 @@ exports.motorAction = async (req, reply) => { } const motor_data = await motorData.save(); - // reply.send({ status_code: 200, data: motor_data }); - + reply.send({ status_code: 200, data: motor_data }); + return motor_data - - reply.send({ status_code: 200, "start time": start_time, data: motor_data}); - console.log(start_time) - return motor_data + + // reply.send({ status_code: 200, "start time": start_time}); + //console.log(start_time) } else if (action === "stop") { - stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + // stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) // console.log(stop_time) // clearInterval(intervalId); 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}); + // reply.send({ status_code: 200, "stop time": stop_time}); } else { throw new Error("Invalid action"); }