소스 검색

scan 完成

yichael 3 주 전
부모
커밋
2c6c577f78
6개의 변경된 파일84개의 추가작업 그리고 73개의 파일을 삭제
  1. 1 1
      bat-tool/adb-connect-test/adb-connect.bat
  2. 14 0
      bat-tool/test.bat
  3. 22 0
      bat-tool/test.js
  4. 5 24
      nodejs/adb-connect.js
  5. 42 37
      src/page/device/device.js
  6. 0 11
      src/page/device/device.jsx

+ 1 - 1
bat-tool/adb-connect-test/adb-connect.bat

@@ -2,7 +2,7 @@
 chcp 65001 >nul
 title ADB Connect Test
 cd /d "%~dp0"
-node adb-connect.js 192.168.2.5 5555
+node adb-connect.js 192.168.2.1 5555
 if errorlevel 1 (
     echo.
     pause

+ 14 - 0
bat-tool/test.bat

@@ -0,0 +1,14 @@
+@echo off
+chcp 65001 >nul
+title Test
+cd /d "%~dp0"
+node test.js
+if errorlevel 1 (
+    echo.
+    pause
+    exit /b 1
+) else (
+    echo.
+    pause
+    exit /b 0
+)

+ 22 - 0
bat-tool/test.js

@@ -0,0 +1,22 @@
+#!/usr/bin/env node
+const { execSync } = require('child_process')
+const path = require('path')
+
+const projectRoot = path.resolve(__dirname, '..')
+const adbPath = path.join(projectRoot, 'exe', 'adb', 'adb.exe')
+
+const deviceIp = process.argv[2] || '192.168.2.5'
+const devicePort = process.argv[3] || '5555'
+
+const connectCommand = `"${adbPath}" connect ${deviceIp}:${devicePort}`
+const output = execSync(connectCommand, { encoding: 'utf-8' })
+const result = output.trim()
+const isConnected = result.includes('connected') || result.includes('already connected')
+
+if (isConnected) {
+  process.stdout.write(`[OK] Device connected successfully: ${deviceIp}:${devicePort}\n`)
+  process.exit(0)
+} else {
+  process.stdout.write(`[ERROR] Failed to connect to device: ${deviceIp}:${devicePort}\n`)
+  process.exit(1)
+}

+ 5 - 24
nodejs/adb-connect.js

@@ -9,36 +9,17 @@ const deviceIp = process.argv[2]
 const devicePort = process.argv[3] || '5555'
 
 if (!deviceIp) {
-  process.stdout.write('false\n')
   process.exit(1)
 }
 
-// Connect to device
 const connectCommand = `"${adbPath}" connect ${deviceIp}:${devicePort}`
-const connectOutput = execSync(connectCommand, { encoding: 'utf-8' })
-const connectResult = connectOutput.trim()
-const connectSuccess = connectResult.includes('connected') || connectResult.includes('already connected')
+const connectOutput = execSync(connectCommand, { encoding: 'utf-8', timeout: 500 })
+const connectSuccess = connectOutput.trim().includes('connected') || connectOutput.trim().includes('already connected')
 
 if (!connectSuccess) {
-  process.stdout.write('false\n')
   process.exit(1)
 }
-
-// Verify device is actually available in device list
-try {
-  const devicesCommand = `"${adbPath}" devices`
-  const devicesOutput = execSync(devicesCommand, { encoding: 'utf-8' })
-  const deviceAddress = `${deviceIp}:${devicePort}`
-  const isDeviceAvailable = devicesOutput.includes(deviceAddress) && devicesOutput.includes('device')
-  
-  if (isDeviceAvailable) {
-    process.stdout.write('success\n')
-    process.exit(0)
-  } else {
-    process.stdout.write('false\n')
-    process.exit(1)
-  }
-} catch (error) {
-  process.stdout.write('false\n')
-  process.exit(1)
+else
+{
+  process.exit(0)
 }

+ 42 - 37
src/page/device/device.js

@@ -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()

+ 0 - 11
src/page/device/device.jsx

@@ -33,19 +33,8 @@ function Device({ show }) {
     alertView.setShowCallback(setShowAlert)
     comfirmView.setShowCallback(setShowComfirmView)
     
-    // 回调设置完成后,初始化 deviceClass
-    if (deviceClass.current && !deviceClass.current._initialized) {
-      deviceClass.current.init()
-    }
   }, [])
 
-  // 当 setDeviceList 更新时,同步更新 deviceClass 的引用
-  useEffect(() => {
-    if (deviceClass.current) {
-      deviceClass.current.setDeviceList = setDeviceList
-    }
-  }, [setDeviceList])
-
   return (
     <>
       <div className="device-container">