ashok 7 months ago
commit c6bd4964d0

@ -7446,3 +7446,76 @@ exports.notificationTiming = async (req, reply) => {
return reply.send({ message: "Preference updated successfully" });
}
exports.adjustMeasurement = async (req, reply) => {
try {
const { tankName, 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 originalHeight = parseFloat(tank.height); // Example: 5.8 feet
const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Example: 87 L/cm
const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in tank (liters)
const capacity = parseFloat(tank.capacity.replace(/,/g, ""));
console.log("originalHeight",originalHeight)
console.log("waterCapacityPerCm",waterCapacityPerCm)
console.log("actualWaterLevel",actualWaterLevel)
console.log("capacity",capacity)
// Perform calculations
const heightDifference = originalHeight - measuredHeight;
const heightDifferenceInCm = heightDifference * 30.48; // Convert feet to cm
const calculatedWaterLevel = heightDifferenceInCm * waterCapacityPerCm; // Estimated water level in liters
console.log("heightDifference",heightDifference)
console.log("heightDifferenceInCm",heightDifferenceInCm)
console.log("calculatedWaterLevel",calculatedWaterLevel)
// **Ensure actualWaterLevel and calculatedWaterLevel do not exceed tank capacity**
const boundedActualWaterLevel = Math.min(actualWaterLevel, capacity);
const boundedCalculatedWaterLevel = Math.min(calculatedWaterLevel, capacity);
console.log("boundedActualWaterLevel",boundedActualWaterLevel)
console.log("boundedCalculatedWaterLevel",boundedCalculatedWaterLevel)
// Calculate original and calculated percentages correctly
const originalPercentage = (boundedActualWaterLevel / capacity) * 100;
const calculatedPercentage = (boundedCalculatedWaterLevel / capacity) * 100;
// Calculate percentage difference
const percentageDifference = Math.abs(originalPercentage - calculatedPercentage);
reply.send({
status_code: 200,
data: {
tankName,
originalHeight,
measuredHeight,
heightDifference: heightDifference.toFixed(2),
heightDifferenceInCm: heightDifferenceInCm.toFixed(2),
calculatedWaterLevel: calculatedWaterLevel.toFixed(2),
actualWaterLevel: actualWaterLevel.toFixed(2),
originalPercentage: originalPercentage.toFixed(2) + "%",
calculatedPercentage: calculatedPercentage.toFixed(2) + "%",
percentageDifference: percentageDifference.toFixed(2) + "%",
message: "Calculation completed. No updates were made."
}
});
} catch (err) {
reply.status(500).send({ message: err.message });
}
};

@ -565,6 +565,49 @@ module.exports = function (fastify, opts, next) {
handler: tanksController.IotDevice
});
fastify.route({
method: "POST",
url: "/api/adjustMeasurement",
schema: {
tags: ["Tank"],
description: "Adjusts the water level measurement based on manual height measurement.",
summary: "Calculate water level difference using measured height.",
body: {
type: "object",
properties: {
tankName: { type: "string" },
measuredHeight: { type: "number" }
},
required: ["tankName", "measuredHeight"]
},
response: {
200: {
type: "object",
properties: {
status_code: { type: "number" },
data: {
type: "object",
properties: {
tankName: { type: "string" },
originalHeight: { type: "number" },
measuredHeight: { type: "number" },
heightDifference: { type: "number" },
heightDifferenceInCm: { type: "number" },
calculatedWaterLevel: { type: "number" },
actualWaterLevel: { type: "number" },
originalPercentage: { type: "string" },
calculatedPercentage: { type: "string" },
percentageDifference: { type: "string" },
message: { type: "string" }
}
}
}
}
}
},
handler: tanksController.adjustMeasurement
});
fastify.route({
method: "POST",

Loading…
Cancel
Save