From 0c2d6bb04dfba759eb0cf102175eb92b00fd3362 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 16 Apr 2025 18:02:19 +0530 Subject: [PATCH] chnages --- src/controllers/installationController.js | 90 ++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 5a2ade4f..e3487326 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1403,6 +1403,94 @@ exports.getMasterSlaveSummary = async (req, reply) => { // } // }; +//important +// exports.getIotDataByCustomer = async (req, reply) => { +// try { +// const { customerId } = req.params; + +// if (!customerId) { +// return reply.status(400).send({ error: "customerId is required" }); +// } + +// console.log("Fetching data for customerId:", customerId); + +// // Step 1: Fetch all sensors for the customer +// const sensors = await Insensors.find({ customerId }); + +// if (!sensors || sensors.length === 0) { +// return reply.send({ status_code: 404, message: "No sensors found for this customer", data: [] }); +// } + +// // Step 2: Fetch latest IoT data for each sensor's hardwareId +// const enrichedSensors = await Promise.all(sensors.map(async (sensor) => { +// const { connected_to: hardwareId, masterName, location } = sensor; + +// // Fetch the latest IoT data for this hardwareId +// const iotData = await IotData.findOne({ hardwareId }) +// .sort({ date: -1 }) // Sort to get the latest data +// .lean(); + +// if (!iotData) { +// return { +// hardwareId, +// message: "No IoT data found", +// masterName, +// location, +// tanks: [] +// }; +// } + +// // Step 3: Get GSM status based on the latest IoT data +// const latestRecord = iotData; +// const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata"); +// const connected_gsm_date = indiaTime.format("YYYY-MM-DD"); +// const connected_gsm_time = indiaTime.format("HH:mm:ss"); + +// const now = moment.tz("Asia/Kolkata"); +// const diffInMinutes = now.diff(indiaTime, "minutes"); +// const isGSMConnected = diffInMinutes <= 1; +// const gsmStatus = isGSMConnected ? "GSM Connected" : "GSM Not Connected"; + +// // Step 4: Annotate each tank with LoRa connection status +// const tanksWithConnectionStatus = latestRecord.tanks.map(tank => { +// const tankMoment = moment.tz(tank.date, "Asia/Kolkata"); +// const tankDiff = now.diff(tankMoment, "minutes"); + +// const loraStatus = tankDiff <= 1 ? "LORA is connected" : "LORA is not connected"; +// return { +// tankhardwareId: tank.tankhardwareId, +// tankName: sensor.tankName ?? null, +// tankLocation: sensor.tankLocation ?? null, +// masterName: sensor.masterName ?? null, +// location: sensor.location ?? null, +// loraMessage: loraStatus, +// latestTankData: tank +// }; +// }); + +// // Step 5: Return enriched sensor data in the desired format +// return { +// hardwareId, +// message: gsmStatus, +// masterName, +// location, +// tanks: tanksWithConnectionStatus +// }; +// })); + +// // Step 6: Return the enriched data for the customer +// return reply.send({ +// status_code: 200, +// message: "Success", +// data: enrichedSensors +// }); + +// } catch (err) { +// console.error("Error in getAllDataForCustomer:", err); +// return reply.status(500).send({ error: "Internal Server Error" }); +// } +// }; + exports.getIotDataByCustomer = async (req, reply) => { try { @@ -1452,7 +1540,7 @@ exports.getIotDataByCustomer = async (req, reply) => { const gsmStatus = isGSMConnected ? "GSM Connected" : "GSM Not Connected"; // Step 4: Annotate each tank with LoRa connection status - const tanksWithConnectionStatus = latestRecord.tanks.map(tank => { + const tanksWithConnectionStatus = latestRecord.tanks.slice(0, 2).map(tank => { // Limit to 2 tanks const tankMoment = moment.tz(tank.date, "Asia/Kolkata"); const tankDiff = now.diff(tankMoment, "minutes");