From 1a27402749ebfc1f272d154d6dd183a9e09d2755 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 16 Sep 2026 17:46:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(camera):=20=E4=BF=AE=E6=8E=89=20TP-Link=20?= =?UTF-8?q?=E7=BA=AF=E4=BF=AF=E4=BB=B0=E5=A4=B1=E6=95=88=EF=BC=88x=3D0=20?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E5=9B=BA=E4=BB=B6=20bug=EF=BC=89=E2=80=94?= =?UTF-8?q?=E2=80=94"=E4=B8=8A=E4=B8=8B=E6=B2=A1=E5=8F=8D=E5=BA=94"?= =?UTF-8?q?=E7=9A=84=E7=9C=9F=E5=9B=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:192.168.8.85 / 192.168.9.226 上"上下操作没反应,左右正常"。 定位过程: - 协议层看是成功的(返回 200/无异常)——不对,重新看日志发现失败全集中在 up/down: cameraPtz.log 里 69 次 `Parse Error: Expected HTTP/, RTSP/ or ICE/` 全部发生在 control(up)/control(down),left/right/斜向 0 次(8.85 家族 22 次、9.226 18 次)。 - 用裸 ONVIF 做对照实验(9.226,各 6 次): x=0, y=-0.4 → 成功 0/6 (Parse Error) x=0.02,y=-0.4 → 成功 6/6 再测 x=0 → 成功 0/6 relativeMove 用 x=0 同样 0/4 失败,x=0.001 则 4/4 成功。 - 也就是说这是 **TP-Link 固件 bug:ContinuousMove 的 PanTilt 里 x=0 且 y≠0 时返回畸形响应**。 左右/斜向都带 x≠0,所以从不触发;纯抬头/低头必然 x=0,于是"上下没反应"。 不同机器严重程度不同(9.226 100% 复现、8.85 大量、8.82/8.86 基本正常)。 修复:纯俯仰时把 x 从 0 换成 ±0.001(按 host 交替正负,避免 pan 单向累积)。 实测 pan 漂移约 0.0006/次,可忽略;9.226 修复后 up/down 8/8 全成功(修复前 0/6)。 同时提交上一轮未入库的两处(已部署但没提交): - `_parseTarget` 有 camera 块时优先用 camera.port(顶层 port 属于录像机) - `_onvifSession` 的端口候选改为 [payload port, 2020, 80](payload 给 2020 时也要能回退 80) 离线回归 46 断言全通过。 --- utils/cameraPtz.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/utils/cameraPtz.js b/utils/cameraPtz.js index c1b07c9..abddb52 100644 --- a/utils/cameraPtz.js +++ b/utils/cameraPtz.js @@ -206,6 +206,8 @@ function _parseTarget(rtspUrl, opts = {}) { if (!host) throw new Error('缺少设备地址(需 rtspUrl 或 ip)'); const p = Number(opts.port); + // 有 camera 块时,camera.port 才是摄像机自己的口(顶层 port 属于录像机) + const camPort = Number(camBlock.port); const target = { host, // ⚠️ 凭据【不能】优先用显式字段:实测平台在密码含 @ 时把它切给了 ip 字段 @@ -215,7 +217,9 @@ function _parseTarget(rtspUrl, opts = {}) { username: info.username || opts.username, password: info.password || opts.password, channel, - port: Number.isInteger(p) && p > 0 ? p : 80, + port: Number.isInteger(camPort) && camPort > 0 + ? camPort + : (Number.isInteger(p) && p > 0 ? p : 80), server: null, }; @@ -566,6 +570,22 @@ function _speedNormalized(speed) { return Math.max(0.15, Math.min(1, Math.round(v * 100) / 100)); } +/** + * 纯俯仰时用的极小 x。 + * 🐞 TP-Link 固件 bug:ContinuousMove 的 PanTilt 里 **x=0 且 y≠0**(即纯抬头/低头) + * 会返回畸形响应(Parse Error: Expected HTTP/, RTSP/ or ICE/),所以“上下没反应、 + * 左右正常”。实测 9.226 是 0/6 全失败 → 改成 x=0.001 后 6/6 全成功(8.82 本来就正常, + * 加上也不受影响);relativeMove 同样受这个 bug 影响。 + * pan 漂移约 0.0006/次(可忽略),并按 host 交替正负,避免长期单向累积。 + */ +const TILT_EPSILON = 0.001; +const _epsFlip = new Map(); +function _tiltEpsilon(host) { + const n = (_epsFlip.get(host) || 0) + 1; + _epsFlip.set(host, n); + return (n % 2 ? 1 : -1) * TILT_EPSILON; +} + /** 探测设备协议(结果永久缓存) */ async function _protocol(target) { if (_protoCache.has(target.host)) return _protoCache.get(target.host); @@ -646,8 +666,10 @@ async function _onvifSession(target) { const cached = _onvifCache.get(target.host); if (cached) return cached; - // 先试平台给的口(默认 80),再试非标的 2020 - const ports = [target.port, 2020].filter((p, i, a) => a.indexOf(p) === i); + // 先试平台给的口,再试 2020(多数TP-Link 的 ONVIF 口)与 80。 + // ⚠️ 必须两个都留:实测 payload 给 2020 时,设备会偶发只认 80(反之亦然), + // 去重后只剩一个口就会直接失败。 + const ports = [target.port, 2020, 80].filter((p, i, a) => a.indexOf(p) === i); for (const port of ports) { const cam = await _onvifConnect(target, port); if (!cam) continue; @@ -697,8 +719,10 @@ async function _onvifAction(target, action, speed, stop) { const timeout = ONVIF_MOVE_TIMEOUT_MS > 0 ? { timeout: ONVIF_MOVE_TIMEOUT_MS } : {}; if (def.kind === 'ptz') { const v = _speedNormalized(speed); - const x = (def.pan || 0) * v * SIGNS.pan; + let x = (def.pan || 0) * v * SIGNS.pan; const y = (def.tilt || 0) * v * SIGNS.tilt; + // 纯俯仰:x=0 会踩到 TP-Link 固件 bug(见 TILT_EPSILON 注释),补一个极小的 x + if (x === 0 && y !== 0) x = _tiltEpsilon(target.host); // onlySendPanTilt:该设备的 zoom 范围是 0~0,不同时发 zoom,避免被拒 await _onvifCall(cam, 'continuousMove', { profileToken, x, y, onlySendPanTilt: true, ...timeout }); _log('ONVIF 运动成功 x=%s y=%s (speed=%s profileToken=%s)', x, y, speed, profileToken);