From fd2b1caf8bc3dcbe2fca41644e3e5f474fa417ce Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 16 Sep 2026 17:37:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(camera):=20ONVIF=20=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E9=98=B6=E6=AE=B5=E7=9A=84=E5=A4=B1=E8=B4=A5=E4=B9=9F=E7=BA=B3?= =?UTF-8?q?=E5=85=A5=E9=87=8D=E8=AF=95=EF=BC=88=E5=B9=B6=E6=8A=8A=E6=A0=B9?= =?UTF-8?q?=E5=9B=A0=E5=B8=A6=E8=BF=9B=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 排查"往下不行"时发现:设备被高频点时会对 ONVIF 认证瞬时拒绝 (`SOAP Fault: Authority failure`,实测 8.85/8.86 都会偶发,几十秒后自恢复), 但这类失败发生在【连接阶段】,而原实现是 `if (!sess) return {ok:false,...}`—— 不抛异常,重试包装根本拦不到,于是直接判失败。 改动: - _onvifSession 记录每个 host 最近一次连接失败原因(超时/异常/取不到 profileToken) - 连接失败改为 throw,并把根因拼进错误信息(否则重试逻辑按文案匹配不到"偶发") - `Authority failure` 纳入偶发错误集合(配合 300ms/800ms 递增退避, 给设备留出从限流中恢复的时间,比快速重试更有效) - 退避从 150/300ms 调整为 300/800ms 验证:错密码场景耗时 1242ms(3 次尝试后失败,错误信息含 Authority failure); 正确密码 138ms 成功;离线回归 46 断言全通过。 注意副作用:凭据真的错时也会多花约 1.1s 重试才报错——这是刻意取舍, 因为高频下把"限流"误判成"密码错"会让云台整个命令失效。 --- utils/cameraPtz.js | 56 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/utils/cameraPtz.js b/utils/cameraPtz.js index 42d3db7..c1b07c9 100644 --- a/utils/cameraPtz.js +++ b/utils/cameraPtz.js @@ -494,10 +494,22 @@ const _protoCache = new Map(); /** host → { cam, profileToken, port }(ONVIF 会话复用;库内部每次请求新建 HTTP,复用安全) */ const _onvifCache = new Map(); +/** + * host → 最近一次 ONVIF 连接失败原因。 + * 必须把它带进抛出的错误信息里:重试逻辑靠文案判断是不是“偶发” + * (例如高频下设备会瞬时回 `SOAP Fault: Authority failure`,过一会儿自恢复)。 + */ +const _onvifErr = new Map(); + function _dropOnvif(host) { _onvifCache.delete(host); } +/** 造一个带根因的错误,供上层重试判断 */ +function _onvifConnectFail(host, port) { + return new Error(`ONVIF 连接失败(${host}:${port}):${_onvifErr.get(host) || '未知原因'}`); +} + /** * 同一台设备的云台操作串行执行。 * core/wsClient.js 是并发处理 WS 消息的,平台把 control/stop 挨得很近发过来时, @@ -521,23 +533,27 @@ function _serialize(host, fn) { * 注意:SOAP Fault(Authority failure / Token does not exist 等)是设备明确的拒绝,不算偶发。 */ function _isOnvifTransient(e) { - return /Parse Error|Wrong ONVIF SOAP response|ECONNRESET|socket hang up|EPIPE|ETIMEDOUT|timeout|ECONNREFUSED|not connected/i + return /Parse Error|Wrong ONVIF SOAP response|Authority failure|ECONNRESET|socket hang up|EPIPE|ETIMEDOUT|timeout|ECONNREFUSED|not connected/i .test(String(e && e.message ? e.message : e)); } -/** 偶发错误就断开重连重试(首次 + 最多 2 次);这些操作是幂等的(stop / 同速度运动 / 预置点) */ +/** + * 偶发错误就断开重连重试(首次 + 最多 2 次);这些操作是幂等的(stop / 同速度运动 / 预置点)。 + * 退避取 300/800ms:TP-Link 在被高频点时会对 ONVIF 认证短暂拒绝(SOAP Fault: Authority failure), + * 实测过一会儿自恢复,给足恢复时间比快重试更有效。 + */ async function _withOnvifRetry(target, label, fn) { - const MAX = 3; + const BACKOFF = [300, 800]; let lastErr; - for (let attempt = 1; attempt <= MAX; attempt++) { + for (let attempt = 1; attempt <= BACKOFF.length + 1; attempt++) { try { return await fn(); } catch (e) { lastErr = e; - if (!_isOnvifTransient(e) || attempt === MAX) break; + if (!_isOnvifTransient(e) || attempt > BACKOFF.length) break; _log('ONVIF %s 偶发错误(%s)→ 断开重连后第 %d 次重试', label, e.message, attempt); _dropOnvif(target.host); - await new Promise(r => setTimeout(r, 150 * attempt)); + await new Promise(r => setTimeout(r, BACKOFF[attempt - 1])); } } throw lastErr; @@ -582,7 +598,12 @@ function _onvifConnect(target, port) { return new Promise((resolve) => { let settled = false; const timer = setTimeout(() => { - if (!settled) { settled = true; _err('ONVIF 连接超时 %s:%d', target.host, port); resolve(null); } + if (!settled) { + settled = true; + _onvifErr.set(target.host, '连接超时'); + _err('ONVIF 连接超时 %s:%d', target.host, port); + resolve(null); + } }, 8000); const cam = new Cam({ hostname: target.host, @@ -595,7 +616,12 @@ function _onvifConnect(target, port) { if (settled) return; settled = true; clearTimeout(timer); - if (err) { _err('ONVIF 连接失败 %s:%d: %s', target.host, port, err.message); resolve(null); return; } + if (err) { + _onvifErr.set(target.host, err.message); + _err('ONVIF 连接失败 %s:%d: %s', target.host, port, err.message); + resolve(null); + return; + } resolve(cam); }); cam.on('error', () => { /* 交给 callback 处理 */ }); @@ -626,7 +652,11 @@ async function _onvifSession(target) { const cam = await _onvifConnect(target, port); if (!cam) continue; const profileToken = await _onvifProfileToken(cam); - if (!profileToken) { _err('ONVIF 已连上但取不到 profileToken %s:%d', target.host, port); continue; } + if (!profileToken) { + _onvifErr.set(target.host, '取不到 profileToken'); + _err('ONVIF 已连上但取不到 profileToken %s:%d', target.host, port); + continue; + } const sess = { cam, profileToken, port }; _onvifCache.set(target.host, sess); _log('ONVIF 会话就绪 host=%s:%d profileToken=%s', target.host, port, profileToken); @@ -651,10 +681,10 @@ function _resolvePresetToken(table, id) { return hit || null; } -/** ONVIF:方向 / 变倍 / 停止 */ +/** ONVIF:方向 / 变倍 / 停止(连接失败要【抛】出来,否则重试包装拦不到) */ async function _onvifAction(target, action, speed, stop) { const sess = await _onvifSession(target); - if (!sess) return { ok: false, protocol: 'onvif', message: `ONVIF 连接失败(${target.host}:${target.port})` }; + if (!sess) throw _onvifConnectFail(target.host, target.port); const { cam, profileToken } = sess; const def = ACTIONS[action]; @@ -686,7 +716,7 @@ async function _onvifAction(target, action, speed, stop) { /** ONVIF:调用预置点 */ async function _onvifPresetGoto(target, id) { const sess = await _onvifSession(target); - if (!sess) return { ok: false, protocol: 'onvif', presetId: id, message: `ONVIF 连接失败(${target.host}:${target.port})` }; + if (!sess) throw _onvifConnectFail(target.host, target.port); const { cam, profileToken } = sess; const table = await _onvifPresets(cam, profileToken); const token = _resolvePresetToken(table, id); @@ -704,7 +734,7 @@ async function _onvifPresetGoto(target, id) { /** ONVIF:设置(保存)预置点 */ async function _onvifPresetSet(target, id, presetName) { const sess = await _onvifSession(target); - if (!sess) return { ok: false, protocol: 'onvif', presetId: id, message: `ONVIF 连接失败(${target.host}:${target.port})` }; + if (!sess) throw _onvifConnectFail(target.host, target.port); const { cam, profileToken } = sess; const name = presetName || `Preset${id}`; const table = await _onvifPresets(cam, profileToken);