|
|
@@ -5,8 +5,9 @@ import alertView from '../public/alert-view/alert-view.js'
|
|
|
// 设备管理类,所有方法都可以通过 this. 访问属性
|
|
|
class DeviceClass {
|
|
|
constructor(setDeviceList) {
|
|
|
- this.deviceArr = []
|
|
|
this.setDeviceList = setDeviceList
|
|
|
+ this.count_ip_x = 0
|
|
|
+ this.count_ip_y = 0
|
|
|
}
|
|
|
|
|
|
async init() {
|
|
|
@@ -15,41 +16,45 @@ class DeviceClass {
|
|
|
|
|
|
if (readResult.stdout === '') {
|
|
|
await window.electronAPI.runNodejsScript('json-parser', 'create', 'device_list.json', JSON.stringify({devices: []}))
|
|
|
- if (this.setDeviceList) {
|
|
|
- this.setDeviceList([])
|
|
|
- }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
const jsonData = JSON.parse(readResult.stdout)
|
|
|
- this.deviceArr = jsonData.data.devices
|
|
|
- if (this.setDeviceList) {
|
|
|
- this.setDeviceList(this.deviceArr)
|
|
|
- }
|
|
|
+ this.setDeviceList([...jsonData.data.devices])//react 替换新值要用...
|
|
|
}
|
|
|
}
|
|
|
|
|
|
async handleRefresh(e, self) {
|
|
|
self.startAnimation()
|
|
|
+ this.count_ip_x = 2
|
|
|
+ this.count_ip_y = 1
|
|
|
|
|
|
+ let that = this;
|
|
|
this.scanDevice(() => {
|
|
|
self.stopAnimation()
|
|
|
- });
|
|
|
+ }, that)
|
|
|
}
|
|
|
|
|
|
- async scanDevice(callback)
|
|
|
- {
|
|
|
- for (let i = 0; i <= 3; i++) {
|
|
|
- for (let j = 0; j <= 255; j++)
|
|
|
- {
|
|
|
- const ip = `192.168.${i}.${j}`
|
|
|
- const result = await window.electronAPI.runNodejsScript('adb-connect', ip, '5555')
|
|
|
- if (result.stdout == 'success') {
|
|
|
- await this.addDevice(ip)
|
|
|
- }
|
|
|
- }
|
|
|
+ async scanDevice(callback, that) {
|
|
|
+
|
|
|
+ if (that.count_ip_y > 255) {
|
|
|
+ callback()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ const ip = `192.168.${that.count_ip_x}.${that.count_ip_y}`
|
|
|
+ const result = await window.electronAPI.runNodejsScript('adb-connect', ip, '5555')
|
|
|
+
|
|
|
+ if (result.exitCode === 0) {
|
|
|
+ that.setDeviceList(prev => [...prev, ip])
|
|
|
+
|
|
|
+ console.log(`连接成功: ${ip}`)
|
|
|
+ } else {
|
|
|
+ console.log(`连接失败: ${ip}`)
|
|
|
}
|
|
|
- callback();
|
|
|
+
|
|
|
+ that.count_ip_y++
|
|
|
+ await this.scanDevice(callback, that)
|
|
|
}
|
|
|
|
|
|
async handleAdd() {
|
|
|
@@ -57,24 +62,25 @@ class DeviceClass {
|
|
|
const ip = document.querySelector('.ip-input input').value
|
|
|
const result = await window.electronAPI.runNodejsScript('adb-connect', ip, '5555')
|
|
|
|
|
|
- if(result.stdout == 'success') {
|
|
|
+ if (result.exitCode === 0) {
|
|
|
hintView.setContent('设备添加成功')
|
|
|
hintView.show()
|
|
|
|
|
|
- await this.addDevice(ip);
|
|
|
+ await this.addDevice(ip)
|
|
|
|
|
|
- return;
|
|
|
+ return
|
|
|
}
|
|
|
alertView.show()
|
|
|
alertView.setContent('无法检测到连接设备')
|
|
|
}
|
|
|
|
|
|
- async addDevice(ip) {
|
|
|
- // 数组中是字符串,直接 push IP 字符串
|
|
|
- this.deviceArr.push(ip)
|
|
|
- await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: this.deviceArr}))
|
|
|
-
|
|
|
- this.setDeviceList(this.deviceArr);
|
|
|
+ async addDevice(ip) {
|
|
|
+ let newArr = null
|
|
|
+ this.setDeviceList(prev => {
|
|
|
+ newArr = [...prev, ip]
|
|
|
+ return newArr
|
|
|
+ })
|
|
|
+ await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: newArr}))
|
|
|
}
|
|
|
|
|
|
async removeDevice(ip) {
|
|
|
@@ -85,13 +91,12 @@ class DeviceClass {
|
|
|
comfirmView.onConfirm = async () => {
|
|
|
comfirmView.hide()
|
|
|
|
|
|
- // 从数组中移除指定 IP(deviceIp 是数组中的每个字符串元素)
|
|
|
- this.deviceArr = this.deviceArr.filter(deviceIp => deviceIp !== ip)
|
|
|
-
|
|
|
- // 更新 JSON 文件
|
|
|
- await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: this.deviceArr}))
|
|
|
-
|
|
|
- this.setDeviceList(this.deviceArr)
|
|
|
+ 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()
|