diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index 1f282ba5..28540979 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -1,5 +1,7 @@ //const Connections = require("../models/CreateConnections"); -const Tank = require("../models/tanks"); +//const Tank = require("../models/tanks"); +const { Tank, MotorData } = require('../models/tanks') + const User = require("../models/User"); const boom = require("boom"); @@ -13,14 +15,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 +123,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 1b11b2a9..1aae06e6 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1,4 +1,6 @@ -const Tank = require("../models/tanks"); +//const Tank = require("../models/tanks"); +const { Tank, MotorData } = require('../models/tanks') + const User = require("../models/User"); const boom = require("boom"); const fastify = require("fastify")({ @@ -33,7 +35,7 @@ console.log(req.params); capacity: req.body.capacity, typeOfWater: req.body.typeOfWater, - tankLocation:req.body.tankLocation, + tankLocation:(req.body.tankLocation).toLowerCase(), }; var tank_name = req.body.tankName @@ -56,7 +58,7 @@ console.log(req.params); tank.capacity = usertobeInserted.capacity; tank.typeOfWater = usertobeInserted.typeOfWater; - tank.tankLocation = usertobeInserted.tankLocation; + tank.tankLocation = (usertobeInserted.tankLocation).toLowerCase(); } } const insertedTank = await tank.save(); @@ -154,20 +156,23 @@ exports.updateTanklevels = async (req, reply) => { let waterLevel = capacity - 100; // initial water level const intervalId = setInterval(async function () { - const newWaterLevel = Math.floor(waterLevel / 1.5); - + const newWaterLevel = Math.floor(waterLevel - 200); + + if (newWaterLevel <= 0) { + clearInterval(intervals[tankId]); + console.log(`Stopped updating tank with ID ${tankId}`); + return; + } + else{ const result = await Tank.findOneAndUpdate( { _id: tankId }, { $set: { waterlevel: newWaterLevel } } ); + } - console.log(result); + // console.log(result); - if (newWaterLevel === 0) { - clearInterval(intervals[tankId]); - console.log(`Stopped updating tank with ID ${tankId}`); - return; - } + waterLevel = newWaterLevel; }, 2000); @@ -197,14 +202,14 @@ exports.updateTanklevels1 = async (req, reply) => { let waterLevel = capacity - 100; // initial water level const intervalId = setInterval(async function () { - const newWaterLevel = Math.floor(waterLevel / 1.5); - + const newWaterLevel = Math.floor(waterLevel - 200); + console.log(tank.tankName,newWaterLevel) const result = await Tank.findOneAndUpdate( { customerId, tankName, }, { $set: { waterlevel: newWaterLevel } } ); - console.log(result); + // console.log(result); if (newWaterLevel === 0) { clearInterval(intervals[tankName]); @@ -235,7 +240,6 @@ exports.getTanklevels = async (req, reply) => { reply.send({ status_code: 200, data: updated_data}); - return { message: 'success' }; } @@ -250,69 +254,58 @@ exports.getTanklevels = async (req, reply) => { 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}); + const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:req.body.to_type}); const receiver_capacity = receiver_tank_info.capacity + const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10) + if(action === "start"){ + // start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + supplier_tank = req.body.from supplier_tank_type = req.body.from_type receiver_type = req.body.to_type + console.log(supplier_tank) - // if(supplier_tank_type==="sump" && receiver_type === "overhead"){ - // const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank}); - // initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; - // await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } }); - // const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); - - //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 () { - // 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/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==="sump" && receiver_type === "overhead"){ + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); - if(supplier_tank_type==="bore" && receiver_type === "sump"){ - const receiver_capacity = receiver_tank_info.capacity + const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); + console.log(supplier_tank_info1) + 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}); + + // 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 () { // Calculate new water levels - const newWaterLevel = receiver_waterlevel+225; + const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); + const newWaterLevel = receiver_waterlevel + 200//Math.floor(supplier_waterlevel * 0.1); + const newSupplierWaterLevel = supplier_waterlevel - 200//Math.floor(supplier_waterlevel * 0.1); + const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10) + // Check if updating should stop - if ((newWaterLevel/receiver_capacity)*100 >= 97) { + if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0") { clearInterval(intervalId) + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); + 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: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }), + Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: newSupplierWaterLevel } }) ]); } }, 2000); @@ -321,49 +314,139 @@ exports.motorAction = async (req, reply) => { - if(supplier_tank_type==="sump" && 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==="sump" && receiver_type === "overhead"){ + // await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 1 } }); - let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) + // // console.log(rcvr_info.motor_status) + // 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 rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank}); + // 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) + // // console.log(rcvr_info.motor_status) + // // console.log(rcvr_info.tankName) + + // // Check if updating should stop + // if ( (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === 0) { + + // clearInterval(intervalId) + // await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 0 } }); + + // 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" && receiver_type === "sump"){ + const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) + 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) + console.log(receiver_waterlevel,"1") 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)); - + const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); + console.log(rcvr_info.motor_status) + const newWaterLevel = receiver_waterlevel+200; + console.log(newWaterLevel,"2") // Check if updating should stop - if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95) { + 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(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) + + console.log((newWaterLevel/receiver_capacity)*100,"4") await Promise.all([ - Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { waterlevel: newWaterLevel } }), - Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: newSupplierWaterLevel } }) + Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }), ]); } }, 2000); - } + console.log(customerId,req.body.from,req.body.from_type,receiver_tank,req.body.to_type,) + motorData = { + + customerId:customerId, + supplierTank : req.body.from, + supplier_type: req.body.from_type, + receiverTank: receiver_tank, + receiver_type: req.body.to_type, + startTime: req.body.startTime, + stopTime: req.body.stopTime, + + + + }; + var motorData = new MotorData(motorData); + + checkFormEncoding = isUserFormUrlEncoded(req); + if (checkFormEncoding.isUserFormUrlEncoded) { + usertobeInserted = checkFormEncoding.motorData; + console.log("thsi true url string"); + motorData.customerId = customerId; + motorData.supplierTank = req.body.from; + motorData.receiverTank = receiver_tank; + motorData.supplier_type = req.body.from_type; + motorData.receiver_type = req.body.to_type; + motorData.startTime = usertobeInserted.startTime; + motorData.stopTime = usertobeInserted.stopTime; + + + } + const motor_data = await motorData.save(); + reply.send({ status_code: 200, data: motor_data }); + return motor_data + + + + + // reply.send({ status_code: 200, "start time": start_time}); + //console.log(start_time) } else if (action === "stop") { - clearInterval(intervalId); + // 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}, { $set: { motor_status: "0" } }); + + // reply.send({ status_code: 200, "stop time": stop_time}); } 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}`); diff --git a/src/controllers/userController.js b/src/controllers/userController.js index b1533634..df0fd65d 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -168,7 +168,7 @@ exports.addUser = async (req, reply) => { // await resetCounter();//to set customer id back to 0 var c_id = await generateCustomerId() var building= ((req.body.buildingName).slice(0, 3)).toUpperCase(); - var customer_id = `AWS${building}${c_id}` + var customer_id = `AWSU${building}${c_id}` // console.log("This is the reply in the handler after the validations", reply); userData = { customerId: customer_id, diff --git a/src/models/tanks.js b/src/models/tanks.js index 93065055..b1edeb69 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -16,12 +16,32 @@ const tanksSchema = new mongoose.Schema({ typeOfWater: { type: String, default: null }, waterlevel: { type: String, default: "0" }, tankLocation: { type: String, default: null }, + motor_status: { type: String, default: 0 }, connections: { source: { type: String}, inputConnections: [{ inputConnections: String,input_type:String }], outputConnections: [{ outputConnections: String,output_type:String }] } + +}); + + +const motordataSchema = new mongoose.Schema({ + customerId: { type: String, default: null }, + supplierTank: { type: String, default: null }, + receiverTank: { type: String, default: null }, + startTime: { type: String, default: null }, + stopTime: { type: String, default: null }, + supplier_type: { type: String, default: null }, + receiver_type: { type: String, default: null }, + + }); -module.exports = mongoose.model("Tank", tanksSchema); \ No newline at end of file +const Tank = mongoose.model("Tank", tanksSchema); +const MotorData = mongoose.model("MotorData", motordataSchema); + +module.exports = { + Tank, MotorData +} diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 572f939e..e96e30ec 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -231,7 +231,9 @@ module.exports = function (fastify, opts, next) { from_type: { type: "string" }, to_type: { type: "string" }, action: { type: "string" }, - + percentage: { type: "string",default: "100" }, + startTime:{ type: "string" }, + stopTime:{ type: "string" }, }, }, security: [