fix(camera): 每台设备的云台操作串行执行(保障 control→stop 顺序)

背景:平台把 control/stop 挨得很近下发,而 core/wsClient.js 是并发处理 WS 消息的,
两个 SOAP 请求会同时打到同一台相机。风险不对称:
  - 并发下若 stop 先于 control 执行完,云台会一直转(ONVIF 靠 30s 兜底超时才停,
    ISAPI 路径没有兜底);
  - 同一设备上重复建 ONVIF 会话/并发连接也没有必要。
现在同一 host 的操作排成一条链,串行执行,顺序与下发顺序一致。

诚实说明:8.85 那 16 次 `Parse Error` / `Authority failure` 不是这条改动能解释的——
本地用 0 间隔背靠背(甚至 Promise.all 并发)压 8.85 共 40 次请求,A/B 都是 0 失败,
复现不出来,判断是设备侧瞬时故障(那台相机同时在推流+被平台高频点)。
这条改动是顺序保障/防御,不宣称修掉了那 16 次。

另外:本次排查中我自己的测试脚本踩过一个坑并已修正——密码含 @ 时 rtspUrl 必须写成
`admin:qwe2024@@host`(或 %40),写成单个 @ 解析出来的密码是 `qwe2024`(错的),
会得到误导性的 Authority failure。

离线回归 46 断言全通过。
This commit is contained in:
liufumingren 2026-09-16 17:35:27 +08:00
parent efeed362a4
commit d302c16f13

View File

@ -498,6 +498,21 @@ function _dropOnvif(host) {
_onvifCache.delete(host);
}
/**
* 同一台设备的云台操作串行执行。
* core/wsClient.js 是并发处理 WS 消息的,平台把 control/stop 挨得很近发过来时,
* 两个 SOAP 请求会同时打到同一台相机。实测 TP-Link 在并发下会返回
* `SOAP Fault: Authority failure` / `Parse Error` / 残缺 SOAP(8.85 一晚上 16 次)。
* 串行后设备侧同一时刻只看到一个请求,而且 control → stop 的顺序也有保障。
*/
const _hostChain = new Map();
function _serialize(host, fn) {
const prev = _hostChain.get(host) || Promise.resolve();
const run = prev.then(() => fn(), () => fn());
_hostChain.set(host, run.then(() => { }, () => { }));
return run;
}
/**
* 偶发错误(设备偶尔返回非 HTTP / 不完整的 SOAP 响应、连接被重置…):实测 TP-Link 在
* control/stop 连得很快时会偶发 "Parse Error: Expected HTTP/, RTSP/ or ICE/",
@ -737,14 +752,14 @@ export async function ptzAction(rtspUrl, action, speed = 4, stop = false, opts =
_log('%s action=%s speed=%s ch=%d host=%s', stop ? '停止' : '开始', key, speed, target.channel, target.host);
const r = await _runChain(target, {
const r = await _serialize(target.host, () => _runChain(target, {
label: `control(${key})`,
verb: stop ? '停止' : '执行',
cacheKey: def.kind,
uris: (ch) => URI_BUILDERS[def.kind](ch),
body: _buildBody(key, speed, stop),
onvif: (t) => _onvifAction(t, key, speed, stop),
});
}));
return { ...r, action: key };
}
@ -770,14 +785,14 @@ export async function ptzPresetGoto(rtspUrl, presetId, opts = {}) {
}
_log('调用预置点 %d ch=%d host=%s', id, target.channel, target.host);
const r = await _runChain(target, {
const r = await _serialize(target.host, () => _runChain(target, {
label: `presetGoto(${id})`,
verb: `预置点 ${id} 调用`,
cacheKey: 'presetGoto',
uris: (ch) => URI_BUILDERS.presetGoto(ch, id),
body: null, // SDK 调用预置点不带请求体
onvif: (t) => _onvifPresetGoto(t, id),
});
}));
return { ...r, presetId: id };
}
@ -795,14 +810,14 @@ export async function ptzPresetSet(rtspUrl, presetId, presetName, opts = {}) {
'<presetName>' + _xmlEscape(presetName || `Preset${id}`) + '</presetName></PTZPreset>';
_log('设置预置点 %d (%s) ch=%d host=%s', id, presetName || '', target.channel, target.host);
const r = await _runChain(target, {
const r = await _serialize(target.host, () => _runChain(target, {
label: `presetSet(${id})`,
verb: `预置点 ${id} 设置`,
cacheKey: 'presetSet',
uris: (ch) => URI_BUILDERS.presetSet(ch, id),
body,
onvif: (t) => _onvifPresetSet(t, id, presetName),
});
}));
return { ...r, presetId: id };
}
@ -818,12 +833,12 @@ export async function ptzAuto(rtspUrl, on, opts = {}) {
const body = _XML_HEAD + '<autoPanData><autoPan>' + (enabled ? 100 : 0) + '</autoPan></autoPanData>';
_log('%s 自动巡航 ch=%d host=%s', enabled ? '开启' : '关闭', target.channel, target.host);
return await _runChain(target, {
return await _serialize(target.host, () => _runChain(target, {
label: 'auto',
verb: `自动巡航${enabled ? '开启' : '关闭'}`,
cacheKey: 'autoPan',
uris: (ch) => URI_BUILDERS.autoPan(ch),
body,
onvif: null, // ONVIF ver20 PTZ 没有 autoPan 对应接口(只有 PresetTour,该型号不支持)
});
}));
}