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);