diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 0c8cf0a8..208852b9 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1905,6 +1905,7 @@ exports.getTankDetailsByMaster = async (req, reply) => { data: { masterName: masterName, location: location, + type:masterDevice.type, hardwareId: hardwareId, tankName: tank.tankName, blockName: tank.blockName, @@ -1995,6 +1996,7 @@ exports.getSlaveTankDetails = async (req, reply) => { masterName, location, slaveHardwareId: hardwareId, + type: slaveDevice.type, tankHardwareId, tankName: tank.tankName, blockName: tank.blockName, diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 001d8cda..68d11662 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -8041,9 +8041,143 @@ exports.validateTankHeight = async (req, reply) => { // } // }; -exports.getActualWaterLevelInCm = 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; + +// 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; + +// let manualWaterLevelLiters = parseFloat(tank.waterlevel) || 0; +// let manualWaterLevelInCm = manualWaterLevelLiters > 0 +// ? manualWaterLevelLiters / capacityPerCm +// : 0; + +// // Always try to also get IoT data +// const iotData = await IotData.findOne({ +// hardwareId: tank.hardwareId, +// "tanks.tankhardwareId": tank.tankhardwareId +// }).sort({ date: -1 }); + +// console.log("iotData", iotData); + +// if (iotData) { +// const matchingTank = iotData.tanks.find( +// t => t.tankhardwareId === tank.tankhardwareId +// ); + +// console.log("matchingTank", matchingTank); + +// if (matchingTank) { +// const tankHeightFromSensor = parseFloat(matchingTank.tankHeight); +// console.log("tankHeightFromSensor", tankHeightFromSensor); + +// if (!isNaN(tankHeightFromSensor)) { +// sensorWaterLevelInCm = tankHeightCm - tankHeightFromSensor; +// sensorWaterLevelLiters = sensorWaterLevelInCm * capacityPerCm; +// } +// } +// } + +// reply.send({ +// status_code: 200, +// data: { +// tankName: tank.tankName, +// 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` +// } +// }); + +// } catch (err) { +// console.error(err); +// reply.status(500).send({ message: err.message || "Internal Server Error" }); +// } +// }; + + +// exports.compareMeasuredHeight = async (req, reply) => { +// try { +// const { measuredHeight, tankName } = req.body; + +// if (!tankName || measuredHeight === undefined) { +// return reply.status(400).send({ message: "Tank name and measured height are required." }); +// } + +// // Convert measuredHeight to a number +// const measuredHeightNum = parseFloat(measuredHeight); +// if (isNaN(measuredHeightNum)) { +// return reply.status(400).send({ message: "Invalid measuredHeight. It must be a number." }); +// } + +// // Fetch tank details using tankName +// const tank = await Tank.findOne({ tankName }); + +// if (!tank) { +// return reply.status(404).send({ message: "Tank not found." }); +// } + +// const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters +// const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm + +// if (isNaN(actualWaterLevel) || isNaN(waterCapacityPerCm)) { +// return reply.status(400).send({ message: "Tank data is incomplete for conversion." }); +// } + +// // Convert actual water level from liters to cm +// const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm; + +// // Calculate difference between measured and actual water level in cm +// const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeightNum); + +// let message; +// if (heightDifferenceInCm <= 10) { +// message = "Manual measurement is match within 10 cm of the sensor data."; +// } else { +// message = "Manual measurement not matched within range 10cm from the sensor data."; +// } + +// reply.send({ +// status_code: 200, +// data: { +// tankName, +// measuredHeight: measuredHeightNum.toFixed(2) + " cm", +// actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm", +// heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm", +// message +// } +// }); + +// } catch (err) { +// console.error("Error in compareMeasuredHeight:", err); +// reply.status(500).send({ message: err.message }); +// } +// }; + +exports.compareMeasuredHeight = async (req, reply) => { try { - const { tankName } = req.params; + const { tankName, measuredHeight } = req.body; if (!tankName) { return reply.status(400).send({ message: "Tank name is required." }); @@ -8055,58 +8189,72 @@ exports.getActualWaterLevelInCm = async (req, reply) => { } const tankHeightFeet = parseFloat(tank.height); - const tankHeightCm = tankHeightFeet * 30.48; + const tankHeightCm = Math.round(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." }); } + const manualWaterLevelLiters = parseFloat(tank.waterlevel) || 0; + const manualWaterLevelInCm = Math.round(manualWaterLevelLiters / capacityPerCm); + let sensorWaterLevelInCm = 0; let sensorWaterLevelLiters = 0; - let manualWaterLevelLiters = parseFloat(tank.waterlevel) || 0; - let manualWaterLevelInCm = manualWaterLevelLiters > 0 - ? manualWaterLevelLiters / capacityPerCm - : 0; - - // Always try to also get IoT data + // Try to get IoT data const iotData = await IotData.findOne({ hardwareId: tank.hardwareId, "tanks.tankhardwareId": tank.tankhardwareId }).sort({ date: -1 }); - console.log("iotData", iotData); - if (iotData) { const matchingTank = iotData.tanks.find( t => t.tankhardwareId === tank.tankhardwareId ); - - console.log("matchingTank", matchingTank); - if (matchingTank) { const tankHeightFromSensor = parseFloat(matchingTank.tankHeight); - console.log("tankHeightFromSensor", tankHeightFromSensor); - if (!isNaN(tankHeightFromSensor)) { - sensorWaterLevelInCm = tankHeightCm - tankHeightFromSensor; - sensorWaterLevelLiters = sensorWaterLevelInCm * capacityPerCm; + const rawSensorWaterLevelInCm = tankHeightCm - Math.round(tankHeightFromSensor); + sensorWaterLevelInCm = Math.max(0, rawSensorWaterLevelInCm); + sensorWaterLevelLiters = Math.round(sensorWaterLevelInCm * capacityPerCm); } } } + // Include comparison if measuredHeight provided + let comparison = null; + if (measuredHeight !== undefined) { + const measuredHeightNum = parseFloat(measuredHeight); + if (!isNaN(measuredHeightNum)) { + const measuredHeightRounded = Math.round(measuredHeightNum); + const heightDifferenceInCm = Math.abs(manualWaterLevelInCm - measuredHeightRounded); + const message = heightDifferenceInCm <= 10 + ? "Manual measurement matches within 10 cm of sensor/manual data." + : "Manual measurement not matched within range 10 cm."; + + comparison = { + measuredHeight: measuredHeightRounded + " cm", + actualWaterLevelInCm: manualWaterLevelInCm + " cm", + heightDifferenceInCm: heightDifferenceInCm + " cm", + message + }; + } else { + comparison = { message: "Invalid measuredHeight; must be a number." }; + } + } + reply.send({ status_code: 200, data: { tankName: tank.tankName, - tankHeight: tankHeightCm, + tankHeightInCm: tankHeightCm, capacity: tank.capacity, - currentWaterLevel: `${manualWaterLevelLiters.toFixed(2)} L`, - currentWaterLevelInCm: `${manualWaterLevelInCm.toFixed(2)} cm`, - sensorWaterLevel: `${sensorWaterLevelLiters.toFixed(2)} L`, - sensorWaterLevelInCm: `${sensorWaterLevelInCm.toFixed(2)} cm` + manualWaterLevel: manualWaterLevelLiters, // as integer + manualWaterLevelInCm: manualWaterLevelInCm + " cm", + sensorWaterLevel: sensorWaterLevelLiters, // as integer + sensorWaterLevelInCm: sensorWaterLevelInCm + " cm", + ...(comparison && { comparison }) } }); @@ -8117,64 +8265,6 @@ exports.getActualWaterLevelInCm = async (req, reply) => { }; -exports.compareMeasuredHeight = async (req, reply) => { - try { - const { measuredHeight, tankName } = req.body; - - if (!tankName || measuredHeight === undefined) { - return reply.status(400).send({ message: "Tank name and measured height are required." }); - } - - // Convert measuredHeight to a number - const measuredHeightNum = parseFloat(measuredHeight); - if (isNaN(measuredHeightNum)) { - return reply.status(400).send({ message: "Invalid measuredHeight. It must be a number." }); - } - - // Fetch tank details using tankName - const tank = await Tank.findOne({ tankName }); - - if (!tank) { - return reply.status(404).send({ message: "Tank not found." }); - } - - const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters - const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm - - if (isNaN(actualWaterLevel) || isNaN(waterCapacityPerCm)) { - return reply.status(400).send({ message: "Tank data is incomplete for conversion." }); - } - - // Convert actual water level from liters to cm - const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm; - - // Calculate difference between measured and actual water level in cm - const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeightNum); - - let message; - if (heightDifferenceInCm <= 10) { - message = "Manual measurement is match within 10 cm of the sensor data."; - } else { - message = "Manual measurement not matched within range 10cm from the sensor data."; - } - - reply.send({ - status_code: 200, - data: { - tankName, - measuredHeight: measuredHeightNum.toFixed(2) + " cm", - actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm", - heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm", - message - } - }); - - } catch (err) { - console.error("Error in compareMeasuredHeight:", err); - reply.status(500).send({ message: err.message }); - } -}; - diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 78e0951a..320ee3e3 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -706,23 +706,23 @@ module.exports = function (fastify, opts, next) { }); - fastify.route({ - method: "GET", - url: "/api/waterlevel/:tankName", - schema: { - tags: ["Tank"], - description: "Get actual water level in cm", - summary: "The actual water level of a tank and convert it to cm", - params: { - type: "object", - properties: { - tankName: { type: "string" } - }, - required: ["tankName"] - } - }, - handler: tanksController.getActualWaterLevelInCm - }); + // fastify.route({ + // method: "GET", + // url: "/api/waterlevel/:tankName", + // schema: { + // tags: ["Tank"], + // description: "Get actual water level in cm", + // summary: "The actual water level of a tank and convert it to cm", + // params: { + // type: "object", + // properties: { + // tankName: { type: "string" } + // }, + // required: ["tankName"] + // } + // }, + // handler: tanksController.getActualWaterLevelInCm + // });