diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index a5903cfe..6d67c4e4 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4687,188 +4687,75 @@ async function getTankHeight(tankhardwareId) { // }; -// 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, hardwareId } = req.params; + const { customerId } = req.params; if (!customerId) { return reply.code(400).send({ error: "customerId is required" }); } - // ✅ Get all sensors for the customer + // 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." }); } - // ✅ 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()); - - // ✅ Build map of masterName/location from Order - const orders = await Order.find({ customerId }).lean(); - const orderMap = {}; - orders.forEach(order => { - order.master_connections.forEach(connection => { - orderMap[connection.hardwareId] = { - masterName: connection.master_name || null, - location: connection.location || null - }; - }); - }); + // Step 2: Get all unique master hardwareIds from connected_to + const masterHardwareIds = [ + ...new Set( + sensors.map(s => s.connected_to?.trim()).filter(Boolean) + ) + ]; - // ✅ Enrich each master with latest IoT data + // 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(); - const orderInfo = orderMap[hardwareId] || {}; - if (!latestRecord) { return { hardwareId, message: "No IoT data found", - masterName: orderInfo.masterName ?? null, - location: orderInfo.location ?? null, tanks: [] }; } - // ✅ 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"; - - // ✅ Find slaves connected to this master + // Get all slaves connected to this master const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); - // ✅ Enrich each slave/tank - const tanks = connectedSlaves.map(slave => { - const slaveId = slave.tankhardwareId?.trim(); - const matchedTank = latestRecord.tanks?.find(t => t.tankhardwareId === slaveId); + const message = latestRecord.tanks.some(t => parseFloat(t.tankHeight || 0) > 0) + ? "GSM is connected" + : "GSM is not connected"; - 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"); - loraMessage = loraDiff <= 1 ? "LORA is connected" : "LORA 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: orderInfo.masterName ?? null, - location: orderInfo.location ?? 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: orderInfo.masterName ?? null, - location: orderInfo.location ?? null, + masterName: connectedSlaves[0]?.masterName ?? null, + location: connectedSlaves[0]?.location ?? null, tanks }; })); @@ -4887,6 +4774,119 @@ exports.getIotDataByCustomer = async (req, reply) => { +// exports.getIotDataByCustomer = async (req, reply) => { +// try { +// const { customerId, hardwareId } = req.params; + +// if (!customerId) { +// return reply.code(400).send({ error: "customerId is required" }); +// } + +// // ✅ Get all sensors for the customer +// const sensors = await Insensors.find({ customerId }); + +// if (!sensors.length) { +// return reply.code(404).send({ message: "No sensors found for this customer." }); +// } + +// // ✅ 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()); + +// // ✅ Build map of masterName/location from Order +// const orders = await Order.find({ customerId }).lean(); +// const orderMap = {}; +// orders.forEach(order => { +// order.master_connections.forEach(connection => { +// orderMap[connection.hardwareId] = { +// masterName: connection.master_name || null, +// location: connection.location || null +// }; +// }); +// }); + +// // ✅ 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(); + +// const orderInfo = orderMap[hardwareId] || {}; + +// if (!latestRecord) { +// return { +// hardwareId, +// message: "No IoT data found", +// masterName: orderInfo.masterName ?? null, +// location: orderInfo.location ?? null, +// tanks: [] +// }; +// } + +// // ✅ 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"; + +// // ✅ Find slaves connected to this master +// const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); + +// // ✅ 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"); +// loraMessage = loraDiff <= 1 ? "LORA is connected" : "LORA is not connected"; +// } + +// return { +// tankhardwareId: slaveId, +// tankName: slave.tankName ?? null, +// tankLocation: slave.tankLocation ?? null, +// masterName: orderInfo.masterName ?? null, +// location: orderInfo.location ?? null, +// loraMessage, +// latestTankData: matchedTank ?? null +// }; +// }); + +// return { +// hardwareId, +// message, +// masterName: orderInfo.masterName ?? null, +// location: orderInfo.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.getIotDataByCustomerAndHardwareId = async (req, reply) => { try { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 95c31d55..5a397277 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -781,7 +781,7 @@ fastify.post( type: "object", properties: { customerId: { type: "string" }, - hardwareId: { type: "string" }, + // hardwareId: { type: "string" }, }, required: [ "customerId"],