changes in water/iot-data

master^2
Varun 5 months ago
parent 4bd3948658
commit 70f9dc1dfb

@ -6216,30 +6216,55 @@ async function processIotData(hw_Id, data) {
console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`);
if (tankHeight <= 0) {
if (existingTank.slave_status !== "not_working") {
const disconnectedTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
existingTank.slave_status = "not_working";
existingTank.slave_disconnected_time = disconnectedTime;
const now = moment().tz('Asia/Kolkata');
if (existingTank.slave_status === "working" || !existingTank.slave_status) {
// 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();
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(`⚠️ Signal lost for tank [${tankhardwareId}] at ${existingTank.slave_signal_lost_time}`);
} else if (existingTank.slave_status === "signal_lost") {
const lostTime = moment(existingTank.slave_signal_lost_time, 'DD-MMM-YYYY - HH:mm:ss');
const minutesElapsed = now.diff(lostTime, 'minutes');
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 {
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) {
existingTank.waterlevel = waterLevel;

Loading…
Cancel
Save