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,该型号不支持)
- });
+ }));
}