Compare water level

master^2
Bhaskar 3 months ago
parent a8b93f4919
commit d724b514ad

@ -1905,6 +1905,7 @@ exports.getTankDetailsByMaster = async (req, reply) => {
data: { data: {
masterName: masterName, masterName: masterName,
location: location, location: location,
type:masterDevice.type,
hardwareId: hardwareId, hardwareId: hardwareId,
tankName: tank.tankName, tankName: tank.tankName,
blockName: tank.blockName, blockName: tank.blockName,
@ -1995,6 +1996,7 @@ exports.getSlaveTankDetails = async (req, reply) => {
masterName, masterName,
location, location,
slaveHardwareId: hardwareId, slaveHardwareId: hardwareId,
type: slaveDevice.type,
tankHardwareId, tankHardwareId,
tankName: tank.tankName, tankName: tank.tankName,
blockName: tank.blockName, blockName: tank.blockName,

@ -8041,9 +8041,143 @@ exports.validateTankHeight = async (req, reply) => {
// } // }
// }; // };
exports.getActualWaterLevelInCm = 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." });
// }
// 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;
// const capacityPerCm = parseFloat(tank.waterCapacityPerCm);
// console.log("capacityPerCm", capacityPerCm);
// if (!capacityPerCm || capacityPerCm <= 0) {
// return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." });
// }
// let sensorWaterLevelInCm = 0;
// let sensorWaterLevelLiters = 0;
// let manualWaterLevelLiters = parseFloat(tank.waterlevel) || 0;
// let manualWaterLevelInCm = manualWaterLevelLiters > 0
// ? manualWaterLevelLiters / capacityPerCm
// : 0;
// // Always try to also get IoT data
// const iotData = await IotData.findOne({
// hardwareId: tank.hardwareId,
// "tanks.tankhardwareId": tank.tankhardwareId
// }).sort({ date: -1 });
// console.log("iotData", iotData);
// if (iotData) {
// const matchingTank = iotData.tanks.find(
// t => t.tankhardwareId === tank.tankhardwareId
// );
// console.log("matchingTank", matchingTank);
// if (matchingTank) {
// const tankHeightFromSensor = parseFloat(matchingTank.tankHeight);
// console.log("tankHeightFromSensor", tankHeightFromSensor);
// if (!isNaN(tankHeightFromSensor)) {
// sensorWaterLevelInCm = tankHeightCm - tankHeightFromSensor;
// sensorWaterLevelLiters = sensorWaterLevelInCm * capacityPerCm;
// }
// }
// }
// reply.send({
// status_code: 200,
// data: {
// tankName: tank.tankName,
// tankHeight: tankHeightCm,
// capacity: tank.capacity,
// currentWaterLevel: `${manualWaterLevelLiters.toFixed(2)} L`,
// currentWaterLevelInCm: `${manualWaterLevelInCm.toFixed(2)} cm`,
// sensorWaterLevel: `${sensorWaterLevelLiters.toFixed(2)} L`,
// sensorWaterLevelInCm: `${sensorWaterLevelInCm.toFixed(2)} cm`
// }
// });
// } catch (err) {
// console.error(err);
// reply.status(500).send({ message: err.message || "Internal Server Error" });
// }
// };
// exports.compareMeasuredHeight = async (req, reply) => {
// try {
// const { measuredHeight, tankName } = req.body;
// if (!tankName || measuredHeight === undefined) {
// 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
// const tank = await Tank.findOne({ tankName });
// 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 (isNaN(actualWaterLevel) || isNaN(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;
// // Calculate difference between measured and actual water level in cm
// const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeightNum);
// let message;
// if (heightDifferenceInCm <= 10) {
// message = "Manual measurement is match within 10 cm of the sensor data.";
// } else {
// message = "Manual measurement not matched within range 10cm from the sensor data.";
// }
// 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) {
// console.error("Error in compareMeasuredHeight:", err);
// reply.status(500).send({ message: err.message });
// }
// };
exports.compareMeasuredHeight = async (req, reply) => {
try { try {
const { tankName } = req.params; const { tankName, measuredHeight } = req.body;
if (!tankName) { if (!tankName) {
return reply.status(400).send({ message: "Tank name is required." }); return reply.status(400).send({ message: "Tank name is required." });
@ -8055,58 +8189,72 @@ exports.getActualWaterLevelInCm = async (req, reply) => {
} }
const tankHeightFeet = parseFloat(tank.height); const tankHeightFeet = parseFloat(tank.height);
const tankHeightCm = tankHeightFeet * 30.48; const tankHeightCm = Math.round(tankHeightFeet * 30.48);
const capacityPerCm = parseFloat(tank.waterCapacityPerCm); const capacityPerCm = parseFloat(tank.waterCapacityPerCm);
console.log("capacityPerCm", capacityPerCm);
if (!capacityPerCm || capacityPerCm <= 0) { if (!capacityPerCm || capacityPerCm <= 0) {
return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." }); return reply.status(400).send({ message: "Invalid waterCapacityPerCm value." });
} }
const manualWaterLevelLiters = parseFloat(tank.waterlevel) || 0;
const manualWaterLevelInCm = Math.round(manualWaterLevelLiters / capacityPerCm);
let sensorWaterLevelInCm = 0; let sensorWaterLevelInCm = 0;
let sensorWaterLevelLiters = 0; let sensorWaterLevelLiters = 0;
let manualWaterLevelLiters = parseFloat(tank.waterlevel) || 0; // Try to get IoT data
let manualWaterLevelInCm = manualWaterLevelLiters > 0
? manualWaterLevelLiters / capacityPerCm
: 0;
// Always try to also get IoT data
const iotData = await IotData.findOne({ const iotData = await IotData.findOne({
hardwareId: tank.hardwareId, hardwareId: tank.hardwareId,
"tanks.tankhardwareId": tank.tankhardwareId "tanks.tankhardwareId": tank.tankhardwareId
}).sort({ date: -1 }); }).sort({ date: -1 });
console.log("iotData", iotData);
if (iotData) { if (iotData) {
const matchingTank = iotData.tanks.find( const matchingTank = iotData.tanks.find(
t => t.tankhardwareId === tank.tankhardwareId t => t.tankhardwareId === tank.tankhardwareId
); );
console.log("matchingTank", matchingTank);
if (matchingTank) { if (matchingTank) {
const tankHeightFromSensor = parseFloat(matchingTank.tankHeight); const tankHeightFromSensor = parseFloat(matchingTank.tankHeight);
console.log("tankHeightFromSensor", tankHeightFromSensor);
if (!isNaN(tankHeightFromSensor)) { if (!isNaN(tankHeightFromSensor)) {
sensorWaterLevelInCm = tankHeightCm - tankHeightFromSensor; const rawSensorWaterLevelInCm = tankHeightCm - Math.round(tankHeightFromSensor);
sensorWaterLevelLiters = sensorWaterLevelInCm * capacityPerCm; sensorWaterLevelInCm = Math.max(0, rawSensorWaterLevelInCm);
sensorWaterLevelLiters = Math.round(sensorWaterLevelInCm * capacityPerCm);
}
} }
} }
// Include comparison if measuredHeight provided
let comparison = null;
if (measuredHeight !== undefined) {
const measuredHeightNum = parseFloat(measuredHeight);
if (!isNaN(measuredHeightNum)) {
const measuredHeightRounded = Math.round(measuredHeightNum);
const heightDifferenceInCm = Math.abs(manualWaterLevelInCm - measuredHeightRounded);
const message = heightDifferenceInCm <= 10
? "Manual measurement matches within 10 cm of sensor/manual data."
: "Manual measurement not matched within range 10 cm.";
comparison = {
measuredHeight: measuredHeightRounded + " cm",
actualWaterLevelInCm: manualWaterLevelInCm + " cm",
heightDifferenceInCm: heightDifferenceInCm + " cm",
message
};
} else {
comparison = { message: "Invalid measuredHeight; must be a number." };
}
} }
reply.send({ reply.send({
status_code: 200, status_code: 200,
data: { data: {
tankName: tank.tankName, tankName: tank.tankName,
tankHeight: tankHeightCm, tankHeightInCm: tankHeightCm,
capacity: tank.capacity, capacity: tank.capacity,
currentWaterLevel: `${manualWaterLevelLiters.toFixed(2)} L`, manualWaterLevel: manualWaterLevelLiters, // as integer
currentWaterLevelInCm: `${manualWaterLevelInCm.toFixed(2)} cm`, manualWaterLevelInCm: manualWaterLevelInCm + " cm",
sensorWaterLevel: `${sensorWaterLevelLiters.toFixed(2)} L`, sensorWaterLevel: sensorWaterLevelLiters, // as integer
sensorWaterLevelInCm: `${sensorWaterLevelInCm.toFixed(2)} cm` sensorWaterLevelInCm: sensorWaterLevelInCm + " cm",
...(comparison && { comparison })
} }
}); });
@ -8117,64 +8265,6 @@ exports.getActualWaterLevelInCm = async (req, reply) => {
}; };
exports.compareMeasuredHeight = async (req, reply) => {
try {
const { measuredHeight, tankName } = req.body;
if (!tankName || measuredHeight === undefined) {
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
const tank = await Tank.findOne({ tankName });
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 (isNaN(actualWaterLevel) || isNaN(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;
// Calculate difference between measured and actual water level in cm
const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeightNum);
let message;
if (heightDifferenceInCm <= 10) {
message = "Manual measurement is match within 10 cm of the sensor data.";
} else {
message = "Manual measurement not matched within range 10cm from the sensor data.";
}
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) {
console.error("Error in compareMeasuredHeight:", err);
reply.status(500).send({ message: err.message });
}
};

@ -706,23 +706,23 @@ module.exports = function (fastify, opts, next) {
}); });
fastify.route({ // fastify.route({
method: "GET", // method: "GET",
url: "/api/waterlevel/:tankName", // url: "/api/waterlevel/:tankName",
schema: { // schema: {
tags: ["Tank"], // tags: ["Tank"],
description: "Get actual water level in cm", // description: "Get actual water level in cm",
summary: "The actual water level of a tank and convert it to cm", // summary: "The actual water level of a tank and convert it to cm",
params: { // params: {
type: "object", // type: "object",
properties: { // properties: {
tankName: { type: "string" } // tankName: { type: "string" }
}, // },
required: ["tankName"] // required: ["tankName"]
} // }
}, // },
handler: tanksController.getActualWaterLevelInCm // handler: tanksController.getActualWaterLevelInCm
}); // });

Loading…
Cancel
Save