diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index c6d54b23..7414b0f0 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6217,61 +6217,61 @@ async function processIotData(hw_Id, data) { console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`); const now = moment().tz('Asia/Kolkata'); - // if (tankHeight <= 0) { - // // Case 1: First time signal is lost - // if ( - // existingTank.slave_status !== "signal_lost1" && - // existingTank.slave_status !== "not_working" - // ) { - // existingTank.slave_status = "signal_lost1"; - // existingTank.slave_disconnected_time = now.format('DD-MMM-YYYY - HH:mm:ss'); - // await existingTank.save(); + if (tankHeight <= 0) { + // Case 1: First time signal is lost + if ( + existingTank.slave_status !== "signal_lost1" && + existingTank.slave_status !== "not_working1" + ) { + existingTank.slave_status = "signal_lost1"; + existingTank.slave_disconnected_time = now.format('DD-MMM-YYYY - HH:mm:ss'); + await existingTank.save(); - // console.log(`⚠️ Signal lost for tank [${tankhardwareId}] at ${existingTank.slave_disconnected_time}`); - // return; // Important: do not continue in this cycle + console.log(`⚠️ Signal lost for tank [${tankhardwareId}] at ${existingTank.slave_disconnected_time}`); + return; // Important: do not continue in this cycle - // } else if (existingTank.slave_status === "signal_lost1") { - // if (!existingTank.slave_disconnected_time) { - // console.log(`❗ Missing slave_disconnected_time despite signal_lost1 for tank [${tankhardwareId}]`); - // return; - // } + } else if (existingTank.slave_status === "signal_lost1") { + if (!existingTank.slave_disconnected_time) { + console.log(`❗ Missing slave_disconnected_time despite signal_lost1 for tank [${tankhardwareId}]`); + return; + } - // const lostTime = moment(existingTank.slave_disconnected_time, 'DD-MMM-YYYY - HH:mm:ss'); - // const minutesElapsed = now.diff(lostTime, 'minutes'); + const lostTime = moment(existingTank.slave_disconnected_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(); + if (minutesElapsed >= 15) { + existingTank.slave_status = "not_working1"; + existingTank.slave_disconnected_time = now.format('DD-MMM-YYYY - HH:mm:ss'); + await existingTank.save(); - // console.log(`❌ Slave marked as not_working for tank [${tankhardwareId}] at ${existingTank.slave_disconnected_time}`); + console.log(`❌ Slave marked as not_working for tank [${tankhardwareId}] at ${existingTank.slave_disconnected_time}`); - // // Update connected output tanks - // for (const outputConnection of existingTank.connections.outputConnections) { - // const linkedTank = await Tank.findOne({ - // customerId, - // tankName: outputConnection.outputConnections, - // tankLocation: outputConnection.output_type - // }); + // Update connected output tanks + 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(`🔗 Linked tank [${linkedTank.tankName}] inputConnection updated to not_working`); - // } - // } - // } - // } + if (linkedTank) { + for (const inputConnection of linkedTank.connections.inputConnections) { + if (inputConnection.inputConnections === tank_name) { + inputConnection.slave_status = "not_working1"; + await linkedTank.save(); + console.log(`🔗 Linked tank [${linkedTank.tankName}] inputConnection updated to not_working`); + } + } + } + } - // } else { - // console.log(`⏳ Still within 15-minute grace: ${minutesElapsed} minutes elapsed for tank [${tankhardwareId}]`); - // } - // } else { - // console.log(`⏩ Tank [${tankhardwareId}] already marked as ${existingTank.slave_status}, skipping update.`); - // } - // } + } else { + console.log(`⏳ Still within 15-minute grace: ${minutesElapsed} minutes elapsed for tank [${tankhardwareId}]`); + } + } else { + console.log(`⏩ Tank [${tankhardwareId}] already marked as ${existingTank.slave_status}, skipping update.`); + } + } @@ -6280,8 +6280,8 @@ async function processIotData(hw_Id, data) { if (tankHeight > 0 && waterLevel >= 0) { existingTank.waterlevel = waterLevel; - // existingTank.slave_status = "working"; - // existingTank.slave_disconnected_time = null; + existingTank.slave_status = "working"; + existingTank.slave_disconnected_time = null; await existingTank.save(); for (const outputConnection of existingTank.connections.outputConnections) { @@ -6290,7 +6290,7 @@ async function processIotData(hw_Id, data) { for (const inputConnection of linkedTank.connections.inputConnections) { if (inputConnection.inputConnections === tank_name) { inputConnection.water_level = waterLevel; - ` // inputConnection. slave_status = "working";` + inputConnection. slave_status = "working"; await linkedTank.save(); } }