feat(camera): 新增摄像头 OSD 叠加功能
- 新增 camera model username/password 字段 - 新增 osd_client.js OSD 客户端库 - 工作台打包时自动叠加 OSD 信息到摄像头(带 Redis 缓存去重) - 新增迁移脚本:摄像头用户名密码字段
This commit is contained in:
parent
edc164298d
commit
45b59ea35c
102
app/lib/osd_client.js
Normal file
102
app/lib/osd_client.js
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
'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;
|
||||||
@ -55,6 +55,16 @@ module.exports = app => {
|
|||||||
allowNull: true,
|
allowNull: true,
|
||||||
comment: '码流类型(auto/main/sub)',
|
comment: '码流类型(auto/main/sub)',
|
||||||
},
|
},
|
||||||
|
username: {
|
||||||
|
type: STRING(100),
|
||||||
|
allowNull: true,
|
||||||
|
comment: '摄像头登录用户名',
|
||||||
|
},
|
||||||
|
password: {
|
||||||
|
type: STRING(100),
|
||||||
|
allowNull: true,
|
||||||
|
comment: '摄像头登录密码',
|
||||||
|
},
|
||||||
custom_name: {
|
custom_name: {
|
||||||
type: STRING(100),
|
type: STRING(100),
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
|
|||||||
@ -6,9 +6,9 @@ class CameraService extends MainService {
|
|||||||
constructor(ctx) {
|
constructor(ctx) {
|
||||||
super(ctx);
|
super(ctx);
|
||||||
const _noNeedLogin = '';
|
const _noNeedLogin = '';
|
||||||
const _noNeedRight = 'list,detail';
|
const _noNeedRight = 'list,detail,setOsd';
|
||||||
const _noNeedLog = '*';
|
const _noNeedLog = '*';
|
||||||
const _noNeedAuth = 'list,detail';
|
const _noNeedAuth = 'list,detail,setOsd';
|
||||||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +41,12 @@ class CameraService extends MainService {
|
|||||||
} catch (e) { return this.fail(e.message); }
|
} catch (e) { return this.fail(e.message); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async setOsd(data) {
|
||||||
|
try {
|
||||||
|
return this.success('设置成功', await this.service.systems.camera.setOsd(data));
|
||||||
|
} catch (e) { return this.fail(e.message); }
|
||||||
|
}
|
||||||
|
|
||||||
async delete(data) {
|
async delete(data) {
|
||||||
try {
|
try {
|
||||||
const { id } = data || {};
|
const { id } = data || {};
|
||||||
|
|||||||
@ -24,10 +24,10 @@ class CameraService extends Service {
|
|||||||
include: [{
|
include: [{
|
||||||
model: ctx.model.CameraServer,
|
model: ctx.model.CameraServer,
|
||||||
as: 'server',
|
as: 'server',
|
||||||
attributes: ['id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'status'],
|
attributes: [ 'id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'status' ],
|
||||||
required: false,
|
required: false,
|
||||||
}],
|
}],
|
||||||
order: [['created_at', 'DESC']],
|
order: [[ 'created_at', 'DESC' ]],
|
||||||
offset: (page - 1) * pageSize,
|
offset: (page - 1) * pageSize,
|
||||||
limit: pageSize,
|
limit: pageSize,
|
||||||
});
|
});
|
||||||
@ -42,7 +42,7 @@ class CameraService extends Service {
|
|||||||
include: [{
|
include: [{
|
||||||
model: ctx.model.CameraServer,
|
model: ctx.model.CameraServer,
|
||||||
as: 'server',
|
as: 'server',
|
||||||
attributes: ['id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'status'],
|
attributes: [ 'id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'status' ],
|
||||||
required: false,
|
required: false,
|
||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
@ -67,6 +67,8 @@ class CameraService extends Service {
|
|||||||
model: data.model || null,
|
model: data.model || null,
|
||||||
serial_number: data.serial_number || null,
|
serial_number: data.serial_number || null,
|
||||||
stream_type: data.stream_type || null,
|
stream_type: data.stream_type || null,
|
||||||
|
username: data.username || null,
|
||||||
|
password: data.password || null,
|
||||||
custom_name: data.custom_name || null,
|
custom_name: data.custom_name || null,
|
||||||
status: data.status !== undefined ? parseInt(data.status) : 1,
|
status: data.status !== undefined ? parseInt(data.status) : 1,
|
||||||
remark: data.remark || null,
|
remark: data.remark || null,
|
||||||
@ -87,6 +89,8 @@ class CameraService extends Service {
|
|||||||
if (data.model !== undefined) fields.model = data.model;
|
if (data.model !== undefined) fields.model = data.model;
|
||||||
if (data.serial_number !== undefined) fields.serial_number = data.serial_number;
|
if (data.serial_number !== undefined) fields.serial_number = data.serial_number;
|
||||||
if (data.stream_type !== undefined) fields.stream_type = data.stream_type;
|
if (data.stream_type !== undefined) fields.stream_type = data.stream_type;
|
||||||
|
if (data.username !== undefined) fields.username = data.username;
|
||||||
|
if (data.password !== undefined) fields.password = data.password;
|
||||||
if (data.custom_name !== undefined) {
|
if (data.custom_name !== undefined) {
|
||||||
if (data.custom_name !== null && String(data.custom_name).trim() === '') {
|
if (data.custom_name !== null && String(data.custom_name).trim() === '') {
|
||||||
throw new Error('自定义名称不能为空');
|
throw new Error('自定义名称不能为空');
|
||||||
@ -101,6 +105,65 @@ class CameraService extends Service {
|
|||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置摄像头 OSD 文字叠加
|
||||||
|
* @param {Object} data - 配置选项
|
||||||
|
* @param {number} data.id - 摄像头ID
|
||||||
|
* @param {string} data.text - OSD 叠加文字
|
||||||
|
* @param {number} [data.x] - X 坐标(-1 到 1,默认 -0.9)
|
||||||
|
* @param {number} [data.y] - Y 坐标(-1 到 1,默认 0.8)
|
||||||
|
* @return {Promise<Object>} 打印服务响应
|
||||||
|
*/
|
||||||
|
async setOsd(data) {
|
||||||
|
const { ctx } = this;
|
||||||
|
const { id, text, x, y } = data || {};
|
||||||
|
const warehouseId = ctx.user.store_id;
|
||||||
|
|
||||||
|
if (!id) throw new Error('摄像头ID不能为空');
|
||||||
|
if (!text) throw new Error('OSD叠加文字不能为空');
|
||||||
|
|
||||||
|
// 查询摄像头及关联服务器
|
||||||
|
const camera = await ctx.model.Camera.findOne({
|
||||||
|
where: { id: parseInt(id), warehouse_id: warehouseId },
|
||||||
|
include: [{
|
||||||
|
model: ctx.model.CameraServer,
|
||||||
|
as: 'server',
|
||||||
|
attributes: [ 'id', 'name', 'username', 'password', 'public_ip', 'internal_ip' ],
|
||||||
|
required: false,
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
if (!camera) throw new Error('摄像头不存在或无权操作');
|
||||||
|
|
||||||
|
const OsdClient = require('../../lib/osd_client');
|
||||||
|
const osdClient = new OsdClient(ctx);
|
||||||
|
|
||||||
|
const result = await osdClient.setOsd({
|
||||||
|
camera,
|
||||||
|
warehouseId,
|
||||||
|
text,
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.error) {
|
||||||
|
throw new Error(`打印服务请求失败: ${result.error}`);
|
||||||
|
}
|
||||||
|
if (!result.response || result.response.status !== 200) {
|
||||||
|
const status = result.response ? result.response.status : '无响应';
|
||||||
|
const resData = result.response && result.response.data;
|
||||||
|
const msg = (resData && resData.message) || (resData && typeof resData === 'string' ? resData : '') || '未知错误';
|
||||||
|
throw new Error(`打印服务返回异常(${status}): ${msg}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
camera_id: camera.id,
|
||||||
|
rtsp_url: result.request.rtspUrl,
|
||||||
|
osd_text: text,
|
||||||
|
position: { x: result.request.x, y: result.request.y },
|
||||||
|
print_service_response: result.response,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async delete(id) {
|
async delete(id) {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
const warehouseId = ctx.user.store_id;
|
const warehouseId = ctx.user.store_id;
|
||||||
@ -110,7 +173,7 @@ class CameraService extends Service {
|
|||||||
// 检查是否被工作台绑定
|
// 检查是否被工作台绑定
|
||||||
const boundWorkbenches = await ctx.model.Workbench.findAll({
|
const boundWorkbenches = await ctx.model.Workbench.findAll({
|
||||||
where: { warehouse_id: warehouseId },
|
where: { warehouse_id: warehouseId },
|
||||||
attributes: ['id', 'name'],
|
attributes: [ 'id', 'name' ],
|
||||||
});
|
});
|
||||||
for (const wb of boundWorkbenches) {
|
for (const wb of boundWorkbenches) {
|
||||||
const deviceIds = Array.isArray(wb.device_ids) ? wb.device_ids : [];
|
const deviceIds = Array.isArray(wb.device_ids) ? wb.device_ids : [];
|
||||||
|
|||||||
@ -649,6 +649,64 @@ class WorkbenchService extends Service {
|
|||||||
ctx.logger.info('[Workbench._packOrder] 写入 %d 个打包人员 operatorText=%s', users.length, operatorText);
|
ctx.logger.info('[Workbench._packOrder] 写入 %d 个打包人员 operatorText=%s', users.length, operatorText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 步骤7.5:下发 OSD 叠加到工作台绑定的摄像头(带 Redis 缓存去重)
|
||||||
|
const cameraIds = Array.isArray(deviceIds) ? deviceIds : [];
|
||||||
|
if (cameraIds.length > 0) {
|
||||||
|
let osdText = '';
|
||||||
|
if (isSandan === 1) {
|
||||||
|
// 散单:用运单号
|
||||||
|
const expressRecord = await ctx.model.OrderExpress.findOne({
|
||||||
|
where: { order_id: orderId, warehouse_id: warehouseId },
|
||||||
|
attributes: ['waybill_code'],
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
osdText = (expressRecord && expressRecord.waybill_code) || deliveryOrderCode;
|
||||||
|
} else {
|
||||||
|
// 团购:波次号+总量
|
||||||
|
osdText = `波次${actualWaveNo} 共${totalOrders}单`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入 OSD 缓存
|
||||||
|
try {
|
||||||
|
const osdCacheKey = `osd_${validWorkbenchId}`;
|
||||||
|
await this.app.setCacheAsync(osdCacheKey, osdText, 3600);
|
||||||
|
ctx.logger.info('[Workbench._packOrder] OSD缓存已写入 workbenchId=%d text=%s',
|
||||||
|
validWorkbenchId, osdText);
|
||||||
|
} catch (cacheErr) {
|
||||||
|
ctx.logger.warn('[Workbench._packOrder] OSD缓存写入失败 workbenchId=%d err=%s',
|
||||||
|
validWorkbenchId, cacheErr.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
const OsdClient = require('../../lib/osd_client');
|
||||||
|
const osdClient = new OsdClient(ctx);
|
||||||
|
|
||||||
|
// 异步下发,不阻塞打包主流程
|
||||||
|
Promise.all(cameraIds.map(async cameraId => {
|
||||||
|
try {
|
||||||
|
const camera = await ctx.model.Camera.findOne({
|
||||||
|
where: { id: cameraId, warehouse_id: warehouseId },
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
if (!camera || !camera.ip) return;
|
||||||
|
|
||||||
|
const result = await osdClient.setOsd({
|
||||||
|
camera,
|
||||||
|
warehouseId,
|
||||||
|
text: osdText,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result.success) {
|
||||||
|
ctx.logger.warn('[Workbench._packOrder] OSD下发失败 cameraId=%d error=%s',
|
||||||
|
cameraId, result.error || '打印服务返回异常');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
ctx.logger.warn('[Workbench._packOrder] OSD下发异常 cameraId=%d err=%s', cameraId, err.message);
|
||||||
|
}
|
||||||
|
})).catch(err => {
|
||||||
|
ctx.logger.warn('[Workbench._packOrder] OSD批量下发异常 err=%s', err.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 步骤8:写入操作日志(散单写单个订单,团购批量写波次所有订单)
|
// 步骤8:写入操作日志(散单写单个订单,团购批量写波次所有订单)
|
||||||
await this._writePackOperationLog({
|
await this._writePackOperationLog({
|
||||||
warehouseId,
|
warehouseId,
|
||||||
@ -1188,10 +1246,27 @@ class WorkbenchService extends Service {
|
|||||||
ctx.logger.warn('[Workbench._queryOrder] 查询订单商品失败: %s', itemsErr.message);
|
ctx.logger.warn('[Workbench._queryOrder] 查询订单商品失败: %s', itemsErr.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 步骤5:查询货主备注(从合约表 store_warehouse.store_remask)
|
||||||
|
let storeRemark = '';
|
||||||
|
const storeId = Number(orderRow.store_id || 0);
|
||||||
|
if (storeId > 0) {
|
||||||
|
try {
|
||||||
|
const swRow = await ctx.model.StoreWarehouse.findOne({
|
||||||
|
where: { store_id: storeId, warehouse_id: warehouseId },
|
||||||
|
attributes: ['store_remask'],
|
||||||
|
raw: true,
|
||||||
|
});
|
||||||
|
storeRemark = (swRow && swRow.store_remask) ? String(swRow.store_remask).trim() : '';
|
||||||
|
} catch (swErr) {
|
||||||
|
ctx.logger.warn('[Workbench._queryOrder] 查询货主备注失败 storeId=%d: %s', storeId, swErr.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
barcode: normalizedBarcode,
|
barcode: normalizedBarcode,
|
||||||
waybill_code: waybillCode,
|
waybill_code: waybillCode,
|
||||||
isSandan,
|
isSandan,
|
||||||
|
store_remark: storeRemark,
|
||||||
express: {
|
express: {
|
||||||
logistics_name: expressRecord.logistics_name,
|
logistics_name: expressRecord.logistics_name,
|
||||||
weighing_weight: expressRecord.weighing_weight,
|
weighing_weight: expressRecord.weighing_weight,
|
||||||
|
|||||||
@ -0,0 +1,43 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const TABLE_NAME = 'wms_camera';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增摄像头认证字段:username(用户名)、password(密码)。
|
||||||
|
* 用于保存摄像头登录认证信息。
|
||||||
|
*/
|
||||||
|
module.exports = {
|
||||||
|
up: async (queryInterface, Sequelize) => {
|
||||||
|
const columns = [
|
||||||
|
{
|
||||||
|
name: 'username',
|
||||||
|
def: { type: Sequelize.STRING(100), allowNull: true, comment: '摄像头登录用户名', after: 'stream_type' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'password',
|
||||||
|
def: { type: Sequelize.STRING(100), allowNull: true, comment: '摄像头登录密码', after: 'username' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const col of columns) {
|
||||||
|
const tableInfo = await queryInterface.describeTable(TABLE_NAME);
|
||||||
|
if (!tableInfo[col.name]) {
|
||||||
|
await queryInterface.addColumn(TABLE_NAME, col.name, col.def);
|
||||||
|
console.log(`[Migration] wms_camera.${col.name} 已添加`);
|
||||||
|
} else {
|
||||||
|
console.log(`[Migration] wms_camera.${col.name} 已存在,跳过`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
down: async queryInterface => {
|
||||||
|
const columns = ['password', 'username'];
|
||||||
|
for (const col of columns) {
|
||||||
|
const tableInfo = await queryInterface.describeTable(TABLE_NAME);
|
||||||
|
if (tableInfo[col]) {
|
||||||
|
await queryInterface.removeColumn(TABLE_NAME, col);
|
||||||
|
console.log(`[Migration] wms_camera.${col} 已回滚删除`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user