|  |  |  | @ -952,21 +952,33 @@ exports.motorAction = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |       } else if (req.body.threshold_type === "litres") { | 
			
		
	
		
			
				
					|  |  |  |  |         // If threshold type is percentage, calculate percentage threshold
 | 
			
		
	
		
			
				
					|  |  |  |  |         const receiver_tank_info = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); | 
			
		
	
		
			
				
					|  |  |  |  |         const supplier_tank_info = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); | 
			
		
	
		
			
				
					|  |  |  |  |         if (!receiver_tank_info) { | 
			
		
	
		
			
				
					|  |  |  |  |           throw new Error("Receiver tank not found."); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         if (!supplier_tank_info) { | 
			
		
	
		
			
				
					|  |  |  |  |           throw new Error("Supplierr tank not found."); | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         const supplier_capacity =  parseInt(supplier_tank_info.capacity, 10); | 
			
		
	
		
			
				
					|  |  |  |  |         const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10);  | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         const capacity = parseInt(receiver_tank_info.capacity, 10); | 
			
		
	
		
			
				
					|  |  |  |  |         const waterLevel = parseInt(receiver_tank_info.waterlevel, 10);  | 
			
		
	
		
			
				
					|  |  |  |  |         const desired_percentage = parseInt(req.body.manual_threshold_litres, 10); | 
			
		
	
		
			
				
					|  |  |  |  |         const threshold_water_level = waterLevel+desired_percentage; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         const supplier_threshold = supplier_waterLevel-desired_percentage | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |         for await (const tank of Tank.find({  "connections.inputConnections.motor_id": motorId })) { | 
			
		
	
		
			
				
					|  |  |  |  |           const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); | 
			
		
	
		
			
				
					|  |  |  |  |           if (index !== -1) { | 
			
		
	
		
			
				
					|  |  |  |  |             await Tank.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |               { customerId, "connections.inputConnections.motor_id": motorId }, | 
			
		
	
		
			
				
					|  |  |  |  |               { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: threshold_water_level.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } | 
			
		
	
		
			
				
					|  |  |  |  |               { $set: { [`connections.inputConnections.${index}.manual_threshold_percentage`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } } | 
			
		
	
		
			
				
					|  |  |  |  |             ); | 
			
		
	
		
			
				
					|  |  |  |  |           } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
	
		
			
				
					|  |  |  | @ -979,8 +991,8 @@ exports.motorAction = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |         // Start monitoring water level based on threshold percentage
 | 
			
		
	
		
			
				
					|  |  |  |  |         const intervalId = setInterval(async () => { | 
			
		
	
		
			
				
					|  |  |  |  |           // Check if water level has reached the threshold percentage
 | 
			
		
	
		
			
				
					|  |  |  |  |           const current_water_level = parseInt(receiver_tank_info.water_level, 10); | 
			
		
	
		
			
				
					|  |  |  |  |           if (current_water_level >= threshold_water_level) { | 
			
		
	
		
			
				
					|  |  |  |  |           const current_water_level = parseInt(supplier_tank_info.water_level, 10); | 
			
		
	
		
			
				
					|  |  |  |  |           if (current_water_level <= supplier_threshold) { | 
			
		
	
		
			
				
					|  |  |  |  |             // Stop the motor pump
 | 
			
		
	
		
			
				
					|  |  |  |  |             await Tank.updateOne( | 
			
		
	
		
			
				
					|  |  |  |  |               { customerId, "connections.inputConnections.motor_id": motorId }, | 
			
		
	
	
		
			
				
					|  |  |  | 
 |