|
|
@ -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 });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|