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