device.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 显示灯色:grey 未执行/已停止 | 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. export function setExecutionStatus(running, executingIps = [], failedIps = []) {
  14. _executionStatus = { running, executingIps: executingIps || [], failedIps: failedIps || [] }
  15. _setExecutionStatusCallback?.({ ..._executionStatus })
  16. }
  17. // 设备管理类,所有方法都可以通过 this. 访问属性
  18. class DeviceClass {
  19. constructor() {}
  20. async init(setDeviceList, inputValue, setSelectedDevice, setInputValue)
  21. {
  22. this.setDeviceList = setDeviceList
  23. this.inputValue = inputValue
  24. this.setSelectedDevice = setSelectedDevice
  25. this.setInputValue = setInputValue
  26. this.count_ip_x = 0
  27. this.count_ip_y = 0
  28. let readResult = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
  29. if (readResult.stdout === '') {
  30. await window.electronAPI.runNodejsScript('json-parser', 'create', 'device_list.json', JSON.stringify({devices: []}))
  31. }
  32. else
  33. {
  34. const jsonData = JSON.parse(readResult.stdout)
  35. this.setDeviceList([...jsonData.data.devices])//react 替换新值要用...
  36. }
  37. }
  38. /** 执行开启无线连接脚本并根据结果提示 */
  39. async onEnableWirlessConnect() {
  40. const result = await window.electronAPI.runNodejsScript('enable-wirless-connect')
  41. const noDevice = (result.stderr || '').includes('No devices') || (result.stdout || '').includes('No devices')
  42. if (result.exitCode === 0) {
  43. hintView.setContent('开启手机无线连接成功')
  44. hintView.show()
  45. return
  46. }
  47. if (noDevice) {
  48. hintView.setContent('未检测到设备,请先用 USB 连接手机')
  49. hintView.show()
  50. return
  51. }
  52. const raw = (result.stderr || result.stdout || '').trim().split('\n')[0] || '开启无线连接失败'
  53. const msg = (raw === 'error: closed' || raw.includes('closed'))
  54. ? 'ADB 连接已断开,请检查 USB 连接后重试'
  55. : raw
  56. hintView.setContent(msg)
  57. hintView.show()
  58. }
  59. async onAddDevice() {
  60. const ip = this.inputValue;
  61. const ipList = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
  62. const jsonData = JSON.parse(ipList.stdout)
  63. const devices = jsonData.data.devices
  64. if (devices.includes(ip)) {
  65. hintView.setContent('设备已存在')
  66. hintView.show()
  67. return
  68. }
  69. this.setSelectedDevice(prev => [...(prev || []), ip])
  70. await this.addDevice(ip, this.currentDeviceList || [])
  71. this.setInputValue?.('192.168.')
  72. }
  73. async addDevice(ip, currentList = []) {
  74. const newArr = [...currentList, ip]
  75. this.setDeviceList(newArr)
  76. await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({ devices: newArr }))
  77. }
  78. async onRemoveDevice(ip) {
  79. comfirmView.show()
  80. comfirmView.setContent('确定要删除设备吗?')
  81. comfirmView.onConfirm = async () => {
  82. comfirmView.hide()
  83. let newArr = null
  84. this.setDeviceList(prev => {
  85. newArr = [...prev.filter(deviceIp => deviceIp !== ip)]
  86. return newArr
  87. })
  88. await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: newArr}))
  89. }
  90. comfirmView.onCancel = () => {
  91. comfirmView.hide()
  92. }
  93. }
  94. }
  95. // 导出类,由组件创建实例并管理生命周期
  96. export { DeviceClass}