fix(camera): ONVIF 连接阶段的失败也纳入重试(并把根因带进错误信息)
排查"往下不行"时发现:设备被高频点时会对 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 重试才报错——这是刻意取舍,
因为高频下把"限流"误判成"密码错"会让云台整个命令失效。
This commit is contained in:
parent
d302c16f13
commit
fd2b1caf8b
@ -494,10 +494,22 @@ const _protoCache = new Map();
|
|||||||
/** host → { cam, profileToken, port }(ONVIF 会话复用;库内部每次请求新建 HTTP,复用安全) */
|
/** host → { cam, profileToken, port }(ONVIF 会话复用;库内部每次请求新建 HTTP,复用安全) */
|
||||||
const _onvifCache = new Map();
|
const _onvifCache = new Map();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* host → 最近一次 ONVIF 连接失败原因。
|
||||||
|
* 必须把它带进抛出的错误信息里:重试逻辑靠文案判断是不是“偶发”
|
||||||
|
* (例如高频下设备会瞬时回 `SOAP Fault: Authority failure`,过一会儿自恢复)。
|
||||||
|
*/
|
||||||
|
const _onvifErr = new Map();
|
||||||
|
|
||||||
function _dropOnvif(host) {
|
function _dropOnvif(host) {
|
||||||
_onvifCache.delete(host);
|
_onvifCache.delete(host);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 造一个带根因的错误,供上层重试判断 */
|
||||||
|
function _onvifConnectFail(host, port) {
|
||||||
|
return new Error(`ONVIF 连接失败(${host}:${port}):${_onvifErr.get(host) || '未知原因'}`);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同一台设备的云台操作串行执行。
|
* 同一台设备的云台操作串行执行。
|
||||||
* core/wsClient.js 是并发处理 WS 消息的,平台把 control/stop 挨得很近发过来时,
|
* core/wsClient.js 是并发处理 WS 消息的,平台把 control/stop 挨得很近发过来时,
|
||||||
@ -521,23 +533,27 @@ function _serialize(host, fn) {
|
|||||||
* 注意:SOAP Fault(Authority failure / Token does not exist 等)是设备明确的拒绝,不算偶发。
|
* 注意:SOAP Fault(Authority failure / Token does not exist 等)是设备明确的拒绝,不算偶发。
|
||||||
*/
|
*/
|
||||||
function _isOnvifTransient(e) {
|
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));
|
.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) {
|
async function _withOnvifRetry(target, label, fn) {
|
||||||
const MAX = 3;
|
const BACKOFF = [300, 800];
|
||||||
let lastErr;
|
let lastErr;
|
||||||
for (let attempt = 1; attempt <= MAX; attempt++) {
|
for (let attempt = 1; attempt <= BACKOFF.length + 1; attempt++) {
|
||||||
try {
|
try {
|
||||||
return await fn();
|
return await fn();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
lastErr = 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);
|
_log('ONVIF %s 偶发错误(%s)→ 断开重连后第 %d 次重试', label, e.message, attempt);
|
||||||
_dropOnvif(target.host);
|
_dropOnvif(target.host);
|
||||||
await new Promise(r => setTimeout(r, 150 * attempt));
|
await new Promise(r => setTimeout(r, BACKOFF[attempt - 1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw lastErr;
|
throw lastErr;
|
||||||
@ -582,7 +598,12 @@ function _onvifConnect(target, port) {
|
|||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
let settled = false;
|
let settled = false;
|
||||||
const timer = setTimeout(() => {
|
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);
|
}, 8000);
|
||||||
const cam = new Cam({
|
const cam = new Cam({
|
||||||
hostname: target.host,
|
hostname: target.host,
|
||||||
@ -595,7 +616,12 @@ function _onvifConnect(target, port) {
|
|||||||
if (settled) return;
|
if (settled) return;
|
||||||
settled = true;
|
settled = true;
|
||||||
clearTimeout(timer);
|
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);
|
resolve(cam);
|
||||||
});
|
});
|
||||||
cam.on('error', () => { /* 交给 callback 处理 */ });
|
cam.on('error', () => { /* 交给 callback 处理 */ });
|
||||||
@ -626,7 +652,11 @@ async function _onvifSession(target) {
|
|||||||
const cam = await _onvifConnect(target, port);
|
const cam = await _onvifConnect(target, port);
|
||||||
if (!cam) continue;
|
if (!cam) continue;
|
||||||
const profileToken = await _onvifProfileToken(cam);
|
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 };
|
const sess = { cam, profileToken, port };
|
||||||
_onvifCache.set(target.host, sess);
|
_onvifCache.set(target.host, sess);
|
||||||
_log('ONVIF 会话就绪 host=%s:%d profileToken=%s', target.host, port, profileToken);
|
_log('ONVIF 会话就绪 host=%s:%d profileToken=%s', target.host, port, profileToken);
|
||||||
@ -651,10 +681,10 @@ function _resolvePresetToken(table, id) {
|
|||||||
return hit || null;
|
return hit || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** ONVIF:方向 / 变倍 / 停止 */
|
/** ONVIF:方向 / 变倍 / 停止(连接失败要【抛】出来,否则重试包装拦不到) */
|
||||||
async function _onvifAction(target, action, speed, stop) {
|
async function _onvifAction(target, action, speed, stop) {
|
||||||
const sess = await _onvifSession(target);
|
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 { cam, profileToken } = sess;
|
||||||
const def = ACTIONS[action];
|
const def = ACTIONS[action];
|
||||||
|
|
||||||
@ -686,7 +716,7 @@ async function _onvifAction(target, action, speed, stop) {
|
|||||||
/** ONVIF:调用预置点 */
|
/** ONVIF:调用预置点 */
|
||||||
async function _onvifPresetGoto(target, id) {
|
async function _onvifPresetGoto(target, id) {
|
||||||
const sess = await _onvifSession(target);
|
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 { cam, profileToken } = sess;
|
||||||
const table = await _onvifPresets(cam, profileToken);
|
const table = await _onvifPresets(cam, profileToken);
|
||||||
const token = _resolvePresetToken(table, id);
|
const token = _resolvePresetToken(table, id);
|
||||||
@ -704,7 +734,7 @@ async function _onvifPresetGoto(target, id) {
|
|||||||
/** ONVIF:设置(保存)预置点 */
|
/** ONVIF:设置(保存)预置点 */
|
||||||
async function _onvifPresetSet(target, id, presetName) {
|
async function _onvifPresetSet(target, id, presetName) {
|
||||||
const sess = await _onvifSession(target);
|
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 { cam, profileToken } = sess;
|
||||||
const name = presetName || `Preset${id}`;
|
const name = presetName || `Preset${id}`;
|
||||||
const table = await _onvifPresets(cam, profileToken);
|
const table = await _onvifPresets(cam, profileToken);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user