|  |  | @ -1037,79 +1037,6 @@ exports.mastrerList = async (req, reply) => { | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // exports.getMasterSlaveSummary = async (req, reply) => {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //   try {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     const { customerId } = req.params;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     if (!customerId) {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       return reply.status(400).send({ message: "Missing customerId" });
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     const allDevices = await Insensors.find({ customerId });
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     const masters = allDevices.filter(device => device.type === 'master');
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     const slaves = allDevices.filter(device => device.type === 'slave');
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     const enrichDeviceWithTimestamp = async (device) => {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       const hardwareId = device.hardwareId?.trim();
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       if (!hardwareId) return device.toObject();
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       const latestData = await IotData.findOne({ hardwareId })
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         .sort({ date: -1 })
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         .lean();
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       const enriched = device.toObject();
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       if (latestData?.date) {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         const indiaTime = moment.tz(latestData.date, "Asia/Kolkata");
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         const date = indiaTime.format("DD-MM-YYYY");
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         const time = indiaTime.format("HH:mm:ss");
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         if (device.type === 'master') {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //           enriched.connected_gsm_date = date;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //           enriched.connected_gsm_time = time;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         if (device.type === 'slave') {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //           enriched.connected_lora_date = date;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //           enriched.connected_lora_time = time;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       return enriched;
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     };
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     // Enrich all devices
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     const enrichedMasters = await Promise.all(masters.map(enrichDeviceWithTimestamp));
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     const enrichedSlaves = await Promise.all(slaves.map(enrichDeviceWithTimestamp));
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     // Match slaves to their masters
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     const masterSummary = enrichedMasters.map(master => {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       const connectedSlaves = enrichedSlaves.filter(slave => slave.connected_to === master.connected_to);
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       return {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         ...master,
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         connected_slave_count: connectedSlaves.length,
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //         connected_slaves: connectedSlaves
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       };
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     });
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     return reply.send({
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       status_code: 200,
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       message: "Success",
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       master_count: enrichedMasters.length,
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       slave_count: enrichedSlaves.length,
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       data: masterSummary
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     });
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //   } catch (err) {
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     console.error("Error in getMasterSlaveSummary:", err);
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     return reply.status(500).send({
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       status_code: 500,
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //       message: "Internal Server Error"
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //     });
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | //   }
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | // };
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | exports.getMasterSlaveSummary = async (req, reply) => { |  |  |  | exports.getMasterSlaveSummary = async (req, reply) => { | 
			
		
	
		
		
			
				
					
					|  |  |  |   try { |  |  |  |   try { | 
			
		
	
		
		
			
				
					
					|  |  |  |     const { customerId } = req.params; |  |  |  |     const { customerId } = req.params; | 
			
		
	
	
		
		
			
				
					|  |  | @ -1133,9 +1060,6 @@ exports.getMasterSlaveSummary = async (req, reply) => { | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |       const enriched = device.toObject(); |  |  |  |       const enriched = device.toObject(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |       // Hide hardwareId in final response (but still keep in internal processing)
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       delete enriched.hardwareId; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       if (latestData?.date) { |  |  |  |       if (latestData?.date) { | 
			
		
	
		
		
			
				
					
					|  |  |  |         const indiaTime = moment.tz(latestData.date, "Asia/Kolkata"); |  |  |  |         const indiaTime = moment.tz(latestData.date, "Asia/Kolkata"); | 
			
		
	
		
		
			
				
					
					|  |  |  |         const date = indiaTime.format("DD-MM-YYYY"); |  |  |  |         const date = indiaTime.format("DD-MM-YYYY"); | 
			
		
	
	
		
		
			
				
					|  |  | @ -1144,31 +1068,22 @@ exports.getMasterSlaveSummary = async (req, reply) => { | 
			
		
	
		
		
			
				
					
					|  |  |  |         if (device.type === 'master') { |  |  |  |         if (device.type === 'master') { | 
			
		
	
		
		
			
				
					
					|  |  |  |           enriched.connected_gsm_date = date; |  |  |  |           enriched.connected_gsm_date = date; | 
			
		
	
		
		
			
				
					
					|  |  |  |           enriched.connected_gsm_time = time; |  |  |  |           enriched.connected_gsm_time = time; | 
			
		
	
		
		
			
				
					
					|  |  |  |           enriched.gsm_connected_status = "connected"; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |         if (device.type === 'slave') { |  |  |  |         if (device.type === 'slave') { | 
			
		
	
		
		
			
				
					
					|  |  |  |           enriched.connected_lora_date = date; |  |  |  |           enriched.connected_lora_date = date; | 
			
		
	
		
		
			
				
					
					|  |  |  |           enriched.connected_lora_time = time; |  |  |  |           enriched.connected_lora_time = time; | 
			
		
	
		
		
			
				
					
					|  |  |  |           enriched.lora_connected_status = "connected"; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       } else { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         if (device.type === 'master') { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |           enriched.gsm_connected_status = "disconnected"; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         if (device.type === 'slave') { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |           enriched.lora_connected_status = "disconnected"; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |       } |  |  |  |       } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |       delete enriched.connected_status; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       return enriched; |  |  |  |       return enriched; | 
			
		
	
		
		
			
				
					
					|  |  |  |     }; |  |  |  |     }; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // Enrich all devices
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     const enrichedMasters = await Promise.all(masters.map(enrichDeviceWithTimestamp)); |  |  |  |     const enrichedMasters = await Promise.all(masters.map(enrichDeviceWithTimestamp)); | 
			
		
	
		
		
			
				
					
					|  |  |  |     const enrichedSlaves = await Promise.all(slaves.map(enrichDeviceWithTimestamp)); |  |  |  |     const enrichedSlaves = await Promise.all(slaves.map(enrichDeviceWithTimestamp)); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // Match slaves to their masters
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     const masterSummary = enrichedMasters.map(master => { |  |  |  |     const masterSummary = enrichedMasters.map(master => { | 
			
		
	
		
		
			
				
					
					|  |  |  |       const connectedSlaves = enrichedSlaves.filter(slave => slave.connected_to === master.connected_to); |  |  |  |       const connectedSlaves = enrichedSlaves.filter(slave => slave.connected_to === master.connected_to); | 
			
		
	
		
		
			
				
					
					|  |  |  |       return { |  |  |  |       return { | 
			
		
	
	
		
		
			
				
					|  |  | 
 |