diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index a51356d1..0825fa16 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -690,8 +690,8 @@ exports.getByHardwareIdSupport = async (req, reply) => { const gsmLastCheckTime = now.format("DD-MM-YYYY HH:mm:ss"); // formatted current time - // ✅ Step 1: Update Insensors with GSM date/time/status and last check time - await Insensors.findOneAndUpdate( + // ✅ Update slaves connected to this master hardwareId + await Insensors.updateMany( { connected_to: hardwareId }, { $set: { @@ -700,11 +700,23 @@ exports.getByHardwareIdSupport = async (req, reply) => { connected_status: gsmStatus, support_gsm_last_check_time: gsmLastCheckTime } - }, - { new: true } + } + ); + + // ✅ Update the master device itself (if it's a master) + await Insensors.updateOne( + { hardwareId }, + { + $set: { + connected_gsm_date, + connected_gsm_time, + connected_status: gsmStatus, + support_gsm_last_check_time: gsmLastCheckTime + } + } ); - // ✅ Step 2: Annotate tanks with LoRa connection status based on tank date + // ✅ Annotate tanks with LoRa connection status const tanksWithConnectionStatus = latestRecord.tanks.map(tank => { const tankMoment = moment.tz(tank.date, "Asia/Kolkata"); const tankDiff = now.diff(tankMoment, "minutes"); @@ -715,7 +727,7 @@ exports.getByHardwareIdSupport = async (req, reply) => { }; }); - // ✅ Step 3: Response + // ✅ Response return reply.send({ status_code: 200, message: "Success", @@ -733,10 +745,10 @@ exports.getByHardwareIdSupport = async (req, reply) => { }); } catch (err) { - console.error("Error in getByHardwareId:", err); + console.error("Error in getByHardwareIdSupport:", err); return reply.status(500).send({ error: "Internal Server Error" }); } -}; +}; exports.getByHardwareAndTankId = async (req, reply) => { @@ -879,7 +891,7 @@ exports.getByHardwareAndTankIdSupport = async (req, reply) => { let connected_lora_date = null; let connected_lora_time = null; - let support_lora_last_check_time = null; + let support_lora_last_check_time = moment.tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss"); if (isLoraConnected) { connected_lora_date = formattedDate; @@ -888,15 +900,24 @@ exports.getByHardwareAndTankIdSupport = async (req, reply) => { updateFields.connected_lora_time = connected_lora_time; } - // ✅ Format LoRa last check time in "YYYY-MM-DD HH:mm:ss" - support_lora_last_check_time = moment.tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm:ss"); updateFields.support_lora_last_check_time = support_lora_last_check_time; - await Insensors.findOneAndUpdate( - { connected_to: hardwareId, hardwareId: tankhardwareId }, - { $set: updateFields }, - { new: true } - ); + // 🔍 Add debugging to check if the document exists + const sensorToUpdate = await Insensors.findOne({ + connected_to: hardwareId, + hardwareId: tankhardwareId + }); + + if (!sensorToUpdate) { + console.warn("No matching Insensors document found for update"); + } else { + await Insensors.findOneAndUpdate( + { connected_to: hardwareId, hardwareId: tankhardwareId }, + { $set: { ...updateFields, support_lora_last_check_time } }, + { new: true } + ); + } + const displayMessage = isLoraConnected ? "LoRa connected" : "LoRa not connected"; @@ -907,10 +928,11 @@ exports.getByHardwareAndTankIdSupport = async (req, reply) => { lora_connected_status: updateFields.connected_status, connected_lora_date, connected_lora_time, - support_lora_last_check_time }); + support_lora_last_check_time + }); } catch (err) { - console.error("Error in getByHardwareAndTankId:", err); + console.error("Error in getByHardwareAndTankIdSupport:", err); return reply.status(500).send({ error: "Internal Server Error" }); } }; @@ -3673,7 +3695,7 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { } }); } - + console.log("order",orderMap) const masterMap = {}; for (const issue of allIssues) { @@ -3704,6 +3726,7 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { } const master = masterMap[masterSensor.hardwareId]; + console.log("master",master) // ✅ Always fetch slaves using connected_to const connectedSlaves = await Insensors.find({