diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 30cdffc9..64218f80 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6147,9 +6147,23 @@ async function processIotData(hw_Id, data) { const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10); 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; + await existingTank.save(); + console.log(`⚠️ Slave disconnected for tank [${tankhardwareId}] at ${disconnectedTime}`); + } else { + console.log(`⏩ Slave already marked as not_working for tank [${tankhardwareId}], skipping update.`); + } + } + if (tankHeight > 0 && waterLevel >= 0) { existingTank.waterlevel = waterLevel; + existingTank.slave_status = "working"; + existingTank.slave_disconnected_time = null; await existingTank.save(); for (const outputConnection of existingTank.connections.outputConnections) { diff --git a/src/models/tanks.js b/src/models/tanks.js index c6d700d3..5627fa8f 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -64,6 +64,8 @@ const tanksSchema = new mongoose.Schema({ notificationSentHigh: { type: Boolean }, all_motor_status: { type: Boolean }, status:{ type: String, default: "active" }, + slave_status:{ type: String, default: "working" }, + slave_disconnected_time :{ type: String, default: null }, connections: { source: { type: String },