diff --git a/utils/cameraOsd.js b/utils/cameraOsd.js index d0fef08..1c8ed92 100644 --- a/utils/cameraOsd.js +++ b/utils/cameraOsd.js @@ -81,12 +81,9 @@ export async function clearCameraOSD(rtspUrl) { * @returns {Promise<{ok: boolean, message: string}>} */ async function _osdActionIsapi(host, username, password, osdText, posX, posY, mode, channel = 1) { - _log('[ISAPI] 开始设置 OSD host=%s mode=%s channel=%d', host, mode, channel); + _log('[ISAPI] 开始设置 OSD host=%s mode=%s', host, mode); try { - const ch = Number.isInteger(channel) && channel >= 1 ? channel : 1; - const baseUri = `/ISAPI/System/Video/inputs/channels/${ch}/overlays/text`; - const mainUri = `/ISAPI/System/Video/inputs/channels/${ch}/overlays`; - let auth = await _isapiAuth(host, username, password, baseUri); + let auth = await _isapiAuth(host, username, password, '/ISAPI/System/Video/inputs/channels/1/overlays/text'); if (!auth) { return { ok: false, message: 'ISAPI 认证失败(Basic/Digest 均不可用)' }; } @@ -95,7 +92,7 @@ async function _osdActionIsapi(host, username, password, osdText, posX, posY, mo const authedReq = async (method, uri, body) => { let resp = await _isapiRequest(host, method, uri, body, auth.headers(method, uri)); if (resp.status === 401) { - const newAuth = await _isapiAuth(host, username, password, baseUri); + const newAuth = await _isapiAuth(host, username, password, '/ISAPI/System/Video/inputs/channels/1/overlays/text'); if (newAuth) { auth = newAuth; resp = await _isapiRequest(host, method, uri, body, auth.headers(method, uri)); @@ -104,96 +101,121 @@ async function _osdActionIsapi(host, username, password, osdText, posX, posY, mo return resp; }; - // 0. 通道存在性检查(避免对不存在的通道写入时设备静默成功) + // 嗅探设备全部通道(不依赖 rtspUrl 的通道号):全部通道都设置 + let channels = []; try { const chListResp = await authedReq('GET', '/ISAPI/System/Video/inputs/channels', null); const chIds = [...(chListResp.body || '').matchAll(/]*>[\s\S]*?(\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)); - - if (mode === 'clear') { - // 清空:提交空列表 - const emptyXml = '\n' + - ''; - const putResp = await authedReq('PUT', baseUri, emptyXml); - _log('[ISAPI] clear PUT status=%s body=%s', putResp.status, (putResp.body || '').slice(0, 300)); - if (putResp.status >= 200 && putResp.status < 300) { - return { ok: true, message: 'OSD 已清空 (ISAPI)', count: 0 }; - } - return { ok: false, message: 'ISAPI 清空 OSD 失败: HTTP ' + putResp.status }; + if (chIds.length > 0) channels = chIds; + } catch (_) { /* 枚举失败则回退单通道 */ } + if (channels.length === 0) { + const ch = Number.isInteger(channel) && channel >= 1 ? channel : 1; + channels = [ch]; + _log('[ISAPI] 通道枚举失败,回退单通道 %d', ch); + } else { + _log('[ISAPI] 设备通道列表: %s,将对全部通道执行 %s', JSON.stringify(channels), mode); } - // 2. 设置文字:设备字段兼容 positionX/positionY/displayText(回读确认过) - // 坐标系:从 overlays 总接口读 normalizedScreenSize(实测 GV413 为 704x576, - // 与主码流 1920x1080 无关)。Y 轴方向:实测该设备 positionY 小=靠底部(bottom-up), - // 因此 ONVIF 归一化 posY(-1=下, +1=上)映射为 py = (1-posY)/2*H - let width = 704, height = 576; - try { - const mainResp = await authedReq('GET', mainUri, null); - const wm = /(\d+)(\d+)\n' + - '\n' + - '\n1\ntrue\n' + - `${px}\n${py}\n` + - `${_xmlEscape(osdText)}\n` + - 'true\n' + - '\n'; - putResp = await authedReq('PUT', baseUri, xml); - _log('[ISAPI] set PUT(%d,%d) status=%s body=%s', px, py, putResp.status, (putResp.body || '').slice(0, 200)); - if (putResp.status >= 200 && putResp.status < 300) break; + // 逐通道执行 + const results = []; + for (const c of channels) { + const r = await _osdChannelIsapi(authedReq, c, osdText, posX, posY, mode); + results.push(r); } - if (!(putResp.status >= 200 && putResp.status < 300)) { - return { ok: false, message: 'ISAPI 设置 OSD 失败: HTTP ' + putResp.status + ' ' + (putResp.body || '').slice(0, 100) }; + const okList = results.filter(r => r.ok); + const failList = results.filter(r => !r.ok); + if (okList.length === 0) { + return { ok: false, message: 'ISAPI 所有通道操作失败: ' + failList.map(r => `ch${r.channel}: ${r.message}`).join('; ') }; } - - // 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 escapedText = _xmlEscape(osdText); - const found = body.includes(`${escapedText}`); - if (found) { - _log('[ISAPI] OK OSD 写入成功:', osdText); - return { ok: true, message: 'OSD 设置成功 (ISAPI): ' + osdText, count: 1 }; - } - _err('[ISAPI] FAIL 回读验证失败: 期望="' + osdText + '" body=' + body.slice(0, 300)); - return { ok: false, message: 'ISAPI OSD 写入验证失败' }; + const msg = mode === 'clear' + ? `OSD 已清空 (ISAPI),通道 ${okList.map(r => r.channel).join(',')}` + : `OSD 设置成功 (ISAPI): ${osdText},通道 ${okList.map(r => r.channel).join(',')}`; + _log('[ISAPI] 汇总: %s', msg); + const out = { ok: true, message: msg, count: okList.length }; + if (failList.length) out.partialFail = failList.map(r => `ch${r.channel}: ${r.message}`); + return out; } catch (e) { _err('[ISAPI] 异常:', e.message); return { ok: false, message: 'ISAPI OSD 操作异常: ' + e.message }; } } +/** + * 对单个通道执行 OSD 设置/清空 + * @returns {Promise<{ok: boolean, message: string, channel: number}>} + */ +async function _osdChannelIsapi(authedReq, ch, osdText, posX, posY, mode) { + const baseUri = `/ISAPI/System/Video/inputs/channels/${ch}/overlays/text`; + const mainUri = `/ISAPI/System/Video/inputs/channels/${ch}/overlays`; + + // 1. 读取现有 OSD + const getResp = await authedReq('GET', baseUri, null); + _log('[ISAPI] ch%d GET overlays status=%s body=%s', ch, getResp.status, (getResp.body || '').slice(0, 300)); + + if (mode === 'clear') { + // 清空:提交空列表 + const emptyXml = '\n' + + ''; + const putResp = await authedReq('PUT', baseUri, emptyXml); + _log('[ISAPI] ch%d clear PUT status=%s body=%s', ch, putResp.status, (putResp.body || '').slice(0, 200)); + if (putResp.status >= 200 && putResp.status < 300) { + _log('[ISAPI] ch%d 已清空', ch); + return { ok: true, message: '已清空', channel: ch }; + } + return { ok: false, message: '清空失败: HTTP ' + putResp.status, channel: ch }; + } + + // 2. 设置文字:坐标系从 overlays 总接口读 normalizedScreenSize(实测 GV413 为 704x576, + // 与主码流分辨率无关)。Y 轴 bottom-up:ONVIF posY(-1=下,+1=上)→ py = (1-posY)/2*H + let width = 704, height = 576; + try { + const mainResp = await authedReq('GET', mainUri, null); + const wm = /(\d+)(\d+)\n' + + '\n' + + '\n1\ntrue\n' + + `${px}\n${py}\n` + + `${_xmlEscape(osdText)}\n` + + 'true\n' + + '\n'; + putResp = await authedReq('PUT', baseUri, xml); + _log('[ISAPI] ch%d set PUT(%d,%d) status=%s body=%s', ch, px, py, putResp.status, (putResp.body || '').slice(0, 160)); + if (putResp.status >= 200 && putResp.status < 300) break; + } + if (!(putResp.status >= 200 && putResp.status < 300)) { + return { ok: false, message: '设置失败: HTTP ' + putResp.status, channel: ch }; + } + + // 3. 回读验证(精确匹配 displayText 标签,避免短文本误命中) + const verifyResp = await authedReq('GET', baseUri, null); + const body = verifyResp.body || ''; + _log('[ISAPI] ch%d verify GET status=%s body=%s', ch, verifyResp.status, body.slice(0, 300)); + const escapedText = _xmlEscape(osdText); + if (body.includes(`${escapedText}`)) { + _log('[ISAPI] ch%d OK OSD 写入成功: %s', ch, osdText); + return { ok: true, message: '写入成功', channel: ch }; + } + _err('[ISAPI] ch%d FAIL 回读验证失败: 期望="%s" body=%s', ch, osdText, body.slice(0, 300)); + return { ok: false, message: '回读验证失败', channel: ch }; +} + function _xmlEscape(s) { return String(s ?? '') .replace(/&/g, '&')