Bhaskar 5 months ago
commit bacca1fe1f

@ -6147,9 +6147,44 @@ 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) {
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`);
}
}
}
} 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) {

@ -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 },

Loading…
Cancel
Save