|
|
|
@ -6217,51 +6217,36 @@ async function processIotData(hw_Id, data) {
|
|
|
|
|
console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`);
|
|
|
|
|
const now = moment().tz('Asia/Kolkata');
|
|
|
|
|
|
|
|
|
|
// Log what we fetched from DB
|
|
|
|
|
console.log(`📥 Fetched Tank [${tankhardwareId}]`);
|
|
|
|
|
console.log("🔍 slave_status:", existingTank.slave_status);
|
|
|
|
|
console.log("🔍 slave_signal_lost_time:", existingTank.slave_signal_lost_time);
|
|
|
|
|
|
|
|
|
|
// ✅ Case 1: Tank is receiving data → set to "working"
|
|
|
|
|
|
|
|
|
|
// 🚨 Case 2: Tank height is 0 — possible signal loss
|
|
|
|
|
if (tankHeight <= 0) {
|
|
|
|
|
// Case 1: First time signal is lost
|
|
|
|
|
if (
|
|
|
|
|
existingTank.slave_status !== "signal_lost1" &&
|
|
|
|
|
existingTank.slave_status !== "not_working"
|
|
|
|
|
) {
|
|
|
|
|
// First time signal lost
|
|
|
|
|
existingTank.slave_status = "signal_lost1";
|
|
|
|
|
|
|
|
|
|
// Only set signal lost time if not already set
|
|
|
|
|
if (!existingTank.slave_signal_lost_time) {
|
|
|
|
|
existingTank.slave_signal_lost_time = now.format('DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
|
console.log(`⚠️ Signal lost DETECTED for tank [${tankhardwareId}] at ${existingTank.slave_signal_lost_time}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`⚠️ Signal lost ALREADY SET earlier for tank [${tankhardwareId}] at ${existingTank.slave_signal_lost_time}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log("💾 Saving signal_lost1...");
|
|
|
|
|
existingTank.slave_disconnected_time = now.format('DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
|
await existingTank.save();
|
|
|
|
|
return; // ⛔ STOP further processing on this cycle
|
|
|
|
|
|
|
|
|
|
console.log(`⚠️ Signal lost for tank [${tankhardwareId}] at ${existingTank.slave_disconnected_time}`);
|
|
|
|
|
return; // Important: do not continue in this cycle
|
|
|
|
|
|
|
|
|
|
} else if (existingTank.slave_status === "signal_lost1") {
|
|
|
|
|
if (!existingTank.slave_signal_lost_time) {
|
|
|
|
|
console.log(`❗ ERROR: slave_status is signal_lost1 but no slave_signal_lost_time set.`);
|
|
|
|
|
if (!existingTank.slave_disconnected_time) {
|
|
|
|
|
console.log(`❗ Missing slave_disconnected_time despite signal_lost1 for tank [${tankhardwareId}]`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lostTime = moment(existingTank.slave_signal_lost_time, 'DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
|
const lostTime = moment(existingTank.slave_disconnected_time, 'DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
|
const minutesElapsed = now.diff(lostTime, 'minutes');
|
|
|
|
|
console.log(`⏳ Time since signal lost for tank [${tankhardwareId}]: ${minutesElapsed} minutes`);
|
|
|
|
|
|
|
|
|
|
if (minutesElapsed >= 15) {
|
|
|
|
|
existingTank.slave_status = "not_working";
|
|
|
|
|
existingTank.slave_disconnected_time = now.format('DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
|
console.log(`❌ Signal lost exceeded 15 minutes. Marking tank [${tankhardwareId}] as not_working.`);
|
|
|
|
|
await existingTank.save();
|
|
|
|
|
|
|
|
|
|
// 🔁 Propagate to connected output tanks
|
|
|
|
|
console.log(`❌ Slave marked as not_working for tank [${tankhardwareId}] at ${existingTank.slave_disconnected_time}`);
|
|
|
|
|
|
|
|
|
|
// Update connected output tanks
|
|
|
|
|
for (const outputConnection of existingTank.connections.outputConnections) {
|
|
|
|
|
const linkedTank = await Tank.findOne({
|
|
|
|
|
customerId,
|
|
|
|
@ -6274,24 +6259,24 @@ async function processIotData(hw_Id, data) {
|
|
|
|
|
if (inputConnection.inputConnections === tank_name) {
|
|
|
|
|
inputConnection.slave_status = "not_working";
|
|
|
|
|
await linkedTank.save();
|
|
|
|
|
console.log(`🔗 Updated linked tank [${linkedTank.tankName}] inputConnection slave_status to not_working`);
|
|
|
|
|
console.log(`🔗 Linked tank [${linkedTank.tankName}] inputConnection updated to not_working`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`✅ Finalized: Tank [${tankhardwareId}] marked not_working at ${existingTank.slave_disconnected_time}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`⏱ Still within grace period. ${15 - minutesElapsed} minutes left for tank [${tankhardwareId}]`);
|
|
|
|
|
console.log(`⏳ Still within 15-minute grace: ${minutesElapsed} minutes elapsed for tank [${tankhardwareId}]`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`⏩ No action: Tank [${tankhardwareId}] already ${existingTank.slave_status}`);
|
|
|
|
|
console.log(`⏩ Tank [${tankhardwareId}] already marked as ${existingTank.slave_status}, skipping update.`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (tankHeight > 0 && waterLevel >= 0) {
|
|
|
|
|
existingTank.waterlevel = waterLevel;
|
|
|
|
|