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

@ -7562,52 +7562,58 @@ exports.getActualWaterLevelInCm = async (req, reply) => {
exports.compareMeasuredHeight = async (req, reply) => {
try {
const { tankName } = req.params;
const { measuredHeight } = req.body;
const { measuredHeight, tankName } = req.body;
if (!tankName || measuredHeight === undefined) {
return reply.status(400).send({ message: "Tank name and measured height are required." });
}
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 });
// 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." });
}
if (!tank) {
return reply.status(404).send({ message: "Tank not found." });
}
// Fetch tank details using tankName
const tank = await Tank.findOne({ tankName });
const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters
const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm
if (!tank) {
return reply.status(404).send({ message: "Tank not found." });
}
if (!actualWaterLevel || !waterCapacityPerCm) {
return reply.status(400).send({ message: "Tank data is incomplete for conversion." });
}
const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters
const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm
// Convert actual water level from liters to cm
const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm;
if (isNaN(actualWaterLevel) || isNaN(waterCapacityPerCm)) {
return reply.status(400).send({ message: "Tank data is incomplete for conversion." });
}
// Calculate difference between measured and actual water level in cm
const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeight);
// Convert actual water level from liters to cm
const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm;
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.";
}
// Calculate difference between measured and actual water level in cm
const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeightNum);
reply.send({
status_code: 200,
data: {
tankName,
measuredHeight: measuredHeight.toFixed(2) + " cm",
actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm",
heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm",
message
}
});
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: measuredHeightNum.toFixed(2) + " cm",
actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm",
heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm",
message
}
});
} 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"],
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"]
},
// params: {
// type: "object",
// properties: {
// tankName: { type: "string" }
// },
// required: ["tankName"]
// },
body: {
type: "object",
properties: {
measuredHeight: { type: "number" }
measuredHeight: { type: "string" },
tankName: { type: "string" }
},
required: ["measuredHeight"]
required: ["measuredHeight","tankName"]
}
},
handler: tanksController.compareMeasuredHeight

Loading…
Cancel
Save