소스 검색

introduce device timeout

Richard Köhl 1 년 전
부모
커밋
e0c7bf2a59
1개의 변경된 파일15개의 추가작업 그리고 2개의 파일을 삭제
  1. 15 2
      helper.py

+ 15 - 2
helper.py

@@ -1,6 +1,7 @@
 import cv2
 import numpy as np
 import os
+import threading
 from io import BytesIO
 from PIL import Image
 from ppadb.client import Client as AdbClient
@@ -35,6 +36,18 @@ def find_center(x1, y1, x2, y2):
     return centerX, centerY
 
 
+def call_device_shell(action, timeout=10):
+    def target():
+        device.shell(action)
+
+    thread = threading.Thread(target=target)
+    thread.start()
+    thread.join(timeout)
+    if thread.is_alive():
+        print("ran into timeout")
+        thread.join()
+
+
 def tap(x, y=None):
     # Check if x is an int
     if isinstance(x, int):
@@ -53,7 +66,7 @@ def tap(x, y=None):
     # Assuming 'device' is a previously defined object with a 'shell' method
     action = f"input tap {location}"
     print(action)
-    device.shell(action)
+    call_device_shell(action, timeout=5)
 
 
 def tap_button(template):
@@ -66,7 +79,7 @@ def tap_button(template):
 def swipe(start, end, duration=1000):
     action = f"input swipe {start} {end} {duration}"
     print(action)
-    device.shell(action)
+    call_device_shell(action, timeout=5)
 
 
 def look_for_templates(templates):