WMS_MAIN/app/lib/osd_client.js
liufumingren 45b59ea35c feat(camera): 新增摄像头 OSD 叠加功能
- 新增 camera model username/password 字段
- 新增 osd_client.js OSD 客户端库
- 工作台打包时自动叠加 OSD 信息到摄像头(带 Redis 缓存去重)
- 新增迁移脚本:摄像头用户名密码字段
2026-08-01 03:44:02 +08:00

103 lines
3.1 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.

'use strict';
/**
* OSD(屏幕菜单叠加)客户端模块。
* 负责组装 RTSP URL 并调用打印服务设置摄像头 OSD 文字叠加。
*
* 使用方式:
* const osdClient = new (require('../lib/osd_client'))(ctx);
* await osdClient.setOsd({ camera, warehouseId, text, x, y });
*/
class OsdClient {
constructor(ctx) {
this.ctx = ctx;
this.logger = ctx.logger || console;
}
/**
* 组装摄像头 RTSP 地址
* 格式: rtsp://{username}:{password}@{ip}:554/stream{channelId}
*
* @param {Object} camera - 摄像头记录(含 ip/username/password/channel_id)
* @return {string} RTSP URL
*/
buildRtspUrl(camera) {
const ip = camera.ip;
if (!ip) {
throw new Error('摄像头未配置IP地址,无法组装RTSP地址');
}
const username = camera.username || 'admin';
const password = camera.password || '';
const channelId = camera.channel_id;
const rtspPort = 554;
return `rtsp://${username}:${password}@${ip}:${rtspPort}/stream${channelId}`;
}
/**
* 发送 OSD 叠加指令到打印服务
*
* @param {Object} options - 配置选项
* @param {Object} options.camera - 摄像头记录
* @param {Object} [options.server] - 关联的录像服务器(fallback 认证信息)
* @param {number} options.warehouseId - 仓库ID
* @param {string} options.text - OSD 叠加文字
* @param {number} [options.x] - X 坐标(-1 到 1,默认 -0.9)
* @param {number} [options.y] - Y 坐标(-1 到 1,默认 0.8)
* @return {Promise<Object>} 打印服务响应 { status, data, error }
*/
async setOsd(options = {}) {
const { ctx } = this;
const { camera, warehouseId, text, x, y } = options;
if (!camera) throw new Error('摄像头信息不能为空');
if (!text) throw new Error('OSD文字不能为空');
if (warehouseId === undefined || warehouseId === null) throw new Error('仓库ID不能为空');
const rtspUrl = this.buildRtspUrl(camera);
const printServiceUrl = ctx.app.config.printService.url;
const requestData = {
warehouse_id: String(warehouseId),
rtspUrl,
text,
x: x !== undefined ? x : -0.9,
y: y !== undefined ? y : 0.8,
};
const targetUrl = `${printServiceUrl}/camera/setOSD`;
ctx.logger.info('[OSD] 发送设置请求', { url: targetUrl, body: requestData });
let response = null;
let error = null;
try {
const res = await ctx.curl(targetUrl, {
method: 'POST',
contentType: 'json',
data: requestData,
dataType: 'json',
timeout: 10000,
});
response = {
status: res.status,
headers: res.headers,
data: res.data,
};
ctx.logger.info('[OSD] 打印服务回信', { status: res.status, data: res.data });
} catch (err) {
error = err.message;
ctx.logger.warn('[OSD] 打印服务请求失败', { camera_id: camera.id, error: err.message });
}
return {
success: !error && response && response.status >= 200 && response.status < 300,
error,
response,
request: requestData,
};
}
}
module.exports = OsdClient;