liufumingren d42fa2d863 feat: 新增云台(PTZ)中转端点,入参口径与 OSD 一致
- 新增 POST /camera/ptzControl、/camera/ptzStop、/camera/ptzPresetGoto
  入参 { warehouse_id, rtspUrl, action, speed? } / { ... , presetId },不再需要 computerId
- 控制器抽出公用方法 _findRecordingClient / _pushToRecordingClient,
  setCameraOSD 改为复用(对外返回与日志逐字不变)
- HTTP 侧 method 换名为客户端 WS method:camera.ptzControl -> camera.ptz.control
  等;warehouse_id 仅用于选目标电脑,不下发给客户端
- 新增对接文档 云台对接说明.md(链路、端点、WS 契约、ISAPI 映射、排查手册)
2026-09-15 20:25:40 +08:00

215 lines
8.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { Controller } = require('egg');
class HikvisionController extends Controller {
/**
* POST /hikvision/forceRecord (或 /record/force)
* 强制推送录制任务,事务重置录像行并推送
* 入参(二选一,与打包/拆包任务对应):
* - 打包任务: { pack_task_id: 123 }
* - 拆包任务: { unpack_id: 456 } (task_type: 'unpack' 可选,服务端按入参自动识别)
*/
async forceRecord() {
const { ctx } = this;
const { pack_task_id, unpack_id, task_type } = ctx.request.body;
if (!pack_task_id && !unpack_id) {
ctx.body = { status: 'fail', message: '缺少 pack_task_id 或 unpack_id' };
return;
}
try {
const result = await ctx.service.hikvision.forceRecord({ pack_task_id, unpack_id, task_type });
ctx.body = result;
} catch (error) {
console.error('[HikvisionController] forceRecord 错误:', error);
ctx.body = { status: 'fail', message: error.message };
}
}
/**
* 找仓库内在线的录制电脑(is_recording === 1)
* @param {string|number} warehouseId 仓库 ID
* @returns {Promise<object|null>} Redis 中的 print_computer_info(含 uniqueId / computerId)
*/
async _findRecordingClient(warehouseId) {
const { app } = this;
const warehousePattern = `warehouse:${warehouseId}:printer:*`;
let cursor = '0';
do {
const [nextCursor, keys] = await app.redis.scan(cursor, 'MATCH', warehousePattern, 'COUNT', 100);
cursor = nextCursor;
for (const key of keys) {
const raw = await app.redis.get(key);
if (!raw) continue;
const info = JSON.parse(raw);
if (info.is_recording === 1) return info;
}
} while (cursor !== '0');
return null;
}
/**
* 按仓库选录制电脑并下发 WS 指令(method / data 原样透传给客户端)
* 客户端不在线或发送失败时返回 status:'fail'
* @returns {Promise<{status:string, message:string, result?:object}>}
*/
async _pushToRecordingClient(warehouseId, method, data) {
const { app } = this;
const client = await this._findRecordingClient(warehouseId);
if (!client || !client.uniqueId) {
return { status: 'fail', message: `仓库 ${warehouseId} 中没有在线的录制电脑` };
}
console.log(`[hikvision] 下发 ${method} → 房间 ${client.uniqueId}(电脑 ${client.computerId})`);
const res = await app.ws.sendTo(client.uniqueId, JSON.stringify({ method, data }));
if (res !== 1) {
return { status: 'fail', message: '录制电脑不在线,消息发送失败' };
}
return { status: 'success', result: { computer_id: client.computerId, method } };
}
/**
* POST /camera/setOSD
* 主程序调用,下发 OSD 叠加信息到仓库中开启录制服务的客户端
* 入参: warehouse_id, rtspUrl, text, x?, y?
*/
async setCameraOSD() {
const { ctx } = this;
const { warehouse_id, rtspUrl, text } = ctx.request.body;
const x = ctx.request.body.x !== undefined ? ctx.request.body.x : -0.9;
const y = ctx.request.body.y !== undefined ? ctx.request.body.y : 0.8;
if (!warehouse_id) {
ctx.body = { status: 'fail', message: '缺少必要参数: warehouse_id' };
return;
}
if (!rtspUrl) {
ctx.body = { status: 'fail', message: '缺少必要参数: rtspUrl' };
return;
}
if (!text) {
ctx.body = { status: 'fail', message: '缺少必要参数: text' };
return;
}
try {
console.log(`[setCameraOSD] 下发 OSD, rtspUrl=${rtspUrl}, text=${text}`);
const pushed = await this._pushToRecordingClient(warehouse_id, 'setting.setCameraOSD', {
rtspUrl, text, x, y,
});
if (pushed.status !== 'success') {
ctx.body = { status: 'fail', message: pushed.message };
return;
}
console.log('[setCameraOSD] 消息发送成功');
ctx.body = {
status: 'success',
message: 'OSD 设置指令已下发',
result: {
warehouse_id,
computer_id: pushed.result.computer_id,
rtspUrl,
text,
x,
y,
},
};
} catch (error) {
console.error('[setCameraOSD] 错误:', error);
ctx.body = { status: 'fail', message: error.message };
}
}
/**
* POST /camera/ptzControl
* 主程序调用,下发云台开始运动(方向 / 变倍 / 聚焦 / 光圈)
* 入参: warehouse_id, rtspUrl, action, speed?, channel?, port?
* ⚠️ 方向/变倍/聚焦/光圈是持续型动作,调用方必须另行下发 /camera/ptzStop,否则云台会一直转
* ⚠️ 下发到客户端的 method 是 camera.ptz.control(与内部 API 名 camera.ptzControl 不同名)
*/
async ptzControl() {
const { ctx } = this;
const { warehouse_id, rtspUrl, action, speed, channel, port } = ctx.request.body;
const missing = [ 'warehouse_id', 'rtspUrl', 'action' ]
.find(field => ctx.request.body[field] === undefined || ctx.request.body[field] === null || ctx.request.body[field] === '');
if (missing) {
ctx.body = { status: 'fail', message: `缺少必要参数: ${missing}` };
return;
}
try {
const pushed = await this._pushToRecordingClient(warehouse_id, 'camera.ptz.control', {
rtspUrl, action, speed, channel, port,
});
ctx.body = pushed.status === 'success'
? { status: 'success', message: '云台指令已下发', result: pushed.result }
: pushed;
} catch (error) {
console.error('[ptzControl] 错误:', error);
ctx.body = { status: 'fail', message: error.message };
}
}
/**
* POST /camera/ptzStop
* 主程序调用,停止云台持续运动
* 入参: warehouse_id, rtspUrl, action(必须与开始时的 action 一致), channel?, port?
* 下发到客户端的 method: camera.ptz.stop
*/
async ptzStop() {
const { ctx } = this;
const { warehouse_id, rtspUrl, action, channel, port } = ctx.request.body;
const missing = [ 'warehouse_id', 'rtspUrl', 'action' ]
.find(field => ctx.request.body[field] === undefined || ctx.request.body[field] === null || ctx.request.body[field] === '');
if (missing) {
ctx.body = { status: 'fail', message: `缺少必要参数: ${missing}` };
return;
}
try {
const pushed = await this._pushToRecordingClient(warehouse_id, 'camera.ptz.stop', {
rtspUrl, action, channel, port,
});
ctx.body = pushed.status === 'success'
? { status: 'success', message: '云台停止指令已下发', result: pushed.result }
: pushed;
} catch (error) {
console.error('[ptzStop] 错误:', error);
ctx.body = { status: 'fail', message: error.message };
}
}
/**
* POST /camera/ptzPresetGoto
* 主程序调用,云台转到指定预置点(一次性动作,无需再发 stop)
* 入参: warehouse_id, rtspUrl, presetId, channel?, port?
* 下发到客户端的 method: camera.ptz.presetGoto
*/
async ptzPresetGoto() {
const { ctx } = this;
const { warehouse_id, rtspUrl, presetId, channel, port } = ctx.request.body;
const missing = [ 'warehouse_id', 'rtspUrl', 'presetId' ]
.find(field => ctx.request.body[field] === undefined || ctx.request.body[field] === null || ctx.request.body[field] === '');
if (missing) {
ctx.body = { status: 'fail', message: `缺少必要参数: ${missing}` };
return;
}
try {
const pushed = await this._pushToRecordingClient(warehouse_id, 'camera.ptz.presetGoto', {
rtspUrl, presetId, channel, port,
});
ctx.body = pushed.status === 'success'
? { status: 'success', message: '预置点调用指令已下发', result: pushed.result }
: pushed;
} catch (error) {
console.error('[ptzPresetGoto] 错误:', error);
ctx.body = { status: 'fail', message: error.message };
}
}
}
module.exports = HikvisionController;