From 1b3172391adbd0335d5c072af7a35e4a229fcecc Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 18 Jul 2025 17:50:21 +0530 Subject: [PATCH] check all connections added hardwareId --- src/controllers/installationController.js | 28 +++++++++++++++-------- src/routes/installationRoute.js | 5 ++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 05db1c54..a5903cfe 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4776,7 +4776,7 @@ async function getTankHeight(tankhardwareId) { exports.getIotDataByCustomer = async (req, reply) => { try { - const { customerId } = req.params; + const { customerId, hardwareId } = req.params; if (!customerId) { return reply.code(400).send({ error: "customerId is required" }); @@ -4789,11 +4789,21 @@ exports.getIotDataByCustomer = async (req, reply) => { return reply.code(404).send({ message: "No sensors found for this customer." }); } - // ✅ Get only master hardware IDs from Insensors directly - const masterSensors = sensors.filter(s => s.type === 'master'); + // ✅ Filter master sensors + let masterSensors = sensors.filter(s => s.type === 'master'); + + // ✅ If hardwareId is provided, filter to that master only + if (hardwareId) { + masterSensors = masterSensors.filter(m => m.hardwareId?.trim() === hardwareId.trim()); + } + + if (!masterSensors.length) { + return reply.code(404).send({ message: "No master found for the given hardwareId or customer." }); + } + const masterHardwareIds = masterSensors.map(m => m.hardwareId?.trim()); - // ✅ Map for masterName/location from Order + // ✅ Build map of masterName/location from Order const orders = await Order.find({ customerId }).lean(); const orderMap = {}; orders.forEach(order => { @@ -4805,7 +4815,7 @@ exports.getIotDataByCustomer = async (req, reply) => { }); }); - // ✅ Prepare final enriched master data + // ✅ Enrich each master with latest IoT data const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => { const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean(); @@ -4821,23 +4831,22 @@ exports.getIotDataByCustomer = async (req, reply) => { }; } - // ✅ Check GSM status + // ✅ GSM connection status 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 slaves connected to this master + // ✅ Find slaves connected to this master const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); - // ✅ Prepare tank info + // ✅ Enrich each slave/tank const tanks = connectedSlaves.map(slave => { const slaveId = slave.tankhardwareId?.trim(); const matchedTank = latestRecord.tanks?.find(t => t.tankhardwareId === slaveId); let loraMessage = "LORA is not connected"; - if (matchedTank?.date && matchedTank.tankHeight !== "0") { const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); const loraDiff = now.diff(tankTime, "minutes"); @@ -4878,6 +4887,7 @@ exports.getIotDataByCustomer = async (req, reply) => { + exports.getIotDataByCustomerAndHardwareId = async (req, reply) => { try { const { customerId, hardwareId } = req.params; diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 62dbd91e..95c31d55 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -772,7 +772,7 @@ fastify.post( handler: installationController.getPendingMasterSlaveSummary, }); - fastify.get("/api/getAllmasterlistwithslaves/:customerId", { + fastify.get("/api/getAllmasterlistwithslaves/:customerId/:hardwareId", { schema: { description: "Get All check masrter connected slave data with full info", tags: ["Installation"], @@ -781,7 +781,8 @@ fastify.post( type: "object", properties: { customerId: { type: "string" }, - + hardwareId: { type: "string" }, + }, required: [ "customerId"], },