Bläddra i källkod

更新工具类

xiao.qiang 8 månader sedan
förälder
incheckning
d956fe54d6
7 ändrade filer med 372 tillägg och 72 borttagningar
  1. 30 1
      README.md
  2. BIN
      assets/img.png
  3. 37 0
      test/deepcion_point.json
  4. 63 71
      test/deepcoin.py
  5. 19 0
      test/deepcoin_point.py
  6. 87 0
      test/deepcoin_test.py
  7. 136 0
      utils/utils.py

+ 30 - 1
README.md

@@ -6,6 +6,8 @@
 
 https://github.com/openatx/uiautomator2
 
+https://www.genymotion.com/product-desktop/download/
+
 ## 初始化项目
 ```shell
 poetry install
@@ -25,4 +27,31 @@ adb devices
 查找APPID
 ```shell
 adb shell pm list packages -f
-```
+```
+
+## 坐标文件xxx_point.json 格式
+示例
+[deepcion_point.json](test/deepcion_point.json)
+
+说明
+这个文件可以 使用 类似于 utils.utils.UiFactory.save_point() 保存
+
+也可以手动编写
+格式:
+```json
+{
+    "btn_buy": {
+    "description": "买入/开多",
+    "point": {
+      "x": 988.0,
+      "y": 1066.5
+    }
+  }
+}
+```
+- 唯一标识:btn_buy
+- description 描述
+- point坐标
+point 可以 使用weditor获取到的坐标
+
+![img.png](assets/img.png)

BIN
assets/img.png


+ 37 - 0
test/deepcion_point.json

@@ -0,0 +1,37 @@
+{
+  "btn_jy": {
+    "xpath": "//*[@content-desc=\"交易\"]",
+    "point": {
+      "x": 720,
+      "y": 3606
+    }
+  },
+  "input_weituo": {
+    "xpath": "//*[@content-desc=\"最优\"]",
+    "point": {
+      "x": 1354,
+      "y": 491
+    }
+  },
+  "input_num": {
+    "xpath": "//android.widget.EditText",
+    "point": {
+      "x": 938,
+      "y": 675
+    }
+  },
+  "btn_buy": {
+    "description": "买入/开多",
+    "point": {
+      "x": 988.0,
+      "y": 1066.5
+    }
+  },
+  "btn_ping": {
+    "description": "平仓",
+    "point": {
+      "x": 823,
+      "y": 1903
+    }
+  }
+}

+ 63 - 71
test/deepcoin.py

@@ -2,86 +2,78 @@ import time
 
 import uiautomator2 as u2
 
+from utils.utils import UiFactory
+
 # 连接到设备,默认连接第一个设备,如果有多个设备可以指定序列号
-d = u2.connect('127.0.0.1:6555')
+ui = UiFactory('127.0.0.1:6555')
+# 从配置中加载坐标
+# 执行deepcoin_point.py 文件进行采集。
+ui.load_point()
+
+is_start = ui.check_app_running("com.niocpeed.dna")
+# tab 交易按钮
+btn_jy = ui.xpath("btn_jy")
 
-# 获取所有运行的APP
-running_apps = d.app_list_running()
-print("安装列表:", running_apps)
-# 如果app没有启动, 启动APP
-if "com.niocpeed.dna" not in running_apps:
-    print("com niocpeed.dna 重新启动")
-    d.app_start("com.niocpeed.dna")
-    # tab 交易按钮
-btn_jy = d.xpath('//*[@content-desc="交易"]')
-# btn_jy.click()
-# 交易方式选择
-btn_sel = d.xpath('//*[@content-desc="USDT合约"]')
-if btn_sel.exists:
-    btn_sel.click()
+if is_start:
+    print("点击交易")
+    btn_jy.click()
 
 # 设置委托
-input_weituo = d.xpath('//*[@content-desc="最优"]')
+input_weituo = ui.xpath("input_weituo")
 # 设置数量
-input_num = d.xpath('//android.widget.ScrollView/android.view.View[39]/android.widget.EditText[1]')
-btn_buy = d(description="买入/开多")
+input_num = ui.xpath('input_num')
 
-buy_x, buy_y = btn_buy.center()
+# 设置数量
+btn_ping = ui.xpath('btn_ping')
 
-start1 = time.time()
-input_weituo.click()
-end1 = time.time()
-print("点击:最优委托 操作耗时:", end1 - start1)
 
-# start1 = time.time()
-# input_weituo.set_text("0.114")
-# end1 = time.time()
-# print("设置:操作耗时:", end1 - start1)
-# start1 = time.time()
-# input_num.click()
-# end1 = time.time()
-# print("点击数量:操作耗时:", end1 - start1)
-# start1 = time.time()
-# input_num.set_text("40")
-# end1 = time.time()
-# print("设置数量:操作耗时:", end1 - start1)
 
-for i in range(10):
-    # 初始化点击按钮
-    start = time.time()
-    # start1 = time.time()
-    # input_weituo.click()
-    # end1 = time.time()
-    # print("点击:最优委托 操作耗时:", end1 - start1)
-    #
-    # # start1 = time.time()
-    # # input_weituo.set_text("0.114")
-    # # end1 = time.time()
-    # # print("设置:操作耗时:", end1 - start1)
-    # start1 = time.time()
-    # input_num.click()
-    # end1 = time.time()
-    # print("点击:操作耗时:", end1 - start1)
-    # start1 = time.time()
-    # input_num.set_text("40")
-    # end1 = time.time()
-    # print("设置:操作耗时:", end1 - start1)
-    start1 = time.time()
-    #  直接点击   xpath直接点击
-    # btn_buy.click()
-    # 比上面的快50毫秒
-    # d.click(1078, 1094)   ui点击 指定坐标
-    # 比上面的快100毫秒  直接调用shell 指定坐标
-    d.shell(f"input tap {buy_x} {buy_y}")
-    end1 = time.time()
-    print("点击下单:操作耗时:", end1 - start1)
-    end = time.time()
-    # print("点击整体:操作耗时:", end - start)
-    time.sleep(5)
 
-# 给开仓确认的弹框 选中 下次不在提醒,能减少时间
-# d(description="确认").click()
-# d.xpath('//*[@content-desc="确认"]').click()
+start1 = time.time()
+input_num.clear()
+end1 = time.time()
+print("清空数量:操作耗时:", end1 - start1)
+start1 = time.time()
+input_num.text("41")
+end1 = time.time()
+print("设置数量:操作耗时:", end1 - start1)
+
+btn_buy = ui.desc("btn_buy")
 
+start1 = time.time()
+input_weituo.click()
+end1 = time.time()
+print("点击:最优委托 操作耗时:", end1 - start1)
 
-# button = d.xpath("//android.widget.Button[@text='Button']")
+start1 = time.time()
+btn_ping.click()
+end1 = time.time()
+print("点击:平仓 操作耗时:", end1 - start1)
+#
+# for i in range(10):
+#     # 初始化点击按钮
+#     start = time.time()
+#     # start1 = time.time()
+#     # input_weituo.click()
+#     # end1 = time.time()
+#     # print("点击:最优委托 操作耗时:", end1 - start1)
+#     #
+#     # # start1 = time.time()
+#     # # input_weituo.set_text("0.114")
+#     # # end1 = time.time()
+#     # # print("设置:操作耗时:", end1 - start1)
+#     # start1 = time.time()
+#     # input_num.click()
+#     # end1 = time.time()
+#     # print("点击:操作耗时:", end1 - start1)
+#     # start1 = time.time()
+#     # input_num.set_text("40")
+#     # end1 = time.time()
+#     # print("设置:操作耗时:", end1 - start1)
+#     start1 = time.time()
+#     btn_buy.click()
+#     end1 = time.time()
+#     print("点击下单:操作耗时:", end1 - start1)
+#     end = time.time()
+#     # print("点击整体:操作耗时:", end - start)
+#     time.sleep(5)

+ 19 - 0
test/deepcoin_point.py

@@ -0,0 +1,19 @@
+from utils.utils import UiFactory
+
+#  坐标采集
+# 连接到设备,默认连接第一个设备,如果有多个设备可以指定序列号
+ui = UiFactory('127.0.0.1:6555')
+
+is_start = ui.check_app_running("com.niocpeed.dna")
+# tab 交易按钮
+btn_jy = ui.xpath("btn_jy", '//*[@content-desc="交易"]')
+
+# 设置委托
+input_weituo = ui.xpath("input_weituo", '//*[@content-desc="最优"]')
+# 设置数量
+# 设置数量
+input_num = ui.xpath('input_num','//android.widget.EditText')
+
+btn_buy = ui.desc("btn_buy", "买入/开多")
+
+ui.save_point()

+ 87 - 0
test/deepcoin_test.py

@@ -0,0 +1,87 @@
+import time
+
+import uiautomator2 as u2
+
+# 连接到设备,默认连接第一个设备,如果有多个设备可以指定序列号
+d = u2.connect('127.0.0.1:6555')
+
+# 获取所有运行的APP
+running_apps = d.app_list_running()
+print("安装列表:", running_apps)
+# 如果app没有启动, 启动APP
+if "com.niocpeed.dna" not in running_apps:
+    print("com niocpeed.dna 重新启动")
+    d.app_start("com.niocpeed.dna")
+    # tab 交易按钮
+btn_jy = d.xpath('//*[@content-desc="交易"]')
+# btn_jy.click()
+# 交易方式选择
+btn_sel = d.xpath('//*[@content-desc="USDT合约"]')
+if btn_sel.exists:
+    btn_sel.click()
+
+# 设置委托
+input_weituo = d.xpath('//*[@content-desc="最优"]')
+# 设置数量
+input_num = d.xpath('//android.widget.ScrollView/android.view.View[18]/android.widget.EditText[1]')
+btn_buy = d(description="买入/开多")
+
+buy_x, buy_y = btn_buy.center()
+
+start1 = time.time()
+input_weituo.click()
+end1 = time.time()
+print("点击:最优委托 操作耗时:", end1 - start1)
+
+# start1 = time.time()
+# input_weituo.set_text("0.114")
+# end1 = time.time()
+# print("设置:操作耗时:", end1 - start1)
+start1 = time.time()
+input_num.click()
+end1 = time.time()
+print("点击数量:操作耗时:", end1 - start1)
+# start1 = time.time()
+# input_num.set_text("40")
+# end1 = time.time()
+# print("设置数量:操作耗时:", end1 - start1)
+
+# for i in range(10):
+#     # 初始化点击按钮
+#     start = time.time()
+#     # start1 = time.time()
+#     # input_weituo.click()
+#     # end1 = time.time()
+#     # print("点击:最优委托 操作耗时:", end1 - start1)
+#     #
+#     # # start1 = time.time()
+#     # # input_weituo.set_text("0.114")
+#     # # end1 = time.time()
+#     # # print("设置:操作耗时:", end1 - start1)
+#     # start1 = time.time()
+#     # input_num.click()
+#     # end1 = time.time()
+#     # print("点击:操作耗时:", end1 - start1)
+#     # start1 = time.time()
+#     # input_num.set_text("40")
+#     # end1 = time.time()
+#     # print("设置:操作耗时:", end1 - start1)
+#     start1 = time.time()
+#     #  直接点击   xpath直接点击
+#     # btn_buy.click()
+#     # 比上面的快50毫秒
+#     # d.click(1078, 1094)  # ui点击 指定坐标
+#     # 比上面的快100毫秒  直接调用shell 指定坐标
+#     d.shell(f"input tap {buy_x} {buy_y}")
+#     end1 = time.time()
+#     print("点击下单:操作耗时:", end1 - start1)
+#     end = time.time()
+#     # print("点击整体:操作耗时:", end - start)
+#     time.sleep(5)
+
+# 给开仓确认的弹框 选中 下次不在提醒,能减少时间
+# d(description="确认").click()
+# d.xpath('//*[@content-desc="确认"]').click()
+
+
+# button = d.xpath("//android.widget.Button[@text='Button']")

+ 136 - 0
utils/utils.py

@@ -0,0 +1,136 @@
+import json
+import this
+import time
+
+import uiautomator2 as u2
+
+
+class UiElement(object):
+
+    def __init__(self, d: u2.Device, xpath: str = None, description: str = None, debug: bool = True, point: dict = None) -> None:
+        x, y = 0, 0
+        self.info = {}
+        self.d = d
+        self.debug = debug
+
+        if point is not None:
+            self.info['point'] = point
+            self.info['xpath'] = xpath
+            self.info['description'] = description
+            return
+
+        if xpath is not None:
+            self.info['xpath'] = xpath
+            el = d.xpath(xpath)
+            x, y = el.center()
+        elif description is not None:
+            self.info['description'] = description
+            el = d(description=description)
+            x, y = el.center()
+
+        self.info['point'] = {'x': x, 'y': y}
+
+    def log(self, *args, sep=' ', end='\n', file=None):
+        if self.debug:
+            print(args, sep=sep, end=end, file=file)
+
+    def click(self):
+        """
+        点击
+        """
+        point = self.info['point']
+        self.log("点击元素:", self.info)
+        self.d.shell(f"input tap {point['x']} {point['y']}")
+
+    def text(self, text: str = None):
+        """
+        设置文本框内容
+        text 文本内容
+        """
+        point = self.info['point']
+        self.log("点击元素:", self.info)
+        self.d.shell(f"input tap {point['x']} {point['y']}")
+        self.log("设置文本:", text)
+        self.d.shell(f"input text '{text}'")  # 输入文本内容
+
+    def clear(self, length: int = 10):
+        """
+        清空文本框
+        """
+        point = self.info['point']
+        self.log("点击元素:", self.info)
+        self.d.shell(f"input tap {point['x'] + length} {point['y']}")
+        self.log("清空文本框:")
+        # 模拟删除键输入
+        # self.d.shell(f"for i in {{1..{20}}}; do input keyevent KEYCODE_CLEAR; done")  # 20 次删除键事件
+        for _ in range(length):
+            self.d.shell("input keyevent 67")
+
+
+class UiFactory(object):
+    _cache = {}
+
+    def __init__(self, serial: str):
+        """
+        :param serial: 设备序列号。 例如 127.0.0.1:6555
+            可以通过 `adb devices`  获取
+        """
+        self.d = u2.connect(serial)
+        pass
+
+    def xpath(self, alisa: str, xpath: str = None):
+        """
+        通过 xpath获取坐标
+        alisa 别名
+        """
+        if alisa in self._cache:
+            return self._cache[alisa]
+
+        el = UiElement(self.d, xpath=xpath)
+
+        self._cache[alisa] = el
+
+        return el
+
+    def desc(self, alisa: str, desc: str = None):
+        if alisa in self._cache:
+            return self._cache[alisa]
+
+        el = UiElement(self.d, description=desc)
+
+        self._cache[alisa] = el
+
+        return el
+
+    def load_point(self):
+        __tmp__ = {}
+        with open("deepcion_point.json", "r", encoding="utf-8") as f:
+            __tmp__ = json.load(f)
+
+        for k, v in __tmp__.items():
+            if 'xpath' in v:
+                self._cache[k] = UiElement(self.d, xpath=v['xpath'], point=v['point'])
+            elif 'description' in v:
+                self._cache[k] = UiElement(self.d, description=v['description'], point=v['point'])
+
+    def save_point(self):
+        """
+        保存坐标
+        """
+        __tmp__ = {}
+        for k, v in self._cache.items():
+            __tmp__[k] = v.info
+
+        with open("deepcion_point.json", "w", encoding="utf-8") as f:
+            json.dump(__tmp__, f, ensure_ascii=False, indent=4)
+
+    def check_app_running(self, appid: str):
+        # 获取所有运行的APP
+        running_apps = self.d.app_list_running()
+        print("安装列表:", running_apps)
+        # 如果app没有启动, 启动APP
+        if appid not in running_apps:
+            print(f"{appid} 重新启动")
+            self.d.app_start(appid)
+            return True
+        return False