From 079cdff3e2cc3cb0923e105d9ee34bfc548fbbc6 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 16 Apr 2025 17:41:58 +0530 Subject: [PATCH] CHANGES --- src/controllers/installationController.js | 111 +++++++++++++++++++++- 1 file changed, 106 insertions(+), 5 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index e509923c..e1674213 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1213,6 +1213,91 @@ 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" }); +// } + +// // Fetch all sensors of the customer +// const sensors = await Insensors.find({ customerId }); + +// if (!sensors || sensors.length === 0) { +// return reply.code(404).send({ message: "No sensors found for this customer." }); +// } + +// // Extract unique hardwareIds +// const hardwareIds = [ +// ...new Set( +// sensors.map(sensor => sensor.connected_to?.trim()).filter(Boolean) +// ) +// ]; + +// if (hardwareIds.length === 0) { +// return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] }); +// } + +// // Fetch latest IoT data and enrich it +// const latestDataPromises = hardwareIds.map(async hardwareId => { +// const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean(); + +// const relatedSensors = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); + +// let message = "GSM is not connected"; +// if (latestRecord?.tanks?.some(tank => parseFloat(tank.tankHeight || 0) > 0)) { +// message = "GSM is connected"; +// } + +// // Prepare individual tank connection status +// const tanksStatus = relatedSensors.map(sensor => { +// const tankhardwareId = sensor.hardwareId?.trim(); + +// const matchedTank = latestRecord?.tanks?.find( +// tank => tank.tankhardwareId === tankhardwareId +// ); + +// const loraMessage = +// matchedTank && parseFloat(matchedTank.tankHeight || 0) > 0 +// ? "LORA is connected" +// : "LORA is not connected"; + +// return { +// tankhardwareId, +// tankName: sensor.tankName ?? null, +// tankLocation: sensor.tankLocation ?? null, +// masterName: sensor.masterName ?? null, +// location: sensor.location ?? null, +// loraMessage, +// latestTankData: matchedTank || null +// }; +// }); + +// return { +// hardwareId, +// message, +// masterName: relatedSensors[0]?.masterName ?? null, +// location: relatedSensors[0]?.location ?? null, +// tanks: tanksStatus.slice(1) // 👈 Skip the first tank +// }; +// }); + +// const iotDataGrouped = await Promise.all(latestDataPromises); + +// return reply.send({ +// status_code: 200, +// message: "Success", +// data: iotDataGrouped +// }); + +// } 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; @@ -1241,19 +1326,34 @@ exports.getIotDataByCustomer = async (req, reply) => { // Fetch latest IoT data and enrich it const latestDataPromises = hardwareIds.map(async hardwareId => { - const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean(); + // Fetch the latest IoT data for the hardwareId + const latestRecord = await IotData.findOne({ hardwareId }) + .sort({ date: -1 }) + .lean(); + // If there's no IoT data for the hardwareId, skip processing + if (!latestRecord) { + return { + hardwareId, + message: "No IoT data found", + tanks: [] + }; + } + + // Find sensors related to this hardwareId const relatedSensors = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); + // Default message indicating GSM connection status let message = "GSM is not connected"; + + // If any tank data indicates a height greater than 0, assume GSM is connected if (latestRecord?.tanks?.some(tank => parseFloat(tank.tankHeight || 0) > 0)) { message = "GSM is connected"; } - // Prepare individual tank connection status + // Prepare individual tank connection statuses const tanksStatus = relatedSensors.map(sensor => { const tankhardwareId = sensor.hardwareId?.trim(); - const matchedTank = latestRecord?.tanks?.find( tank => tank.tankhardwareId === tankhardwareId ); @@ -1279,12 +1379,14 @@ exports.getIotDataByCustomer = async (req, reply) => { message, masterName: relatedSensors[0]?.masterName ?? null, location: relatedSensors[0]?.location ?? null, - tanks: tanksStatus.slice(1) // 👈 Skip the first tank + tanks: tanksStatus.slice(1) // Skip the first tank as needed }; }); + // Wait for all promises to resolve const iotDataGrouped = await Promise.all(latestDataPromises); + // Return the enriched IoT data return reply.send({ status_code: 200, message: "Success", @@ -1296,4 +1398,3 @@ exports.getIotDataByCustomer = async (req, reply) => { return reply.code(500).send({ error: "Internal Server Error" }); } }; -