diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 1d5ef4bd..001d8cda 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -7867,6 +7867,180 @@ exports.validateTankHeight = async (req, reply) => { // } // }; +// exports.getActualWaterLevelInCm = async (req, reply) => { +// try { +// const { tankName } = req.params; + +// if (!tankName) { +// return reply.status(400).send({ message: "Tank name is required." }); +// } + +// const tank = await Tank.findOne({ tankName }); +// if (!tank) { +// return reply.status(404).send({ message: "Tank not found." }); +// } + +// const tankHeightFeet = parseFloat(tank.height); +// const tankHeightCm = tankHeightFeet * 30.48; + +// let actualWaterLevelInCm; +// let actualWaterLevelLiters; + +// if (parseFloat(tank.waterlevel) > 0) { +// // ✅ Direct conversion from liters to cm +// const waterlevelLiters = parseFloat(tank.waterlevel); +// const capacityPerCm = parseFloat(tank.waterCapacityPerCm); + +// if (!capacityPerCm || capacityPerCm <= 0) { +// return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." }); +// } + +// actualWaterLevelInCm = waterlevelLiters / capacityPerCm; +// actualWaterLevelLiters = waterlevelLiters; + +// } else { +// // ✅ Fallback to IoT data to calculate cm & liters +// const iotData = await IotData.findOne({ +// hardwareId: tank.hardwareId, +// "tanks.tankhardwareId": tank.tankhardwareId +// }).sort({ date: -1 }); +// console.log("iotData",iotData) +// if (!iotData) { +// return reply.status(404).send({ message: "No IoT data found for this tank." }); +// } + +// const matchingTank = iotData.tanks.find( +// t => t.tankhardwareId === tank.tankhardwareId +// ); + +// if (!matchingTank) { +// return reply.status(404).send({ message: "No matching tank found in IoT data." }); +// } + +// const tankHeightFromSensor = parseFloat(matchingTank.tankHeight); +// console.log("tankHeightFromSensor",tankHeightFromSensor) + +// if (isNaN(tankHeightFromSensor)) { +// return reply.status(400).send({ message: "Invalid tankHeight from IoT data." }); +// } + +// actualWaterLevelInCm = tankHeightCm - tankHeightFromSensor; + +// const capacityPerCm = parseFloat(tank.waterCapacityPerCm); +// if (!capacityPerCm || capacityPerCm <= 0) { +// return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." }); +// } + +// // ✅ Convert back to liters using height in cm +// actualWaterLevelLiters = actualWaterLevelInCm * capacityPerCm; +// } + +// reply.send({ +// status_code: 200, +// data: { +// tankName: tank.tankName, +// tankHeight:tankHeightCm, +// actualWaterLevel: `${actualWaterLevelLiters.toFixed(2)} L`, +// actualWaterLevelInCm: `${actualWaterLevelInCm.toFixed(2)} cm` +// } +// }); + +// } catch (err) { +// console.error(err); +// reply.status(500).send({ message: err.message || "Internal Server Error" }); +// } +// }; + +// exports.getActualWaterLevelInCm = async (req, reply) => { +// try { +// const { tankName } = req.params; + +// if (!tankName) { +// return reply.status(400).send({ message: "Tank name is required." }); +// } + +// const tank = await Tank.findOne({ tankName }); +// if (!tank) { +// return reply.status(404).send({ message: "Tank not found." }); +// } + +// const tankHeightFeet = parseFloat(tank.height); +// const tankHeightCm = tankHeightFeet * 30.48; + +// const capacityPerCm = parseFloat(tank.waterCapacityPerCm); +// console.log("capacityPerCm",capacityPerCm) +// if (!capacityPerCm || capacityPerCm <= 0) { +// return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." }); +// } + +// let sensorWaterLevelInCm = 0; +// let sensorWaterLevelLiters = 0; + +// // By default, try to use tank.waterlevel +// let waterlevelLiters = parseFloat(tank.waterlevel); + +// if (waterlevelLiters > 0) { +// // Use manual waterlevel +// sensorWaterLevelInCm = waterlevelLiters / capacityPerCm; +// sensorWaterLevelLiters = waterlevelLiters; +// // } else { +// // Fallback to IoT data +// const iotData = await IotData.findOne({ +// hardwareId: tank.hardwareId, +// "tanks.tankhardwareId": tank.tankhardwareId +// }).sort({ date: -1 }); + +// console.log("iotData", iotData); + +// if (!iotData) { +// return reply.status(404).send({ message: "No IoT data found for this tank." }); +// } + +// const matchingTank = iotData.tanks.find( +// t => t.tankhardwareId === tank.tankhardwareId +// ); + +// console.log("matchingTank", matchingTank); + + +// if (!matchingTank) { +// return reply.status(404).send({ message: "No matching tank found in IoT data." }); +// } + +// const tankHeightFromSensor = parseFloat(matchingTank.tankHeight); +// console.log("tankHeightFromSensor", tankHeightFromSensor); + +// if (isNaN(tankHeightFromSensor)) { +// return reply.status(400).send({ message: "Invalid tankHeight from IoT data." }); +// } + +// // sensorWaterLevelInCm = full tank height in cm - distance from sensor +// sensorWaterLevelInCm = tankHeightCm - tankHeightFromSensor; +// console.log("sensorWaterLevelInCm",sensorWaterLevelInCm) +// sensorWaterLevelLiters = sensorWaterLevelInCm * capacityPerCm; +// console.log("sensorWaterLevelLiters",sensorWaterLevelLiters) + +// } + +// reply.send({ +// status_code: 200, +// data: { +// tankName: tank.tankName, +// tankHeight: tankHeightCm, +// TankWaterLevel: tank.waterlevel, +// capacity: tank.capacity, +// sensorWaterLevelInCm: `${sensorWaterLevelInCm.toFixed(2)} cm`, +// sensorWaterLevel: `${sensorWaterLevelLiters.toFixed(2)} L`, + +// } +// }); + +// } catch (err) { +// console.error(err); +// reply.status(500).send({ message: err.message || "Internal Server Error" }); +// } +// }; + exports.getActualWaterLevelInCm = async (req, reply) => { try { const { tankName } = req.params; @@ -7883,63 +8057,56 @@ exports.getActualWaterLevelInCm = async (req, reply) => { const tankHeightFeet = parseFloat(tank.height); const tankHeightCm = tankHeightFeet * 30.48; - let actualWaterLevelInCm; - let actualWaterLevelLiters; + const capacityPerCm = parseFloat(tank.waterCapacityPerCm); + console.log("capacityPerCm", capacityPerCm); + if (!capacityPerCm || capacityPerCm <= 0) { + return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." }); + } - if (parseFloat(tank.waterlevel) > 0) { - // ✅ Direct conversion from liters to cm - const waterlevelLiters = parseFloat(tank.waterlevel); - const capacityPerCm = parseFloat(tank.waterCapacityPerCm); + let sensorWaterLevelInCm = 0; + let sensorWaterLevelLiters = 0; - if (!capacityPerCm || capacityPerCm <= 0) { - return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." }); - } + let manualWaterLevelLiters = parseFloat(tank.waterlevel) || 0; + let manualWaterLevelInCm = manualWaterLevelLiters > 0 + ? manualWaterLevelLiters / capacityPerCm + : 0; - actualWaterLevelInCm = waterlevelLiters / capacityPerCm; - actualWaterLevelLiters = waterlevelLiters; + // Always try to also get IoT data + const iotData = await IotData.findOne({ + hardwareId: tank.hardwareId, + "tanks.tankhardwareId": tank.tankhardwareId + }).sort({ date: -1 }); - } else { - // ✅ Fallback to IoT data to calculate cm & liters - const iotData = await IotData.findOne({ - hardwareId: tank.hardwareId, - "tanks.tankhardwareId": tank.tankhardwareId - }).sort({ date: -1 }); - - if (!iotData) { - return reply.status(404).send({ message: "No IoT data found for this tank." }); - } + console.log("iotData", iotData); + if (iotData) { const matchingTank = iotData.tanks.find( t => t.tankhardwareId === tank.tankhardwareId ); - if (!matchingTank) { - return reply.status(404).send({ message: "No matching tank found in IoT data." }); - } - - const tankHeightFromSensor = parseFloat(matchingTank.tankHeight); + console.log("matchingTank", matchingTank); - if (isNaN(tankHeightFromSensor)) { - return reply.status(400).send({ message: "Invalid tankHeight from IoT data." }); - } - - actualWaterLevelInCm = tankHeightCm - tankHeightFromSensor; + if (matchingTank) { + const tankHeightFromSensor = parseFloat(matchingTank.tankHeight); + console.log("tankHeightFromSensor", tankHeightFromSensor); - const capacityPerCm = parseFloat(tank.waterCapacityPerCm); - if (!capacityPerCm || capacityPerCm <= 0) { - return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." }); + if (!isNaN(tankHeightFromSensor)) { + sensorWaterLevelInCm = tankHeightCm - tankHeightFromSensor; + sensorWaterLevelLiters = sensorWaterLevelInCm * capacityPerCm; + } } - - // ✅ Convert back to liters using height in cm - actualWaterLevelLiters = actualWaterLevelInCm * capacityPerCm; } reply.send({ status_code: 200, data: { tankName: tank.tankName, - actualWaterLevel: `${actualWaterLevelLiters.toFixed(2)} L`, - actualWaterLevelInCm: `${actualWaterLevelInCm.toFixed(2)} cm` + tankHeight: tankHeightCm, + capacity: tank.capacity, + currentWaterLevel: `${manualWaterLevelLiters.toFixed(2)} L`, + currentWaterLevelInCm: `${manualWaterLevelInCm.toFixed(2)} cm`, + sensorWaterLevel: `${sensorWaterLevelLiters.toFixed(2)} L`, + sensorWaterLevelInCm: `${sensorWaterLevelInCm.toFixed(2)} cm` } });