packServer/backend/app/lib/workstation-snapshot.js
liufumingren f8aa4c4899 feat(工作台): 成员扫码登入、面单摄像头与虚拟通道
- 工作台成员扫码登入:workstation_login_records 记录谁在什么时候登入/下线了哪台工作台,
  超时(团购 1h / 散单 30min)由 schedule/workstation_idle_offline 自动下线;
  运单查询的 packers 取当时的登入成员
- 面单摄像头识别:workstation_cameras.is_label_camera + service/label_scan 下发本机该跑的识别配置,
  设备端(桌面端 LabelScan 工作线程)按配置做面单识别并上报
- 虚拟通道 CAM-<工作台id>:工作台没接扫码枪时,面单摄像头那一路顶替扫码枪上报,records 规则按 ws_type 区分
- 桌面端:新增 LabelScanMonitor/LabelScanWorker/ScanDeviceKind 与面单预览页,扫描通道支持改名,静默窗口 5s

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-29 17:19:24 +08:00

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