|
|
@ -8270,7 +8270,7 @@ exports.compareMeasuredHeight = async (req, reply) => {
|
|
|
|
const { tankName, measuredHeight, tankHeight } = req.body;
|
|
|
|
const { tankName, measuredHeight, tankHeight } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
if (!tankName || typeof measuredHeight !== 'number' || typeof tankHeight !== 'number') {
|
|
|
|
if (!tankName || typeof measuredHeight !== 'number' || typeof tankHeight !== 'number') {
|
|
|
|
return reply.status(400).send({ message: "tankName, tankHeight and measuredHeight (all required, as integers)." });
|
|
|
|
return reply.status(400).send({ message: "tankName, tankHeight and measuredHeight are required and must be numbers." });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (tankHeight <= 0 || measuredHeight < 0) {
|
|
|
|
if (tankHeight <= 0 || measuredHeight < 0) {
|
|
|
@ -8287,7 +8287,14 @@ exports.compareMeasuredHeight = async (req, reply) => {
|
|
|
|
return reply.status(400).send({ message: "Invalid waterCapacityPerCm in tank data." });
|
|
|
|
return reply.status(400).send({ message: "Invalid waterCapacityPerCm in tank data." });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 🔹 Sensor data
|
|
|
|
// DB tank height (in feet → cm)
|
|
|
|
|
|
|
|
const tankHeightFeetFromDB = parseFloat(tank.height);
|
|
|
|
|
|
|
|
const tankHeightInCmFromDB = Math.round(tankHeightFeetFromDB * 30.48);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// tankHeight from body is already in cm
|
|
|
|
|
|
|
|
const tankHeightInCmFromBody = Math.round(tankHeight);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 🔹 Sensor data (use DB tank height)
|
|
|
|
let sensorGapCm = null;
|
|
|
|
let sensorGapCm = null;
|
|
|
|
let sensorWaterLevelInCm = null;
|
|
|
|
let sensorWaterLevelInCm = null;
|
|
|
|
let sensorWaterLevelLiters = null;
|
|
|
|
let sensorWaterLevelLiters = null;
|
|
|
@ -8302,17 +8309,17 @@ exports.compareMeasuredHeight = async (req, reply) => {
|
|
|
|
t => t.tankhardwareId === tank.tankhardwareId
|
|
|
|
t => t.tankhardwareId === tank.tankhardwareId
|
|
|
|
);
|
|
|
|
);
|
|
|
|
if (matchingTank) {
|
|
|
|
if (matchingTank) {
|
|
|
|
const tankHeightFromSensor = parseInt(matchingTank.tankHeight, 10);
|
|
|
|
const tankHeightFromSensor = parseFloat(matchingTank.tankHeight);
|
|
|
|
if (!isNaN(tankHeightFromSensor) && tankHeightFromSensor >= 0) {
|
|
|
|
if (!isNaN(tankHeightFromSensor) && tankHeightFromSensor >= 0) {
|
|
|
|
sensorGapCm = tankHeightFromSensor;
|
|
|
|
sensorGapCm = Math.round(tankHeightFromSensor);
|
|
|
|
sensorWaterLevelInCm = Math.max(0, tankHeight - sensorGapCm);
|
|
|
|
sensorWaterLevelInCm = Math.max(0, tankHeightInCmFromDB - sensorGapCm);
|
|
|
|
sensorWaterLevelLiters = Math.round(sensorWaterLevelInCm * capacityPerCm);
|
|
|
|
sensorWaterLevelLiters = Math.round(sensorWaterLevelInCm * capacityPerCm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 🔹 Manual data
|
|
|
|
// 🔹 Manual data (use tankHeight from body)
|
|
|
|
const manualWaterLevelInCm = measuredHeight;
|
|
|
|
const manualWaterLevelInCm = Math.round(measuredHeight); // measuredHeight in cm
|
|
|
|
const manualWaterLevelLiters = Math.round(manualWaterLevelInCm * capacityPerCm);
|
|
|
|
const manualWaterLevelLiters = Math.round(manualWaterLevelInCm * capacityPerCm);
|
|
|
|
|
|
|
|
|
|
|
|
// 🔹 Comparison
|
|
|
|
// 🔹 Comparison
|
|
|
@ -8325,18 +8332,16 @@ exports.compareMeasuredHeight = async (req, reply) => {
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
data: {
|
|
|
|
data: {
|
|
|
|
tankName,
|
|
|
|
tankName,
|
|
|
|
tankHeightInCm: tankHeight,
|
|
|
|
|
|
|
|
capacity: tank.capacity,
|
|
|
|
capacity: tank.capacity,
|
|
|
|
sensor: {
|
|
|
|
sensor: {
|
|
|
|
tankHeightInCm: tankHeight,
|
|
|
|
tankHeightInCm: tankHeightInCmFromDB, // from DB
|
|
|
|
sensorGapCm: sensorGapCm ?? null,
|
|
|
|
sensorGapCm,
|
|
|
|
waterLevelInCm: sensorWaterLevelInCm ?? null,
|
|
|
|
waterLevelInCm: sensorWaterLevelInCm,
|
|
|
|
waterLevelLiters: sensorWaterLevelLiters ?? null
|
|
|
|
waterLevelLiters: sensorWaterLevelLiters
|
|
|
|
},
|
|
|
|
},
|
|
|
|
manual: {
|
|
|
|
manual: {
|
|
|
|
tankHeightInCm: tankHeight,
|
|
|
|
tankHeightInCm: tankHeightInCmFromBody, // passed in body
|
|
|
|
//measuredHeightCm: measuredHeight,
|
|
|
|
measuredHeightCm: manualWaterLevelInCm,
|
|
|
|
waterLevelInCm: manualWaterLevelInCm,
|
|
|
|
|
|
|
|
waterLevelLiters: manualWaterLevelLiters
|
|
|
|
waterLevelLiters: manualWaterLevelLiters
|
|
|
|
},
|
|
|
|
},
|
|
|
|
comparison: {
|
|
|
|
comparison: {
|
|
|
@ -8354,6 +8359,8 @@ exports.compareMeasuredHeight = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//const ExcelJS = require('exceljs');
|
|
|
|
//const ExcelJS = require('exceljs');
|
|
|
|
//const IotData = require('../models/IotData'); // adjust the path
|
|
|
|
//const IotData = require('../models/IotData'); // adjust the path
|
|
|
|
|
|
|
|
|
|
|
|