1
0

settings_control.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import logging
  2. import subprocess
  3. from typing import List
  4. import tkinter as tk
  5. from adbutils import adb_path
  6. from control.base.base_control import BaseControl
  7. from control.plat.deepcoin_control import DeepCoinControl
  8. from utils.control_util import loading
  9. class SettingsControl(BaseControl):
  10. def __init__(self, name: str):
  11. """
  12. 设置相关的控制器
  13. :param name:
  14. """
  15. super().__init__(name)
  16. def init_adb(self, commands: List[str], *args, **kwargs):
  17. def func():
  18. adb = adb_path()
  19. logging.info(f'adb_path:{adb}')
  20. log_widget = kwargs.get('log')
  21. _commons = list(commands)
  22. # _commons.append(f'adb devices')
  23. for command in _commons:
  24. command = command.strip()
  25. if command == '':
  26. continue
  27. logging.info(f'初始化[{self.name}]:{command}')
  28. try:
  29. cmd = command.split(' ')
  30. if cmd[1] == 'connect':
  31. self._init_connect(cmd)
  32. cmd[0] = adb
  33. logging.info(f"执行命令:{cmd}")
  34. result = subprocess.run(cmd, capture_output=True, text=True, check=True)
  35. logging.info(f"命令执行结果:\n{result.stdout}")
  36. if log_widget:
  37. log_widget.insert(tk.END, f"命令【{command}】执行成功:\n{result.stdout}")
  38. except subprocess.CalledProcessError as cpe:
  39. logging.info(f"命令 '{command}' 执行失败: {cpe}")
  40. if log_widget:
  41. log_widget.insert(tk.END, f"{cpe}\n")
  42. log_widget.see(tk.END)
  43. self.devices_list(*args, **kwargs)
  44. # loading 相对的元素
  45. top = kwargs['top'] if 'top' in kwargs else None
  46. if top:
  47. loading(top, func)
  48. else:
  49. func()
  50. def _init_connect(self, cmds: List[str]):
  51. """
  52. 初始化一些连接,将信息保存到 connect_dict
  53. :param cmds: 要执行的命令行 ['adb', 'connect', '127.0.0.1:64079']
  54. :return:
  55. """
  56. try:
  57. cmd = list(cmds).pop()
  58. d = self.connect_adb(cmd)
  59. info = d.info
  60. logging.info(f'设备信息{info}')
  61. pkg = info['currentPackageName']
  62. self.connect_dict[cmd] = {
  63. 'd': d,
  64. 'info': info,
  65. 'serial': cmd,
  66. 'status': 'online',
  67. 'pkg': self.app_pkg[pkg],
  68. 'name': str(d.shell('settings get global device_name').output).strip(),
  69. }
  70. if pkg == 'com.niocpeed.dna':
  71. self.connect_dict[cmd]['control'] = DeepCoinControl(serial=cmd, ctx=self.connect_dict[cmd])
  72. except Exception as e:
  73. logging.error(f"无法连接:{e}")
  74. pass
  75. def test_device(self):
  76. """
  77. 测试连接
  78. :return:
  79. """
  80. pass
  81. def devices_list(self, *args, **kwargs) -> List[str]:
  82. """
  83. 获取设备列表
  84. :param command:
  85. :return:
  86. """
  87. log = kwargs.get('log')
  88. command = [adb_path(), 'devices']
  89. logging.info(f"获取设备列表:{command}")
  90. result = subprocess.run(command, capture_output=True, text=True, check=True)
  91. devices = result.stdout.splitlines()
  92. logging.info(f"设备列表:{devices}")
  93. if log:
  94. log.insert(tk.END, f"===检查结果:====\n{result.stdout}\n===============\n")
  95. log.see(tk.END)
  96. a = [x for x in devices if x and ':' in x]
  97. # 去除每一项中的 \tdevice 并且过滤offline数据
  98. a = [x.split('\t')[0] for x in a if x.split('\t')[1] != 'offline']
  99. for k, v in self.connect_dict.items():
  100. if k not in a:
  101. logging.info(f"移除掉线的Key:{k}")
  102. self.connect_dict.pop(k)
  103. return a