feat: 测试操作页非在线状态禁止发送指令
- 新增 currentDeviceState 变量跟踪设备状态 - refreshDeviceStatus 同步更新 currentDeviceState - sendCmd/manual cmds(B0/B1/BA/BB/BC) 调用前检查在线状态 - startAuto(自动化开始) 调用前检查在线状态 - 非在线时弹窗提示「设备当前状态为「离线/通信不良」,无法发送指令」
This commit is contained in:
@@ -8,6 +8,7 @@ let autoRemaining = 0;
|
|||||||
let autoStartTime = "";
|
let autoStartTime = "";
|
||||||
let localSinceStr = "";
|
let localSinceStr = "";
|
||||||
let currentTestMode = null; // 0=灵敏度, 1=波动, null=未加载
|
let currentTestMode = null; // 0=灵敏度, 1=波动, null=未加载
|
||||||
|
let currentDeviceState = null; // 当前设备状态 (0=离线 1=在线 2=通信不良 null=未加载)
|
||||||
|
|
||||||
let pollInterval = null;
|
let pollInterval = null;
|
||||||
let nextCmdTimer = null; // 间隔等待定时器
|
let nextCmdTimer = null; // 间隔等待定时器
|
||||||
@@ -18,9 +19,22 @@ let cmdSentAt = 0; // 最近一次发送 0xB0 时间
|
|||||||
let intervalMs = 10000; // 默认 10s
|
let intervalMs = 10000; // 默认 10s
|
||||||
let timeoutMs = 5000; // 默认 5s
|
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) {
|
async function sendCmd(cmd) {
|
||||||
|
if (!checkDeviceOnline()) return;
|
||||||
try {
|
try {
|
||||||
const resp = await fetch("/api/command", {
|
const resp = await fetch("/api/command", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -47,6 +61,7 @@ async function toggleAuto() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function startAuto() {
|
async function startAuto() {
|
||||||
|
if (!checkDeviceOnline()) return;
|
||||||
const count = parseInt(document.getElementById("test-count").value) || 10;
|
const count = parseInt(document.getElementById("test-count").value) || 10;
|
||||||
if (count < 1) return;
|
if (count < 1) return;
|
||||||
|
|
||||||
@@ -309,6 +324,7 @@ async function refreshDeviceStatus() {
|
|||||||
const resp = await fetch(`/api/devices/${DNT_ID}/status`);
|
const resp = await fetch(`/api/devices/${DNT_ID}/status`);
|
||||||
const data = await resp.json();
|
const data = await resp.json();
|
||||||
if (data.ok) {
|
if (data.ok) {
|
||||||
|
currentDeviceState = data.state;
|
||||||
const el = document.getElementById("device-status-text");
|
const el = document.getElementById("device-status-text");
|
||||||
if (el) {
|
if (el) {
|
||||||
el.textContent = data.state_name;
|
el.textContent = data.state_name;
|
||||||
|
|||||||
Reference in New Issue
Block a user