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

@ -7562,13 +7562,18 @@ 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." });
} }
// 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 // Fetch tank details using tankName
const tank = await Tank.findOne({ tankName }); const tank = await Tank.findOne({ tankName });
@ -7579,7 +7584,7 @@ exports.compareMeasuredHeight = async (req, reply) => {
const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters
const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm
if (!actualWaterLevel || !waterCapacityPerCm) { if (isNaN(actualWaterLevel) || isNaN(waterCapacityPerCm)) {
return reply.status(400).send({ message: "Tank data is incomplete for conversion." }); return reply.status(400).send({ message: "Tank data is incomplete for conversion." });
} }
@ -7587,7 +7592,7 @@ exports.compareMeasuredHeight = async (req, reply) => {
const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm; const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm;
// Calculate difference between measured and actual water level in cm // Calculate difference between measured and actual water level in cm
const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeight); const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeightNum);
let message; let message;
if (heightDifferenceInCm <= 10) { // Within 10 cm is considered a match if (heightDifferenceInCm <= 10) { // Within 10 cm is considered a match
@ -7600,7 +7605,7 @@ exports.compareMeasuredHeight = async (req, reply) => {
status_code: 200, status_code: 200,
data: { data: {
tankName, tankName,
measuredHeight: measuredHeight.toFixed(2) + " cm", measuredHeight: measuredHeightNum.toFixed(2) + " cm",
actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm", actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm",
heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm", heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm",
message message
@ -7608,6 +7613,7 @@ exports.compareMeasuredHeight = async (req, reply) => {
}); });
} catch (err) { } catch (err) {
console.error("Error in compareMeasuredHeight:", err);
reply.status(500).send({ message: err.message }); 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