diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 651d7edd..8357711d 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1007,10 +1007,16 @@ exports.getMasterSlaveSummary = async (req, reply) => { // Update connection status and timestamps const updateStatusPromises = allDevices.map(async device => { - const hwId = device.connected_to; - if (!hwId) return; + let checkHardwareId = device.hardwareId?.trim(); + if (!checkHardwareId) return; - const latestData = await IotData.findOne({ hardwareId: hwId }) + // For slave, check connection via master's hardwareId (i.e., connected_to) + if (device.type === 'slave') { + checkHardwareId = device.connected_to?.trim(); + if (!checkHardwareId) return; + } + + const latestData = await IotData.findOne({ hardwareId: checkHardwareId }) .sort({ date: -1 }) .lean(); @@ -1024,18 +1030,18 @@ exports.getMasterSlaveSummary = async (req, reply) => { if (diff <= 10) { status = "connected"; - + const indiaTime = moment.tz(latestData.date, "Asia/Kolkata"); const date = indiaTime.format("YYYY-MM-DD"); const time = indiaTime.format("HH:mm:ss"); - + if (device.type === 'master') { updateFields.connected_gsm_date = date; updateFields.connected_gsm_time = time; device.connected_gsm_date = date; device.connected_gsm_time = time; } - + if (device.type === 'slave') { updateFields.connected_lora_date = date; updateFields.connected_lora_time = time; @@ -1043,7 +1049,6 @@ exports.getMasterSlaveSummary = async (req, reply) => { device.connected_lora_time = time; } } - } updateFields.connected_status = status;