diff --git a/edc-web/app/static/js/test_op.js b/edc-web/app/static/js/test_op.js index 6001a91..04a2d6d 100644 --- a/edc-web/app/static/js/test_op.js +++ b/edc-web/app/static/js/test_op.js @@ -8,6 +8,7 @@ let autoRemaining = 0; let autoStartTime = ""; let localSinceStr = ""; let currentTestMode = null; // 0=灵敏度, 1=波动, null=未加载 +let currentDeviceState = null; // 当前设备状态 (0=离线 1=在线 2=通信不良 null=未加载) let pollInterval = null; let nextCmdTimer = null; // 间隔等待定时器 @@ -18,9 +19,22 @@ let cmdSentAt = 0; // 最近一次发送 0xB0 时间 let intervalMs = 10000; // 默认 10s let timeoutMs = 5000; // 默认 5s +// ─── 设备在线检查 ────────────────────────────── + +function checkDeviceOnline() { + if (currentDeviceState !== 1) { + const stateName = currentDeviceState === 2 ? '通信不良' : + currentDeviceState === 0 ? '离线' : '未知'; + alert(`设备当前状态为「${stateName}」,无法发送指令`); + return false; + } + return true; +} + // ─── 手动指令 ───────────────────────────────── async function sendCmd(cmd) { + if (!checkDeviceOnline()) return; try { const resp = await fetch("/api/command", { method: "POST", @@ -47,6 +61,7 @@ async function toggleAuto() { } async function startAuto() { + if (!checkDeviceOnline()) return; const count = parseInt(document.getElementById("test-count").value) || 10; if (count < 1) return; @@ -309,6 +324,7 @@ async function refreshDeviceStatus() { const resp = await fetch(`/api/devices/${DNT_ID}/status`); const data = await resp.json(); if (data.ok) { + currentDeviceState = data.state; const el = document.getElementById("device-status-text"); if (el) { el.textContent = data.state_name;