From fce456b73bb62a40a4c274fc422272d208773771 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 4 Jul 2025 15:51:23 +0530 Subject: [PATCH] master and conected slaves response in installation --- src/controllers/installationController.js | 144 +++++++++++++++------- 1 file changed, 99 insertions(+), 45 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 3aa0831c..a246cc88 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1227,68 +1227,124 @@ exports.createMasterSlaveData = async (req, reply) => { reply.status(500).send({ message: err.message }); } }; -exports.masterConnectedSlaveList = async (req, reply) => { - try { - const { connectedTo } = req.params; +// exports.masterConnectedSlaveList = async (req, reply) => { +// try { +// const { connectedTo } = req.params; - // Step 1: Find all slave tanks where connected_to matches - const slaveTanks = await Insensors.find({ connected_to: connectedTo }).lean(); - console.log(slaveTanks, "slaveTanks"); +// // Step 1: Find all slave tanks where connected_to matches +// const slaveTanks = await Insensors.find({ connected_to: connectedTo }).lean(); +// console.log(slaveTanks, "slaveTanks"); - if (!slaveTanks || slaveTanks.length === 0) { - return reply.status(404).send({ - success: false, - message: "No tanks found for the given connected_to value" - }); - } +// if (!slaveTanks || slaveTanks.length === 0) { +// return reply.status(404).send({ +// success: false, +// message: "No tanks found for the given connected_to value" +// }); +// } - // Step 2: Fetch `tankLocation` and `typeOfWater` from the Tank schema (for master device display) - const tankDetails = await Tank.findOne( - { hardwareId: connectedTo }, - { tankLocation: 1, typeOfWater: 1 } - ); +// // Step 2: Fetch `tankLocation` and `typeOfWater` from the Tank schema (for master device display) +// const tankDetails = await Tank.findOne( +// { hardwareId: connectedTo }, +// { tankLocation: 1, typeOfWater: 1 } +// ); + +// if (!tankDetails) { +// return reply.status(404).send({ +// success: false, +// message: "Tank details not found for the given connectedTo" +// }); +// } + +// // Step 3: Count the number of connected slaves +// const slaveCount = slaveTanks.length; + +// // Step 4: Fetch latest IotData for the master to extract tankHeight for each slave +// const latestIotData = await IotData.findOne({ hardwareId: connectedTo }).sort({ date: -1 }).lean(); + +// // Step 5: Prepare processed response for each tank +// const processedSlaves = slaveTanks.map(slave => { +// if (slave.type === 'slave') { +// // Find matching tank data for this slave in IotData +// const matchingTankData = latestIotData?.tanks?.find(tank => tank.tankhardwareId === slave.hardwareId); + +// return { +// ...slave, +// tankHeight: matchingTankData?.tankHeight ?? null // Add tankHeight if found +// }; +// } else { +// // Remove unnecessary fields for master type +// const { +// tankName, +// tankLocation, +// typeOfWater, +// ...rest +// } = slave; +// return rest; +// } +// }); + +// return reply.send({ +// success: true, +// tankLocation: tankDetails.tankLocation, +// typeOfWater: tankDetails.typeOfWater, +// connectedSlaveCount: slaveCount, +// data: processedSlaves +// }); - if (!tankDetails) { +// } catch (error) { +// console.error("Error fetching master connected slave data:", error); +// return reply.status(500).send({ +// success: false, +// message: "Internal Server Error" +// }); +// } +// }; + + +exports.masterConnectedSlaveList = async (req, reply) => { + try { + const { connectedTo } = req.params; + + // Step 1: Get master details + const master = await Insensors.findOne({ hardwareId: connectedTo, type: 'master' }).lean(); + if (!master) { return reply.status(404).send({ success: false, - message: "Tank details not found for the given connectedTo" + message: "Master device not found" }); } - // Step 3: Count the number of connected slaves + // Step 2: Get slave tanks connected to master + const slaveTanks = await Insensors.find({ connected_to: connectedTo, type: 'slave' }).lean(); const slaveCount = slaveTanks.length; - // Step 4: Fetch latest IotData for the master to extract tankHeight for each slave + // Step 3: Get Tank metadata for master + const tankDetails = await Tank.findOne({ hardwareId: connectedTo }, { tankLocation: 1, typeOfWater: 1 }).lean(); + + // Step 4: Get latest IotData for master const latestIotData = await IotData.findOne({ hardwareId: connectedTo }).sort({ date: -1 }).lean(); - // Step 5: Prepare processed response for each tank + // Step 5: Process slaves const processedSlaves = slaveTanks.map(slave => { - if (slave.type === 'slave') { - // Find matching tank data for this slave in IotData - const matchingTankData = latestIotData?.tanks?.find(tank => tank.tankhardwareId === slave.hardwareId); - - return { - ...slave, - tankHeight: matchingTankData?.tankHeight ?? null // Add tankHeight if found - }; - } else { - // Remove unnecessary fields for master type - const { - tankName, - tankLocation, - typeOfWater, - ...rest - } = slave; - return rest; - } + const matchingTankData = latestIotData?.tanks?.find(tank => tank.tankhardwareId === slave.hardwareId); + return { + ...slave, + tankHeight: matchingTankData?.tankHeight ?? null + }; }); + // Step 6: Prepare full master object + const masterResponse = { + ...master, + tankLocation: tankDetails?.tankLocation || null, + typeOfWater: tankDetails?.typeOfWater || null + }; + return reply.send({ success: true, - tankLocation: tankDetails.tankLocation, - typeOfWater: tankDetails.typeOfWater, + master: masterResponse, connectedSlaveCount: slaveCount, - data: processedSlaves + slaves: processedSlaves }); } catch (error) { @@ -1302,8 +1358,6 @@ exports.masterConnectedSlaveList = async (req, reply) => { - - exports.mastrerList = async (req, reply) => { try { const { customerId, installationId } = req.params;