@ -88,11 +88,10 @@ async function deleteOldRecords() {
 
			
		
	
		
			
				
					exports . addTanks  =  async  ( req ,  reply )  =>  {  
			
		
	
		
			
				
					  try  { 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    const  InstallerId =  req . params . Install  erId; 
 
			
		
	
		
			
				
					    const  {  customerId,  hardwareId,  tankhardwareId , tankName , tankLocation  }  =  req . body ; 
 
			
		
	
		
			
				
					    const  customerId =  req . params . custom  erId; 
 
			
		
	
		
			
				
					    const  {  ,  tankhardwareId , tankName , tankLocation  }  =  req . body ; 
 
			
		
	
		
			
				
					    
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    // Check if the combination of hardwareId and tankhardwareId already exists
 
 
			
		
	
		
			
				
					    const  existingTank  =  await  Tank . findOne ( { 
 
			
		
	
		
			
				
					      customerId :  customerId , 
 
			
		
	
		
			
				
					      hardwareId :  hardwareId , 
 
			
		
	
	
		
			
				
					
						
						
						
							
								 
						
					 
				
				@ -106,7 +105,7 @@ exports.addTanks = async (req, reply) => {
 
			
		
	
		
			
				
					    } 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    const  tankData  =  { 
 
			
		
	
		
			
				
					      
 
			
		
	
		
			
				
					     // 
 
 
			
		
	
		
			
				
					      customerId :  customerId , 
 
			
		
	
		
			
				
					      hardwareId :  hardwareId , 
 
			
		
	
		
			
				
					      tankhardwareId :  tankhardwareId , 
 
			
		
	
	
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
				
				@ -942,15 +941,17 @@ let supplier_tanks = [];
 
			
		
	
		
			
				
					exports . consumption  =  async  ( request ,  reply )  =>  {  
			
		
	
		
			
				
					  try  { 
 
			
		
	
		
			
				
					    const  {  customerId  }  =  request . params ; 
 
			
		
	
		
			
				
					    const  {  startDate ,  stopDate ,  block ,  typeofwater  }  =  request . body ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    const  {  startDate ,  stopDate ,  block  }  =  req . body ; 
 
			
		
	
		
			
				
					    let  {  typeofwater  }  =  req . body ; 
 
			
		
	
		
			
				
					    // Convert typeofwater to lowercase
 
 
			
		
	
		
			
				
					    typeofwater  =  typeofwater . toLowerCase ( ) ; 
 
			
		
	
		
			
				
					    const  start  =  startDate ; 
 
			
		
	
		
			
				
					    const  end  =  stopDate ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    // Construct the query object based on block and typeofwater inputs
 
 
			
		
	
		
			
				
					    const  tankQuery  =  {  customerId ,  tankLocation :  "overhead"  } ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    if  ( block  !==  " a ll")  { 
 
			
		
	
		
			
				
					    if  ( block  !==  " A ll")  { 
 
			
		
	
		
			
				
					      tankQuery . blockName  =  block ;  // Filter by specific block if not "all"
 
 
			
		
	
		
			
				
					    } 
 
			
		
	
		
			
				
					
 
			
		
	
	
		
			
				
					
						
						
						
							
								 
						
					 
				
				@ -975,7 +976,7 @@ exports.consumption = async (request, reply) => {
 
			
		
	
		
			
				
					          $gte :  start , 
 
			
		
	
		
			
				
					          $lte :  end 
 
			
		
	
		
			
				
					        } , 
 
			
		
	
		
			
				
					        ... ( block  !==  " a ll" &&  {  block :  tank . blockName  } ) ,  // Ensure correct field names
 
 
			
		
	
		
			
				
					        ... ( block  !==  " A ll" &&  {  block :  tank . blockName  } ) ,  // Ensure correct field names
 
 
			
		
	
		
			
				
					        ... ( typeofwater  !==  "all"  &&  {  typeofwater :  tank . typeOfWater  } )  // Ensure correct field names
 
 
			
		
	
		
			
				
					      } ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
	
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
				
				@ -1307,13 +1308,39 @@ admin.initializeApp({
 
			
		
	
		
			
				
					  credential :  admin . credential . cert ( serviceAccount ) , 
 
			
		
	
		
			
				
					} ) ;  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					const  sendPushNotification  =  async  ( registrationToken ,  title ,  body )  =>  {  
			
		
	
		
			
				
					  const  message  =  { 
 
			
		
	
		
			
				
					    notification :  { 
 
			
		
	
		
			
				
					      title :  title , 
 
			
		
	
		
			
				
					      body :  body , 
 
			
		
	
		
			
				
					    } , 
 
			
		
	
		
			
				
					    data :  { 
 
			
		
	
		
			
				
					      title :  title , 
 
			
		
	
		
			
				
					      body :  body , 
 
			
		
	
		
			
				
					    } , 
 
			
		
	
		
			
				
					  } ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					  const  options  =  { 
 
			
		
	
		
			
				
					    priority :  "high" , 
 
			
		
	
		
			
				
					    timeToLive :  60  *  60  *  24 ,  
 
			
		
	
		
			
				
					  } ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					  try  { 
 
			
		
	
		
			
				
					    const  response  =  await  admin . messaging ( ) . sendToDevice ( registrationToken ,  message ,  options ) ; 
 
			
		
	
		
			
				
					    console . log ( 'FCM response:' ,  response ) ;  // Log the FCM response
 
 
			
		
	
		
			
				
					    return  response ;  // Return the FCM response object
 
 
			
		
	
		
			
				
					  }  catch  ( error )  { 
 
			
		
	
		
			
				
					    console . error ( 'FCM error:' ,  error ) ; 
 
			
		
	
		
			
				
					    throw  error ;  // Throw the error to handle it further up the call stack
 
 
			
		
	
		
			
				
					  } 
 
			
		
	
		
			
				
					} ;  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					exports . motorAction  =  async  ( req ,  reply )  =>  {  
			
		
	
		
			
				
					  try  { 
 
			
		
	
		
			
				
					    const  customerId  =  req . params . customerId ; 
 
			
		
	
		
			
				
					    const  action  =  req . body . action ; 
 
			
		
	
		
			
				
					    const  motorId  =  req . body . motor _id ; 
 
			
		
	
		
			
				
					    const  start _instance _id  =  req . body . start _instance _id ; 
 
			
		
	
		
			
				
					    //const fcmIds = req.body.fcmIds; // Assume this is provided in the request to notify users
 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    // Ensure motor_id is provided
 
 
			
		
	
		
			
				
					    if  ( ! motorId )  { 
 
			
		
	
	
		
			
				
					
						
						
						
							
								 
						
					 
				
				@ -1329,6 +1356,7 @@ exports.motorAction = async (req, reply) => {
 
			
		
	
		
			
				
					    }  else  { 
 
			
		
	
		
			
				
					      throw  new  Error ( "Invalid action provided." ) ; 
 
			
		
	
		
			
				
					    } 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    const  users  =  await  User . find ( {  customerId :  customerId  } ) ; 
 
			
		
	
		
			
				
					    const  fcmIds  =  users . map ( user  =>  user . fcmId ) . filter ( fcmId  =>  fcmId ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
	
		
			
				
					
						
						
						
							
								 
						
					 
				
				@ -1337,8 +1365,8 @@ exports.motorAction = async (req, reply) => {
 
			
		
	
		
			
				
					      // Update the motor stop status and other fields
 
 
			
		
	
		
			
				
					      await  Tank . updateOne ( 
 
			
		
	
		
			
				
					        {  customerId ,  "connections.inputConnections.motor_id" :  motorId  } , 
 
			
		
	
		
			
				
					        {   
 
			
		
	
		
			
				
					          $set :  {   
 
			
		
	
		
			
				
					        { 
 
			
		
	
		
			
				
					          $set :  { 
 
			
		
	
		
			
				
					            "connections.inputConnections.$.motor_stop_status" :  "1" , 
 
			
		
	
		
			
				
					            "connections.inputConnections.$.stopTime" :  req . body . stopTime , 
 
			
		
	
		
			
				
					            "connections.inputConnections.$.threshold_type" :  null , 
 
			
		
	
	
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
				
				@ -1385,8 +1413,8 @@ exports.motorAction = async (req, reply) => {
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					          await  MotorData . updateOne ( 
 
			
		
	
		
			
				
					            {  customerId ,  motor _id :  motorId ,  start _instance _id :  start _instance _id  } , 
 
			
		
	
		
			
				
					            {   
 
			
		
	
		
			
				
					              $set :  {   
 
			
		
	
		
			
				
					            { 
 
			
		
	
		
			
				
					              $set :  { 
 
			
		
	
		
			
				
					                stopTime :  req . body . stopTime , 
 
			
		
	
		
			
				
					                receiverfinalwaterlevel :  receiverFinalWaterLevel . toString ( ) , 
 
			
		
	
		
			
				
					                quantity _delivered :  quantityDelivered . toString ( ) 
 
			
		
	
	
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
				
				@ -1504,120 +1532,395 @@ exports.motorAction = async (req, reply) => {
 
			
		
	
		
			
				
					                {  customerId ,  motor _id :  motorId ,  start _instance _id :  start _instance _id  } , 
 
			
		
	
		
			
				
					                { 
 
			
		
	
		
			
				
					                  $set :  { 
 
			
		
	
		
			
				
					                    stopTime :  req. body . stopTime  , 
 
			
		
	
		
			
				
					                    stopTime :  formatDate( new  Date ( ) )  , 
 
			
		
	
		
			
				
					                    receiverfinalwaterlevel :  receiverFinalWaterLevel . toString ( ) , 
 
			
		
	
		
			
				
					                    quantity _delivered :  quantityDelivered . toString ( ) 
 
			
		
	
		
			
				
					                  } 
 
			
		
	
		
			
				
					                } 
 
			
		
	
		
			
				
					              ) ; 
 
			
		
	
		
			
				
					            } 
 
			
		
	
		
			
				
					          } 
 
			
		
	
		
			
				
					        } ,  60000 ) ;  // Check every minute
 
 
			
		
	
		
			
				
					      }   else  if  ( req . body . threshold _type  ===  "litres" )  { 
 
			
		
	
		
			
				
					        console . log ( "entered litres" ) 
 
			
		
	
		
			
				
					        const  receiver _tank _info7  =  await  Tank . findOne ( {  customerId ,  tankName :  req . body . to ,  tankLocation :  req . body . to _type . toLowerCase ( )  } ) ; 
 
			
		
	
		
			
				
					          }  else  { 
 
			
		
	
		
			
				
					            // Monitor the receiver tank's water level and send notification if max capacity is reached
 
 
			
		
	
		
			
				
					            const  receiverTank  =  await  Tank . findOne ( {  customerId ,  tankName :  req . body . to ,  tankLocation :  req . body . to _type . toLowerCase ( )  } ) ; 
 
			
		
	
		
			
				
					            const  currentWaterLevel  =  parseInt ( receiverTank . waterlevel ,  10 ) ; 
 
			
		
	
		
			
				
					            const  receiverTankCapacity  =  parseInt ( receiverTank . capacity ,  10 ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					            if  ( currentWaterLevel  >=  receiverTankCapacity )  { 
 
			
		
	
		
			
				
					              // Send a notification for maximum water level reached
 
 
			
		
	
		
			
				
					              for  ( const  fcmId  of  fcmIds )  { 
 
			
		
	
		
			
				
					                try  { 
 
			
		
	
		
			
				
					                  const  response  =  await  sendPushNotification ( fcmId ,  'Maximum Water Level Alert' ,  ` The receiver tank has reached its maximum capacity. ` ) ; 
 
			
		
	
		
			
				
					                  console . log ( 'Notification sent successfully:' ,  response ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					                  if  ( response . results [ 0 ] . error  ===  'NotRegistered' )  { 
 
			
		
	
		
			
				
					                    await  User . updateOne ( {  fcmId :  fcmId  } ,  {  $unset :  {  fcmId :  ""  }  } ) ; 
 
			
		
	
		
			
				
					                    console . log ( ` Removed unregistered FCM ID:  ${ fcmId } ` ) ; 
 
			
		
	
		
			
				
					                  } 
 
			
		
	
		
			
				
					                }  catch  ( error )  { 
 
			
		
	
		
			
				
					                  console . error ( 'Error sending notification:' ,  error ) ; 
 
			
		
	
		
			
				
					                } 
 
			
		
	
		
			
				
					              } 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					        const  newMotorData  =  new  MotorData ( { 
 
			
		
	
		
			
				
					          customerId :  customerId , 
 
			
		
	
		
			
				
					          motor _id :  motorId , 
 
			
		
	
		
			
				
					          start _instance _id :  start _instance _id , 
 
			
		
	
		
			
				
					          supplierTank :  req . body . from , 
 
			
		
	
		
			
				
					          receiverTank :  req . body . to , 
 
			
		
	
		
			
				
					          supplier _type :  req . body . from _type , 
 
			
		
	
		
			
				
					          receiver _type :  req . body . to _type , 
 
			
		
	
		
			
				
					          startTime :  req . body . startTime , 
 
			
		
	
		
			
				
					          receiverInitialwaterlevel : parseInt ( receiver _tank _info7 . waterlevel ,  10 ) 
 
			
		
	
		
			
				
					        } ) ; 
 
			
		
	
		
			
				
					        await  newMotorData . save ( ) ; 
 
			
		
	
		
			
				
					        // 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 ) ;  
 
			
		
	
		
			
				
					              // Stop the motor
 
 
			
		
	
		
			
				
					              await  Tank . updateOne ( 
 
			
		
	
		
			
				
					                {  customerId ,  "connections.inputConnections.motor_id" :  motorId  } , 
 
			
		
	
		
			
				
					                { 
 
			
		
	
		
			
				
					                  $set :  { 
 
			
		
	
		
			
				
					                    "connections.inputConnections.$.motor_stop_status" :  "1" , 
 
			
		
	
		
			
				
					                    "connections.inputConnections.$.threshold_type" :  null , 
 
			
		
	
		
			
				
					                    "connections.inputConnections.$.manual_threshold_time" :  null , 
 
			
		
	
		
			
				
					                    "connections.inputConnections.$.manual_threshold_percentage" :  null 
 
			
		
	
		
			
				
					                  } 
 
			
		
	
		
			
				
					                } 
 
			
		
	
		
			
				
					              ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					        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 . replace ( /,/g ,  '' ) ,  10 ) ; 
 
			
		
	
		
			
				
					              clearInterval ( intervalId ) ;  // Stop monitoring
 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					        console . log ( desired _percentage ) 
 
			
		
	
		
			
				
					        const  threshold _water _level  =  waterLevel + desired _percentage ; 
 
			
		
	
		
			
				
					              await  delay ( 300000 ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					        const  supplier _threshold  =  supplier _waterLevel - desired _percentage 
 
			
		
	
		
			
				
					        console . log ( supplier _threshold , "supplier_threshold" ) 
 
			
		
	
		
			
				
					              const  motorData  =  await  MotorData . findOne ( {  customerId ,  motor _id :  motorId ,  start _instance _id :  start _instance _id  } ) ; 
 
			
		
	
		
			
				
					              if  ( motorData )  { 
 
			
		
	
		
			
				
					                const  receiverFinalWaterLevel  =  currentWaterLevel ; 
 
			
		
	
		
			
				
					                const  quantityDelivered  =  receiverFinalWaterLevel  -  parseInt ( motorData . receiverInitialwaterlevel ,  10 ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					        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 ` ] :  supplier _threshold . toString ( ) ,  [ ` connections.inputConnections. ${ index } .startTime ` ] :  req . body . startTime  }  } 
 
			
		
	
		
			
				
					            ) ; 
 
			
		
	
		
			
				
					                const  stopTime  =  formatDate ( new  Date ( ) ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					                await  MotorData . updateOne ( 
 
			
		
	
		
			
				
					                  {  customerId ,  motor _id :  motorId ,  start _instance _id :  start _instance _id  } , 
 
			
		
	
		
			
				
					                  { 
 
			
		
	
		
			
				
					                    $set :  { 
 
			
		
	
		
			
				
					                      stopTime :  stopTime , 
 
			
		
	
		
			
				
					                      receiverfinalwaterlevel :  receiverFinalWaterLevel . toString ( ) , 
 
			
		
	
		
			
				
					                      quantity _delivered :  quantityDelivered . toString ( ) 
 
			
		
	
		
			
				
					                    } 
 
			
		
	
		
			
				
					                  } 
 
			
		
	
		
			
				
					                ) ; 
 
			
		
	
		
			
				
					              } 
 
			
		
	
		
			
				
					            } 
 
			
		
	
		
			
				
					          } 
 
			
		
	
		
			
				
					        } 
 
			
		
	
		
			
				
					        } ,  20000 ) ;  // Check every 20 seconds
 
 
			
		
	
		
			
				
					      } 
 
			
		
	
		
			
				
					    } 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    reply . code ( 200 ) . send ( {  message :  "Motor action processed successfully."  } ) ; 
 
			
		
	
		
			
				
					  }  catch  ( error )  { 
 
			
		
	
		
			
				
					    console . error ( error ) ; 
 
			
		
	
		
			
				
					    reply . code ( 500 ) . send ( {  error :  error . message  } ) ; 
 
			
		
	
		
			
				
					  } 
 
			
		
	
		
			
				
					} ;  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					// exports.motorAction = async (req, reply) => {
  
			
		
	
		
			
				
					//   try {
  
			
		
	
		
			
				
					//     const customerId = req.params.customerId;
  
			
		
	
		
			
				
					//     const action = req.body.action;
  
			
		
	
		
			
				
					//     const motorId = req.body.motor_id;
  
			
		
	
		
			
				
					//     const start_instance_id = req.body.start_instance_id;
  
			
		
	
		
			
				
					//     //const fcmIds = req.body.fcmIds; // Assume this is provided in the request to notify users
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//     // Ensure motor_id is provided
  
			
		
	
		
			
				
					//     if (!motorId) {
  
			
		
	
		
			
				
					//       throw new Error("Motor ID is required.");
  
			
		
	
		
			
				
					//     }
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//     // Determine the motor stop status based on the action
  
			
		
	
		
			
				
					//     let motorStopStatus;
  
			
		
	
		
			
				
					//     if (action === "start") {
  
			
		
	
		
			
				
					//       motorStopStatus = "2"; // If action is start, set stop status to "2"
  
			
		
	
		
			
				
					//     } else if (action === "stop") {
  
			
		
	
		
			
				
					//       motorStopStatus = "1"; // If action is stop, set stop status to "1"
  
			
		
	
		
			
				
					//     } else {
  
			
		
	
		
			
				
					//       throw new Error("Invalid action provided.");
  
			
		
	
		
			
				
					//     }
  
			
		
	
		
			
				
					//     const users = await User.find({ customerId: customerId });
  
			
		
	
		
			
				
					//     const fcmIds = users.map(user => user.fcmId).filter(fcmId => fcmId);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//     // Handle motor stop action
  
			
		
	
		
			
				
					//     if (action === "stop") {
  
			
		
	
		
			
				
					//       // Update the motor stop status and other fields
  
			
		
	
		
			
				
					//       await Tank.updateOne(
  
			
		
	
		
			
				
					//         { customerId, "connections.inputConnections.motor_id": motorId },
  
			
		
	
		
			
				
					//         { 
  
			
		
	
		
			
				
					//           $set: { 
  
			
		
	
		
			
				
					//             "connections.inputConnections.$.motor_stop_status": "1",
  
			
		
	
		
			
				
					//             "connections.inputConnections.$.stopTime": req.body.stopTime,
  
			
		
	
		
			
				
					//             "connections.inputConnections.$.threshold_type": null,
  
			
		
	
		
			
				
					//             "connections.inputConnections.$.manual_threshold_time": null,
  
			
		
	
		
			
				
					//             "connections.inputConnections.$.manual_threshold_percentage": null
  
			
		
	
		
			
				
					//           }
  
			
		
	
		
			
				
					//         }
  
			
		
	
		
			
				
					//       );
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//       // Send immediate response to the client
  
			
		
	
		
			
				
					//       reply.code(200).send({ message: "Motor stopped successfully." });
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//       // Send notification for motor stop
  
			
		
	
		
			
				
					//       for (const fcmId of fcmIds) {
  
			
		
	
		
			
				
					//         try {
  
			
		
	
		
			
				
					//           const response = await sendPushNotification(fcmId, 'Motor Stopped', `Motor has stopped at ${req.body.stopTime}.`);
  
			
		
	
		
			
				
					//           console.log('Notification sent successfully:', response);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//           if (response.results[0].error === 'NotRegistered') {
  
			
		
	
		
			
				
					//             await User.updateOne({ fcmId: fcmId }, { $unset: { fcmId: "" } });
  
			
		
	
		
			
				
					//             console.log(`Removed unregistered FCM ID: ${fcmId}`);
  
			
		
	
		
			
				
					//           }
  
			
		
	
		
			
				
					//         } catch (error) {
  
			
		
	
		
			
				
					//           console.error('Error sending notification:', error);
  
			
		
	
		
			
				
					//         }
  
			
		
	
		
			
				
					//       }
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//       // Perform stop operations in the background
  
			
		
	
		
			
				
					//       (async () => {
  
			
		
	
		
			
				
					//         await delay(300000);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//         // Update the existing motor data entry with stop details
  
			
		
	
		
			
				
					//         const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
  
			
		
	
		
			
				
					//         if (motorData) {
  
			
		
	
		
			
				
					//           const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
  
			
		
	
		
			
				
					//           const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
  
			
		
	
		
			
				
					//           const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
  
			
		
	
		
			
				
					//           const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
  
			
		
	
		
			
				
					//           const totalwaterpumped = quantityDelivered + water_pumped_till_now;
  
			
		
	
		
			
				
					//           await Tank.findOneAndUpdate(
  
			
		
	
		
			
				
					//             { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() },
  
			
		
	
		
			
				
					//             { $set: { total_water_added_from_midnight: totalwaterpumped } }
  
			
		
	
		
			
				
					//           );
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//           await MotorData.updateOne(
  
			
		
	
		
			
				
					//             { customerId, motor_id: motorId, start_instance_id: start_instance_id },
  
			
		
	
		
			
				
					//             { 
  
			
		
	
		
			
				
					//               $set: { 
  
			
		
	
		
			
				
					//                 stopTime: req.body.stopTime,
  
			
		
	
		
			
				
					//                 receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
  
			
		
	
		
			
				
					//                 quantity_delivered: quantityDelivered.toString()
  
			
		
	
		
			
				
					//               }
  
			
		
	
		
			
				
					//             }
  
			
		
	
		
			
				
					//           );
  
			
		
	
		
			
				
					//         }
  
			
		
	
		
			
				
					//       })();
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//       // Return here to ensure the rest of the code is not executed for the stop action
  
			
		
	
		
			
				
					//       return;
  
			
		
	
		
			
				
					//     } else {
  
			
		
	
		
			
				
					//       // Update the motor stop status to "2" for start action
  
			
		
	
		
			
				
					//       await Tank.updateOne(
  
			
		
	
		
			
				
					//         { customerId, "connections.inputConnections.motor_id": motorId },
  
			
		
	
		
			
				
					//         { $set: { "connections.inputConnections.$.motor_stop_status": "2" } }
  
			
		
	
		
			
				
					//       );
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//       // Send notification for motor start
  
			
		
	
		
			
				
					//       for (const fcmId of fcmIds) {
  
			
		
	
		
			
				
					//         try {
  
			
		
	
		
			
				
					//           const response = await sendPushNotification(fcmId, 'Motor Started', `Motor has started at ${req.body.startTime}.`);
  
			
		
	
		
			
				
					//           console.log('Notification sent successfully:', response);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//           if (response.results[0].error === 'NotRegistered') {
  
			
		
	
		
			
				
					//             await User.updateOne({ fcmId: fcmId }, { $unset: { fcmId: "" } });
  
			
		
	
		
			
				
					//             console.log(`Removed unregistered FCM ID: ${fcmId}`);
  
			
		
	
		
			
				
					//           }
  
			
		
	
		
			
				
					//         } catch (error) {
  
			
		
	
		
			
				
					//           console.error('Error sending notification:', error);
  
			
		
	
		
			
				
					//         }
  
			
		
	
		
			
				
					//       }
  
			
		
	
		
			
				
					//     }
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//     // Check threshold settings if action is start
  
			
		
	
		
			
				
					//     if (action === "start") {
  
			
		
	
		
			
				
					//       if (req.body.threshold_type === "time") {
  
			
		
	
		
			
				
					//         const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//         const newMotorData = new MotorData({
  
			
		
	
		
			
				
					//           customerId: customerId,
  
			
		
	
		
			
				
					//           motor_id: motorId,
  
			
		
	
		
			
				
					//           start_instance_id: start_instance_id,
  
			
		
	
		
			
				
					//           supplierTank: req.body.from,
  
			
		
	
		
			
				
					//           receiverTank: req.body.to,
  
			
		
	
		
			
				
					//           supplier_type: req.body.from_type,
  
			
		
	
		
			
				
					//           receiver_type: req.body.to_type,
  
			
		
	
		
			
				
					//           startTime: req.body.startTime,
  
			
		
	
		
			
				
					//           receiverInitialwaterlevel: parseInt(receiver_tank_info7.waterlevel, 10)
  
			
		
	
		
			
				
					//         });
  
			
		
	
		
			
				
					//         await newMotorData.save();
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//         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_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.startTime`]: req.body.startTime, [`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } }
  
			
		
	
		
			
				
					//             );
  
			
		
	
		
			
				
					//           }
  
			
		
	
		
			
				
					//         }
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//         // Start monitoring water level based on threshold time
  
			
		
	
		
			
				
					//         const thresholdTime = moment().add(req.body.manual_threshold_time, 'minutes').toDate();
  
			
		
	
		
			
				
					//         const intervalId = setInterval(async () => {
  
			
		
	
		
			
				
					//           const splr_tank_info3 = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() });
  
			
		
	
		
			
				
					//           const splr_tank_info3_waterlevel = parseInt(splr_tank_info3.waterlevel, 10);
  
			
		
	
		
			
				
					//           const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity.replace(/,/g, ''), 10);
  
			
		
	
		
			
				
					//           const splr_tank_info3_percentage = (splr_tank_info3_waterlevel / splr_tank_info3_capacity) * 100;
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//           if (new Date() >= thresholdTime || splr_tank_info3_percentage <= 20) {
  
			
		
	
		
			
				
					//             // Send notification for low supplier tank percentage
  
			
		
	
		
			
				
					//             for (const fcmId of fcmIds) {
  
			
		
	
		
			
				
					//               try {
  
			
		
	
		
			
				
					//                 const response = await sendPushNotification(fcmId, 'Low Water Level Alert', `Supplier tank water level is below 20% (${splr_tank_info3_percentage.toFixed(2)}%).`);
  
			
		
	
		
			
				
					//                 console.log('Notification sent successfully:', response);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//                 if (response.results[0].error === 'NotRegistered') {
  
			
		
	
		
			
				
					//                   await User.updateOne({ fcmId: fcmId }, { $unset: { fcmId: "" } });
  
			
		
	
		
			
				
					//                   console.log(`Removed unregistered FCM ID: ${fcmId}`);
  
			
		
	
		
			
				
					//                 }
  
			
		
	
		
			
				
					//               } catch (error) {
  
			
		
	
		
			
				
					//                 console.error('Error sending notification:', error);
  
			
		
	
		
			
				
					//               }
  
			
		
	
		
			
				
					//             }
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//             await Tank.updateOne(
  
			
		
	
		
			
				
					//               { customerId, "connections.inputConnections.motor_id": motorId },
  
			
		
	
		
			
				
					//               {
  
			
		
	
		
			
				
					//                 $set: {
  
			
		
	
		
			
				
					//                   "connections.inputConnections.$.motor_stop_status": "1",
  
			
		
	
		
			
				
					//                   "connections.inputConnections.$.threshold_type": null,
  
			
		
	
		
			
				
					//                   "connections.inputConnections.$.manual_threshold_time": null,
  
			
		
	
		
			
				
					//                   "connections.inputConnections.$.manual_threshold_percentage": null
  
			
		
	
		
			
				
					//                 }
  
			
		
	
		
			
				
					//               }
  
			
		
	
		
			
				
					//             );
  
			
		
	
		
			
				
					//             clearInterval(intervalId);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//             await delay(300000);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//             const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id: start_instance_id });
  
			
		
	
		
			
				
					//             if (motorData) {
  
			
		
	
		
			
				
					//               const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
  
			
		
	
		
			
				
					//               const receiverFinalWaterLevel = parseInt(receiverTank.waterlevel, 10);
  
			
		
	
		
			
				
					//               const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
  
			
		
	
		
			
				
					//               const water_pumped_till_now = parseInt(receiverTank.total_water_added_from_midnight, 10);
  
			
		
	
		
			
				
					//               const totalwaterpumped = quantityDelivered + water_pumped_till_now;
  
			
		
	
		
			
				
					//               await Tank.findOneAndUpdate(
  
			
		
	
		
			
				
					//                 { customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() },
  
			
		
	
		
			
				
					//                 { $set: { total_water_added_from_midnight: totalwaterpumped } }
  
			
		
	
		
			
				
					//               );
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//               await MotorData.updateOne(
  
			
		
	
		
			
				
					//                 { customerId, motor_id: motorId, start_instance_id: start_instance_id },
  
			
		
	
		
			
				
					//                 {
  
			
		
	
		
			
				
					//                   $set: {
  
			
		
	
		
			
				
					//                     stopTime: req.body.stopTime,
  
			
		
	
		
			
				
					//                     receiverfinalwaterlevel: receiverFinalWaterLevel.toString(),
  
			
		
	
		
			
				
					//                     quantity_delivered: quantityDelivered.toString()
  
			
		
	
		
			
				
					//                   }
  
			
		
	
		
			
				
					//                 }
  
			
		
	
		
			
				
					//               );
  
			
		
	
		
			
				
					//             }
  
			
		
	
		
			
				
					//           }
  
			
		
	
		
			
				
					//         }, 60000); // Check every minute
  
			
		
	
		
			
				
					//       }  else if (req.body.threshold_type === "litres") {
  
			
		
	
		
			
				
					//         console.log("entered litres")
  
			
		
	
		
			
				
					//         const receiver_tank_info7 = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//         const newMotorData = new MotorData({
  
			
		
	
		
			
				
					//           customerId: customerId,
  
			
		
	
		
			
				
					//           motor_id: motorId,
  
			
		
	
		
			
				
					//           start_instance_id: start_instance_id,
  
			
		
	
		
			
				
					//           supplierTank: req.body.from,
  
			
		
	
		
			
				
					//           receiverTank: req.body.to,
  
			
		
	
		
			
				
					//           supplier_type: req.body.from_type,
  
			
		
	
		
			
				
					//           receiver_type: req.body.to_type,
  
			
		
	
		
			
				
					//           startTime: req.body.startTime,
  
			
		
	
		
			
				
					//           receiverInitialwaterlevel:parseInt(receiver_tank_info7.waterlevel, 10)
  
			
		
	
		
			
				
					//         });
  
			
		
	
		
			
				
					//         await newMotorData.save();
  
			
		
	
		
			
				
					//         // 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.replace(/,/g, ''), 10);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					//         console.log(desired_percentage)
  
			
		
	
		
			
				
					//         const threshold_water_level = 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 })) {
  
			
		
	
		
			
				
					//           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`]: supplier_threshold.toString(), [`connections.inputConnections.${index}.startTime`]: req.body.startTime } }
  
			
		
	
		
			
				
					//             );
  
			
		
	
		
			
				
					//           }
  
			
		
	
		
			
				
					//         }
  
			
		
	
		
			
				
					        
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					       
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					        // Start monitoring water level based on threshold percentage
 
 
			
		
	
		
			
				
					        const  intervalId  =  setInterval ( async  ( )  =>  { 
 
			
		
	
		
			
				
					          // Check if water level has reached the threshold percentage
 
 
			
		
	
		
			
				
					          const  supplier _tank _info1  =  await  Tank . findOne ( {  customerId ,  tankName :  req . body . from ,  tankLocation :  req . body . from _type . toLowerCase ( )  } ) ; 
 
			
		
	
		
			
				
					          const  current _water _level  =  parseInt ( supplier _tank _info1 . waterlevel ,  10 ) ; 
 
			
		
	
		
			
				
					          if  ( current _water _level  <=  supplier _threshold )  { 
 
			
		
	
		
			
				
					            // Stop the motor pump
 
 
			
		
	
		
			
				
					            await  Tank . updateOne ( 
 
			
		
	
		
			
				
					              {  customerId ,  "connections.inputConnections.motor_id" :  motorId  } , 
 
			
		
	
		
			
				
					              {  
 
			
		
	
		
			
				
					                $set :  {  
 
			
		
	
		
			
				
					                  "connections.inputConnections.$.motor_stop_status" :  "1" , 
 
			
		
	
		
			
				
					//   
			
		
	
		
			
				
					//         const intervalId = setInterval(async () => {
  
			
		
	
		
			
				
					//   
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//   
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					                
 
			
		
	
		
			
				
					                  "connections.inputConnections.$.threshold_type" :  null , 
 
			
		
	
		
			
				
					                  "connections.inputConnections.$.manual_threshold_time" :  null , 
 
			
		
	
		
			
				
					                  "connections.inputConnections.$.manual_threshold_percentage" :  null 
 
			
		
	
		
			
				
					                } 
 
			
		
	
		
			
				
					              } 
 
			
		
	
		
			
				
					            ) ; 
 
			
		
	
		
			
				
					            clearInterval ( intervalId ) ;  // Stop monitoring water level
 
 
			
		
	
		
			
				
					            await  delay ( 300000 ) ; 
 
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//             clearInterval(intervalId);   
			
		
	
		
			
				
					//             await delay(300000);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					            const  motorData  =  await  MotorData . findOne ( {  customerId ,  motor _id :  motorId ,  start _instance _id :  start _instance _id  } ) ; 
 
			
		
	
		
			
				
					            if  ( motorData )  { 
 
			
		
	
		
			
				
					              const  receiverTank  =  await  Tank . findOne ( {  customerId ,  tankName :  motorData . receiverTank ,  tankLocation :  motorData . receiver _type . toLowerCase ( )  } ) ; 
 
			
		
	
		
			
				
					              const  receiverFinalWaterLevel  =  parseInt ( receiverTank . waterlevel ,  10 ) ; 
 
			
		
	
		
			
				
					              const  quantityDelivered  =  receiverFinalWaterLevel  -  parseInt ( motorData . receiverInitialwaterlevel ,  10 ) ; 
 
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//               const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() });
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//               const quantityDelivered = receiverFinalWaterLevel - parseInt(motorData.receiverInitialwaterlevel, 10);
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					              const  stopTime  =  formatDate ( new  Date ( ) ) ; 
 
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					              await  MotorData . updateOne ( 
 
			
		
	
		
			
				
					                {  customerId ,  motor _id :  motorId ,  start _instance _id :  start _instance _id  } , 
 
			
		
	
		
			
				
					                { 
 
			
		
	
		
			
				
					                  $set :  { 
 
			
		
	
		
			
				
					                    stopTime : stopTime , 
 
			
		
	
		
			
				
					                    receiverfinalwaterlevel :  receiverFinalWaterLevel . toString ( ) , 
 
			
		
	
		
			
				
					                    quantity _delivered :  quantityDelivered . toString ( ) 
 
			
		
	
		
			
				
					                  } 
 
			
		
	
		
			
				
					                } 
 
			
		
	
		
			
				
					              ) ; 
 
			
		
	
		
			
				
					            } 
 
			
		
	
		
			
				
					          } 
 
			
		
	
		
			
				
					        } ,  20000 ) ;  
 
			
		
	
		
			
				
					      } 
 
			
		
	
		
			
				
					    } 
 
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//     }
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    reply . code ( 200 ) . send ( {  message :  ` Motor  ${ action  ===  "start"  ?  "started"  :  "stopped" }  successfully. `  } ) ; 
 
			
		
	
		
			
				
					  }  catch  ( err )  { 
 
			
		
	
		
			
				
					    // Handle errors
 
 
			
		
	
		
			
				
					    throw  boom . boomify ( err ) ; 
 
			
		
	
		
			
				
					  } 
 
			
		
	
		
			
				
					} ;  
			
		
	
		
			
				
					//     reply.code(200).send({ message: `Motor ${action === "start" ? "started" : "stopped"} successfully.` });
  
			
		
	
		
			
				
					//   } catch (err) {
  
			
		
	
		
			
				
					//   
			
		
	
		
			
				
					//  
  
			
		
	
		
			
				
					//   }
  
			
		
	
		
			
				
					// };
  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					const  motorActionAuto  =  async  ( req ,  reply )  =>  {  
			
		
	
		
			
				
					  try  { 
 
			
		
	
	
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
				
				@ -3131,4 +3434,27 @@ setInterval(storeWaterLevels, 15 * 60 * 1000);
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					console . log ( 'Cron job scheduled to update water levels at midnight' ) ;  
			
		
	
		
			
				
					console . log ( 'Cron job scheduled to update water levels at midnight' ) ;  
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					exports . getBlockData  =  async  ( req ,  reply )  =>  {  
			
		
	
		
			
				
					  try  { 
 
			
		
	
		
			
				
					    const  customerId  =  req . params . customerId ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    // Get all tank documents for the current customerId
 
 
			
		
	
		
			
				
					    const  tanks  =  await  Tank . find ( {  customerId  } ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    // Extract the blockName from each tank
 
 
			
		
	
		
			
				
					    const  blockNames  =  tanks . map ( tank  =>  tank . blockName ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    // Remove duplicates by converting the array to a Set and then back to an array
 
 
			
		
	
		
			
				
					    const  uniqueBlockNames  =  [ ... new  Set ( blockNames ) ] ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					    // Send the unique blockNames in the response
 
 
			
		
	
		
			
				
					    reply . code ( 200 ) . send ( {  blockNames :  uniqueBlockNames  } ) ; 
 
			
		
	
		
			
				
					
 
			
		
	
		
			
				
					  }  catch  ( err )  { 
 
			
		
	
		
			
				
					    // Send an error response
 
 
			
		
	
		
			
				
					    reply . code ( 500 ) . send ( {  error :  err . message  } ) ; 
 
			
		
	
		
			
				
					  } 
 
			
		
	
		
			
				
					} ;