From 58da4a71443807b3e7b2f7b3454b15760bcb4200 Mon Sep 17 00:00:00 2001 From: LmeSzinc <37934724+LmeSzinc@users.noreply.github.com> Date: Sun, 11 Aug 2024 00:44:23 +0800 Subject: [PATCH] Pref: [ALAS] Connect all possible serial at once (cherry picked from commit 14a3f57d789fba50297c1371934da82474d5690b) --- module/device/connection.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/module/device/connection.py b/module/device/connection.py index 74ef2fa3d..557e8e07e 100644 --- a/module/device/connection.py +++ b/module/device/connection.py @@ -692,13 +692,9 @@ class Connection(ConnectionAttr): # Brute force connect nearby ports to handle serial switches if self.is_mumu12_family: before = self.serial - for port_offset in [1, -1, 2, -2]: - port = self.port + port_offset - serial = self.serial.replace(str(self.port), str(port)) - msg = self.adb_client.connect(serial) - logger.info(msg) - if 'connected' in msg: - break + serial_list = [self.serial.replace(str(self.port), str(self.port + offset)) + for offset in [1, -1, 2, -2]] + self.adb_brute_force_connect(serial_list) self.detect_device() if self.serial != before: return True @@ -711,6 +707,25 @@ class Connection(ConnectionAttr): self.detect_device() return False + def adb_brute_force_connect(self, serial_list): + """ + Args: + serial_list (list[str]): + """ + import asyncio + ev = asyncio.new_event_loop() + + def _connect(serial): + msg = self.adb_client.connect(serial) + logger.info(msg) + return msg + + async def connect(): + tasks = [ev.run_in_executor(None, _connect, serial) for serial in serial_list] + await asyncio.gather(*tasks) + + ev.run_until_complete(connect()) + @Config.when(DEVICE_OVER_HTTP=True) def adb_connect(self): # No adb connect if over http