11# -*- coding: utf-8 -*-
22
33import abc
4- import traceback
54import tempfile
65from typing import List , Dict , Union , Tuple
76from functools import cached_property # python3.8+
1312import wda
1413import uiautomator2 as u2
1514from hmdriver2 import hdc
16- from hmdriver2 .driver import Driver
1715from fastapi import HTTPException
1816
1917from uiviewer ._utils import file_to_base64 , image_to_base64
@@ -48,21 +46,21 @@ def dump_hierarchy(self) -> Dict:
4846class HarmonyDevice (DeviceMeta ):
4947 def __init__ (self , serial : str ):
5048 self .serial = serial
51- self .client = Driver (serial )
49+ self .hdc = hdc . HdcWrapper (serial )
5250
5351 @cached_property
5452 def _display_size (self ) -> Tuple :
55- return self .client .display_size
53+ return self .hdc .display_size ()
5654
5755 def take_screenshot (self ) -> str :
5856 with tempfile .NamedTemporaryFile (delete = True , suffix = ".png" ) as f :
5957 path = f .name
60- self .client .screenshot (path )
58+ self .hdc .screenshot (path )
6159 return file_to_base64 (path )
6260
6361 def dump_hierarchy (self ) -> BaseHierarchy :
64- packageName , pageName = self .client .current_app ()
65- raw : Dict = self .client .dump_hierarchy ()
62+ packageName , pageName = self .hdc .current_app ()
63+ raw : Dict = self .hdc .dump_hierarchy ()
6664 hierarchy : Dict = harmony_hierarchy .convert_harmony_hierarchy (raw )
6765 return BaseHierarchy (
6866 jsonHierarchy = hierarchy ,
@@ -76,7 +74,6 @@ def dump_hierarchy(self) -> BaseHierarchy:
7674class AndroidDevice (DeviceMeta ):
7775 def __init__ (self , serial : str ):
7876 self .serial = serial
79- adbutils .AdbClient ()
8077 self .d : u2 .Device = u2 .connect (serial )
8178
8279 @cached_property
@@ -115,6 +112,11 @@ def scale(self) -> int:
115112 def _window_size (self ) -> Tuple :
116113 return self .client .window_size ()
117114
115+ def _check_wda_health (self ) -> bool :
116+ resp = request ("GET" , f"{ self .wda_url } /status" , timeout = 5 ).json ()
117+ state = resp .get ("value" , {}).get ("state" )
118+ return state == "success"
119+
118120 def take_screenshot (self ) -> str :
119121 img : Image .Image = self .client .screenshot ()
120122 return image_to_base64 (img )
@@ -136,13 +138,8 @@ def dump_hierarchy(self) -> BaseHierarchy:
136138 scale = self .scale
137139 )
138140
139- def wda_health (self ) -> bool :
140- resp = request ("GET" , f"{ self .wda_url } /status" , timeout = 5 ).json ()
141- state = resp .get ("value" , {}).get ("state" )
142- return state == "success"
143-
144141
145- def get_device (platform : str , serial : str , wda_url : str , max_depth : int ) -> Union [HarmonyDevice , AndroidDevice ]:
142+ def get_device (platform : str , serial : str , wda_url : str , max_depth : int ) -> Union [HarmonyDevice , AndroidDevice , IosDevice ]:
146143 if platform == Platform .HARMONY :
147144 return HarmonyDevice (serial )
148145 elif platform == Platform .ANDROID :
@@ -165,9 +162,8 @@ def init_device(platform: str, serial: str, wda_url: str = None, max_depth: int
165162 cached_devices [(platform , serial )] = device
166163
167164 if isinstance (device , IosDevice ):
168- return device .wda_health ()
169- except Exception :
170- error = traceback .format_exc ()
171- raise HTTPException (status_code = 500 , detail = error )
165+ return device ._check_wda_health ()
166+ except Exception as e :
167+ raise HTTPException (status_code = 500 , detail = str (e ))
172168
173169 return True
0 commit comments