|  |  | @ -1180,10 +1180,12 @@ exports.IotDevice = async (req, reply) => { | 
			
		
	
		
		
			
				
					
					|  |  |  |   try { |  |  |  |   try { | 
			
		
	
		
		
			
				
					
					|  |  |  |     const { hardwareId, mode, tanks } = req.body; |  |  |  |     const { hardwareId, mode, tanks } = req.body; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // create a new tank document with the current date and time
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     const currentDate = new Date(); |  |  |  |     const currentDate = new Date(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     const date = currentDate.toISOString(); |  |  |  |     const date = currentDate.toISOString(); // save the date as an ISO string
 | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); |  |  |  |     const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // Create an array of tank documents
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     const tankDocuments = tanks.map(tank => ({ |  |  |  |     const tankDocuments = tanks.map(tank => ({ | 
			
		
	
		
		
			
				
					
					|  |  |  |       tankhardwareId: tank.tankhardwareId, |  |  |  |       tankhardwareId: tank.tankhardwareId, | 
			
		
	
		
		
			
				
					
					|  |  |  |       tankHeight: tank.tankHeight, |  |  |  |       tankHeight: tank.tankHeight, | 
			
		
	
	
		
		
			
				
					|  |  | @ -1193,9 +1195,13 @@ exports.IotDevice = async (req, reply) => { | 
			
		
	
		
		
			
				
					
					|  |  |  |       time: time |  |  |  |       time: time | 
			
		
	
		
		
			
				
					
					|  |  |  |     })); |  |  |  |     })); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // create a new IotData document with the provided data
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time }); |  |  |  |     const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time }); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // save the document to MongoDB
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     await ottank.save(); |  |  |  |     await ottank.save(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // Delete excess records (keep only the latest three records)
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     const recordsToKeep = 3; |  |  |  |     const recordsToKeep = 3; | 
			
		
	
		
		
			
				
					
					|  |  |  |     const recordsToDelete = await IotData.find({ hardwareId }) |  |  |  |     const recordsToDelete = await IotData.find({ hardwareId }) | 
			
		
	
		
		
			
				
					
					|  |  |  |       .sort({ date: -1, time: -1 }) |  |  |  |       .sort({ date: -1, time: -1 }) | 
			
		
	
	
		
		
			
				
					|  |  | @ -1205,44 +1211,70 @@ exports.IotDevice = async (req, reply) => { | 
			
		
	
		
		
			
				
					
					|  |  |  |       await record.remove(); |  |  |  |       await record.remove(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // Update waterlevel in tanksSchema for each tank
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     for (const tank of tanks) { |  |  |  |     for (const tank of tanks) { | 
			
		
	
		
		
			
				
					
					|  |  |  |       const { tankhardwareId, tankHeight } = tank; |  |  |  |       const { tankhardwareId, tankHeight } = tank; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       // Find the corresponding tank in tanksSchema
 | 
			
		
	
		
		
			
				
					
					|  |  |  |       const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); |  |  |  |       const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); | 
			
		
	
		
		
			
				
					
					|  |  |  |       if (!existingTank) continue; |  |  |  |        | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |      | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       if (existingTank) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // Update the waterlevel using the tankHeight value
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         console.log(tank_height1, 25); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // The value of tank_height1 is a number, not a string, so you cannot use replace on it.
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // If you want to format it with commas, you can create a function to add commas to a number.
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         function numberWithCommas(x) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |           return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         // Now you can use the function to format the tank_height1 value with commas.
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         const formatted_tank_height1 = numberWithCommas(tank_height1); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         console.log(formatted_tank_height1, 25); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |          | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         console.log(tank_height); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |      // console.log(tank_height,1)
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       const water_level_height = tank_height - tankHeight | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       console.log(water_level_height,2) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |        | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       console.log(waterCapacityPerCm,3) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       const water_level = water_level_height * waterCapacityPerCm; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | console.log(water_level, 4); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | // Function to add commas to a number
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | function numberWithCommas(x) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |       const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10); |  |  |  | const formatted_water_level = numberWithCommas(water_level); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       const water_level = (parseInt(existingTank.height.replace(/,/g, ''), 10) * 30.48) - tankHeight * waterCapacityPerCm; |  |  |  | console.log(formatted_water_level, 4); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       existingTank.waterlevel = water_level; |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |       await existingTank.save(); |  |  |  | existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | console.log(existingTank.waterlevel); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |       // Now, find and update the water level of tanks connected via outputConnections
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       if (existingTank.connections && existingTank.connections.outputConnections) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         for (const output of existingTank.connections.outputConnections) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |           const connectedTank = await Tank.findOne({ tankName: output.outputConnections, customerId: output.customerId }); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |           if (connectedTank) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             // Check for specific input connection for "Tank A" and update it
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             const inputConnectionIndex = connectedTank.connections.inputConnections.findIndex(input => input.inputConnections === existingTank.tankName); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |             if (inputConnectionIndex > -1) { |  |  |  |         // Save the updated tank document
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |               connectedTank.connections.inputConnections[inputConnectionIndex].water_level = water_level.toString(); |  |  |  |         await existingTank.save(); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |               await connectedTank.save(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |           } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |       } |  |  |  |       } | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // Send the latest three documents
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     const latestOttanks = await IotData.find({ hardwareId }) |  |  |  |     const latestOttanks = await IotData.find({ hardwareId }) | 
			
		
	
		
		
			
				
					
					|  |  |  |       .sort({ date: -1, time: -1 }); |  |  |  |       .sort({ date: -1, time: -1 }); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     reply.code(200).send({ latestOttanks }); |  |  |  |     reply.code(200).send({ latestOttanks }); | 
			
		
	
		
		
			
				
					
					|  |  |  |   } catch (err) { |  |  |  |   } catch (err) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     // send an error response
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     reply.code(500).send({ error: err.message }); |  |  |  |     reply.code(500).send({ error: err.message }); | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | }; |  |  |  | }; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // exports.getIotD = async(req, reply) => {
 |  |  |  | // exports.getIotD = async(req, reply) => {
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //   try {
 |  |  |  | //   try {
 | 
			
		
	
		
		
			
				
					
					|  |  |  | //     await IotData.find({hardwareId: req.query.hardwareId})
 |  |  |  | //     await IotData.find({hardwareId: req.query.hardwareId})
 | 
			
		
	
	
		
		
			
				
					|  |  | 
 |