| 
						
						
							
								
							
						
						
					 | 
					 | 
					@ -895,9 +895,6 @@ exports.motorAction = async (req, reply) => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      );
 | 
					 | 
					 | 
					 | 
					      );
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      
 | 
					 | 
					 | 
					 | 
					      
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      
 | 
					 | 
					 | 
					 | 
					      
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    } else {
 | 
					 | 
					 | 
					 | 
					    } else {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      // Update the motor stop status to "2" for start action
 | 
					 | 
					 | 
					 | 
					      // Update the motor stop status to "2" for start action
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -967,16 +964,13 @@ exports.motorAction = async (req, reply) => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const supplier_capacity =  parseInt(supplier_tank_info.capacity, 10);
 | 
					 | 
					 | 
					 | 
					        const supplier_capacity =  parseInt(supplier_tank_info.capacity, 10);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10); 
 | 
					 | 
					 | 
					 | 
					        const supplier_waterLevel = parseInt(supplier_tank_info.waterlevel, 10); 
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const capacity = parseInt(receiver_tank_info.capacity, 10);
 | 
					 | 
					 | 
					 | 
					        const capacity = parseInt(receiver_tank_info.capacity, 10);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const waterLevel = parseInt(receiver_tank_info.waterlevel, 10); 
 | 
					 | 
					 | 
					 | 
					        const waterLevel = parseInt(receiver_tank_info.waterlevel, 10); 
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const desired_percentage = parseInt(req.body.manual_threshold_litres, 10);
 | 
					 | 
					 | 
					 | 
					        const desired_percentage = parseInt(req.body.manual_threshold_litres, 10);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const threshold_water_level = waterLevel+desired_percentage;
 | 
					 | 
					 | 
					 | 
					        const threshold_water_level = waterLevel+desired_percentage;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        const supplier_threshold = supplier_waterLevel-desired_percentage
 | 
					 | 
					 | 
					 | 
					        const supplier_threshold = supplier_waterLevel-desired_percentage
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					        console.log(supplier_threshold,"supplier_threshold")
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        for await (const tank of Tank.find({  "connections.inputConnections.motor_id": motorId })) {
 | 
					 | 
					 | 
					 | 
					        for await (const tank of Tank.find({  "connections.inputConnections.motor_id": motorId })) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
 | 
					 | 
					 | 
					 | 
					          const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId);
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -2237,8 +2231,7 @@ exports.writeMotorStatus = async (req, reply) => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  }
 | 
					 | 
					 | 
					 | 
					  }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					};
 | 
					 | 
					 | 
					 | 
					};
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					// exports.writeMotorStatus = async (req, reply) => {
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					//   try {
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					//     const motor_id = req.body.motor_id;
 | 
					 | 
					 | 
					 | 
					//     const motor_id = req.body.motor_id;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					//     const status = req.body.status;
 | 
					 | 
					 | 
					 | 
					//     const status = req.body.status;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -2377,10 +2370,14 @@ exports.update_auto_percentage = async (req, reply) => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					//storing water level for every 15 minutes
 | 
					 | 
					 | 
					 | 
					//storing water level for every 15 minutes
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					const getFormattedISTTime = () => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  return moment().tz('Asia/Kolkata').format('DD-MM-YYYY hh:mm:ss A');
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					};
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					const storeWaterLevels = async () => {
 | 
					 | 
					 | 
					 | 
					const storeWaterLevels = async () => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  try {
 | 
					 | 
					 | 
					 | 
					  try {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    const tanks = await Tank.find({});
 | 
					 | 
					 | 
					 | 
					    const tanks = await Tank.find({});
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    const currentTime = new Date().toISOString();
 | 
					 | 
					 | 
					 | 
					    const currentTime = getFormattedISTTime();
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    
 | 
					 | 
					 | 
					 | 
					    
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    const waterLevelRecords = tanks.map(tank => ({
 | 
					 | 
					 | 
					 | 
					    const waterLevelRecords = tanks.map(tank => ({
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      customerId: tank.customerId,
 | 
					 | 
					 | 
					 | 
					      customerId: tank.customerId,
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -2397,4 +2394,21 @@ const storeWaterLevels = async () => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  }
 | 
					 | 
					 | 
					 | 
					  }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					};
 | 
					 | 
					 | 
					 | 
					};
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					setInterval(storeWaterLevels, 15 * 60 * 1000); // Run every 15 minutes
 | 
					 | 
					 | 
					 | 
					setInterval(storeWaterLevels, 15 * 60 * 1000); 
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					const updateWaterLevelAtMidnight = async () => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  try {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      await Tank.updateMany({}, { $set: { waterlevel_at_midnight: '$waterlevel' } });
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      console.log('Water levels updated at midnight');
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  } catch (error) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					      console.error('Error updating water levels:', error);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					};
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					// Schedule the job to run at midnight every day
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					cron.schedule('0 0 * * *', async () => {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  await updateWaterLevelAtMidnight();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					});
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					console.log('Cron job scheduled to update water levels at midnight');
 |