fix(osd): ISAPI写入前校验通道存在性,回读验证精确匹配displayText标签

This commit is contained in:
liufumingren 2026-08-26 23:51:12 +08:00
parent a7aae1cda3
commit a6569b5cc3

View File

@ -104,6 +104,17 @@ async function _osdActionIsapi(host, username, password, osdText, posX, posY, mo
return resp;
};
// 0. 通道存在性检查(避免对不存在的通道写入时设备静默成功)
try {
const chListResp = await authedReq('GET', '/ISAPI/System/Video/inputs/channels', null);
const chIds = [...(chListResp.body || '').matchAll(/<VideoInputChannel[^>]*>[\s\S]*?<id>(\d+)<\/id>/g)].map(m => +m[1]);
_log('[ISAPI] 设备通道列表: %s', JSON.stringify(chIds));
if (chIds.length > 0 && !chIds.includes(ch)) {
_err('[ISAPI] 设备不存在通道 %d,现有通道: %s', ch, JSON.stringify(chIds));
return { ok: false, message: `设备不存在通道 ${ch},现有通道: ${chIds.join(',')}` };
}
} catch (_) { /* 通道列表查询失败则继续,由后续写入结果决定 */ }
// 1. 读取现有 OSD
const getResp = await authedReq('GET', baseUri, null);
_log('[ISAPI] GET overlays status=%s body=%s', getResp.status, (getResp.body || '').slice(0, 500));
@ -162,14 +173,15 @@ async function _osdActionIsapi(host, username, password, osdText, posX, posY, mo
return { ok: false, message: 'ISAPI 设置 OSD 失败: HTTP ' + putResp.status + ' ' + (putResp.body || '').slice(0, 100) };
}
// 3. 回读验证
// 3. 回读验证(精确匹配 displayText 标签,避免短文本如 "1" 误命中 id/version 字段)
const verifyResp = await authedReq('GET', baseUri, null);
const body = verifyResp.body || '';
_log('[ISAPI] verify GET status=%s body=%s', verifyResp.status, body.slice(0, 500));
if (mode === 'clear') {
return { ok: true, message: 'OSD 已清空 (ISAPI)', count: 0 };
}
const found = body.includes(_xmlEscape(osdText)) || body.includes(osdText);
const escapedText = _xmlEscape(osdText);
const found = body.includes(`<displayText>${escapedText}</displayText>`);
if (found) {
_log('[ISAPI] OK OSD 写入成功:', osdText);
return { ok: true, message: 'OSD 设置成功 (ISAPI): ' + osdText, count: 1 };