TEST_DL_URL = BASE_URL.format(VERSION, TEST_APK) MAIN_DL_URL = BASE_URL.format(VERSION, MAIN_APK) process_dict = {} def install_sh(device_id): print('install simhand2 in {}'.format(device_id)) pya = PYAToolkit(device_id) pya.install_from(url=TEST_DL_URL) pya.install_from(url=MAIN_DL_URL) print('install simhand2 ok in {}'.format(device_id)) new_process = subprocess.Popen( "adb shell am instrument -w -r -e debug false -e class 'com.github.williamfzc.simhand2.StubTestCase' com.github.williamfzc.simhand2.test/com.github.williamfzc.simhand2.SHInstrumentationTestRunner" ) process_dict[device_id] = new_process new_process.kill() sys.exit() def stop_simhand(device_id): p = process_dict[device_id] p.kill() del process_dict[device_id] when_connect(device='all', do=install_sh) when_disconnect(device='all', do=stop_simhand)
def start_device_detect(): when_connect(device=DEVICE_LIST, do=install_simhand) when_connect(device=DEVICE_LIST, do=add_device) when_disconnect(device=DEVICE_LIST, do=remove_device) start_detect(with_log=False)
def is_device_available(cls, device_id: str) -> bool: return cls.is_device_existed(device_id) and cls.is_device_busy( device_id) @classmethod def acquire(cls, device_id: str): device = cls.DEVICE_DICT[device_id] device.acquire() @classmethod def release(cls, device_id: str): device = cls.DEVICE_DICT[device_id] device.release() when_connect(device='any', do=DeviceManager.add) when_disconnect(device='any', do=DeviceManager.remove) # --- API below --- app = FastAPI() @app.get("/") def hello(): return {"hello": "from lockadb server"} @app.get("/devices") def all_devices(): device_list = [i.__dict__ for i in DeviceManager.DEVICE_DICT.values()]
device_dict = dict() def add_device(device_id): if '.' in device_id: return # if you don't need remote connect, just remove argument `mode` new_device = PYAToolkit(device_id, mode='remote') device_dict[device_id] = new_device def remove_device(device_id): if '.' in device_id: return if device_id in device_dict: device_dict[device_id].terminate() del device_dict[device_id] when_connect(device='all', do=add_device) when_disconnect(device='all', do=remove_device) start_detect() while True: time.sleep(2) if device_dict: for k, v in device_dict.items(): print(k, v.device_id, v.device_ip)
def special_thing(device): print('do something special!', device) def normal_thing(device): print('do something normal.', device) def lose_connect(device): print('{} lost!'.format(device)) # init when_connect # if no need for log, you can set 'with_log' to False. Default to True. start_detect(with_log=False) # set device list when_connect(device=['123', 'def456'], do=special_thing) # or command mode when_connect(device='any', do=normal_thing) # of course, when disconnect: when_disconnect(device='any', do=lose_connect) # CARE ONLY WHAT U REALLY NEED while True: pass