()
| 79 | |
| 80 | // 初始化设备连接 |
| 81 | async function initializeDevice() { |
| 82 | const deviceType = document.getElementById('deviceType').value; |
| 83 | const adbEndpoint = document.getElementById('adbEndpoint').value.trim() || null; |
| 84 | const initBtn = document.getElementById('initDeviceBtn'); |
| 85 | const statusDiv = document.getElementById('deviceStatus'); |
| 86 | const statusText = document.getElementById('deviceStatusText'); |
| 87 | |
| 88 | try { |
| 89 | initBtn.disabled = true; |
| 90 | statusText.textContent = '正在连接...'; |
| 91 | statusDiv.classList.remove('connected', 'disconnected'); |
| 92 | |
| 93 | const response = await fetch('/init_device', { |
| 94 | method: 'POST', |
| 95 | headers: { |
| 96 | 'Content-Type': 'application/json' |
| 97 | }, |
| 98 | body: JSON.stringify({ |
| 99 | device_type: deviceType, |
| 100 | adb_endpoint: adbEndpoint |
| 101 | }) |
| 102 | }); |
| 103 | |
| 104 | if (!response.ok) { |
| 105 | // 处理HTTP错误 |
| 106 | const errorData = await response.json().catch(() => ({})); |
| 107 | const errorMessage = errorData.detail || `连接失败 (HTTP ${response.status})`; |
| 108 | throw new Error(errorMessage); |
| 109 | } |
| 110 | |
| 111 | const result = await response.json(); |
| 112 | |
| 113 | if (result.status === 'success') { |
| 114 | deviceConnected = true; |
| 115 | currentDeviceType = deviceType; |
| 116 | statusDiv.classList.add('connected'); |
| 117 | statusText.textContent = `✅ ${deviceType}设备已连接`; |
| 118 | updateStatus(`${deviceType}设备连接成功!`, 'success'); |
| 119 | |
| 120 | // 启用开始收集按钮 |
| 121 | document.getElementById('startBtn').disabled = false; |
| 122 | } else { |
| 123 | deviceConnected = false; |
| 124 | statusDiv.classList.add('disconnected'); |
| 125 | statusText.textContent = `❌ 连接失败: ${result.message || '未知错误'}`; |
| 126 | updateStatus(`设备连接失败: ${result.message || '未知错误'}`, 'error'); |
| 127 | } |
| 128 | } catch (error) { |
| 129 | deviceConnected = false; |
| 130 | statusDiv.classList.add('disconnected'); |
| 131 | statusText.textContent = `❌ 连接错误: ${error.message}`; |
| 132 | updateStatus(`设备连接错误: ${error.message}`, 'error'); |
| 133 | console.error('设备连接详细错误:', error); |
| 134 | } finally { |
| 135 | initBtn.disabled = false; |
| 136 | } |
| 137 | } |
| 138 |
nothing calls this directly
no test coverage detected