diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 11436892..83d4ce20 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -7519,3 +7519,51 @@ exports.adjustMeasurement = async (req, reply) => { reply.status(500).send({ message: err.message }); } }; + + +exports.validateTankHeight = async (req, reply) => { + try { + const { tankhardwareId, hardwareId } = req.body; + + if (!tankhardwareId || !hardwareId) { + return reply.status(400).send({ message: "Both tankhardwareId and hardwareId are required." }); + } + + // Find tank details from tanksSchema + const tank = await Tank.findOne({ tankhardwareId, hardwareId }); + + if (!tank) { + return reply.status(404).send({ message: "Tank not found with the given tankhardwareId and hardwareId." }); + } + + // Find corresponding IoT data from IOttankSchema + const iotTank = await IotData.findOne({ hardwareId, "tanks.tankhardwareId": tankhardwareId }); + + if (!iotTank) { + return reply.status(404).send({ message: "IoT tank data not found for the given hardwareId and tankhardwareId." }); + } + + // Convert tank height from feet to cm + const heightInCm = parseFloat(tank.height) * 30.48; + const iotTankHeight = parseFloat(iotTank.tanks.find(t => t.tankhardwareId === tankhardwareId).tankHeight); + + console.log("Converted Tank Height (cm):", heightInCm); + console.log("IoT Tank Height (cm):", iotTankHeight); + + // Check for extreme values + if (heightInCm < iotTankHeight) { + return reply.status(400).send({ + message: "Extreme high values detected! Tank height exceeds IoT data." + }); + } + + reply.send({ + status_code: 200, + message: "Tank height is within the valid range." + }); + + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; + diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 9bd6e3bf..0855cb76 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -565,6 +565,28 @@ module.exports = function (fastify, opts, next) { handler: tanksController.IotDevice }); + fastify.route({ + method: "POST", + url: "/api/validateRange", + schema: { + tags: ["Tank"], + description: "Validate tank height range", + summary: "Validate tank height range", + body: { + type: "object", + properties: { + tankhardwareId: { type: "string" }, + hardwareId: { type: "string" } + + + }, + required: ["tankhardwareId"] + }, + + }, + handler: tanksController.validateTankHeight + }); + fastify.route({ method: "POST", url: "/api/adjustMeasurement",