- 新增 camera model username/password 字段 - 新增 osd_client.js OSD 客户端库 - 工作台打包时自动叠加 OSD 信息到摄像头(带 Redis 缓存去重) - 新增迁移脚本:摄像头用户名密码字段
190 lines
7.2 KiB
JavaScript
190 lines
7.2 KiB
JavaScript
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
|
||
class CameraService extends Service {
|
||
async list(params = {}) {
|
||
const { ctx } = this;
|
||
const { Op } = ctx.app.Sequelize;
|
||
const warehouseId = ctx.user.store_id;
|
||
const { pagination = {}, where = {} } = params;
|
||
|
||
const page = Math.max(1, parseInt(pagination.page) || 1);
|
||
const pageSize = Math.min(200, Math.max(1, parseInt(pagination.pageSize) || 20));
|
||
|
||
const queryWhere = { warehouse_id: warehouseId };
|
||
if (where.server_id) queryWhere.server_id = parseInt(where.server_id);
|
||
if (where.custom_name) queryWhere.custom_name = { [Op.like]: `%${where.custom_name}%` };
|
||
if (where.status !== undefined && where.status !== null && where.status !== '') {
|
||
queryWhere.status = parseInt(where.status);
|
||
}
|
||
|
||
const { count, rows } = await ctx.model.Camera.findAndCountAll({
|
||
where: queryWhere,
|
||
include: [{
|
||
model: ctx.model.CameraServer,
|
||
as: 'server',
|
||
attributes: [ 'id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'status' ],
|
||
required: false,
|
||
}],
|
||
order: [[ 'created_at', 'DESC' ]],
|
||
offset: (page - 1) * pageSize,
|
||
limit: pageSize,
|
||
});
|
||
|
||
return { list: rows, pagination: { page, pageSize, total: count, totalPages: Math.ceil(count / pageSize), hasPrevious: page > 1, hasNext: page * pageSize < count } };
|
||
}
|
||
|
||
async detail(id) {
|
||
const { ctx } = this;
|
||
const record = await ctx.model.Camera.findOne({
|
||
where: { id, warehouse_id: ctx.user.store_id },
|
||
include: [{
|
||
model: ctx.model.CameraServer,
|
||
as: 'server',
|
||
attributes: [ 'id', 'name', 'brand', 'public_ip', 'internal_ip', 'public_port', 'internal_port', 'status' ],
|
||
required: false,
|
||
}],
|
||
});
|
||
if (!record) throw new Error('摄像头不存在或无权访问');
|
||
return record;
|
||
}
|
||
|
||
async create(data) {
|
||
const { ctx } = this;
|
||
if (!data.server_id) throw new Error('所属服务器ID不能为空');
|
||
if (data.channel_id === undefined || data.channel_id === null) throw new Error('通道编号不能为空');
|
||
if (data.custom_name !== undefined && data.custom_name !== null && String(data.custom_name).trim() === '') {
|
||
throw new Error('自定义名称不能为空');
|
||
}
|
||
return await ctx.model.Camera.create({
|
||
warehouse_id: ctx.user.store_id,
|
||
server_id: parseInt(data.server_id),
|
||
channel_id: parseInt(data.channel_id),
|
||
channel_name: data.channel_name || null,
|
||
ip: data.ip || null,
|
||
port: data.port !== undefined && data.port !== null ? parseInt(data.port) : null,
|
||
model: data.model || null,
|
||
serial_number: data.serial_number || null,
|
||
stream_type: data.stream_type || null,
|
||
username: data.username || null,
|
||
password: data.password || null,
|
||
custom_name: data.custom_name || null,
|
||
status: data.status !== undefined ? parseInt(data.status) : 1,
|
||
remark: data.remark || null,
|
||
});
|
||
}
|
||
|
||
async update(id, data) {
|
||
const { ctx } = this;
|
||
const record = await ctx.model.Camera.findOne({ where: { id, warehouse_id: ctx.user.store_id } });
|
||
if (!record) throw new Error('摄像头不存在或无权操作');
|
||
|
||
const fields = {};
|
||
if (data.server_id !== undefined) fields.server_id = parseInt(data.server_id);
|
||
if (data.channel_id !== undefined) fields.channel_id = parseInt(data.channel_id);
|
||
if (data.channel_name !== undefined) fields.channel_name = data.channel_name;
|
||
if (data.ip !== undefined) fields.ip = data.ip;
|
||
if (data.port !== undefined) fields.port = data.port !== null ? parseInt(data.port) : null;
|
||
if (data.model !== undefined) fields.model = data.model;
|
||
if (data.serial_number !== undefined) fields.serial_number = data.serial_number;
|
||
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 !== null && String(data.custom_name).trim() === '') {
|
||
throw new Error('自定义名称不能为空');
|
||
}
|
||
fields.custom_name = data.custom_name;
|
||
}
|
||
if (data.status !== undefined) fields.status = parseInt(data.status);
|
||
if (data.remark !== undefined) fields.remark = data.remark;
|
||
if (Object.keys(fields).length === 0) throw new Error('没有需要更新的字段');
|
||
|
||
await record.update(fields);
|
||
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) {
|
||
const { ctx } = this;
|
||
const warehouseId = ctx.user.store_id;
|
||
const record = await ctx.model.Camera.findOne({ where: { id, warehouse_id: warehouseId } });
|
||
if (!record) throw new Error('摄像头不存在或无权操作');
|
||
|
||
// 检查是否被工作台绑定
|
||
const boundWorkbenches = await ctx.model.Workbench.findAll({
|
||
where: { warehouse_id: warehouseId },
|
||
attributes: [ 'id', 'name' ],
|
||
});
|
||
for (const wb of boundWorkbenches) {
|
||
const deviceIds = Array.isArray(wb.device_ids) ? wb.device_ids : [];
|
||
if (deviceIds.includes(id)) {
|
||
throw new Error(`摄像头已被工作台「${wb.name}」绑定,请先解绑后再删除`);
|
||
}
|
||
}
|
||
|
||
await record.destroy();
|
||
}
|
||
}
|
||
|
||
module.exports = CameraService;
|