- 工作台成员扫码登入: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>
214 lines
8.8 KiB
JavaScript
214 lines
8.8 KiB
JavaScript
'use strict';
|
||
|
||
const { app, mock, assert } = require('egg-mock/bootstrap');
|
||
|
||
/**
|
||
* 面单摄像头识别配置(`service/label_scan`)
|
||
*
|
||
* 覆盖三件最容易错的事:
|
||
* 1. **散单和团购都支持**面单摄像头,但记录规则不同(下发 `wsType`,由设备端决定);
|
||
* 2. **上报通道只认本机的**:别的设备的通道不能用来上报(服务端按 scanner+channel 找工作台快照);
|
||
* 工作台没接扫码枪时自动用虚拟通道 `CAM-<工作台id>`;
|
||
* 3. **缺条件要给提示**(录像机没密码等),不能让设备端拿到半份配置。
|
||
*/
|
||
describe('test/app/service/label-scan.test.js', () => {
|
||
const subjectId = 1;
|
||
const scanner = { id: 7, subjectId, sn: 'MINGREN' };
|
||
|
||
const instance = (data) => Object.assign({
|
||
toJSON: () => data,
|
||
update: async (fields) => Object.assign(data, fields),
|
||
}, data);
|
||
|
||
const workstation = (id, name, wsType) => instance({ id, code: `WS-${id}`, name, wsType, status: 1 });
|
||
|
||
const cameraLink = (id, workstationId, camera) => instance({
|
||
id,
|
||
workstationId,
|
||
cameraId: camera.id,
|
||
streamType: 0,
|
||
isLabelCamera: 1,
|
||
camera: instance({
|
||
id: camera.id,
|
||
name: camera.name,
|
||
channelNo: camera.channelNo,
|
||
enabled: 1,
|
||
status: 1,
|
||
nvr: instance({ id: 1, name: '录像机1', host: '192.168.8.241', port: 8000, username: 'admin' }),
|
||
}),
|
||
});
|
||
|
||
const channelLink = (workstationId, scannerId, channelId, channelName, sortNo = 1) =>
|
||
instance({ id: workstationId * 10 + sortNo, workstationId, scannerId, channelId, channelName, sortNo });
|
||
|
||
/** 录像机密码(测试里按需改) */
|
||
let nvrPassword = 'p@ss';
|
||
|
||
// ⚠️ 每个用例都要重新 mock:egg-mock 会在用例结束后把 mock 还原
|
||
beforeEach(() => {
|
||
nvrPassword = 'p@ss';
|
||
app.mockService('nvr', 'passwordOf', () => nvrPassword);
|
||
});
|
||
|
||
function mockModels({ workstations, links, channels }) {
|
||
mock(app, 'model', {
|
||
Workstation: { async findAll() { return workstations; } },
|
||
WorkstationCamera: { async findAll() { return links; } },
|
||
WorkstationScanChannel: {
|
||
async findAll() { return channels; },
|
||
async findOrCreate({ defaults }) {
|
||
return [ instance(Object.assign({}, defaults)), true ];
|
||
},
|
||
},
|
||
ScanChannel: {
|
||
async findOne() { return null; },
|
||
async create(fields) { return instance(fields); },
|
||
},
|
||
Nvr: { async findByPk() { return instance({ id: 1, name: '录像机1', host: '192.168.8.241', passwordEnc: 'x' }); } },
|
||
});
|
||
}
|
||
|
||
it('散单工作台 → 下发一路(工作台/摄像头/上报通道/录像机凭据)', async () => {
|
||
mockModels({
|
||
workstations: [ workstation(2, '散单1号', 1) ],
|
||
links: [ cameraLink(11, 2, { id: 12, name: '测试散单面单', channelNo: 64 }) ],
|
||
channels: [ channelLink(2, 7, '2', '测试安卓') ],
|
||
});
|
||
|
||
const ctx = app.createAnonymousContext();
|
||
const data = await ctx.service.labelScan.configOf(scanner);
|
||
|
||
assert(data.items.length === 1, JSON.stringify(data));
|
||
const item = data.items[0];
|
||
assert(item.workstationId === 2 && item.workstationName === '散单1号');
|
||
assert(item.channelId === '2' && item.channelName === '测试安卓');
|
||
assert(item.virtualChannel === false, '绑了真实扫码枪就不是虚拟通道');
|
||
assert(item.cameraId === 12 && item.cameraChannelNo === 64);
|
||
assert(item.streamType === 0);
|
||
assert(item.nvr.host === '192.168.8.241' && item.nvr.password === 'p@ss', '要带凭据给设备拉流');
|
||
assert(/^sha1:[0-9a-f]{16}$/.test(data.revision), data.revision);
|
||
assert(data.warnings.length === 0);
|
||
});
|
||
|
||
it('工作台没绑本机通道 → 用虚拟通道 CAM-<工作台id> 上报(摄像头替代扫码枪)', async () => {
|
||
mockModels({
|
||
workstations: [ workstation(2, '散单1号', 1) ],
|
||
links: [ cameraLink(11, 2, { id: 12, name: '测试散单面单', channelNo: 64 }) ],
|
||
channels: [], // 本机没有绑任何通道
|
||
});
|
||
|
||
const ctx = app.createAnonymousContext();
|
||
const data = await ctx.service.labelScan.configOf(scanner);
|
||
|
||
assert(data.items.length === 1, JSON.stringify(data));
|
||
const item = data.items[0];
|
||
assert(item.channelId === 'CAM-2', item.channelId);
|
||
assert(item.channelName === '面单摄像头·测试散单面单', item.channelName);
|
||
assert(item.virtualChannel === true, '要标记成虚拟通道,设备端不用去找本机通道');
|
||
// 虚拟通道也要进 channels:设备端靠它判断走「取第一次」还是「取最后一次」
|
||
assert(data.channels.some(row => row.channelId === 'CAM-2' && row.wsType === 1), JSON.stringify(data.channels));
|
||
assert(data.warnings.length === 0, '不需要报警:摄像头自己就是一条通道');
|
||
});
|
||
|
||
it('历史遗留的虚拟通道绑定 → 仍按虚拟通道下发(不会当成真实扫码枪)', async () => {
|
||
mockModels({
|
||
workstations: [ workstation(2, '散单1号', 1) ],
|
||
links: [ cameraLink(11, 2, { id: 12, name: '测试散单面单', channelNo: 64 }) ],
|
||
// 上一轮拉配置时自愈出来的绑定,会被当成「本机通道」查出来
|
||
channels: [ channelLink(2, 7, 'CAM-2', '面单摄像头·测试散单面单') ],
|
||
});
|
||
|
||
const ctx = app.createAnonymousContext();
|
||
const data = await ctx.service.labelScan.configOf(scanner);
|
||
|
||
assert(data.items.length === 1, JSON.stringify(data));
|
||
assert(data.items[0].channelId === 'CAM-2');
|
||
assert(data.items[0].virtualChannel === true);
|
||
assert(data.channels.length === 1 && data.channels[0].channelId === 'CAM-2');
|
||
});
|
||
|
||
it('录像机没密码 → 不下发,给提示', async () => {
|
||
mockModels({
|
||
workstations: [ workstation(2, '散单1号', 1) ],
|
||
links: [ cameraLink(11, 2, { id: 12, name: '测试散单面单', channelNo: 64 }) ],
|
||
channels: [ channelLink(2, 7, '2', '测试安卓') ],
|
||
});
|
||
nvrPassword = '';
|
||
|
||
const ctx = app.createAnonymousContext();
|
||
const data = await ctx.service.labelScan.configOf(scanner);
|
||
|
||
assert(data.items.length === 0);
|
||
assert(/没有保存密码/.test(data.warnings[0].message), data.warnings[0].message);
|
||
nvrPassword = 'p@ss';
|
||
});
|
||
|
||
it('revision 随内容变化(改通道 / 改密码都会变)', async () => {
|
||
mockModels({
|
||
workstations: [ workstation(2, '散单1号', 1) ],
|
||
links: [ cameraLink(11, 2, { id: 12, name: '测试散单面单', channelNo: 64 }) ],
|
||
channels: [ channelLink(2, 7, '2', '测试安卓') ],
|
||
});
|
||
|
||
const ctx = app.createAnonymousContext();
|
||
const first = await ctx.service.labelScan.configOf(scanner);
|
||
|
||
nvrPassword = 'another';
|
||
const second = await ctx.service.labelScan.configOf(scanner);
|
||
nvrPassword = 'p@ss';
|
||
|
||
assert(first.revision !== second.revision, '密码变了必须换 revision,设备才会重建');
|
||
});
|
||
|
||
describe('notifyChanged(配置变了要立刻叫醒设备,别等兜底轮询)', () => {
|
||
it('按主体给每台启用中的设备入队 labelScan.refresh', async () => {
|
||
const enqueued = [];
|
||
app.mockService('edgeTask', 'enqueue', async (scannerId, type, payload) => {
|
||
enqueued.push({ scannerId, type, payload });
|
||
return { id: `task-${scannerId}` };
|
||
});
|
||
mock(app, 'model', {
|
||
Scanner: {
|
||
async findAll({ where }) {
|
||
assert(where.subjectId === 1 && where.status === 1, '只通知本主体启用中的设备');
|
||
return [ { id: 7 }, { id: 9 } ];
|
||
},
|
||
},
|
||
});
|
||
|
||
const ctx = app.createAnonymousContext();
|
||
const count = await ctx.service.labelScan.notifyChanged(1);
|
||
|
||
assert(count === 2, `应通知 2 台,实际 ${count}`);
|
||
assert(enqueued.length === 2, JSON.stringify(enqueued));
|
||
assert(enqueued.every(item => item.type === 'labelScan.refresh'), '任务类型必须是设备端认识的 labelScan.refresh');
|
||
assert(enqueued.map(item => item.scannerId).join(',') === '7,9');
|
||
});
|
||
|
||
it('没有主体 id 时什么都不做(不查库、不入队)', async () => {
|
||
let enqueued = 0;
|
||
let queried = false;
|
||
app.mockService('edgeTask', 'enqueue', async () => { enqueued++; });
|
||
mock(app, 'model', {
|
||
Scanner: { async findAll() { queried = true; return []; } },
|
||
});
|
||
|
||
const ctx = app.createAnonymousContext();
|
||
const count = await ctx.service.labelScan.notifyChanged(0);
|
||
|
||
assert(count === 0);
|
||
assert(enqueued === 0 && queried === false);
|
||
});
|
||
|
||
it('入队失败不抛错(配置已落库,不能因为通知失败回滚)', async () => {
|
||
app.mockService('edgeTask', 'enqueue', async () => { throw new Error('redis down'); });
|
||
mock(app, 'model', { Scanner: { async findAll() { return [ { id: 7 } ]; } } });
|
||
|
||
const ctx = app.createAnonymousContext();
|
||
const count = await ctx.service.labelScan.notifyChanged(1);
|
||
|
||
assert(count === 0, '失败返回 0,不向调用方抛异常');
|
||
});
|
||
});
|
||
});
|