| 
						
						
							
								
							
						
						
					 | 
					 | 
					@ -1343,75 +1343,40 @@ exports.IotDevice = async (req, reply) => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    for (const tank of tanks) {
 | 
					 | 
					 | 
					 | 
					    for (const tank of tanks) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      const { tankhardwareId, tankHeight } = tank;
 | 
					 | 
					 | 
					 | 
					      const { tankhardwareId, tankHeight } = tank;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					     // console.log(tankhardwareId)
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      // Find the corresponding tank in tanksSchema
 | 
					 | 
					 | 
					 | 
					      // Find the corresponding tank in tanksSchema
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
 | 
					 | 
					 | 
					 | 
					      const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      const customerId = existingTank.customerId
 | 
					 | 
					 | 
					 | 
					      if (!existingTank) {
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					     // console.log(existingTank,tankhardwareId,customerId)
 | 
					 | 
					 | 
					 | 
					        continue; // Skip to the next tank if not found
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      const tank_name = existingTank.tankName
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      if (existingTank) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        // Update the waterlevel using the tankHeight value
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					       
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        // 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 customerId = existingTank.customerId;
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const formatted_tank_height1 = numberWithCommas(tank_height1);
 | 
					 | 
					 | 
					 | 
					      const tank_name = existingTank.tankName;
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					       
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      const water_level_height = tank_height - tankHeight
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10)
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      // Update the waterlevel using the tankHeight value
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      const tank_height = parseInt(tank_height1.toFixed(0), 10); // Ensure it's an integer
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      const water_level_height = tank_height - tankHeight;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      const water_level = water_level_height * waterCapacityPerCm;
 | 
					 | 
					 | 
					 | 
					      const water_level = water_level_height * waterCapacityPerCm;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					      existingTank.waterlevel = water_level;
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					// Function to add commas to a number
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					function numberWithCommas(x) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					}
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					const formatted_water_level = numberWithCommas(water_level);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      // Save the updated tank document
 | 
					 | 
					 | 
					 | 
					      // Save the updated tank document
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      await existingTank.save();
 | 
					 | 
					 | 
					 | 
					      await existingTank.save();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        for (const outputConnection of existingTank.connections.outputConnections) {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
 | 
					 | 
					 | 
					 | 
					      // Update linked tanks
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      for (const outputConnection of existingTank.connections.outputConnections) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        const linkedTank = await Tank.findOne({ customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        if (linkedTank) {
 | 
					 | 
					 | 
					 | 
					        if (linkedTank) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            console.log(linkedTank)
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					           // linkedTank.waterlevel = existingTank.waterlevel;
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            //await linkedTank.save();
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            // Update water level of tanks linked through input connections of the linked tank
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          for (const inputConnection of linkedTank.connections.inputConnections) {
 | 
					 | 
					 | 
					 | 
					          for (const inputConnection of linkedTank.connections.inputConnections) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            if (inputConnection.inputConnections === tank_name) {
 | 
					 | 
					 | 
					 | 
					            if (inputConnection.inputConnections === tank_name) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                inputConnection.water_level = parseInt(formatted_water_level.replace(/,/g, ''), 10)
 | 
					 | 
					 | 
					 | 
					              inputConnection.water_level = water_level;
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					              await linkedTank.save();
 | 
					 | 
					 | 
					 | 
					              await linkedTank.save();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            }
 | 
					 | 
					 | 
					 | 
					            }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          }
 | 
					 | 
					 | 
					 | 
					          }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        }
 | 
					 | 
					 | 
					 | 
					        }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      }
 | 
					 | 
					 | 
					 | 
					      }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    // Send the latest three documents
 | 
					 | 
					 | 
					 | 
					    // Send the latest three documents
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    const latestOttanks = await IotData.find({ hardwareId })
 | 
					 | 
					 | 
					 | 
					    const latestOttanks = await IotData.find({ hardwareId })
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -1430,6 +1395,7 @@ existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					// exports.IotDevice3 = async (req, reply) => { 
 | 
					 | 
					 | 
					 | 
					// exports.IotDevice3 = async (req, reply) => { 
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					//   try { 
 | 
					 | 
					 | 
					 | 
					//   try { 
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					//   const { hardwareId, mode, tanks } = req.body; 
 | 
					 | 
					 | 
					 | 
					//   const { hardwareId, mode, tanks } = req.body; 
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
					 | 
					
 
 |