diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 17a031e0..beeeba29 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -2658,19 +2658,25 @@ exports.getMasterSlaveSummary = async (req, reply) => { return reply.status(400).send({ error: "customerId is required" }); } - // Fetch all master devices for the customer + // Fetch all master devices from Insensors const masters = await Insensors.find({ customerId, type: "master" }).lean(); - // Fetch orders to map hardwareId to masterName/location + // Fetch orders to build orderMap: hardwareId → { masterName, location, work_status } const orders = await Order.find({ customerId }).lean(); - const orderMap = {}; + const orderMap = {}; // key: hardwareId + orders.forEach(order => { - order.master_connections.forEach(connection => { - orderMap[connection.hardwareId] = { - masterName: connection.master_name || null, - location: connection.location || null, - }; - }); + if (Array.isArray(order.master_connections)) { + order.master_connections.forEach(connection => { + if (connection.hardwareId) { + orderMap[connection.hardwareId] = { + masterName: connection.master_name || null, + location: connection.location || null, + work_status: connection.work_status || null + }; + } + }); + } }); const result = []; @@ -2678,11 +2684,16 @@ exports.getMasterSlaveSummary = async (req, reply) => { for (const master of masters) { const orderInfo = orderMap[master.hardwareId] || {}; - // Prefer Insensors info, fallback to order info + // ✅ Only keep masters where work_status === 'active' + if (orderInfo.work_status !== 'active') { + continue; // skip this master + } + + // Prefer Insensors name/location, fallback to order info const masterName = master.masterName || orderInfo.masterName || null; const location = master.location || orderInfo.location || null; - // Fetch the latest GSM data for the master device + // Fetch latest GSM data const latestGsmData = await IotData.findOne({ hardwareId: master.hardwareId }) .sort({ date: -1, time: -1 }) .lean(); @@ -2690,17 +2701,15 @@ exports.getMasterSlaveSummary = async (req, reply) => { let connectedGsmDate = null; let connectedGsmTime = null; let gsmStatus = "unknown"; - let gsmLastCheckTime = null; let gsmLastDisconnect = master.gsm_last_disconnect_time || null; + let gsmLastCheckTime = master.gsm_last_check_time || null; if (latestGsmData?.date && latestGsmData?.time) { - // Combine date + time and parse as Asia/Kolkata timezone const indiaTime = moment.tz( `${moment(latestGsmData.date).format("YYYY-MM-DD")} ${latestGsmData.time}`, "YYYY-MM-DD HH:mm:ss", "Asia/Kolkata" ); - connectedGsmDate = indiaTime.format("DD-MM-YYYY"); connectedGsmTime = indiaTime.format("HH:mm:ss"); @@ -2708,14 +2717,12 @@ exports.getMasterSlaveSummary = async (req, reply) => { const diffInMinutes = now.diff(indiaTime, "minutes"); gsmStatus = diffInMinutes <= 1 ? "connected" : "disconnected"; - gsmLastCheckTime =master.gsm_last_check_time; if (gsmStatus === "disconnected") { - // Update disconnect time with latest disconnect timestamp gsmLastDisconnect = `${connectedGsmDate} ${connectedGsmTime}`; } - // Update master device with latest GSM info + // Update master Insensors record await Insensors.updateOne( { hardwareId: master.hardwareId }, { @@ -2724,68 +2731,65 @@ exports.getMasterSlaveSummary = async (req, reply) => { connected_gsm_date: connectedGsmDate, connected_gsm_time: connectedGsmTime, gsm_last_check_time: gsmLastCheckTime, - gsm_last_disconnect_time: gsmLastDisconnect, - }, + gsm_last_disconnect_time: gsmLastDisconnect + } } ); } - // Process connected slave devices for the master + // Process connected slaves const connectedSlaves = []; const slaves = await Insensors.find({ connected_to: master.hardwareId, type: "slave" }).lean(); - console.log("slaves",slaves) - for (const slave of slaves) { const now = moment.tz("Asia/Kolkata"); let connectedLoraDate = null; let connectedLoraTime = null; let loraStatus = "disconnected"; let loraLastDisconnect = slave.lora_last_disconnect_time || null; - let loraLastCheckTime = slave.lora_last_check_time; - let tankHeight = null; - - // Fetch latest IotData for slave's hardwareId (NOT master's) + let loraLastCheckTime = slave.lora_last_check_time || null; + let typeOfWater = null; + + // Fetch latest slave IotData (using master hardwareId because slave data comes there) const slaveIot = await IotData.findOne({ hardwareId: slave.connected_to }) .sort({ date: -1, time: -1 }) .lean(); - + if (slaveIot?.tanks?.length && slave.tankhardwareId) { - const matchedTank = slaveIot.tanks.find( - t => t.tankhardwareId === slave.tankhardwareId - ); - + const matchedTank = slaveIot.tanks.find(t => t.tankhardwareId === slave.tankhardwareId); + if (matchedTank) { const indiaTime = moment.tz( `${moment(matchedTank.date).format("YYYY-MM-DD")} ${matchedTank.time}`, "YYYY-MM-DD HH:mm:ss", "Asia/Kolkata" ); - + connectedLoraDate = indiaTime.format("DD-MM-YYYY"); connectedLoraTime = indiaTime.format("HH:mm:ss"); - - tankHeight = parseFloat(matchedTank.tankHeight) || 0; - + const diffMinutes = now.diff(indiaTime, "minutes"); - - // Connected if tankHeight > 0 and data not older than 5 mins + const tankHeight = parseFloat(matchedTank.tankHeight) || 0; + loraStatus = (tankHeight > 0 && diffMinutes <= 1) ? "connected" : "disconnected"; - + if (loraStatus === "disconnected") { loraLastDisconnect = `${connectedLoraDate} ${connectedLoraTime}`; } } } + + // Enrich with tank typeOfWater if exists const matchedTankDetails = await Tank.findOne({ customerId, - tankhardwareId: slave.tankhardwareId, + tankhardwareId: slave.tankhardwareId }).lean(); - + if (matchedTankDetails?.typeOfWater) { typeOfWater = matchedTankDetails.typeOfWater; } - // Update DB with new status and timestamps + + // Update slave Insensors record await Insensors.updateOne( { hardwareId: slave.hardwareId }, { @@ -2794,11 +2798,11 @@ exports.getMasterSlaveSummary = async (req, reply) => { connected_lora_date: connectedLoraDate, connected_lora_time: connectedLoraTime, lora_last_check_time: loraLastCheckTime, - lora_last_disconnect_time: loraLastDisconnect, - }, + lora_last_disconnect_time: loraLastDisconnect + } } ); - + connectedSlaves.push({ hardwareId: slave.hardwareId, tankhardwareId: slave.tankhardwareId || null, @@ -2811,12 +2815,10 @@ exports.getMasterSlaveSummary = async (req, reply) => { lora_last_disconnect_time: loraLastDisconnect, type: slave.type || "slave", typeOfWater, - connected_to: slave.connected_to || null, + connected_to: slave.connected_to || null }); } - - - + result.push({ hardwareId: master.hardwareId, masterName, @@ -2830,15 +2832,16 @@ exports.getMasterSlaveSummary = async (req, reply) => { gsm_last_check_time: gsmLastCheckTime, gsm_last_disconnect_time: gsmLastDisconnect, connected_lora_date: master.connected_lora_date || null, - connected_lora_time: master.connected_lora_time || null, + connected_lora_time: master.connected_lora_time || null }); } return reply.send({ status_code: 200, message: "Master-slave summary retrieved successfully", - data: result, + data: result }); + } catch (error) { console.error("Error in getMasterSlaveSummary:", error); return reply.status(500).send({ error: "Internal Server Error" }); @@ -2848,6 +2851,7 @@ exports.getMasterSlaveSummary = async (req, reply) => { + // 🔍 Helper to get tankHeight from latest IotData record async function getTankHeight(tankhardwareId) { const iotData = await IotData.findOne({ 'tanks.tankhardwareId': tankhardwareId })