|
|
@ -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) => {
|
|
|
|
exports.getActualWaterLevelInCm = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
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) {
|
|
|
|
if (!capacityPerCm || capacityPerCm <= 0) {
|
|
|
|
return reply.status(400).send({ message: "Tank name is required." });
|
|
|
|
return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch tank details using tankName
|
|
|
|
actualWaterLevelInCm = waterlevelLiters / capacityPerCm;
|
|
|
|
const tank = await Tank.findOne({ tankName });
|
|
|
|
actualWaterLevelLiters = waterlevelLiters;
|
|
|
|
|
|
|
|
|
|
|
|
if (!tank) {
|
|
|
|
} else {
|
|
|
|
return reply.status(404).send({ message: "Tank not found." });
|
|
|
|
// ✅ 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 matchingTank = iotData.tanks.find(
|
|
|
|
const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm
|
|
|
|
t => t.tankhardwareId === tank.tankhardwareId
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (!actualWaterLevel || !waterCapacityPerCm) {
|
|
|
|
if (!matchingTank) {
|
|
|
|
return reply.status(400).send({ message: "Tank data is incomplete for conversion." });
|
|
|
|
return reply.status(404).send({ message: "No matching tank found in IoT data." });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Convert actual water level from liters to cm
|
|
|
|
const tankHeightFromSensor = parseFloat(matchingTank.tankHeight);
|
|
|
|
const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({
|
|
|
|
if (isNaN(tankHeightFromSensor)) {
|
|
|
|
status_code: 200,
|
|
|
|
return reply.status(400).send({ message: "Invalid tankHeight from IoT data." });
|
|
|
|
data: {
|
|
|
|
}
|
|
|
|
tankName,
|
|
|
|
|
|
|
|
actualWaterLevel: actualWaterLevel.toFixed(2) + " L",
|
|
|
|
actualWaterLevelInCm = tankHeightCm - tankHeightFromSensor;
|
|
|
|
actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm"
|
|
|
|
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
} catch (err) {
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
reply.status(500).send({ message: err.message || "Internal Server Error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|