| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import comfirmView from '../public/comfirm-view/comfirm-view.js'
- import hintView from '../public/hint-view/hint-view.js'
- import alertView from '../public/alert-view/alert-view.js'
- // 模块级 store,供 ProcessItemClass 等普通类读取
- let _selectedDevices = []
- export function getSelectedDevices() { return _selectedDevices }
- export function setSelectedDevicesStore(devices) { _selectedDevices = devices || [] }
- // 脚本执行状态,供 ConnectItem 显示灯色:grey 未执行/已停止 | green 执行中 | red 执行失败
- let _executionStatus = { running: false, executingIps: [], failedIps: [] }
- let _setExecutionStatusCallback = null
- export function getExecutionStatus() { return _executionStatus }
- export function setExecutionStatusCallback(cb) { _setExecutionStatusCallback = cb }
- export function setExecutionStatus(running, executingIps = [], failedIps = []) {
- _executionStatus = { running, executingIps: executingIps || [], failedIps: failedIps || [] }
- _setExecutionStatusCallback?.({ ..._executionStatus })
- }
- // 设备管理类,所有方法都可以通过 this. 访问属性
- class DeviceClass {
- constructor() {}
- async init(setDeviceList, inputValue, setSelectedDevice, setInputValue)
- {
- this.setDeviceList = setDeviceList
- this.inputValue = inputValue
- this.setSelectedDevice = setSelectedDevice
- this.setInputValue = setInputValue
- this.count_ip_x = 0
- this.count_ip_y = 0
- let readResult = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
- if (readResult.stdout === '') {
- await window.electronAPI.runNodejsScript('json-parser', 'create', 'device_list.json', JSON.stringify({devices: []}))
- }
- else
- {
- const jsonData = JSON.parse(readResult.stdout)
- this.setDeviceList([...jsonData.data.devices])//react 替换新值要用...
- }
- }
- /** 执行开启无线连接脚本并根据结果提示 */
- async onEnableWirlessConnect() {
- const result = await window.electronAPI.runNodejsScript('enable-wirless-connect')
- const noDevice = (result.stderr || '').includes('No devices') || (result.stdout || '').includes('No devices')
- if (result.exitCode === 0) {
- hintView.setContent('开启手机无线连接成功')
- hintView.show()
- return
- }
- if (noDevice) {
- hintView.setContent('未检测到设备,请先用 USB 连接手机')
- hintView.show()
- return
- }
- const raw = (result.stderr || result.stdout || '').trim().split('\n')[0] || '开启无线连接失败'
- const msg = (raw === 'error: closed' || raw.includes('closed'))
- ? 'ADB 连接已断开,请检查 USB 连接后重试'
- : raw
- hintView.setContent(msg)
- hintView.show()
- }
-
- async onAddDevice() {
- const ip = this.inputValue;
- const ipList = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')
- const jsonData = JSON.parse(ipList.stdout)
- const devices = jsonData.data.devices
- if (devices.includes(ip)) {
-
- hintView.setContent('设备已存在')
- hintView.show()
- return
- }
- this.setSelectedDevice(prev => [...(prev || []), ip])
- await this.addDevice(ip, this.currentDeviceList || [])
- this.setInputValue?.('192.168.')
- }
- async addDevice(ip, currentList = []) {
- const newArr = [...currentList, ip]
- this.setDeviceList(newArr)
- await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({ devices: newArr }))
- }
- async onRemoveDevice(ip) {
- comfirmView.show()
- comfirmView.setContent('确定要删除设备吗?')
- comfirmView.onConfirm = async () => {
- comfirmView.hide()
-
- let newArr = null
- this.setDeviceList(prev => {
- newArr = [...prev.filter(deviceIp => deviceIp !== ip)]
- return newArr
- })
- await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: newArr}))
- }
- comfirmView.onCancel = () => {
- comfirmView.hide()
- }
- }
- }
- // 导出类,由组件创建实例并管理生命周期
- export { DeviceClass}
|