diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 7547395e..b22e9ff9 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -7520,3 +7520,94 @@ 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." }); + } + + // 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 (!actualWaterLevel || !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; + + reply.send({ + status_code: 200, + data: { + tankName, + actualWaterLevel: actualWaterLevel.toFixed(2) + " L", + actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm" + } + }); + + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; + + +exports.compareMeasuredHeight = async (req, reply) => { + try { + const { tankName } = req.params; + const { measuredHeight } = req.body; + + if (!tankName || measuredHeight === undefined) { + return reply.status(400).send({ message: "Tank name and measured height are required." }); + } + + // 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 (!actualWaterLevel || !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 - measuredHeight); + + let message; + if (heightDifferenceInCm <= 10) { // Within 10 cm is considered a match + message = "Measured height is within an acceptable range of actual water level."; + } else { + message = "Measured height differs significantly from actual water level."; + } + + reply.send({ + status_code: 200, + data: { + tankName, + measuredHeight: measuredHeight.toFixed(2) + " cm", + actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm", + heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm", + message + } + }); + + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; \ No newline at end of file diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 1ad46b57..b4aac760 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -627,6 +627,52 @@ module.exports = function (fastify, opts, next) { handler: tanksController.adjustMeasurement }); + + 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: "POST", + url: "/api/compareWaterLevel/:tankName", + schema: { + tags: ["Tank"], + description: "Compare measured height with actual water level", + summary: "Checks if measured height is within 10 cm of actual water level", + params: { + type: "object", + properties: { + tankName: { type: "string" } + }, + required: ["tankName"] + }, + body: { + type: "object", + properties: { + measuredHeight: { type: "number" } + }, + required: ["measuredHeight"] + } + }, + handler: tanksController.compareMeasuredHeight + }); + fastify.route({ method: "POST",