|
|
@ -6216,30 +6216,55 @@ async function processIotData(hw_Id, data) {
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`);
|
|
|
|
console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`);
|
|
|
|
if (tankHeight <= 0) {
|
|
|
|
if (tankHeight <= 0) {
|
|
|
|
if (existingTank.slave_status !== "not_working") {
|
|
|
|
const now = moment().tz('Asia/Kolkata');
|
|
|
|
const disconnectedTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
|
|
|
|
existingTank.slave_status = "not_working";
|
|
|
|
if (existingTank.slave_status === "working" || !existingTank.slave_status) {
|
|
|
|
existingTank.slave_disconnected_time = disconnectedTime;
|
|
|
|
// First time signal is lost
|
|
|
|
|
|
|
|
existingTank.slave_status = "signal_lost";
|
|
|
|
|
|
|
|
existingTank.slave_signal_lost_time = now.format('DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
await existingTank.save();
|
|
|
|
await existingTank.save();
|
|
|
|
|
|
|
|
|
|
|
|
for (const outputConnection of existingTank.connections.outputConnections) {
|
|
|
|
console.log(`⚠️ Signal lost for tank [${tankhardwareId}] at ${existingTank.slave_signal_lost_time}`);
|
|
|
|
const linkedTank = await Tank.findOne({ customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
|
|
|
|
|
|
|
|
if (linkedTank) {
|
|
|
|
} else if (existingTank.slave_status === "signal_lost") {
|
|
|
|
for (const inputConnection of linkedTank.connections.inputConnections) {
|
|
|
|
const lostTime = moment(existingTank.slave_signal_lost_time, 'DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
if (inputConnection.inputConnections === tank_name) {
|
|
|
|
const minutesElapsed = now.diff(lostTime, 'minutes');
|
|
|
|
inputConnection. slave_status = "not_working";
|
|
|
|
|
|
|
|
await linkedTank.save();
|
|
|
|
if (minutesElapsed >= 15) {
|
|
|
|
|
|
|
|
existingTank.slave_status = "not_working";
|
|
|
|
|
|
|
|
existingTank.slave_disconnected_time = now.format('DD-MMM-YYYY - HH:mm:ss');
|
|
|
|
|
|
|
|
await existingTank.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Propagate status to output connections
|
|
|
|
|
|
|
|
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 marked as not_working for tank [${tankhardwareId}] at ${existingTank.slave_disconnected_time}`);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
console.log(`⏳ Slave still in signal_lost for tank [${tankhardwareId}], waiting for 15 mins.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`⚠️ Slave disconnected for tank [${tankhardwareId}] at ${disconnectedTime}`);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.log(`⏩ Slave already marked as not_working for tank [${tankhardwareId}], skipping update.`);
|
|
|
|
console.log(`⏩ Slave already marked as ${existingTank.slave_status} for tank [${tankhardwareId}], skipping update.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (tankHeight > 0 && waterLevel >= 0) {
|
|
|
|
if (tankHeight > 0 && waterLevel >= 0) {
|
|
|
|
existingTank.waterlevel = waterLevel;
|
|
|
|
existingTank.waterlevel = waterLevel;
|
|
|
|