@@ -286,6 +286,9 @@ def _create_ssl_context() -> ssl.SSLContext:
286286 status
287287 device {
288288 ... on Gateway {
289+ deviceType
290+ hiloId
291+ physicalAddress
289292 connectionStatus
290293 controllerSoftwareVersion
291294 lastConnectionTime
@@ -542,7 +545,7 @@ def _create_ssl_context() -> ssl.SSLContext:
542545 }
543546}"""
544547
545- async def call_get_location_query (self , location_hilo_id : str ) -> None :
548+ async def call_get_location_query (self , location_hilo_id : str ) -> list [ str ] :
546549 """This functions calls the digital-twin and requests location id"""
547550 access_token = await self ._get_access_token ()
548551 url = f"https://{ PLATFORM_HOST } /api/digital-twin/v3/graphql"
@@ -568,7 +571,7 @@ async def call_get_location_query(self, location_hilo_id: str) -> None:
568571 response_json = response .json ()
569572 except Exception as e :
570573 LOG .error ("Error parsing response: %s" , e )
571- return
574+ return []
572575
573576 if "errors" in response_json :
574577 for error in response_json ["errors" ]:
@@ -582,12 +585,12 @@ async def call_get_location_query(self, location_hilo_id: str) -> None:
582585 response_json = response .json ()
583586 except Exception as e :
584587 LOG .error ("Error parsing response on retry: %s" , e )
585- return
588+ return []
586589 break
587590
588591 if "errors" in response_json :
589592 LOG .error ("GraphQL errors: %s" , response_json ["errors" ])
590- return
593+ return []
591594
592595 if "data" in response_json :
593596 devices = (
@@ -600,7 +603,9 @@ async def call_get_location_query(self, location_hilo_id: str) -> None:
600603 "Gateway devices in getLocation response: %s" ,
601604 json .dumps (gateways , indent = 2 ),
602605 )
603- self ._handle_query_result (response_json ["data" ])
606+ return self ._handle_query_result (response_json ["data" ])
607+
608+ return []
604609
605610 async def subscribe_to_device_updated (
606611 self ,
@@ -664,7 +669,7 @@ def _handle_sse_message(
664669 if "data" in data :
665670 LOG .debug ("Received subscription result %s" , data ["data" ])
666671 handler_result = handler (data ["data" ])
667- if callback :
672+ if callback and handler_result :
668673 callback (handler_result )
669674 return False , True
670675 return False , False
@@ -752,6 +757,7 @@ async def _listen_to_sse(
752757 "SSE connection closed by server. Reconnecting in 5 seconds..."
753758 )
754759 await asyncio .sleep (5 )
760+ await self ._resync_after_reconnect (location_hilo_id , callback )
755761
756762 except asyncio .TimeoutError :
757763 # Keepalive timeout: the connection was recently live, so start
@@ -765,15 +771,7 @@ async def _listen_to_sse(
765771 backoff_delay = min (backoff_delay * 2 , self ._BACKOFF_MAX )
766772 if "query" in payload :
767773 del payload ["query" ]
768- if location_hilo_id :
769- try :
770- await self .call_get_location_query (location_hilo_id )
771- LOG .debug ("call_get_location_query success after reconnect" )
772- except Exception as e2 :
773- LOG .error (
774- "exception while RE-connecting, retrying: %s" ,
775- e2 ,
776- )
774+ await self ._resync_after_reconnect (location_hilo_id , callback )
777775
778776 except Exception as e :
779777 LOG .debug (
@@ -785,21 +783,30 @@ async def _listen_to_sse(
785783 backoff_delay = min (backoff_delay * 2 , self ._BACKOFF_MAX )
786784 if "query" in payload :
787785 del payload ["query" ]
788- if location_hilo_id :
789- try :
790- await self .call_get_location_query (location_hilo_id )
791- LOG .debug ("call_get_location_query success after reconnect" )
792- except Exception as e2 :
793- LOG .error (
794- "exception while RE-connecting, retrying: %s" ,
795- e2 ,
796- )
786+ await self ._resync_after_reconnect (location_hilo_id , callback )
787+
788+ async def _resync_after_reconnect (
789+ self ,
790+ location_hilo_id : Optional [str ],
791+ callback : Optional [Callable [[str ], None ]],
792+ ) -> None :
793+ if not location_hilo_id :
794+ return
795+ try :
796+ hilo_ids = await self .call_get_location_query (location_hilo_id )
797+ LOG .debug ("call_get_location_query success after reconnect" )
798+ except Exception as err :
799+ LOG .error ("exception while RE-connecting, retrying: %s" , err )
800+ return
801+ if callback :
802+ for hilo_id in hilo_ids :
803+ callback (hilo_id )
797804
798805 async def _get_access_token (self ) -> str :
799806 """Get the access token."""
800807 return await self ._api .async_get_access_token ()
801808
802- def _handle_query_result (self , result : Dict [str , Any ]) -> None :
809+ def _handle_query_result (self , result : Dict [str , Any ]) -> list [ str ] :
803810 """Handle the result of the GraphQL query for location and devices."""
804811 devices_values : List [Dict [str , Any ]] = result ["getLocation" ]["devices" ]
805812
@@ -813,7 +820,8 @@ def _handle_query_result(self, result: Dict[str, Any]) -> None:
813820 self ._devices .devices .append (gw_dev )
814821
815822 attributes = self .mapper .map_query_values (devices_values )
816- self ._devices .parse_values_received (attributes )
823+ updated_devices = self ._devices .parse_values_received (attributes )
824+ return [device .hilo_id for device in updated_devices if device .hilo_id ]
817825
818826 def _build_gateway_dict (self , raw_device : Dict [str , Any ]) -> Dict [str , Any ]:
819827 """Build a dictionary representing the gateway device from raw GraphQL data."""
@@ -829,6 +837,7 @@ def _build_gateway_dict(self, raw_device: Dict[str, Any]) -> Dict[str, Any]:
829837 "type" : "Gateway" ,
830838 "category" : "Gateway" ,
831839 "id" : 1 ,
840+ "hilo_id" : hilo_id ,
832841 "identifier" : mac ,
833842 "sdi" : mac ,
834843 "provider" : 1 ,
@@ -854,7 +863,7 @@ def _handle_device_subscription_result(self, result: Dict[str, Any]) -> str:
854863 updated_device = self ._devices .parse_values_received (attributes )
855864 # callback to update the device in the UI
856865 LOG .debug ("Device updated: %s" , updated_device )
857- return str ( device_value . get ( "hiloId" ))
866+ return updated_device [ 0 ]. hilo_id if updated_device else ""
858867
859868 def _handle_location_subscription_result (self , result : Dict [str , Any ]) -> str :
860869 """Handle the result of the GraphQL subscription for location updates."""
0 commit comments