diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 3dacce7d..be6c63cd 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1290,16 +1290,17 @@ exports.IotDevice = async (req, reply) => { // Update waterlevel in tanksSchema for each tank for (const tank of tanks) { const { tankhardwareId, tankHeight } = tank; - console.log(tankhardwareId) + // console.log(tankhardwareId) // Find the corresponding tank in tanksSchema const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); - - + const customerId = existingTank.customerId + // console.log(existingTank,tankhardwareId,customerId) + const tank_name = existingTank.tankName 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. @@ -1309,18 +1310,17 @@ exports.IotDevice = async (req, reply) => { // 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) { @@ -1328,18 +1328,38 @@ function numberWithCommas(x) { } 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); + // Save the updated tank document await existingTank.save(); + for (const outputConnection of existingTank.connections.outputConnections) { + + + const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type }); + if (linkedTank) { + console.log(linkedTank) + // linkedTank.waterlevel = existingTank.waterlevel; + //await linkedTank.save(); + + // Update water level of tanks linked through input connections of the linked tank + for (const inputConnection of linkedTank.connections.inputConnections) { + if (inputConnection.inputConnections === tank_name) { + inputConnection.water_level = parseInt(formatted_water_level.replace(/,/g, ''), 10) + await linkedTank.save(); + } + } + } + } + } } - + + // Send the latest three documents const latestOttanks = await IotData.find({ hardwareId }) .sort({ date: -1, time: -1 });