From d302c16f13041cd64f29d33705f1e12ce5744783 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 16 Sep 2026 17:35:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(camera):=20=E6=AF=8F=E5=8F=B0=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=9A=84=E4=BA=91=E5=8F=B0=E6=93=8D=E4=BD=9C=E4=B8=B2?= =?UTF-8?q?=E8=A1=8C=E6=89=A7=E8=A1=8C=EF=BC=88=E4=BF=9D=E9=9A=9C=20contro?= =?UTF-8?q?l=E2=86=92stop=20=E9=A1=BA=E5=BA=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 背景:平台把 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 断言全通过。 --- utils/cameraPtz.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/utils/cameraPtz.js b/utils/cameraPtz.js index c37100e..42d3db7 100644 --- a/utils/cameraPtz.js +++ b/utils/cameraPtz.js @@ -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 = {}) { '' + _xmlEscape(presetName || `Preset${id}`) + ''; _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 + '' + (enabled ? 100 : 0) + ''; _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,该型号不支持) - }); + })); }