packServer/backend/app/lib/workstation-snapshot.js
liufumingren 2ed4fa8483 feat(workstation): 一台工作台支持多路面单摄像头 + 绑定管理可改画布尺寸
- 面单摄像头不再限制一台工作台一路:设置新的不清旧标记,取消最后一路时才清理虚拟通道
- 面单识别配置按 工作台+摄像头 逐路下发,多路共用上报通道;多路时虚拟通道名改用工作台名
- 绑定管理新增画布尺寸(宽×高)编辑:160~7680、自动取偶数;改尺寸时已摆放摄像头按比例缩放
- 画布组件在宽度变化时重新计算等比缩放,避免新画布溢出或缩不满
- 补充后端单测与相关文档

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-08 23:45:25 +08:00

89 lines
3.1 KiB
JavaScript
Raw Permalink 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';
/**
* 工作台快照(写进 `scan_records.workstation_snapshot`)
*
* 一句话:把扫码那一刻的「工作台 + 录像机 + 摄像头 + 画布布局 + 录制范围」冻成一份 JSON,
* 以后工作台改过/删了,历史运单照样能按**当时**的配置去拉回放。
*
* 为什么单独放一层(不在 service 里):
* - `service/pack` 入库时要用它;`scripts/backfill-snapshot.js` 补历史数据也要用它 ——
* 两边必须是同一份逻辑,否则补出来的快照和实时写入的结构会不一致。
*
* ⚠️ **不含录像机密码**:密码只在本地软件真正登录 NVR 时按需取(本地软件持有用户令牌),
* 后端不把密钥塞进业务数据里。
*/
/** 快照版本(结构变了要升,方便前端/桌面端兼容旧数据) */
const SNAPSHOT_VERSION = 1;
function asInt(value, fallback) {
const num = Number(value);
return Number.isFinite(num) ? num : fallback;
}
/**
* 把一个工作台(含摄像头绑定)压成快照对象
*
* @param {object} workstation 工作台(`cameraLinks[].camera.nvr` 是 sequelize 实例或普通对象都行)
* @param {{channelId?:string, channelName?:string}} [link] 绑定的扫码通道
* @return {object|null} 没有工作台时返回 null
*/
function buildSnapshot(workstation, link) {
if (!workstation) return null;
const cameras = (workstation.cameraLinks || [])
.filter(item => item && item.camera)
.map(item => {
const camera = item.camera;
const nvr = camera.nvr || {};
return {
cameraId: Number(camera.id),
cameraName: camera.name || camera.channelName || `通道${camera.channelNo}`,
channelNo: Number(camera.channelNo) || 0,
streamType: asInt(item.streamType, 0),
/** 面单摄像头(散单/团购都可能为 1,一台工作台可多路) */
isLabelCamera: asInt(item.isLabelCamera, 0),
nvrId: Number(camera.nvrId) || 0,
nvrName: nvr.name || '',
nvrHost: nvr.host || '',
nvrPort: asInt(nvr.port, 8000),
nvrUsername: nvr.username || 'admin',
layout: {
x: asInt(item.x, 0),
y: asInt(item.y, 0),
width: asInt(item.width, 0),
height: asInt(item.height, 0),
zIndex: asInt(item.zIndex, 0),
sortNo: asInt(item.sortNo, 0),
},
};
})
.sort((a, b) => a.layout.sortNo - b.layout.sortNo || a.cameraId - b.cameraId);
return {
v: SNAPSHOT_VERSION,
capturedAt: new Date(),
channelId: link ? link.channelId : '',
channelName: link ? link.channelName || '' : '',
workstation: {
id: Number(workstation.id),
code: workstation.code,
name: workstation.name || '',
wsType: asInt(workstation.wsType, 1),
recordBeforeSec: asInt(workstation.recordBeforeSec, 60),
recordAfterSec: asInt(workstation.recordAfterSec, 60),
recordMaxSec: asInt(workstation.recordMaxSec, 240),
canvasWidth: asInt(workstation.canvasWidth, 1920),
canvasHeight: asInt(workstation.canvasHeight, 1080),
},
cameras,
};
}
module.exports = {
SNAPSHOT_VERSION,
asInt,
buildSnapshot,
};