@@ -546,23 +546,25 @@ async def call_get_location_query(self, location_hilo_id: str) -> None:
546546 async def subscribe_to_device_updated (
547547 self , location_hilo_id : str , callback : callable = None
548548 ) -> None :
549- transport = WebsocketsTransport (
550- url = f"wss://platform.hiloenergie.com/api/digital-twin/v3/graphql?access_token={ self .access_token } "
551- )
552- client = Client (transport = transport , fetch_schema_from_transport = True )
553- query = gql (self .SUBSCRIPTION_DEVICE_UPDATED )
554- try :
555- async with client as session :
556- async for result in session .subscribe (
557- query , variable_values = {"locationHiloId" : location_hilo_id }
558- ):
559- print (f"Received subscription result { result } " )
560- device_hilo_id = self ._handle_device_subscription_result (result )
561- callback (device_hilo_id )
562- except asyncio .CancelledError :
563- print ("Subscription cancelled." )
564- asyncio .sleep (1 )
565- await self .subscribe_to_device_updated (location_hilo_id )
549+ while True : # Loop to reconnect if the connection is lost
550+ transport = WebsocketsTransport (
551+ url = f"wss://platform.hiloenergie.com/api/digital-twin/v3/graphql?access_token={ self .access_token } "
552+ )
553+ client = Client (transport = transport , fetch_schema_from_transport = True )
554+ query = gql (self .SUBSCRIPTION_DEVICE_UPDATED )
555+ try :
556+ async with client as session :
557+ async for result in session .subscribe (
558+ query , variable_values = {"locationHiloId" : location_hilo_id }
559+ ):
560+ print (f"Received subscription result { result } " )
561+ device_hilo_id = self ._handle_device_subscription_result (result )
562+ if callback :
563+ callback (device_hilo_id )
564+ except Exception as e :
565+ print (f"Connection lost: { e } . Reconnecting in 5 seconds..." )
566+ await asyncio .sleep (5 )
567+ self .call_get_location_query (location_hilo_id )
566568
567569 async def subscribe_to_location_updated (
568570 self , location_hilo_id : str , callback : callable = None
0 commit comments