diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index a03a08fe..73ab212a 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1457,6 +1457,7 @@ exports.mastrerList = async (req, reply) => { // } // }; + exports.getMasterSlaveSummary = async (req, reply) => { try { const { customerId } = req.params; @@ -1465,7 +1466,6 @@ exports.getMasterSlaveSummary = async (req, reply) => { return reply.status(400).send({ error: 'customerId is required' }); } - // Fetch all devices for the customer const allDevices = await Insensors.find({ customerId }).lean(); const masters = allDevices.filter(dev => dev.type === 'master'); @@ -1473,7 +1473,6 @@ exports.getMasterSlaveSummary = async (req, reply) => { const slaveMap = {}; - // Process each slave for (const slave of slaves) { const masterId = slave.connected_to; if (!slaveMap[masterId]) { @@ -1489,17 +1488,23 @@ exports.getMasterSlaveSummary = async (req, reply) => { // If disconnected, update LoRa last disconnect time let loraLastDisconnect = slave.lora_last_disconnect_time || null; if (slave.connected_status === 'disconnected') { - const date = slave.connected_lora_date || moment().tz('Asia/Kolkata').format('YYYY-MM-DD'); - const time = slave.connected_lora_time || moment().tz('Asia/Kolkata').format('HH:mm:ss'); - const combined = `${date} ${time}`; - - await Insensors.updateOne( - { hardwareId: slave.hardwareId }, - { $set: { lora_last_disconnect_time: combined } } - ); - loraLastDisconnect = combined; + // If connected_lora_date and connected_lora_time are available, combine them + const loraDate = slave.connected_lora_date; // e.g., "23-04-2025" + const loraTime = slave.connected_lora_time; // e.g., "15:38:07" + + if (loraDate && loraTime) { + // Combine to get formatted lora_last_disconnect_time + const formattedTime = `${loraDate} ${loraTime}`; // e.g., "23-04-2025 15:38:07" + + // Update the database with this formatted value + await Insensors.updateOne( + { hardwareId: slave.hardwareId }, + { $set: { lora_last_disconnect_time: formattedTime } } + ); + loraLastDisconnect = formattedTime; // Save the updated value to loraLastDisconnect + } } - + // Get tankHeight from IotData const tankHeight = await getTankHeight(slave.hardwareId); @@ -1522,23 +1527,30 @@ exports.getMasterSlaveSummary = async (req, reply) => { const response = []; - // Process each master for (const master of masters) { const connectedSlaves = slaveMap[master.hardwareId] || []; // If disconnected, update GSM last disconnect time let gsmLastDisconnect = master.gsm_last_disconnect_time || null; if (master.connected_status === 'disconnected') { - const date = master.connected_gsm_date || moment().tz('Asia/Kolkata').format('YYYY-MM-DD'); - const time = master.connected_gsm_time || moment().tz('Asia/Kolkata').format('HH:mm:ss'); - const combined = `${date} ${time}`; - - await Insensors.updateOne( - { hardwareId: master.hardwareId }, - { $set: { gsm_last_disconnect_time: combined } } - ); - gsmLastDisconnect = combined; + // If connected_gsm_date and connected_gsm_time are available, combine them + const gsmDate = master.connected_gsm_date; // e.g., "23-04-2025" + const gsmTime = master.connected_gsm_time; // e.g., "15:38:07" + + if (gsmDate && gsmTime) { + // Combine to get formatted gsm_last_disconnect_time + const formattedTime = `${gsmDate} ${gsmTime}`; // e.g., "23-04-2025 15:38:07" + + // Update the database with this formatted value + await Insensors.updateOne( + { hardwareId: master.hardwareId }, + { $set: { gsm_last_disconnect_time: formattedTime } } + ); + gsmLastDisconnect = formattedTime; // Save the updated value to gsmLastDisconnect + } } + + const enriched = { hardwareId: master.hardwareId, @@ -1590,7 +1602,6 @@ async function getTankHeight(hardwareId) { } - // exports.getIotDataByCustomer = async (req, reply) => { // try { // const { customerId } = req.params;