|  |  |  | @ -4776,7 +4776,7 @@ async function getTankHeight(tankhardwareId) { | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | exports.getIotDataByCustomer = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |   try { | 
			
		
	
		
			
				
					|  |  |  |  |     const { customerId } = req.params; | 
			
		
	
		
			
				
					|  |  |  |  |     const { customerId, hardwareId } = req.params; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (!customerId) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ error: "customerId is required" }); | 
			
		
	
	
		
			
				
					|  |  |  | @ -4789,11 +4789,21 @@ exports.getIotDataByCustomer = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(404).send({ message: "No sensors found for this customer." }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // ✅ Get only master hardware IDs from Insensors directly
 | 
			
		
	
		
			
				
					|  |  |  |  |     const masterSensors = sensors.filter(s => s.type === 'master'); | 
			
		
	
		
			
				
					|  |  |  |  |     // ✅ Filter master sensors
 | 
			
		
	
		
			
				
					|  |  |  |  |     let masterSensors = sensors.filter(s => s.type === 'master'); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // ✅ If hardwareId is provided, filter to that master only
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (hardwareId) { | 
			
		
	
		
			
				
					|  |  |  |  |       masterSensors = masterSensors.filter(m => m.hardwareId?.trim() === hardwareId.trim()); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (!masterSensors.length) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(404).send({ message: "No master found for the given hardwareId or customer." }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     const masterHardwareIds = masterSensors.map(m => m.hardwareId?.trim()); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // ✅ Map for masterName/location from Order
 | 
			
		
	
		
			
				
					|  |  |  |  |     // ✅ Build map of masterName/location from Order
 | 
			
		
	
		
			
				
					|  |  |  |  |     const orders = await Order.find({ customerId }).lean(); | 
			
		
	
		
			
				
					|  |  |  |  |     const orderMap = {}; | 
			
		
	
		
			
				
					|  |  |  |  |     orders.forEach(order => { | 
			
		
	
	
		
			
				
					|  |  |  | @ -4805,7 +4815,7 @@ exports.getIotDataByCustomer = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |       }); | 
			
		
	
		
			
				
					|  |  |  |  |     }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // ✅ Prepare final enriched master data
 | 
			
		
	
		
			
				
					|  |  |  |  |     // ✅ Enrich each master with latest IoT data
 | 
			
		
	
		
			
				
					|  |  |  |  |     const enrichedMasters = await Promise.all(masterHardwareIds.map(async (hardwareId) => { | 
			
		
	
		
			
				
					|  |  |  |  |       const latestRecord = await IotData.findOne({ hardwareId }).sort({ date: -1 }).lean(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -4821,23 +4831,22 @@ exports.getIotDataByCustomer = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |         }; | 
			
		
	
		
			
				
					|  |  |  |  |       } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       // ✅ Check GSM status
 | 
			
		
	
		
			
				
					|  |  |  |  |       // ✅ GSM connection status
 | 
			
		
	
		
			
				
					|  |  |  |  |       const indiaTime = moment.tz(latestRecord.date, "Asia/Kolkata"); | 
			
		
	
		
			
				
					|  |  |  |  |       const now = moment.tz("Asia/Kolkata"); | 
			
		
	
		
			
				
					|  |  |  |  |       const diffInMinutes = now.diff(indiaTime, "minutes"); | 
			
		
	
		
			
				
					|  |  |  |  |       const gsmConnected = diffInMinutes <= 1; | 
			
		
	
		
			
				
					|  |  |  |  |       const message = gsmConnected ? "GSM is connected" : "GSM is not connected"; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       // ✅ Get slaves connected to this master
 | 
			
		
	
		
			
				
					|  |  |  |  |       // ✅ Find slaves connected to this master
 | 
			
		
	
		
			
				
					|  |  |  |  |       const connectedSlaves = sensors.filter(sensor => sensor.connected_to?.trim() === hardwareId); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       // ✅ Prepare tank info
 | 
			
		
	
		
			
				
					|  |  |  |  |       // ✅ Enrich each slave/tank
 | 
			
		
	
		
			
				
					|  |  |  |  |       const tanks = connectedSlaves.map(slave => { | 
			
		
	
		
			
				
					|  |  |  |  |         const slaveId = slave.tankhardwareId?.trim(); | 
			
		
	
		
			
				
					|  |  |  |  |         const matchedTank = latestRecord.tanks?.find(t => t.tankhardwareId === slaveId); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         let loraMessage = "LORA is not connected"; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         if (matchedTank?.date && matchedTank.tankHeight !== "0") { | 
			
		
	
		
			
				
					|  |  |  |  |           const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); | 
			
		
	
		
			
				
					|  |  |  |  |           const loraDiff = now.diff(tankTime, "minutes"); | 
			
		
	
	
		
			
				
					|  |  |  | @ -4878,6 +4887,7 @@ exports.getIotDataByCustomer = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | exports.getIotDataByCustomerAndHardwareId = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |   try { | 
			
		
	
		
			
				
					|  |  |  |  |     const { customerId, hardwareId } = req.params; | 
			
		
	
	
		
			
				
					|  |  |  | 
 |