From 70f9dc1dfbcb0578682fac37ab4029a3bc2ac01a Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 15 May 2025 11:45:22 +0530 Subject: [PATCH] changes in water/iot-data --- src/controllers/tanksController.js | 55 ++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index e0ca8921..cd03507e 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6216,30 +6216,55 @@ async function processIotData(hw_Id, data) { console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`); if (tankHeight <= 0) { - if (existingTank.slave_status !== "not_working") { - const disconnectedTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss'); - existingTank.slave_status = "not_working"; - existingTank.slave_disconnected_time = disconnectedTime; + const now = moment().tz('Asia/Kolkata'); + + if (existingTank.slave_status === "working" || !existingTank.slave_status) { + // First time signal is lost + existingTank.slave_status = "signal_lost"; + existingTank.slave_signal_lost_time = now.format('DD-MMM-YYYY - HH:mm:ss'); await existingTank.save(); - - for (const outputConnection of existingTank.connections.outputConnections) { - const linkedTank = await Tank.findOne({ customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type }); - if (linkedTank) { - for (const inputConnection of linkedTank.connections.inputConnections) { - if (inputConnection.inputConnections === tank_name) { - inputConnection. slave_status = "not_working"; - await linkedTank.save(); + + console.log(`⚠️ Signal lost for tank [${tankhardwareId}] at ${existingTank.slave_signal_lost_time}`); + + } else if (existingTank.slave_status === "signal_lost") { + const lostTime = moment(existingTank.slave_signal_lost_time, 'DD-MMM-YYYY - HH:mm:ss'); + const minutesElapsed = now.diff(lostTime, 'minutes'); + + if (minutesElapsed >= 15) { + existingTank.slave_status = "not_working"; + existingTank.slave_disconnected_time = now.format('DD-MMM-YYYY - HH:mm:ss'); + await existingTank.save(); + + // Propagate status to output connections + for (const outputConnection of existingTank.connections.outputConnections) { + const linkedTank = await Tank.findOne({ + customerId, + tankName: outputConnection.outputConnections, + tankLocation: outputConnection.output_type + }); + + if (linkedTank) { + for (const inputConnection of linkedTank.connections.inputConnections) { + if (inputConnection.inputConnections === tank_name) { + inputConnection.slave_status = "not_working"; + await linkedTank.save(); + } + } } } + + console.log(`❌ Slave marked as not_working for tank [${tankhardwareId}] at ${existingTank.slave_disconnected_time}`); + } else { + console.log(`⏳ Slave still in signal_lost for tank [${tankhardwareId}], waiting for 15 mins.`); } - } - console.log(`⚠️ Slave disconnected for tank [${tankhardwareId}] at ${disconnectedTime}`); + } else { - console.log(`⏩ Slave already marked as not_working for tank [${tankhardwareId}], skipping update.`); + console.log(`⏩ Slave already marked as ${existingTank.slave_status} for tank [${tankhardwareId}], skipping update.`); } } + if (tankHeight > 0 && waterLevel >= 0) { existingTank.waterlevel = waterLevel;