diff --git a/utils/cameraPtz.js b/utils/cameraPtz.js index abddb52..e6df62a 100644 --- a/utils/cameraPtz.js +++ b/utils/cameraPtz.js @@ -509,9 +509,21 @@ function _dropOnvif(host) { _onvifCache.delete(host); } +/** + * host → 下次允许尝试建立 ONVIF 连接的时间戳。 + * 设备对 ONVIF 会有会话/频率限制:一旦开始回 `SOAP Fault: Authority failure`, + * 我们每条指令的 2~3 次重连会把限流越打越死(实测 9.101 整队被拒,而同刻裸探针可通)。 + * 认证类拒绝就静默几秒,把“静静等它恢复”交给下一次下发。 + */ +const _connBackoff = new Map(); +const CONN_BACKOFF_MS = 5000; + /** 造一个带根因的错误,供上层重试判断 */ function _onvifConnectFail(host, port) { - return new Error(`ONVIF 连接失败(${host}:${port}):${_onvifErr.get(host) || '未知原因'}`); + const msg = _onvifErr.get(host) || '未知原因'; + const wait = Math.ceil(((_connBackoff.get(host) || 0) - Date.now()) / 1000); + const hint = wait > 0 ? `;已暂停重连 ${wait}s(疑似设备限流)` : ''; + return new Error(`ONVIF 连接失败(${host}:${port}):${msg}${hint}`); } /** @@ -638,6 +650,10 @@ function _onvifConnect(target, port) { clearTimeout(timer); if (err) { _onvifErr.set(target.host, err.message); + // 认证类拒绝:静默几秒,不再刷重连 + if (/Authority failure/i.test(err.message)) { + _connBackoff.set(target.host, Date.now() + CONN_BACKOFF_MS); + } _err('ONVIF 连接失败 %s:%d: %s', target.host, port, err.message); resolve(null); return; @@ -666,6 +682,13 @@ async function _onvifSession(target) { const cached = _onvifCache.get(target.host); if (cached) return cached; + // 刚被拒过就不要再刷连接(见 _connBackoff 注释) + const until = _connBackoff.get(target.host) || 0; + if (Date.now() < until) { + if (!_onvifErr.has(target.host)) _onvifErr.set(target.host, '认证被拒后暂时不再重连'); + return null; + } + // 先试平台给的口,再试 2020(多数TP-Link 的 ONVIF 口)与 80。 // ⚠️ 必须两个都留:实测 payload 给 2020 时,设备会偶发只认 80(反之亦然), // 去重后只剩一个口就会直接失败。