diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c171cc27..0110fa94 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1175,7 +1175,6 @@ exports.calculateCapacity = async (req, reply) => { // reply.code(500).send({ error: err.message }); // } // }; - exports.IotDevice = async (req, reply) => { try { const { hardwareId, mode, tanks } = req.body; @@ -1218,49 +1217,34 @@ exports.IotDevice = async (req, reply) => { // Find the corresponding tank in tanksSchema const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); - if (existingTank) { // Update the waterlevel using the tankHeight value - - const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48; - console.log(tank_height1, 25); - - // The value of tank_height1 is a number, not a string, so you cannot use replace on it. - // If you want to format it with commas, you can create a function to add commas to a number. - function numberWithCommas(x) { - return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); - } - - // Now you can use the function to format the tank_height1 value with commas. - const formatted_tank_height1 = numberWithCommas(tank_height1); - console.log(formatted_tank_height1, 25); - - const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10); - console.log(tank_height); - // console.log(tank_height,1) - const water_level_height = tank_height - tankHeight - console.log(water_level_height,2) - - const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10) - console.log(waterCapacityPerCm,3) - const water_level = water_level_height * waterCapacityPerCm; -console.log(water_level, 4); - -// Function to add commas to a number -function numberWithCommas(x) { - return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); -} - -const formatted_water_level = numberWithCommas(water_level); -console.log(formatted_water_level, 4); - -existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10); -console.log(existingTank.waterlevel); - - + const tank_height = parseInt(existingTank.height.replace(/,/g, ''), 10) * 30.48; + const water_level_height = tank_height - tankHeight; + const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10); + const water_level = water_level_height * waterCapacityPerCm; + existingTank.waterlevel = water_level.toString(); // Convert to string as per schema definition // Save the updated tank document await existingTank.save(); + + // Update water level of tanks linked through output connections + for (const outputConnection of existingTank.connections.outputConnections) { + const linkedTank = await Tank.findOne({ hardwareId: outputConnection.outputConnections }); + if (linkedTank) { + linkedTank.waterlevel = existingTank.waterlevel; + await linkedTank.save(); + } + } + + // Update water level of tanks linked through input connections + for (const inputConnection of existingTank.connections.inputConnections) { + const linkedTank = await Tank.findOne({ hardwareId: inputConnection.inputConnections }); + if (linkedTank) { + linkedTank.waterlevel = existingTank.waterlevel; + await linkedTank.save(); + } + } } }