diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 42534c66..ac584ff2 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6150,27 +6150,24 @@ async function processIotData(hw_Id, data) { console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`); if (tankHeight <= 0) { - const now = moment().tz('Asia/Kolkata'); - - if (!existingTank.slave_status || existingTank.slave_status !== "not_working") { - if (!existingTank.slave_disconnected_time) { - // First time signal lost - const disconnectedTime = now.format('DD-MMM-YYYY - HH:mm:ss'); - existingTank.slave_status = "signal_lost"; - existingTank.slave_disconnected_time = disconnectedTime; - await existingTank.save(); - console.log(`⚠️ Slave signal lost for tank [${tankhardwareId}] at ${disconnectedTime}`); - } else if (existingTank.slave_status === "signal_lost") { - const disconnectedMoment = moment(existingTank.slave_disconnected_time, 'DD-MMM-YYYY - HH:mm:ss'); - const minutesDiff = now.diff(disconnectedMoment, 'minutes'); - if (minutesDiff >= 5) { - existingTank.slave_status = "not_working"; - await existingTank.save(); - console.log(`❌ Slave marked as not_working for tank [${tankhardwareId}] after ${minutesDiff} minutes`); - } else { - console.log(`⏳ Slave still within grace period (${minutesDiff} min) for tank [${tankhardwareId}]`); + 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; + 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(`⚠️ Slave disconnected for tank [${tankhardwareId}] at ${disconnectedTime}`); } else { console.log(`⏩ Slave already marked as not_working for tank [${tankhardwareId}], skipping update.`); } @@ -6190,6 +6187,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"; await linkedTank.save(); } } diff --git a/src/models/tanks.js b/src/models/tanks.js index 5627fa8f..28a56a70 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -89,7 +89,8 @@ const tanksSchema = new mongoose.Schema({ startTime: { type: String, default: null }, start_instance_id: { type: String, default: null }, stopTime: { type: String, default: null }, - waterlevelPercentage: { type: String, default: null } + waterlevelPercentage: { type: String, default: null } , + slave_status:{ type: String, default: "working" }, } ], outputConnections: [