From f470c2109e2435c889238f2ecac8c0fd07bcf40f Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 17 Jul 2025 13:13:18 +0530 Subject: [PATCH 1/2] changes on compare water level --- src/controllers/tanksController.js | 116 +++++++++++++++++++++-------- 1 file changed, 86 insertions(+), 30 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 25ac71ba..e6375e93 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -8293,6 +8293,8 @@ exports.compareMeasuredHeight = async (req, reply) => { // tankHeight from body is already in cm const tankHeightInCmFromBody = Math.round(tankHeight); + console.log("tankHeightInCmFromBody",tankHeightInCmFromBody) + console.log("tankHeightInCmFromDB",tankHeightInCmFromDB) // 🔹 Sensor data (use DB tank height) let sensorGapCm = null; @@ -8322,37 +8324,91 @@ exports.compareMeasuredHeight = async (req, reply) => { } // 🔹 Manual data (use tankHeight from body) - const manualWaterLevelInCm = Math.round(measuredHeight); // measuredHeight in cm - const manualWaterLevelLiters = Math.round(manualWaterLevelInCm * capacityPerCm); - - // 🔹 Comparison - const heightDifferenceInCm = Math.abs(manualWaterLevelInCm - (sensorWaterLevelInCm ?? 0)); - const comparisonMessage = heightDifferenceInCm <= 10 - ? "Manual measurement matches within 10 cm of sensor data." - : "Manual measurement not matched within range 10 cm."; + // const manualWaterLevelInCm = Math.round(measuredHeight); // measuredHeight in cm + // const manualWaterLevelLiters = Math.round(manualWaterLevelInCm * capacityPerCm); + + // // 🔹 Comparison + // const heightDifferenceInCm = Math.abs(manualWaterLevelInCm - (sensorWaterLevelInCm ?? 0)); + // const comparisonMessage = heightDifferenceInCm <= 10 + // ? "Manual measurement matches within 10 cm of sensor data." + // : "Manual measurement not matched within range 10 cm."; + + // reply.send({ + // status_code: 200, + // data: { + // tankName, + // capacity: tank.capacity, + // sensor: { + // tankHeightInCm: dynamicTankHeightInCm, // from DB + // sensorGapCm, + // waterLevelInCm: sensorWaterLevelInCm, + // waterLevelLiters: sensorWaterLevelLiters + // }, + // manual: { + // tankHeightInCm: tankHeightInCmFromBody, // passed in body + // measuredHeightCm: manualWaterLevelInCm, + // waterLevelLiters: manualWaterLevelLiters + // }, + // comparison: { + // heightDifferenceInCm, + // message: comparisonMessage + // } + // } + // }); + + // 🔹 Manual data +// 🔹 Manual data +const manualWaterLevelInCm = Math.round(measuredHeight); // measuredHeight in cm +const manualWaterLevelLiters = Math.round(manualWaterLevelInCm * capacityPerCm); + +// 🔹 Comparison +const waterLevelDifferenceInCm = Math.abs(manualWaterLevelInCm - (sensorWaterLevelInCm ?? 0)); +const tankHeightDifferenceInCm = Math.abs(tankHeightInCmFromDB - (tankHeightInCmFromBody ?? 0 )); + +// require *both* to be within 10cm +// const isWithinRange = waterLevelDifferenceInCm <= 10 && tankHeightDifferenceInCm <= 10; + +// const comparisonMessage = isWithinRange +// ? "Manual measurement matches within 10 cm of sensor data and tank height." +// : "Manual measurement not matched within range 10 cm."; +const isWaterLevelMatch = waterLevelDifferenceInCm <= 10; +const isTankHeightMatch = tankHeightDifferenceInCm <= 10; + +let comparisonMessage = ""; + +if (isWaterLevelMatch && isTankHeightMatch) { + comparisonMessage = "✅ Manual measurement matches: water level and tank height both within 10 cm range."; +} else if (isWaterLevelMatch) { + comparisonMessage = "✅ Manual water level matches within 10 cm, but tank height does not."; +} else if (isTankHeightMatch) { + comparisonMessage = "✅ Manual tank height matches within 10 cm, but water level does not."; +} else { + comparisonMessage = "❌ Manual measurement does not match within 10 cm range for either water level or tank height."; +} - reply.send({ - status_code: 200, - data: { - tankName, - capacity: tank.capacity, - sensor: { - tankHeightInCm: dynamicTankHeightInCm, // from DB - sensorGapCm, - waterLevelInCm: sensorWaterLevelInCm, - waterLevelLiters: sensorWaterLevelLiters - }, - manual: { - tankHeightInCm: tankHeightInCmFromBody, // passed in body - measuredHeightCm: manualWaterLevelInCm, - waterLevelLiters: manualWaterLevelLiters - }, - comparison: { - heightDifferenceInCm, - message: comparisonMessage - } - } - }); +reply.send({ + status_code: 200, + data: { + tankName, + capacity: tank.capacity, + sensor: { + tankHeightInCm: dynamicTankHeightInCm, // from DB + IoT data + sensorGapCm, + waterLevelInCm: sensorWaterLevelInCm, + waterLevelLiters: sensorWaterLevelLiters + }, + manual: { + tankHeightInCm: tankHeightInCmFromBody, + measuredHeightCm: manualWaterLevelInCm, + waterLevelLiters: manualWaterLevelLiters + }, + comparison: { + waterLevelDifferenceInCm, + tankHeightDifferenceInCm, + message: comparisonMessage + } + } +}); } catch (err) { console.error(err); From eb1261ce5f7689548a6203bd57a209e887f3709c Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 17 Jul 2025 13:17:08 +0530 Subject: [PATCH 2/2] changes --- src/controllers/tanksController.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e6375e93..c8c5b326 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -8386,6 +8386,7 @@ if (isWaterLevelMatch && isTankHeightMatch) { comparisonMessage = "❌ Manual measurement does not match within 10 cm range for either water level or tank height."; } + reply.send({ status_code: 200, data: {