The actual water level of a tank and convert it to cm

master^2
Bhaskar 3 months ago
parent 7971f7a30b
commit 7f33b13f2c

@ -7828,42 +7828,124 @@ 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 });
// console.log("tank",tank)
// 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.getActualWaterLevelInCm = async (req, reply) => {
try {
const { tankName } = req.params;
const { tankName } = req.params;
if (!tankName) {
return reply.status(400).send({ message: "Tank name is required." });
}
const tank = await Tank.findOne({ tankName });
if (!tank) {
return reply.status(404).send({ message: "Tank not found." });
}
const tankHeightFeet = parseFloat(tank.height);
const tankHeightCm = tankHeightFeet * 30.48;
let actualWaterLevelInCm;
let actualWaterLevelLiters;
if (parseFloat(tank.waterlevel) > 0) {
// ✅ Direct conversion from liters to cm
const waterlevelLiters = parseFloat(tank.waterlevel);
const capacityPerCm = parseFloat(tank.waterCapacityPerCm);
if (!tankName) {
return reply.status(400).send({ message: "Tank name is required." });
if (!capacityPerCm || capacityPerCm <= 0) {
return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." });
}
// Fetch tank details using tankName
const tank = await Tank.findOne({ tankName });
actualWaterLevelInCm = waterlevelLiters / capacityPerCm;
actualWaterLevelLiters = waterlevelLiters;
if (!tank) {
return reply.status(404).send({ message: "Tank not found." });
} else {
// ✅ Fallback to IoT data to calculate cm & liters
const iotData = await IotData.findOne({
hardwareId: tank.hardwareId,
"tanks.tankhardwareId": tank.tankhardwareId
}).sort({ date: -1 });
if (!iotData) {
return reply.status(404).send({ message: "No IoT data found for this tank." });
}
const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters
const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm
const matchingTank = iotData.tanks.find(
t => t.tankhardwareId === tank.tankhardwareId
);
if (!actualWaterLevel || !waterCapacityPerCm) {
return reply.status(400).send({ message: "Tank data is incomplete for conversion." });
if (!matchingTank) {
return reply.status(404).send({ message: "No matching tank found in IoT data." });
}
// Convert actual water level from liters to cm
const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm;
const tankHeightFromSensor = parseFloat(matchingTank.tankHeight);
reply.send({
status_code: 200,
data: {
tankName,
actualWaterLevel: actualWaterLevel.toFixed(2) + " L",
actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm"
}
});
if (isNaN(tankHeightFromSensor)) {
return reply.status(400).send({ message: "Invalid tankHeight from IoT data." });
}
actualWaterLevelInCm = tankHeightCm - tankHeightFromSensor;
const capacityPerCm = parseFloat(tank.waterCapacityPerCm);
if (!capacityPerCm || capacityPerCm <= 0) {
return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." });
}
// ✅ Convert back to liters using height in cm
actualWaterLevelLiters = actualWaterLevelInCm * capacityPerCm;
}
reply.send({
status_code: 200,
data: {
tankName: tank.tankName,
actualWaterLevel: `${actualWaterLevelLiters.toFixed(2)} L`,
actualWaterLevelInCm: `${actualWaterLevelInCm.toFixed(2)} cm`
}
});
} catch (err) {
reply.status(500).send({ message: err.message });
console.error(err);
reply.status(500).send({ message: err.message || "Internal Server Error" });
}
};

Loading…
Cancel
Save