diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 1acab815..65431a46 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -160,7 +160,7 @@ exports.updateTanklevels = async (req, reply) => { const tankId = tank._id; const tank_name = tank.tankName let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); - let waterLevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); + //let waterLevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); const intervalId = setInterval(async function () { const tank_data = await Tank.findOne({ _id:tankId }); @@ -231,15 +231,15 @@ exports.motorAction = async (req, reply) => { const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10) const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10) const intervals = {}; - + 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) if(action === "start"){ const 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() - console.log(supplier_tank) + if(supplier_tank_type==="sump" && receiver_type === "overhead"){ @@ -251,6 +251,17 @@ exports.motorAction = async (req, reply) => { // 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 overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); + const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); + + if (connection) { + connection.motor_status = "1"; + await overheadTank.save(); + } + + + + // await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) // console.log(supplier_waterlevel) @@ -259,7 +270,7 @@ exports.motorAction = async (req, reply) => { // Calculate new water levels const supplier_tank_info2 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); - const newWaterLevel = receiver_waterlevel + 350//Math.floor(supplier_waterlevel * 0.1); + const newWaterLevel = receiver_waterlevel + 250//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) console.log((newSupplierWaterLevel/supplier_capacity)*100) @@ -268,7 +279,14 @@ exports.motorAction = async (req, reply) => { clearInterval(intervals[receiver_tank]); // Clear the interval for this tank delete intervals[receiver_tank]; await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); - + const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); + const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); + + if (connection) { + connection.motor_status = "0"; + await overheadTank.save(); + } + console.log("end for"+receiver_tank); } else { // Update water levels in database @@ -279,7 +297,7 @@ exports.motorAction = async (req, reply) => { console.log(receiver_tank+""+newWaterLevel+""+"bore to sump") await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }) if (supplier_tank_info2.motor_status==="0"){ - supplier_waterlevel = parseInt(supplier_tank_info2.waterlevel.replace(/,/g, ''), 10)-350 + supplier_waterlevel = parseInt(supplier_tank_info2.waterlevel.replace(/,/g, ''), 10)-250 console.log(supplier_tank+""+newSupplierWaterLevel+""+"s to oh") await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: supplier_waterlevel } }) } @@ -350,7 +368,7 @@ exports.motorAction = async (req, reply) => { 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; + const newWaterLevel = receiver_waterlevel+250; //console.log(newWaterLevel,"2",receiver_tank_info.tankName) // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { @@ -416,6 +434,13 @@ exports.motorAction = async (req, reply) => { else if (action === "stop") { const stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + const overheadTank = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); + const connection = overheadTank.connections.inputConnections.find((conn) => conn.inputConnections === supplier_tank); + + if (connection) { + connection.motor_status = "0"; + await overheadTank.save(); + } // console.log(stop_time) // clearInterval(intervalId); await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); @@ -461,117 +486,181 @@ exports.consumption = async (req, reply) => { } }; -exports.calculateCapacity = async (req, reply) => { - try { - const shape = req.body.shape - if(shape==="rectangle"){ - const { length, width, height } = req.body - - // Ensure all parameters are valid numbers - if (isNaN(length) || isNaN(width) || isNaN(height)) { - reply.code(400).send('Invalid input parameters') - return - } +// exports.calculateCapacity = async (req, reply) => { +// try { +// const shape = req.body.shape +// if(shape==="rectangle"){ +// const { length, width, height } = req.body + +// // Ensure all parameters are valid numbers +// if (isNaN(length) || isNaN(width) || isNaN(height)) { +// reply.code(400).send('Invalid input parameters') +// return +// } - // Calculate the capacity of the water tank in liters - const capacity = length * width * height * 1000 +// // Calculate the capacity of the water tank in liters +// const capacity = length * width * height * 1000 - reply.send({ status_code: 200, capacity: capacity}); +// reply.send({ status_code: 200, capacity: capacity}); - return { message: 'success' }; +// return { message: 'success' }; - } - if(shape==="cylinder"){ - console.log("hii1") - const { length,diameter } = req.body - - // Ensure all parameters are valid numbers - if (isNaN(length) || isNaN(diameter)) { - reply.code(400).send('Invalid input parameters') - return - } +// } +// if(shape==="cylinder"){ +// console.log("hii1") +// const { length,diameter } = req.body - // Calculate the capacity of the water tank in liters - const radius = diameter / 2 - const volume = Math.PI * Math.pow(radius, 2) * length - const capacity = volume * 1000 +// // Ensure all parameters are valid numbers +// if (isNaN(length) || isNaN(diameter)) { +// reply.code(400).send('Invalid input parameters') +// return +// } + +// // Calculate the capacity of the water tank in liters +// const radius = diameter / 2 +// const volume = Math.PI * Math.pow(radius, 2) * length +// const capacity = volume * 1000 - reply.send({ status_code: 200, capacity: capacity}); +// reply.send({ status_code: 200, capacity: capacity}); - return { message: 'success' }; +// return { message: 'success' }; - } +// } -// if(shape==="oval"){ -// console.log("hii3") -// const { length, width, height } = req.body - -// // Ensure all parameters are valid numbers -// if (isNaN(length) || isNaN(width) || isNaN(height)) { -// reply.code(400).send('Invalid input parameters') -// return -// } +// // if(shape==="oval"){ +// // console.log("hii3") +// // const { length, width, height } = req.body + +// // // Ensure all parameters are valid numbers +// // if (isNaN(length) || isNaN(width) || isNaN(height)) { +// // reply.code(400).send('Invalid input parameters') +// // return +// // } -// // Calculate the capacity of the water tank in liters -// const radius = height / 2 -// const a = width - height -// const area = Math.PI * radius * radius + 2 * radius * a -// const volume = area * length -// const capacity = volume * 1000 - -// // Format the result with two decimal places and comma-separated thousands -// const formattedCapacity = capacity.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') +// // // Calculate the capacity of the water tank in liters +// // const radius = height / 2 +// // const a = width - height +// // const area = Math.PI * radius * radius + 2 * radius * a +// // const volume = area * length +// // const capacity = volume * 1000 + +// // // Format the result with two decimal places and comma-separated thousands +// // const formattedCapacity = capacity.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') -// reply.send({ status_code: 200, capacity: formattedCapacity}); +// // reply.send({ status_code: 200, capacity: formattedCapacity}); -// return { message: 'success' }; +// // return { message: 'success' }; -// } +// // } -// if(shape==="horizontalellipse"){ +// // if(shape==="horizontalellipse"){ -// const { length, width, height } = req.body +// // const { length, width, height } = req.body -// // Ensure all parameters are valid numbers -// if (isNaN(length) || isNaN(width) || isNaN(height)) { -// reply.code(400).send('Invalid input parameters') -// return -// } +// // // Ensure all parameters are valid numbers +// // if (isNaN(length) || isNaN(width) || isNaN(height)) { +// // reply.code(400).send('Invalid input parameters') +// // return +// // } + +// // // Calculate the capacity of the water tank in liters +// // const radius1 = length / 2 +// // const radius2 = width / 2 +// // const volume = Math.PI * radius1 * radius2 * height +// // const capacity = volume * 1000 +// // reply.send({ status_code: 200, capacity: capacity}); + + +// // return { message: 'success' }; + +// // } +// if(shape==="userdefined"){ +// const capacity = req.body -// // Calculate the capacity of the water tank in liters -// const radius1 = length / 2 -// const radius2 = width / 2 -// const volume = Math.PI * radius1 * radius2 * height -// const capacity = volume * 1000 // reply.send({ status_code: 200, capacity: capacity}); // return { message: 'success' }; // } -if(shape==="userdefined"){ - const capacity = req.body - reply.send({ status_code: 200, capacity: capacity}); +// } - return { message: 'success' }; +// catch (err) { +// throw boom.boomify(err); +// } +// }; -} +exports.calculateCapacity = async (req, reply) => { + try { + const shape = req.body.shape; + if (shape === "rectangle") { + const { length, width, height } = req.body; + + // Convert input units from feet to meters + const length_m = length * 0.3048; + const width_m = width * 0.3048; + const height_m = height * 0.3048; + console.log(length_m,width_m,height_m) + // Ensure all parameters are valid numbers + if (isNaN(length_m) || isNaN(width_m) || isNaN(height_m)) { + reply.code(400).send("Invalid input parameters"); + return; + } + // Calculate the capacity of the water tank in liters + const capacity = length_m * width_m * height_m * 1000; + + reply.send({ status_code: 200, capacity: capacity }); + + return { message: "success" }; } + if (shape === "cylinder") { + console.log("hii1"); + const { length, diameter } = req.body; - catch (err) { + // Convert input units from feet to meters + const length_m = length * 0.3048; + const diameter_m = diameter * 0.3048; + + // Ensure all parameters are valid numbers + if (isNaN(length_m) || isNaN(diameter_m)) { + reply.code(400).send("Invalid input parameters"); + return; + } + + // Calculate the capacity of the water tank in liters + const radius = diameter_m / 2; + const volume = Math.PI * Math.pow(radius, 2) * length_m; + const capacity = volume * 1000; + + reply.send({ status_code: 200, capacity: capacity }); + + return { message: "success" }; + } + + // Add similar conversions for other shapes if necessary + + if (shape === "userdefined") { + const capacity = req.body; + + reply.send({ status_code: 200, capacity: capacity }); + + return { message: "success" }; + } + } catch (err) { throw boom.boomify(err); } }; + exports.IotDevice = async (req, reply) => { try { const { hardwareId, tankHeight, maxLevel, minLevel, mode } = req.body; diff --git a/src/models/tanks.js b/src/models/tanks.js index 0eb3a490..7ab93b34 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -23,7 +23,7 @@ const tanksSchema = new mongoose.Schema({ motor_status: { type: String, default: 0 }, connections: { source: { type: String}, - inputConnections: [{ inputConnections: String,input_type:String }], + inputConnections: [{ inputConnections: String,input_type:String,motor_status:String }], outputConnections: [{ outputConnections: String,output_type:String }] }