master^2
Varun 5 months ago
parent 4051432d6e
commit 17d041dc50

@ -6147,44 +6147,9 @@ async function processIotData(hw_Id, data) {
const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10); const waterLevel = parseInt(waterLevelHeight * waterCapacityPerCm, 10);
console.log(`🚰 Tank [${tankhardwareId}] - Level: ${tankHeight}, Calculated Water Level: ${waterLevel}`); 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`);
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`);
}
}
}
} else {
console.log(`⏩ Slave already marked as not_working for tank [${tankhardwareId}], skipping update`);
}
}
if (tankHeight > 0 && waterLevel >= 0) { if (tankHeight > 0 && waterLevel >= 0) {
existingTank.waterlevel = waterLevel; existingTank.waterlevel = waterLevel;
existingTank.slave_status = "working";
existingTank.slave_disconnected_time = null;
await existingTank.save(); await existingTank.save();
for (const outputConnection of existingTank.connections.outputConnections) { for (const outputConnection of existingTank.connections.outputConnections) {
@ -6224,7 +6189,6 @@ async function processIotData(hw_Id, data) {
const startTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss'); const startTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss');
const startInstanceId = `${hw_Id}${formattedTime}`; const startInstanceId = `${hw_Id}${formattedTime}`;
inputConnection.motor_stop_status = "2"; inputConnection.motor_stop_status = "2";
inputConnection.motor_on_type = "forced_manual"; inputConnection.motor_on_type = "forced_manual";
inputConnection.startTime = startTime1; inputConnection.startTime = startTime1;
@ -7754,4 +7718,4 @@ exports.compareMeasuredHeight = async (req, reply) => {
console.error("Error in compareMeasuredHeight:", err); console.error("Error in compareMeasuredHeight:", err);
reply.status(500).send({ message: err.message }); reply.status(500).send({ message: err.message });
} }
}; };
Loading…
Cancel
Save