From b6286cf82cd836851302fe14a9a8fd9509e6c126 Mon Sep 17 00:00:00 2001 From: varun Date: Fri, 10 Mar 2023 01:31:45 -0500 Subject: [PATCH] made changes in create connections and tanks controllers regarding start and stop --- src/controllers/createConnectionController.js | 65 +++++++++++++++++- src/controllers/tanksController.js | 68 +++++++++---------- 2 files changed, 98 insertions(+), 35 deletions(-) diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index 1f282ba5..27e1d0e3 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -13,14 +13,52 @@ const fastify = require("fastify")({ exports.createConnections = async (req, body) => { try { const customerId = req.params.customerId; - var tankname = req.body.tankname; + const tankname = req.body.tankname; const tankInfo = await Tank.findOne({ customerId: customerId.toString(),tankName:tankname }) const usertobeInserted = req.body; tankInfo.connections.source = tankInfo.tankName; if (usertobeInserted.inputConnections) tankInfo.connections.inputConnections = usertobeInserted.inputConnections; if (usertobeInserted.outputConnections) tankInfo.connections.outputConnections = usertobeInserted.outputConnections; + + const connection_data = req.body.outputConnections + for (const data of connection_data){ + if(data['output_type'] === "overhead"){ + const tankName = data['outputConnections'] + const tank_info = await Tank.findOne({ customerId: customerId.toString(),tankName:tankName }) + const connection_data_check =tank_info.connections.inputConnections + const sump_names=[] + for(const d of connection_data_check){ + if (sump_names.includes(d.inputConnections)) { + console.log(`${d.inputConnections} exists in ${sump_names}`); + } else { + sump_names.push(d.inputConnections) + } + + } + console.log(sump_names) + //tank_info.connections.inputConnections = tankname.toString() + if (sump_names.includes(tankname)) { + console.log(`${tankname} exists in ${sump_names}`); + } else { + const tankConnections = await Tank.findOneAndUpdate( + { customerId: customerId.toString(), tankName: tankName }, + { $addToSet: { 'connections.inputConnections': { $each: [{ inputConnections: tankname, input_type: 'sump' }] } } }, + { new: true } + ); + } + + + //console.log(tankConnections) + + } + + } + + const tank_connections = await tankInfo.save(); + + return tank_connections; } catch (err) { throw boom.boomify(err); @@ -83,4 +121,29 @@ exports.getConnections = async (req, reply) => { + // exports.createConnectionsforOh = async (req, body) => { + // try { + // const customerId = req.params.customerId; + // var tankname = req.body.tankname; + + // const tankInfo = await Tank.findOne({ customerId: customerId.toString(),tankName:tankname ,tankLocation:"sump"}) + // const connection_data = tankinfo.connections.outputConnections + // for (data in connection_data) { + // console.log(data) + + + // } + // // const usertobeInserted = req.body; + // //tankInfo.connections.source = tankInfo.tankName; + // //if (usertobeInserted.inputConnections) tankInfo.connections.inputConnections = usertobeInserted.inputConnections; + // //if (usertobeInserted.outputConnections) tankInfo.connections.outputConnections = usertobeInserted.outputConnections; + // //const tank_connections = await tankInfo.save(); + // // return tank_connections; + // } catch (err) {. + // throw boom.boomify(err); + // } + // }; + + + diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c9b66618..3dfbcdaa 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -329,41 +329,41 @@ exports.motorAction = async (req, reply) => { - if(supplier_tank_type==="bore" && receiver_type === "overhead"){ - const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank}); - // initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)/2; - // await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } }); - const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); + // if(supplier_tank_type==="bore" && receiver_type === "overhead"){ + // const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank}); + // // initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)/2; + // // await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } }); + // const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); - 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 + 200//Math.floor(supplier_waterlevel * 0.1); - const newSupplierWaterLevel = Math.min(supplier_capacity, supplier_waterlevel + 350);// Math.floor(supplier_waterlevel * 0.15)); - console.log(newWaterLevel) - console.log(newSupplierWaterLevel) - - // Check if updating should stop - if ( (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage ) { - 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); - - - } + // 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 + 200//Math.floor(supplier_waterlevel * 0.1); + // const newSupplierWaterLevel = Math.min(supplier_capacity, supplier_waterlevel + 350);// Math.floor(supplier_waterlevel * 0.15)); + // console.log(newWaterLevel) + // console.log(newSupplierWaterLevel) + + // // Check if updating should stop + // if ( (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage ) { + // 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); + + + // } // reply.send({ status_code: 200, "start time": start_time}); //console.log(start_time)