Skip to content

Commit afd4e52

Browse files
committed
feat: check whether the device is online before running
1 parent 527c0a1 commit afd4e52

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

hmdriver2/driver.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ class Driver:
1919

2020
def __init__(self, serial: str):
2121
self.serial = serial
22-
if not self._is_device_online():
23-
raise DeviceNotFoundError(f"Device [{self.serial}] not found")
24-
2522
self._client = HmClient(self.serial)
2623
self.hdc = self._client.hdc
2724

@@ -46,10 +43,6 @@ def __del__(self):
4643
def _init_hmclient(self):
4744
self._client.start()
4845

49-
def _is_device_online(self):
50-
_serials = list_devices()
51-
return True if self.serial in _serials else False
52-
5346
def _invoke(self, api: str, args: List = []) -> HypiumResponse:
5447
return self._client.invoke(api, this="Driver#0", args=args)
5548

hmdriver2/hdc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from . import logger
1212
from .utils import FreePort
1313
from .proto import CommandResult, KeyCode
14-
from .exception import HdcError
14+
from .exception import HdcError, DeviceNotFoundError
1515

1616

1717
def _execute_command(cmdargs: Union[str, List[str]]) -> CommandResult:
@@ -48,6 +48,12 @@ def list_devices() -> List[str]:
4848
class HdcWrapper:
4949
def __init__(self, serial: str) -> None:
5050
self.serial = serial
51+
if not self.is_online():
52+
raise DeviceNotFoundError(f"Device [{self.serial}] not found")
53+
54+
def is_online(self):
55+
_serials = list_devices()
56+
return True if self.serial in _serials else False
5157

5258
def forward_port(self, rport: int) -> int:
5359
lport: int = FreePort().get()

0 commit comments

Comments
 (0)