Bhaskar 5 months ago
commit d622093381

@ -6134,7 +6134,9 @@ async function processIotData(hw_Id, data) {
for (const tank of tanks) {
const { Id: tankhardwareId, level: tankHeight } = tank;
const existingTank = await Tank.findOne({ hardwareId: hw_Id, tankhardwareId });
console.log(hw_Id,"hw_Id")
console.log(tankhardwareId,"tankhardwareId")
console.log(existingTank,"existingTank")
if (!existingTank) continue;
const customerId = existingTank.customerId;
@ -6148,39 +6150,31 @@ async function processIotData(hw_Id, data) {
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`);
const disconnectedTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
existingTank.slave_status = "not_working";
existingTank.slave_disconnected_time = disconnectedTime;
await existingTank.save();
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`);
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 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 not_working for tank [${tankhardwareId}], skipping update.`);
}
}
if (tankHeight > 0 && waterLevel >= 0) {
existingTank.waterlevel = waterLevel;
existingTank.slave_status = "working";
@ -6193,6 +6187,7 @@ async function processIotData(hw_Id, data) {
for (const inputConnection of linkedTank.connections.inputConnections) {
if (inputConnection.inputConnections === tank_name) {
inputConnection.water_level = waterLevel;
inputConnection. slave_status = "working";
await linkedTank.save();
}
}

@ -89,7 +89,8 @@ const tanksSchema = new mongoose.Schema({
startTime: { type: String, default: null },
start_instance_id: { type: String, default: null },
stopTime: { type: String, default: null },
waterlevelPercentage: { type: String, default: null }
waterlevelPercentage: { type: String, default: null } ,
slave_status:{ type: String, default: "working" },
}
],
outputConnections: [

Loading…
Cancel
Save