From 0d0b0b8db226340f4fe3f0123ade6f701a400bda Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 15 Sep 2026 22:13:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(camera):=20=E4=BA=91=E5=8F=B0=E5=87=AD?= =?UTF-8?q?=E6=8D=AE=E4=BB=A5=20rtspUrl=20=E4=B8=BA=E5=87=86=20+=20ISAPI?= =?UTF-8?q?=20=E9=80=9A=E9=81=93=E5=8F=B7=E4=BB=A5=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E8=87=AA=E6=8A=A5=E4=B8=BA=E5=87=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 线上实测(69 条指令、5 台相机)暴露两个问题: 1) ONVIF 全部报 "SOAP Fault: Authority failure" 平台下发的 username/password 字段不可靠:密码含 @ 时它把 @ 切给了 ip 字段 (下发 ip="@192.168.11.247" + password="qwe2024",而 rtspUrl 里是 admin:qwe2024@…)。 原来 _parseTarget 让显式字段优先,于是拿着错密码去连 → 认证失败(4 台全挂)。 改为凭据以 rtspUrl 解析结果为准,显式字段只在 rtspUrl 缺项时兜底 (ip / channel / port 仍以上层下发为准)。 2) 海康 IPC 直连时通道号错了 → 403 平台从 rtspUrl 的 /streamN 推出来的 channel 是它自己的相机编号。实测 192.168.8.165 (海康 DS-2XD8F826E 双目 IPC)只有 PTZ 通道 1,发通道 6 直接 403。 新增:枚举 GET /ISAPI/PTZCtrl/channels,以设备自报通道为准(下发编号在列表里就用它, 否则用第一个);枚举不到(如 NVR 不支持该接口)则沿用原推导逻辑;结果按 host 缓存。 3) 顺带:无云台机构的摄像机给出明确原因 同一台 8.165 的 panSupport/tiltSupport/zoomSupport 全是 false(没有云台机构), 发通道 1 设备会回 200 但什么也不动(属假成功)。现在直接返回 "该摄像机没有云台机构,已跳过发送",避免日志里出现误导性的成功。 另:上一版试验过的"ISAPI 二次确认"(要求 deviceInfo 返回 )已撤除—— 实测未发现 ISAPI 误判(8.165 本就是真海康),而它会把权限受限/返回体不含 DeviceInfo 的真海康设备误判成 ONVIF。 真机复验:11.247 用平台那条"错密码"报文 → ONVIF 认证通过并成功转向(凭据修复生效); 8.165 → 明确报无云台机构,不再 403。离线回归 42 断言全通过(新增通道回退 / 无云台 4 条)。 --- utils/cameraPtz.js | 93 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 7 deletions(-) diff --git a/utils/cameraPtz.js b/utils/cameraPtz.js index dcf91d3..fe008b7 100644 --- a/utils/cameraPtz.js +++ b/utils/cameraPtz.js @@ -201,8 +201,12 @@ function _parseTarget(rtspUrl, opts = {}) { const p = Number(opts.port); return { host, - username: opts.username || info.username, - password: opts.password || info.password, + // ⚠️ 凭据【不能】优先用显式字段:实测平台在密码含 @ 时把它切给了 ip 字段 + // (rtspUrl 是 admin:qwe2024@…,而下发的是 ip="@192.168.11.247" + password="qwe2024"), + // 用错密码会让 ONVIF 直接报 SOAP Fault: Authority failure。 + // → 以 rtspUrl 解析结果为准,显式字段只在 rtspUrl 缺项时兜底。 + username: info.username || opts.username, + password: info.password || opts.password, channel, port: Number.isInteger(p) && p > 0 ? p : 80, }; @@ -255,9 +259,73 @@ async function _openSession(host, username, password, port) { } return resp; }, + async get(uri) { + let resp = await isapiRequest(host, 'GET', uri, null, auth.headers('GET', uri), 5000, port); + if (resp.status === 401) { + const fresh = await acquireIsapiAuth(host, username, password, '/ISAPI/System/deviceInfo', port); + if (fresh) { + auth = fresh; + resp = await isapiRequest(host, 'GET', uri, null, auth.headers('GET', uri), 5000, port); + } + } + return resp; + }, }; } +/** host → { ids:number[], noPtzIds:number[] }(设备自报的 ISAPI PTZ 通道,只枚举一次) */ +const _chCache = new Map(); + +/** + * 枚举设备自报的 PTZ 通道与云台能力。 + * 实测 192.168.8.165(DS-2XD8F826E 双目 IPC):平台从 rtspUrl 的 /stream6 推出 channel=6, + * 而该机只有 PTZ 通道 1(发 6 → 403);且该机 panSupport/tiltSupport/zoomSupport 均为 false + * (压根没有云台机构,发通道 1 会回 200 但什么也不动)。 + */ +async function _isapiChannelInfo(session, host) { + if (_chCache.has(host)) return _chCache.get(host); + let info = { ids: [], noPtzIds: [] }; + try { + const resp = await session.get('/ISAPI/PTZCtrl/channels'); + if (resp.status === 200) { + const ids = []; + const noPtzIds = []; + for (const m of String(resp.body || '').matchAll(/]([\s\S]*?)<\/PTZChannel>/g)) { + const block = m[1]; + const id = Number(/(\d+)<\/id>/.exec(block)?.[1]); + if (!id) continue; + if (/false<\/enabled>/.test(block)) continue; + ids.push(id); + const flags = ['panSupport', 'tiltSupport', 'zoomSupport']; + const present = flags.filter(f => new RegExp(`<${f}>`).test(block)); + if (present.length && present.every(f => new RegExp(`<${f}>false<\/${f}>`).test(block))) noPtzIds.push(id); + } + info = { ids, noPtzIds }; + _log('设备 PTZ 通道 host=%s: ids=[%s] 无云台机构=[%s]', host, ids.join(','), noPtzIds.join(',')); + } else { + _log('枚举 PTZ 通道失败 host=%s status=%s(沿用推导的通道号)', host, resp.status); + } + } catch (e) { + _err('枚举 PTZ 通道异常 host=%s: %s', host, e.message); + } + _chCache.set(host, info); + return info; +} + +/** + * 决定实际要用的 PTZ 通道: + * 设备能枚举出通道时以设备为准(下发的编号在列表里就用它,否则用第一个); + * 枚举不到(如 NVR 不提供该接口)则沿用推导出的编号。 + * @returns {Promise<{channel:number, noPtz:boolean}>} + */ +async function _resolveIsapiChannel(session, host, requested) { + const info = await _isapiChannelInfo(session, host); + if (!info.ids.length) return { channel: requested, noPtz: false }; + if (info.ids.includes(requested)) return { channel: requested, noPtz: info.noPtzIds.includes(requested) }; + _log('下发通道 %d 不在设备 PTZ 通道列表 [%s] 内(多为平台内部编号)→ 改用 %d', requested, info.ids.join(','), info.ids[0]); + return { channel: info.ids[0], noPtz: info.noPtzIds.includes(info.ids[0]) }; +} + /** * 按候选 URI 依次尝试,成功即缓存该设备可用的路径 * @returns {Promise<{ok:boolean, uri?:string, status?:number, body?:string, message?:string}>} @@ -433,6 +501,7 @@ async function _onvifAction(target, action, speed, stop) { if (stop) { await _onvifCall(cam, 'stop', { profileToken, panTilt: true, zoom: true }); + _log('ONVIF 停止成功 profileToken=%s', profileToken); return { ok: true, protocol: 'onvif', message: '停止成功 (ONVIF Stop)' }; } @@ -443,11 +512,13 @@ async function _onvifAction(target, action, speed, stop) { const y = (def.tilt || 0) * v * SIGNS.tilt; // 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); return { ok: true, protocol: 'onvif', message: `执行成功 (ONVIF ContinuousMove x=${x} y=${y})` }; } if (def.kind === 'zoom') { const zoom = (def.zoom || 0) * _speedNormalized(speed) * SIGNS.zoom; await _onvifCall(cam, 'continuousMove', { profileToken, zoom, onlySendZoom: true, ...timeout }); + _log('ONVIF 变倍成功 zoom=%s (speed=%s profileToken=%s)', zoom, speed, profileToken); return { ok: true, protocol: 'onvif', message: `执行成功 (ONVIF Zoom=${zoom})` }; } return { ok: false, protocol: 'onvif', action, message: `${action} 在 ONVIF 协议下无对应接口(${def.kind} 不支持)` }; @@ -467,6 +538,7 @@ async function _onvifPresetGoto(target, id) { }; } await _onvifCall(cam, 'gotoPreset', { profileToken, preset: token }); + _log('ONVIF 预置点 %d 已调用(token=%s profileToken=%s)', id, token, profileToken); return { ok: true, protocol: 'onvif', presetId: id, token, message: `预置点 ${id}(token=${token}) 已调用 (ONVIF GotoPreset)` }; } @@ -485,6 +557,7 @@ async function _onvifPresetSet(target, id, presetName) { const r = await _onvifCall(cam, 'setPreset', { profileToken, presetName: name, presetToken: existing || String(id) }); const token = pickToken(r) || existing || String(id); if (String(token) !== String(id)) _log('注意:设备给回的 token=%s ≠ 请求的 presetId=%s', token, id); + _log('ONVIF 预置点 %d 已设置(token=%s profileToken=%s)', id, token, profileToken); return { ok: true, protocol: 'onvif', presetId: id, token, message: `预置点 ${id} 已设置(token=${token},ONVIF SetPreset)` }; } catch (e) { if (existing) throw e; // 覆盖已知 token 都失败 → 真实错误,抛出交给上层 @@ -533,12 +606,15 @@ export async function ptzAction(rtspUrl, action, speed = 4, stop = false, opts = } const body = _buildBody(key, speed, stop); - const uris = URI_BUILDERS[def.kind](target.channel); try { const session = await _openSession(target.host, target.username, target.password, target.port); if (!session) return { ok: false, message: 'ISAPI 认证失败(Basic/Digest 均不可用)' }; - const r = await _putWithFallback(session, target.host, `${def.kind}:${target.channel}`, uris, body); + const ch = await _resolveIsapiChannel(session, target.host, target.channel); + if (ch.noPtz) { + return { ok: false, action: key, protocol: 'isapi', message: `设备 PTZ 通道 ${ch.channel} 的 pan/tilt/zoom 能力均为 false(该摄像机没有云台机构),已跳过发送` }; + } + const r = await _putWithFallback(session, target.host, `${def.kind}:${ch.channel}`, URI_BUILDERS[def.kind](ch.channel), body); return r.ok ? { ok: true, action: key, uri: r.uri, message: `${stop ? '停止' : '执行'}成功 (HTTP ${r.status})` } : { ok: false, action: key, message: r.message }; @@ -584,8 +660,9 @@ export async function ptzPresetGoto(rtspUrl, presetId, opts = {}) { try { const session = await _openSession(target.host, target.username, target.password, target.port); if (!session) return { ok: false, message: 'ISAPI 认证失败(Basic/Digest 均不可用)' }; + const ch = await _resolveIsapiChannel(session, target.host, target.channel); // SDK 调用预置点不带请求体 - const r = await _putWithFallback(session, target.host, `presetGoto:${target.channel}`, URI_BUILDERS.presetGoto(target.channel, id), null); + const r = await _putWithFallback(session, target.host, `presetGoto:${ch.channel}`, URI_BUILDERS.presetGoto(ch.channel, id), null); return r.ok ? { ok: true, presetId: id, uri: r.uri, message: `预置点 ${id} 已调用 (HTTP ${r.status})` } : { ok: false, presetId: id, message: r.message }; @@ -623,7 +700,8 @@ export async function ptzPresetSet(rtspUrl, presetId, presetName, opts = {}) { try { const session = await _openSession(target.host, target.username, target.password, target.port); if (!session) return { ok: false, message: 'ISAPI 认证失败(Basic/Digest 均不可用)' }; - const r = await _putWithFallback(session, target.host, `presetSet:${target.channel}`, URI_BUILDERS.presetSet(target.channel, id), body); + const ch = await _resolveIsapiChannel(session, target.host, target.channel); + const r = await _putWithFallback(session, target.host, `presetSet:${ch.channel}`, URI_BUILDERS.presetSet(ch.channel, id), body); return r.ok ? { ok: true, presetId: id, uri: r.uri, message: `预置点 ${id} 已设置 (HTTP ${r.status})` } : { ok: false, presetId: id, message: r.message }; @@ -654,7 +732,8 @@ export async function ptzAuto(rtspUrl, on, opts = {}) { try { const session = await _openSession(target.host, target.username, target.password, target.port); if (!session) return { ok: false, message: 'ISAPI 认证失败(Basic/Digest 均不可用)' }; - const r = await _putWithFallback(session, target.host, `autoPan:${target.channel}`, URI_BUILDERS.autoPan(target.channel), body); + const ch = await _resolveIsapiChannel(session, target.host, target.channel); + const r = await _putWithFallback(session, target.host, `autoPan:${ch.channel}`, URI_BUILDERS.autoPan(ch.channel), body); return r.ok ? { ok: true, uri: r.uri, message: `自动巡航已${enabled ? '开启' : '关闭'} (HTTP ${r.status})` } : { ok: false, message: r.message };