From 92ae56ce5fae88e54859bb40e26759d37f925b07 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 5 Jun 2023 06:12:17 -0400 Subject: [PATCH] made changes in start and stop and conmsumption --- src/controllers/tanksController.js | 109 ++++++++++++++++++++++++----- src/models/tanks.js | 3 +- 2 files changed, 92 insertions(+), 20 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 232aaa98..83dcebc7 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -260,33 +260,51 @@ exports.getTanklevels = async (req, reply) => { }; const intervals = {}; -let start_time="" +let sump_water_levels=[]; +let supplier_tanks = []; exports.motorAction = async (req, reply) => { try { - +7 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 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 + // console.log(supplier_tank) + // const suplr_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); + + + + const interval_variable = supplier_tank+receiver_tank + let currentTank = supplier_tanks.find(tank => tank.supplier_tank === supplier_tank); + let currentSump = sump_water_levels.find(tank => tank.supplier_tank === supplier_tank); + if(action === "start"){ - start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) - console.log(start_time) + + if (!currentTank) { + currentTank = { + supplier_tank: supplier_tank, + start_time: new Date().toLocaleString('en-US', { timeZone: 'Asia/Kolkata' }) + }; + supplier_tanks.push(currentTank); + } + // start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + console.log(supplier_tanks) // const stop_at = req.body.stop_at - + if(supplier_tank_type==="sump" && receiver_type === "overhead"){ + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); @@ -294,7 +312,23 @@ exports.motorAction = async (req, reply) => { //const initial_update = parseInt(supplier_tank_info1.waterlevel.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}); + const sump_water_level= parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) + const receiver_tank_info2 = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}); + const water_added_from_midnight = parseInt((receiver_tank_info2.total_water_added_from_midnight).replace(/,/g, ''), 10) + + if (!currentSump) { + currentSump = { + supplier_tank: supplier_tank, + supplier_initial_waterlevel:sump_water_level, + receiver_tank_total_water_added_from_midnight:water_added_from_midnight + + + }; + sump_water_levels.push(currentSump); + } + + console.log(sump_water_levels) const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -423,8 +457,6 @@ exports.motorAction = async (req, reply) => { // } - - if(supplier_tank_type==="sump" && receiver_type === "sump"){ const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) @@ -574,10 +606,47 @@ exports.motorAction = async (req, reply) => { } else if (action === "stop") { - stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + //stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) clearInterval(intervals[interval_variable]); // Clear the interval for this tank delete intervals[interval_variable]; + + const stopTime = new Date().toLocaleString('en-US', { timeZone: 'Asia/Kolkata' }); + // console.log(currentTank.start_time) + const startTime = currentTank.start_time; + // const duration = calculateDuration(startTime, stopTime); + + // Store the duration or perform any required operations + supplier_tanks = supplier_tanks.filter(tank => tank.supplier_tank !== supplier_tank); + // console.log(supplier_tanks) + + // storing data of how amny water supplied from sump to overhead to calculate the consumption + const suplr_tank_info2 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); + + let water_added_from_midnight1=0 + if (supplier_tank_type === "sump") { + // const rcvr_info2 = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); + console.log(currentSump.receiver_tank_total_water_added_from_midnight,"5") + water_added_from_midnight1=currentSump.receiver_tank_total_water_added_from_midnight + + console.log(water_added_from_midnight1) + + } + const sump_water_level1 =currentSump.supplier_initial_waterlevel + console.log(sump_water_level1,"1") + // console.log(water_added_from_midnight) + const sump_final_water_level= parseInt(suplr_tank_info2.waterlevel.replace(/,/g, ''), 10) + console.log(sump_final_water_level,"2") + sump_water_levels = sump_water_levels.filter(tank => tank.supplier_tank !== supplier_tank); + + const quantity_delivered = sump_water_level1-sump_final_water_level + + if (supplier_tank_type === "sump") { + + final_added_water=water_added_from_midnight1+quantity_delivered + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { total_water_added_from_midnight: final_added_water } }) + + } const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); @@ -601,17 +670,18 @@ exports.motorAction = async (req, reply) => { ); } - motorData = { + //saving the motor run time and data + motorData = { - customerId:customerId, supplierTank : supplier_tank, supplier_type: supplier_tank_type, receiverTank: receiver_tank, receiver_type: receiver_type, - startTime: start_time, - stopTime: stop_time, + startTime: startTime, + stopTime: stopTime, + quantity_delivered:quantity_delivered @@ -627,10 +697,11 @@ exports.motorAction = async (req, reply) => { motorData.receiverTank = receiver_tank; motorData.supplier_type = supplier_type; motorData.receiver_type = receiver_type; - motorData.startTime = start_time; - motorData.stopTime = stop_time; + motorData.startTime = startTime; + motorData.stopTime = stopTime; + motorData.quantity_delivered = quantity_delivered; - + } const motor_data = await motorData.save(); console.log(motor_data) @@ -640,7 +711,7 @@ exports.motorAction = async (req, reply) => { // reply.send({ status_code: 200, "start time": start_time, data: motor_data}); - console.log(start_time) + // console.log(start_time) // return motor_data @@ -650,7 +721,7 @@ exports.motorAction = async (req, reply) => { // 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,data: motor_data}); + reply.send({ status_code: 200, "stop time": stopTime,data: motor_data}); } else { throw new Error("Invalid action"); } diff --git a/src/models/tanks.js b/src/models/tanks.js index 83274ac9..19cb4c05 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -43,6 +43,7 @@ const tanksSchema = new mongoose.Schema({ tankLocation: { type: String, default: null }, motor_status: { type: String, default: "0" }, waterlevel_at_midnight:{ type: String,default:"0" }, + total_water_added_from_midnight:{ type: String,default:"0" }, connections: { source: { type: String }, inputConnections: [ @@ -74,7 +75,7 @@ const motordataSchema = new mongoose.Schema({ stopTime: { type: String, default: null }, supplier_type: { type: String, default: null }, receiver_type: { type: String, default: null }, - + quantity_delivered:{ type: String, default: null }, });