const BUSY_DISABLED_BUTTONS = new Set(["btnStart", "btnSave", "btnPauseResume"]); let logIdx = 0; let pollTimer = null; let statsTimer = null; let shotTimer = null; let lastLogUpdateTime = 0; // 最后成功获取日志的时间 let logErrorCount = 0; // 连续错误计数 let emptyResponseCount = 0; // 连续空响应计数(用于检测索引卡住) // 双日志缓冲与过滤 let runLogBuffer = []; let runLogFilter = ""; const runLogMaxLines = UI_LIMITS.runLogMaxLines; let runLogMode = 'filter'; // 'filter' or 'search' let runSearchResultsBuffer = []; let runSearchActive = false; let actionsLogBuffer = []; let actionsLogFilter = ""; const actionsLogMaxLines = UI_LIMITS.actionsLogMaxLines; let deviceOverviewState = { devices: [], adb: "" }; window.addEventListener("DOMContentLoaded", async () => { initTheme(); initTabs(); initConfigSearch(); initFooter(); attachCopyButtons(); initShotFullscreen(); initHeaderScroll(); initKeyboardShortcuts(); initShortcutsHelp(); initStickySectionShadow(); attachNumberSteppers(); applyKbdHints(); startReservationsCountdownTick(); // 首屏统计数字加骨架,待第一次 pollStats 成功后移除 document.querySelectorAll(".stat .statValue").forEach((el) => { if ((el.textContent || "").trim() === "-") el.classList.add("skeleton"); }); $("btnRefresh").addEventListener("click", async () => { setButtonBusy("btnRefresh", true); try { await refreshDevices(); const n = (deviceOverviewState.devices || []).length; toast(n > 0 ? `已刷新 · ${n} 台在线` : "已刷新 · 无在线设备", n > 0 ? "success" : "warn"); } catch (e) { toast("刷新失败:" + (e?.message || e), "error", { duration: 4000 }); } finally { setButtonBusy("btnRefresh", false); } }); $("btnLoad").addEventListener("click", async () => { await loadConfig(); }); $("btnSave").addEventListener("click", async () => { await saveConfig(); }); $("btnStart").addEventListener("click", async () => { await startRun(); }); $("btnPauseResume").addEventListener("click", async () => { await pauseResumeRun(); }); $("btnStop").addEventListener("click", async () => { await stopRun(); }); const deviceSelect = $("device"); if (deviceSelect) deviceSelect.addEventListener("change", updateDeviceOverview); const btnForceStop = $("btnForceStop"); if (btnForceStop) btnForceStop.addEventListener("click", async () => { await forceStopRun(); }); const btnBackKey = $("btnBackKey"); if (btnBackKey) btnBackKey.addEventListener("click", async () => { await sendBackKey(); }); const btnWifiEnable = $("btnWifiEnable"); if (btnWifiEnable) btnWifiEnable.addEventListener("click", async () => { await wifiEnable(); }); const btnWifiConnect = $("btnWifiConnect"); if (btnWifiConnect) btnWifiConnect.addEventListener("click", async () => { await wifiConnect(); }); const btnWifiDisconnect = $("btnWifiDisconnect"); if (btnWifiDisconnect) btnWifiDisconnect.addEventListener("click", async () => { await wifiDisconnect(); }); const btnWifiScan = $("btnWifiScan"); if (btnWifiScan) btnWifiScan.addEventListener("click", async () => { await wifiScan(); }); const btnLicenseRefresh = $("btnLicenseRefresh"); if (btnLicenseRefresh) btnLicenseRefresh.addEventListener("click", async () => { await refreshLicenseStatus(); }); const btnLicenseInstall = $("btnLicenseInstall"); if (btnLicenseInstall) btnLicenseInstall.addEventListener("click", async () => { await installLicenseFromUI(); }); // 截图按钮 $("btnScreenshot").addEventListener("click", async () => { const deviceId = $("device").value || ""; if (!deviceId) { appendLog(["[截图] 请先选择设备"]); return; } try { appendLog(["[截图] 正在截图..."]); const result = await apiPost("/api/screenshot", { device_id: deviceId }); if (result.ok) { appendLog(["[截图] 截图成功,已保存到 pic/screenshot.png"]); // 刷新截图显示 pollShot(); } else { appendLog([`[截图] 截图失败: ${result.error || "未知错误"}`]); } } catch (e) { appendLog([`[截图] 截图失败: ${e?.message || e}`]); } }); // 日志搜索与动作日志相关交互绑定 // 日志搜索与动作日志相关交互绑定 const runModeEl = $("runLogMode"); const runSearchEl = $("runLogSearch"); const runSearchBtn = $("btnRunLogSearch"); const runClear = $("btnRunLogClear"); async function doRunLogSearch() { const q = runSearchEl ? String(runSearchEl.value || '') : ''; if (!q) { runSearchActive = false; runSearchResultsBuffer = []; renderRunLog(); return; } try { const data = await apiGet(`/api/logs?q=${encodeURIComponent(q)}&lines=1000`); runSearchResultsBuffer = Array.isArray(data.lines) ? data.lines : []; runSearchActive = true; renderRunLog(); } catch (e) { console.error('doRunLogSearch error', e); } } if (runModeEl) { runModeEl.addEventListener('change', (e) => { runLogMode = String(e.target.value || 'filter'); runSearchActive = false; renderRunLog(); }); } if (runSearchEl) { runSearchEl.addEventListener('input', (e) => { const v = String(e.target.value || ''); if (runLogMode === 'filter') { runLogFilter = v; renderRunLog(); } }); runSearchEl.addEventListener('keydown', async (e) => { if (e.key === 'Enter' && runLogMode === 'search') { await doRunLogSearch(); } }); } if (runSearchBtn) runSearchBtn.addEventListener('click', async () => { await doRunLogSearch(); }); if (runClear) runClear.addEventListener('click', () => { runLogBuffer = []; runSearchResultsBuffer = []; runSearchActive = false; runLogFilter = ''; if (runSearchEl) runSearchEl.value=''; renderRunLog(); }); syncMirrorFields(); const actionsSearchEl = $("actionsLogSearch"); if (actionsSearchEl) { actionsSearchEl.addEventListener('input', (e) => { actionsLogFilter = String(e.target.value || ''); renderActionsLog(); }); } const actionsRefresh = $("btnActionsRefresh"); if (actionsRefresh) actionsRefresh.addEventListener('click', () => { fetchActionsLog(); }); // 首次加载动作日志 fetchActionsLog(); await refreshDevices(); await refreshLicenseStatus(); await loadConfig(); // 启动时若无设备 + 有上次 WiFi 记录 → 后台自动重连(不阻塞) tryAutoReconnectLastWifi(); startPolling(); startStatsPolling(); startShotPolling(); pollLogs(); pollStats(); pollShot(); pollReservations(); pollHighValueRooms(); }); window.addEventListener("beforeunload", () => { try { if (pollTimer) clearInterval(pollTimer); } catch (e) {} try { if (statsTimer) clearInterval(statsTimer); } catch (e) {} try { if (shotTimer) clearInterval(shotTimer); } catch (e) {} try { if (window.__reservationTickTimer) { clearInterval(window.__reservationTickTimer); window.__reservationTickTimer = null; } } catch (e) {} });