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);