diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 07284d1d..1e0e08aa 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1952,10 +1952,9 @@ exports.getIotDataByCustomer = async (req, reply) => { const masterHardwareIds = [ ...new Set(sensors.map(s => s.connected_to?.trim()).filter(Boolean)) ]; - const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => { const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean(); - + if (!latestRecord) { return { hardwareId, @@ -1963,44 +1962,48 @@ exports.getIotDataByCustomer = async (req, reply) => { tanks: [] }; } - + // ✅ Use timestamp to check GSM connection const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata"); const now = moment.tz("Asia/Kolkata"); const diffInMinutes = now.diff(indiaTime, "minutes"); const gsmConnected = diffInMinutes <= 1; - + const message = gsmConnected ? "GSM is connected" : "GSM is not connected"; - + // Get all connected slaves const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); - + // Enrich tanks let tanks = connectedSlaves.map(slave => { const slaveId = slave.hardwareId?.trim(); const matchedTank = latestRecord.tanks.find(tank => tank.tankhardwareId === slaveId); - + let loraConnected = false; - if (matchedTank?.date) { + let loraMessage = "LORA is not connected"; // Default message for LoRa not connected + + // If matchedTank exists and tankHeight is not zero, check LoRa connection + if (matchedTank?.date && matchedTank.tankHeight !== "0") { const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); const loraDiff = now.diff(tankTime, "minutes"); loraConnected = loraDiff <= 1; + loraMessage = loraConnected ? "LORA is connected" : "LORA is not connected"; } - + return { tankhardwareId: slaveId, tankName: slave.tankName ?? null, tankLocation: slave.tankLocation ?? null, masterName: slave.masterName ?? null, location: slave.location ?? null, - loraMessage: loraConnected ? "LORA is connected" : "LORA is not connected", + loraMessage, // Updated LoRa message latestTankData: matchedTank ?? null }; }); - + // ✅ Remove the first tank tanks = tanks.slice(1); - + return { hardwareId, message, @@ -2009,6 +2012,7 @@ exports.getIotDataByCustomer = async (req, reply) => { tanks }; })); + return reply.send({ status_code: 200,