This commit is contained in:
Feng Wang 2024-12-21 21:52:45 +08:00
parent 794505f082
commit 569e1512e3

View File

@ -180,7 +180,11 @@ class MIoTLanDevice:
# All functions SHOULD be called from the internal loop # All functions SHOULD be called from the internal loop
def __init__( def __init__(
self, manager: 'MIoTLan', did: str, token: str, ip: Optional[str] = None self,
manager: 'MIoTLan',
did: str,
token: str,
ip: Optional[str] = None
) -> None: ) -> None:
self._manager: MIoTLan = manager self._manager: MIoTLan = manager
self.did = did self.did = did
@ -427,9 +431,10 @@ class MIoTLanDevice:
self.online = True self.online = True
else: else:
_LOGGER.info('unstable device detected, %s', self.did) _LOGGER.info('unstable device detected, %s', self.did)
self._online_offline_timer = self._manager.internal_loop.call_later( self._online_offline_timer = \
self.NETWORK_UNSTABLE_RESUME_TH, self._manager.internal_loop.call_later(
self.__online_resume_handler) self.NETWORK_UNSTABLE_RESUME_TH,
self.__online_resume_handler)
def __online_resume_handler(self) -> None: def __online_resume_handler(self) -> None:
_LOGGER.info('unstable resume threshold past, %s', self.did) _LOGGER.info('unstable resume threshold past, %s', self.did)
@ -491,13 +496,13 @@ class MIoTLan:
# The following should be called from the main loop # The following should be called from the main loop
def __init__( def __init__(
self, self,
net_ifs: list[str], net_ifs: list[str],
network: MIoTNetwork, network: MIoTNetwork,
mips_service: MipsService, mips_service: MipsService,
enable_subscribe: bool = False, enable_subscribe: bool = False,
virtual_did: Optional[int] = None, virtual_did: Optional[int] = None,
loop: Optional[asyncio.AbstractEventLoop] = None loop: Optional[asyncio.AbstractEventLoop] = None
) -> None: ) -> None:
if not network: if not network:
raise ValueError('network is required') raise ValueError('network is required')
@ -513,7 +518,9 @@ class MIoTLan:
key='miot_lan', group_id='*', key='miot_lan', group_id='*',
handler=self.__on_mips_service_change) handler=self.__on_mips_service_change)
self._enable_subscribe = enable_subscribe self._enable_subscribe = enable_subscribe
self._virtual_did = str(virtual_did) if (virtual_did is not None) else str(secrets.randbits(64)) self._virtual_did = str(virtual_did) \
if (virtual_did is not None) \
else str(secrets.randbits(64))
# Init socket probe message # Init socket probe message
probe_bytes = bytearray(self.OT_PROBE_LEN) probe_bytes = bytearray(self.OT_PROBE_LEN)
probe_bytes[:20] = ( probe_bytes[:20] = (
@ -718,8 +725,12 @@ class MIoTLan:
@final @final
def sub_prop( def sub_prop(
self, did: str, handler: Callable[[dict, Any], None], self,
siid: Optional[int] = None, piid: Optional[int] = None, handler_ctx: Any = None did: str,
handler: Callable[[dict, Any], None],
siid: Optional[int] = None,
piid: Optional[int] = None,
handler_ctx: Any = None
) -> bool: ) -> bool:
if not self._enable_subscribe: if not self._enable_subscribe:
return False return False
@ -733,7 +744,12 @@ class MIoTLan:
return True return True
@final @final
def unsub_prop(self, did: str, siid: Optional[int] = None, piid: Optional[int] = None) -> bool: def unsub_prop(
self,
did: str,
siid: Optional[int] = None,
piid: Optional[int] = None
) -> bool:
if not self._enable_subscribe: if not self._enable_subscribe:
return False return False
key = ( key = (
@ -746,8 +762,12 @@ class MIoTLan:
@final @final
def sub_event( def sub_event(
self, did: str, handler: Callable[[dict, Any], None], self,
siid: Optional[int] = None, eiid: Optional[int] = None, handler_ctx: Any = None did: str,
handler: Callable[[dict, Any], None],
siid: Optional[int] = None,
eiid: Optional[int] = None,
handler_ctx: Any = None
) -> bool: ) -> bool:
if not self._enable_subscribe: if not self._enable_subscribe:
return False return False
@ -761,7 +781,12 @@ class MIoTLan:
return True return True
@final @final
def unsub_event(self, did: str, siid: Optional[int] = None, eiid: Optional[int] = None) -> bool: def unsub_event(
self,
did: str,
siid: Optional[int] = None,
eiid: Optional[int] = None
) -> bool:
if not self._enable_subscribe: if not self._enable_subscribe:
return False return False
key = ( key = (
@ -985,8 +1010,8 @@ class MIoTLan:
def broadcast_device_state(self, did: str, state: dict) -> None: def broadcast_device_state(self, did: str, state: dict) -> None:
for handler in self._device_state_sub_map.values(): for handler in self._device_state_sub_map.values():
self._main_loop.call_soon_threadsafe( self._main_loop.call_soon_threadsafe(
lambda: self._main_loop.create_task( self._main_loop.create_task,
handler.handler(did, state, handler.handler_ctx))) handler.handler(did, state, handler.handler_ctx))
def __gen_msg_id(self) -> int: def __gen_msg_id(self) -> int:
if not self._msg_id_counter: if not self._msg_id_counter:
@ -996,7 +1021,14 @@ class MIoTLan:
self._msg_id_counter = 1 self._msg_id_counter = 1
return self._msg_id_counter return self._msg_id_counter
def __call_api(self, did: str, msg: dict, handler: Callable, handler_ctx: Any, timeout_ms: int = 10000) -> None: def __call_api(
self,
did: str,
msg: dict,
handler: Callable,
handler_ctx: Any,
timeout_ms: int = 10000
) -> None:
try: try:
self.send2device( self.send2device(
did=did, did=did,
@ -1304,11 +1336,11 @@ class MIoTLan:
def __scan_devices(self) -> None: def __scan_devices(self) -> None:
if self._scan_timer: if self._scan_timer:
self._scan_timer.cancel() self._scan_timer.cancel()
# Ignore any exceptions to avoid blocking the loop
try: try:
# Scan devices # Scan devices
self.ping(if_name=None, target_ip='255.255.255.255') self.ping(if_name=None, target_ip='255.255.255.255')
except: except:
# Ignore any exceptions to avoid blocking the loop
pass pass
scan_time = self.__get_next_scan_time() scan_time = self.__get_next_scan_time()
self._scan_timer = self._internal_loop.call_later( self._scan_timer = self._internal_loop.call_later(