diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 69f80fc9..ac584ff2 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6134,7 +6134,9 @@ async function processIotData(hw_Id, data) { for (const tank of tanks) { const { Id: tankhardwareId, level: tankHeight } = tank; const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId }); - + console.log(hw_Id,"hw_Id") + console.log(tankhardwareId,"tankhardwareId") + console.log(existingTank,"existingTank") if (!existingTank) continue; const customerId = existingTank.customerId; @@ -6148,38 +6150,30 @@ 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'); - const formattedNow = now.format('DD-MMM-YYYY - HH:mm:ss'); - if (existingTank.slave_status !== "not_working") { - if (!existingTank.slave_disconnected_time) { - // First time signal is lost - existingTank.slave_status = "signal_lost"; - existingTank.slave_disconnected_time = formattedNow; - await existingTank.save(); - console.log(`⚠️ Slave signal lost for tank [${tankhardwareId}] at ${formattedNow}`); - } else if (existingTank.slave_status === "signal_lost") { - const disconnectedMoment = moment(existingTank.slave_disconnected_time, 'DD-MMM-YYYY - HH:mm:ss'); - if (!disconnectedMoment.isValid()) { - console.error(`❌ Invalid slave_disconnected_time format for tank [${tankhardwareId}]`); - } else { - const minutesDiff = now.diff(disconnectedMoment, 'minutes'); - console.log(`⏳ Minutes since disconnect for tank [${tankhardwareId}]: ${minutesDiff} min`); - - 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(`ℹ️ Still within 5-minute window for tank [${tankhardwareId}], no update yet`); + 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`); + console.log(`⏩ Slave already marked as not_working for tank [${tankhardwareId}], skipping update.`); } } + if (tankHeight > 0 && waterLevel >= 0) { existingTank.waterlevel = waterLevel; @@ -6193,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(); } } @@ -7753,4 +7748,4 @@ exports.compareMeasuredHeight = async (req, reply) => { console.error("Error in compareMeasuredHeight:", err); reply.status(500).send({ message: err.message }); } -}; +}; \ No newline at end of file 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: [