From 1bdb5d4facba7e92ab9589adb5a6856c0a22a05a Mon Sep 17 00:00:00 2001 From: varun Date: Fri, 17 May 2024 05:57:54 -0400 Subject: [PATCH] change in iotdata if tank not found for tankhardwareId4 --- src/controllers/tanksController.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 6015d0ab..123d0dbb 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -952,21 +952,33 @@ exports.motorAction = async (req, reply) => { } else if (req.body.threshold_type === "litres") { // If threshold type is percentage, calculate percentage threshold const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); + const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); if (!receiver_tank_info) { throw new Error("Receiver tank not found."); } + if (!supplier_tank_info) { + throw new Error("Supplierr tank not found."); + } + const supplier_capacity = parseInt(supplier_tank_info.capacity, 10); + const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10); + + + + + const capacity = parseInt(receiver_tank_info.capacity, 10); const waterLevel = parseInt(receiver_tank_info.waterlevel, 10); const desired_percentage = parseInt(req.body.manual_threshold_litres, 10); const threshold_water_level = waterLevel+desired_percentage; + const supplier_threshold = supplier_waterLevel-desired_percentage for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); if (index !== -1) { await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, - { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: threshold_water_level.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } + { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } ); } } @@ -979,8 +991,8 @@ exports.motorAction = async (req, reply) => { // Start monitoring water level based on threshold percentage const intervalId = setInterval(async () => { // Check if water level has reached the threshold percentage - const current_water_level = parseInt(receiver_tank_info.water_level, 10); - if (current_water_level >= threshold_water_level) { + const current_water_level = parseInt(supplier_tank_info.water_level, 10); + if (current_water_level <= supplier_threshold) { // Stop the motor pump await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId },