diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 62c11089..1df1c564 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1574,6 +1574,92 @@ exports.getMasterSlaveSummary = async (req, reply) => { // }; +// exports.getIotDataByCustomer = async (req, reply) => { +// try { +// const { customerId } = req.params; + +// if (!customerId) { +// return reply.code(400).send({ error: "customerId is required" }); +// } + +// // Step 1: Get all devices for this customer +// const sensors = await Insensors.find({ customerId }); + +// if (!sensors.length) { +// return reply.code(404).send({ message: "No sensors found for this customer." }); +// } + +// // Step 2: Get all unique master hardwareIds from connected_to +// const masterHardwareIds = [ +// ...new Set( +// sensors.map(s => s.connected_to?.trim()).filter(Boolean) +// ) +// ]; + +// // Step 3: Loop over each master (connected_to) and build enriched data +// const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => { +// const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean(); + +// if (!latestRecord) { +// return { +// hardwareId, +// message: "No IoT data found", +// tanks: [] +// }; +// } + +// // Get all slaves connected to this master +// const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); + +// const message = latestRecord.tanks.some(t => parseFloat(t.tankHeight || 0) > 0) +// ? "GSM is connected" +// : "GSM is not connected"; + +// // Map connected slaves to tank details +// let tanks = connectedSlaves.map(slave => { +// const slaveId = slave.hardwareId?.trim(); +// const matchedTank = latestRecord.tanks.find(tank => tank.tankhardwareId === slaveId); + +// const loraMessage = matchedTank && parseFloat(matchedTank.tankHeight || 0) > 0 +// ? "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, +// latestTankData: matchedTank ?? null +// }; +// }); + +// // ❗ Remove the first tank entry +// tanks = tanks.slice(1); + +// return { +// hardwareId, +// message, +// masterName: connectedSlaves[0]?.masterName ?? null, +// location: connectedSlaves[0]?.location ?? null, +// tanks +// }; +// })); + +// return reply.send({ +// status_code: 200, +// message: "Success", +// data: enrichedMasters +// }); + +// } catch (err) { +// console.error("Error fetching IoT data by customerId:", err); +// return reply.code(500).send({ error: "Internal Server Error" }); +// } +// }; + + exports.getIotDataByCustomer = async (req, reply) => { try { const { customerId } = req.params; @@ -1582,21 +1668,16 @@ exports.getIotDataByCustomer = async (req, reply) => { return reply.code(400).send({ error: "customerId is required" }); } - // Step 1: Get all devices for this customer const sensors = await Insensors.find({ customerId }); if (!sensors.length) { return reply.code(404).send({ message: "No sensors found for this customer." }); } - // Step 2: Get all unique master hardwareIds from connected_to const masterHardwareIds = [ - ...new Set( - sensors.map(s => s.connected_to?.trim()).filter(Boolean) - ) + ...new Set(sensors.map(s => s.connected_to?.trim()).filter(Boolean)) ]; - // Step 3: Loop over each master (connected_to) and build enriched data const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => { const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean(); @@ -1608,21 +1689,28 @@ exports.getIotDataByCustomer = async (req, reply) => { }; } - // Get all slaves connected to this master - const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); + // ✅ 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 = latestRecord.tanks.some(t => parseFloat(t.tankHeight || 0) > 0) - ? "GSM is connected" - : "GSM is not connected"; + const message = gsmConnected ? "GSM is connected" : "GSM is not connected"; + + // Get all connected slaves + const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); - // Map connected slaves to tank details - let tanks = connectedSlaves.map(slave => { + // Enrich tanks + const tanks = connectedSlaves.map(slave => { const slaveId = slave.hardwareId?.trim(); const matchedTank = latestRecord.tanks.find(tank => tank.tankhardwareId === slaveId); - const loraMessage = matchedTank && parseFloat(matchedTank.tankHeight || 0) > 0 - ? "LORA is connected" - : "LORA is not connected"; + let loraConnected = false; + if (matchedTank?.date) { + const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); + const loraDiff = now.diff(tankTime, "minutes"); + loraConnected = loraDiff <= 1; + } return { tankhardwareId: slaveId, @@ -1630,14 +1718,11 @@ exports.getIotDataByCustomer = async (req, reply) => { tankLocation: slave.tankLocation ?? null, masterName: slave.masterName ?? null, location: slave.location ?? null, - loraMessage, + loraMessage: loraConnected ? "LORA is connected" : "LORA is not connected", latestTankData: matchedTank ?? null }; }); - // ❗ Remove the first tank entry - tanks = tanks.slice(1); - return { hardwareId, message, @@ -1658,4 +1743,3 @@ exports.getIotDataByCustomer = async (req, reply) => { return reply.code(500).send({ error: "Internal Server Error" }); } }; -