packServer/backend/app/lib/workstation-snapshot.js

87 lines
3.0 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';
/**
* 工作台快照(写进 `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),
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,
};