1
0

settings_control.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. class SettingsControl(BaseControl):
  9. def __init__(self, name: str):
  10. """
  11. 设置相关的控制器
  12. :param name:
  13. """
  14. super().__init__(name)
  15. def init_adb(self, commands: List[str], *args, **kwargs):
  16. def func():
  17. adb = adb_path()
  18. logging.info(f'adb_path:{adb}')
  19. log_widget = args[0] if args else None
  20. _commons = list(commands)
  21. _commons.append(f'adb devices')
  22. for command in _commons:
  23. command = command.strip()
  24. if command == '':
  25. continue
  26. logging.info(f'初始化[{self.name}]:{command}')
  27. try:
  28. cmd = command.split(' ')
  29. if cmd[1] == 'connect':
  30. self._init_connect(cmd)
  31. cmd[0] = adb
  32. logging.info(f"执行命令:{cmd}")
  33. result = subprocess.run(cmd, capture_output=True, text=True, check=True)
  34. logging.info(f"命令执行结果:\n{result.stdout}")
  35. if log_widget:
  36. log_widget.insert(tk.END, f"命令【{command}】执行成功:\n{result.stdout}")
  37. except subprocess.CalledProcessError as cpe:
  38. logging.info(f"命令 '{command}' 执行失败: {cpe}")
  39. if log_widget:
  40. log_widget.insert(tk.END, f"{cpe}\n")
  41. top = kwargs['ui']
  42. if top:
  43. self.loadding(top, func)
  44. else:
  45. func()
  46. def _init_connect(self, cmds: List[str]):
  47. """
  48. 初始化一些连接,将信息保存到 connect_dict
  49. :param cmds: 要执行的命令行 ['adb', 'connect', '127.0.0.1:64079']
  50. :return:
  51. """
  52. cmd = list(cmds).pop()
  53. d = self.connect_adb(cmd)
  54. info = d.info
  55. logging.info(f'设备信息{info}')
  56. pkg = info['currentPackageName']
  57. self.connect_dict[cmd] = {
  58. 'd': d,
  59. 'info': info,
  60. 'serial': cmd,
  61. 'status': 'online',
  62. 'pkg': self.app_pkg[pkg],
  63. 'name': str(d.shell('settings get global device_name').output).strip(),
  64. }
  65. if pkg == 'com.niocpeed.dna':
  66. self.connect_dict[cmd]['control'] = DeepCoinControl(serial=cmd, ctx=self.connect_dict[cmd])
  67. pass
  68. def devices_list(self) -> List[str]:
  69. """
  70. 获取设备列表
  71. :param command:
  72. :return:
  73. """
  74. command = [adb_path(), 'devices']
  75. logging.info(f"获取设备列表:{command}")
  76. result = subprocess.run(command, capture_output=True, text=True, check=True)
  77. devices = result.stdout.splitlines()
  78. logging.info(f"设备列表:{devices}")
  79. a = [x for x in devices if x and ':' in x]
  80. # 去除每一项中的 \tdevice
  81. a = [x.split('\t')[0] for x in a]
  82. return a