master^2
Bhaskar 7 months ago
parent e6073afeb3
commit 2e5766792a

@ -7562,52 +7562,58 @@ exports.getActualWaterLevelInCm = async (req, reply) => {
exports.compareMeasuredHeight = async (req, reply) => { exports.compareMeasuredHeight = async (req, reply) => {
try { try {
const { tankName } = req.params; const { measuredHeight, tankName } = req.body;
const { measuredHeight } = req.body;
if (!tankName || measuredHeight === undefined) { if (!tankName || measuredHeight === undefined) {
return reply.status(400).send({ message: "Tank name and measured height are required." }); return reply.status(400).send({ message: "Tank name and measured height are required." });
} }
// Fetch tank details using tankName // Convert measuredHeight to a number
const tank = await Tank.findOne({ tankName }); const measuredHeightNum = parseFloat(measuredHeight);
if (isNaN(measuredHeightNum)) {
return reply.status(400).send({ message: "Invalid measuredHeight. It must be a number." });
}
if (!tank) { // Fetch tank details using tankName
return reply.status(404).send({ message: "Tank not found." }); const tank = await Tank.findOne({ tankName });
}
const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters if (!tank) {
const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm return reply.status(404).send({ message: "Tank not found." });
}
if (!actualWaterLevel || !waterCapacityPerCm) { const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters
return reply.status(400).send({ message: "Tank data is incomplete for conversion." }); const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm
}
// Convert actual water level from liters to cm if (isNaN(actualWaterLevel) || isNaN(waterCapacityPerCm)) {
const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm; return reply.status(400).send({ message: "Tank data is incomplete for conversion." });
}
// Calculate difference between measured and actual water level in cm // Convert actual water level from liters to cm
const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeight); const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm;
let message; // Calculate difference between measured and actual water level in cm
if (heightDifferenceInCm <= 10) { // Within 10 cm is considered a match const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeightNum);
message = "Measured height is within an acceptable range of actual water level.";
} else {
message = "Measured height differs significantly from actual water level.";
}
reply.send({ let message;
status_code: 200, if (heightDifferenceInCm <= 10) { // Within 10 cm is considered a match
data: { message = "Measured height is within an acceptable range of actual water level.";
tankName, } else {
measuredHeight: measuredHeight.toFixed(2) + " cm", message = "Measured height differs significantly from actual water level.";
actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm", }
heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm",
message 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) { } catch (err) {
reply.status(500).send({ message: err.message }); console.error("Error in compareMeasuredHeight:", err);
reply.status(500).send({ message: err.message });
} }
}; };

@ -655,19 +655,21 @@ module.exports = function (fastify, opts, next) {
tags: ["Tank"], tags: ["Tank"],
description: "Compare measured height with actual water level", description: "Compare measured height with actual water level",
summary: "Checks if measured height is within 10 cm of actual water level", summary: "Checks if measured height is within 10 cm of actual water level",
params: { // params: {
type: "object", // type: "object",
properties: { // properties: {
tankName: { type: "string" } // tankName: { type: "string" }
}, // },
required: ["tankName"] // required: ["tankName"]
}, // },
body: { body: {
type: "object", type: "object",
properties: { properties: {
measuredHeight: { type: "number" } measuredHeight: { type: "string" },
tankName: { type: "string" }
}, },
required: ["measuredHeight"] required: ["measuredHeight","tankName"]
} }
}, },
handler: tanksController.compareMeasuredHeight handler: tanksController.compareMeasuredHeight

Loading…
Cancel
Save