From afd4e527f8d3c4233e4fa39d45e91272ba76eaac Mon Sep 17 00:00:00 2001 From: codematrixer Date: Mon, 30 Sep 2024 14:22:03 +0800 Subject: [PATCH] feat: check whether the device is online before running --- hmdriver2/driver.py | 7 ------- hmdriver2/hdc.py | 8 +++++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/hmdriver2/driver.py b/hmdriver2/driver.py index ea7a699..f8adf50 100644 --- a/hmdriver2/driver.py +++ b/hmdriver2/driver.py @@ -19,9 +19,6 @@ class Driver: def __init__(self, serial: str): self.serial = serial - if not self._is_device_online(): - raise DeviceNotFoundError(f"Device [{self.serial}] not found") - self._client = HmClient(self.serial) self.hdc = self._client.hdc @@ -46,10 +43,6 @@ def __del__(self): def _init_hmclient(self): self._client.start() - def _is_device_online(self): - _serials = list_devices() - return True if self.serial in _serials else False - def _invoke(self, api: str, args: List = []) -> HypiumResponse: return self._client.invoke(api, this="Driver#0", args=args) diff --git a/hmdriver2/hdc.py b/hmdriver2/hdc.py index 0f0c914..0daae4b 100644 --- a/hmdriver2/hdc.py +++ b/hmdriver2/hdc.py @@ -11,7 +11,7 @@ from . import logger from .utils import FreePort from .proto import CommandResult, KeyCode -from .exception import HdcError +from .exception import HdcError, DeviceNotFoundError def _execute_command(cmdargs: Union[str, List[str]]) -> CommandResult: @@ -48,6 +48,12 @@ def list_devices() -> List[str]: class HdcWrapper: def __init__(self, serial: str) -> None: self.serial = serial + if not self.is_online(): + raise DeviceNotFoundError(f"Device [{self.serial}] not found") + + def is_online(self): + _serials = list_devices() + return True if self.serial in _serials else False def forward_port(self, rport: int) -> int: lport: int = FreePort().get()