device.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import comfirmView from '../public/comfirm-view/comfirm-view.js'
  2. import hintView from '../public/hint-view/hint-view.js'
  3. import alertView from '../public/alert-view/alert-view.js'
  4. // 模块级 store,供 ProcessItemClass 等普通类读取
  5. let _selectedDevices = []
  6. export function getSelectedDevices() { return _selectedDevices }
  7. export function setSelectedDevicesStore(devices) { _selectedDevices = devices || [] }
  8. // 脚本执行状态,供 ConnectItem 显示灯色:gray 未执行/已停止 | green 执行中 | red 执行失败
  9. let _executionStatus = { running: false, executingIps: [], failedIps: [] }
  10. let _setExecutionStatusCallback = null
  11. export function getExecutionStatus() { return _executionStatus }
  12. export function setExecutionStatusCallback(cb) { _setExecutionStatusCallback = cb }
  13. /**
  14. * 设置设备执行状态灯色(只传一个参数 color,red 时可选传失败 IP 列表)
  15. * @param {'gray'|'green'|'red'} color
  16. * @param {string[]} [failedIps] - 仅当 color === 'red' 时传入
  17. */
  18. export function setExecutionStatus(color, failedIps = []) {
  19. const normalized = (color || 'gray').toLowerCase()
  20. const failed = Array.isArray(failedIps) ? [...failedIps] : []
  21. let running = false
  22. let executingIps = []
  23. if (normalized === 'green') {
  24. running = true
  25. executingIps = [...getSelectedDevices()]
  26. } else if (normalized === 'red') {
  27. // failedIps 已取自上参
  28. }
  29. _executionStatus = { running, executingIps, failedIps: failed }
  30. const payload = { running, executingIps: [...executingIps], failedIps: [...failed] }
  31. _setExecutionStatusCallback?.(payload)
  32. }
  33. // 设备管理类,所有方法都可以通过 this. 访问属性
  34. class DeviceClass {
  35. constructor() {}
  36. async init(setDeviceList, inputValue, setSelectedDevice, setInputValue)
  37. {
  38. this.setDeviceList = setDeviceList
  39. this.inputValue = inputValue
  40. this.setSelectedDevice = setSelectedDevice
  41. this.setInputValue = setInputValue
  42. this.count_ip_x = 0
  43. this.count_ip_y = 0
  44. let readResult = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
  45. if (readResult.stdout === '') {
  46. await window.electronAPI.runNodejsScript('json-parser', 'create', 'device_list.json', JSON.stringify({devices: []}))
  47. }
  48. else
  49. {
  50. const jsonData = JSON.parse(readResult.stdout)
  51. this.setDeviceList([...jsonData.data.devices])//react 替换新值要用...
  52. }
  53. }
  54. /** 执行开启无线连接脚本并根据结果提示 */
  55. async onEnableWirlessConnect() {
  56. const result = await window.electronAPI.runNodejsScript('enable-wirless-connect')
  57. const noDevice = (result.stderr || '').includes('No devices') || (result.stdout || '').includes('No devices')
  58. if (result.exitCode === 0) {
  59. hintView.setContent('开启手机无线连接成功')
  60. hintView.show()
  61. return
  62. }
  63. if (noDevice) {
  64. hintView.setContent('未检测到设备,请先用 USB 连接手机')
  65. hintView.show()
  66. return
  67. }
  68. const raw = (result.stderr || result.stdout || '').trim().split('\n')[0] || '开启无线连接失败'
  69. const msg = (raw === 'error: closed' || raw.includes('closed'))
  70. ? 'ADB 连接已断开,请检查 USB 连接后重试'
  71. : raw
  72. hintView.setContent(msg)
  73. hintView.show()
  74. }
  75. async onAddDevice() {
  76. const ip = this.inputValue;
  77. const ipList = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
  78. const jsonData = JSON.parse(ipList.stdout)
  79. const devices = jsonData.data.devices
  80. if (devices.includes(ip)) {
  81. hintView.setContent('设备已存在')
  82. hintView.show()
  83. return
  84. }
  85. this.setSelectedDevice(prev => [...(prev || []), ip])
  86. await this.addDevice(ip, this.currentDeviceList || [])
  87. this.setInputValue?.('192.168.')
  88. }
  89. async addDevice(ip, currentList = []) {
  90. const newArr = [...currentList, ip]
  91. this.setDeviceList(newArr)
  92. await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({ devices: newArr }))
  93. }
  94. async onRemoveDevice(ip) {
  95. comfirmView.show()
  96. comfirmView.setContent('确定要删除设备吗?')
  97. comfirmView.onConfirm = async () => {
  98. comfirmView.hide()
  99. let newArr = null
  100. this.setDeviceList(prev => {
  101. newArr = [...prev.filter(deviceIp => deviceIp !== ip)]
  102. return newArr
  103. })
  104. await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: newArr}))
  105. }
  106. comfirmView.onCancel = () => {
  107. comfirmView.hide()
  108. }
  109. }
  110. }
  111. // 导出类,由组件创建实例并管理生命周期
  112. export { DeviceClass}