- 工作台成员扫码登入: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>
228 lines
7.5 KiB
JavaScript
228 lines
7.5 KiB
JavaScript
'use strict';
|
||
|
||
const jwt = require('jsonwebtoken');
|
||
const { app, mock, assert } = require('egg-mock/bootstrap');
|
||
|
||
/** 伪造的 sequelize 实例(toJSON + update 直接改同一份数据) */
|
||
const instance = (data) => Object.assign(
|
||
{
|
||
toJSON: () => data,
|
||
update: async (fields) => Object.assign(data, fields),
|
||
},
|
||
data,
|
||
);
|
||
|
||
/**
|
||
* 工作台「面单摄像头」(`workstation_cameras.is_label_camera`)
|
||
*
|
||
* 1. 散单 / 团购工作台都可以设置、取消,一台工作台只保留一路(设新的先清旧的);
|
||
* 2. 记录规则的区别在设备端(按工作台类型:散单取第一次、团购取最后一次),后端只存标记。
|
||
*/
|
||
describe('test/app/controller/workstation-camera-label.test.js', () => {
|
||
const me = { id: 1, username: 'admin', nickname: '小王', status: 1, passwordChangedAt: null };
|
||
const SUBJECT_ID = 5;
|
||
const WS_ID = 3;
|
||
const CAMERA_ID = 21;
|
||
|
||
const token = () => {
|
||
const cfg = app.config.jwt;
|
||
const signed = jwt.sign({ sub: String(me.id), typ: 'user' }, cfg.secret, {
|
||
algorithm: 'HS256',
|
||
expiresIn: 3600,
|
||
issuer: cfg.issuer,
|
||
audience: cfg.audience,
|
||
});
|
||
return `Bearer ${signed}`;
|
||
};
|
||
const withToken = (req) => req.set('Authorization', token());
|
||
const withSubject = (req) => withToken(req).set('X-Subject-Id', String(SUBJECT_ID));
|
||
|
||
const subjectInstance = () => instance({
|
||
id: SUBJECT_ID,
|
||
name: '测试主体',
|
||
adminUserId: 1,
|
||
status: 1,
|
||
expireAt: new Date(Date.now() + 86400000),
|
||
});
|
||
|
||
const memberInstance = () => instance({
|
||
id: 11,
|
||
subjectId: SUBJECT_ID,
|
||
userId: 1,
|
||
name: 'K0012',
|
||
role: 1,
|
||
status: 1,
|
||
});
|
||
|
||
const workstation = (wsType) => instance({
|
||
id: WS_ID,
|
||
subjectId: SUBJECT_ID,
|
||
code: 'WS-01',
|
||
name: '1号工作台',
|
||
wsType,
|
||
status: 1,
|
||
});
|
||
|
||
/** 已绑定的摄像头(详情里的 `cameras[]`) */
|
||
const cameraLink = () => instance({
|
||
id: 31,
|
||
workstationId: WS_ID,
|
||
cameraId: CAMERA_ID,
|
||
sortNo: 1,
|
||
streamType: 0,
|
||
isLabelCamera: 0,
|
||
camera: {
|
||
id: CAMERA_ID,
|
||
name: '面单特写',
|
||
channelNo: 2,
|
||
nvr: { id: 9, name: '录像机', host: '192.168.1.64', port: 8000 },
|
||
},
|
||
});
|
||
|
||
/** 主体 / 用户 / 成员三件套(鉴权与主体上下文都要用) */
|
||
function baseModels(extra = {}) {
|
||
return Object.assign({
|
||
User: { async findByPk() { return instance(me); } },
|
||
Subject: { async findByPk() { return subjectInstance(); } },
|
||
SubjectMember: { async findOne() { return memberInstance(); } },
|
||
}, extra);
|
||
}
|
||
|
||
it('PUT .../cameras/:cameraId/label:散单设置面单摄像头(先清掉旧的)', async () => {
|
||
const ws = workstation(1);
|
||
const link = cameraLink();
|
||
const cleared = [];
|
||
|
||
mock(app, 'model', baseModels({
|
||
Workstation: { async findByPk() { return ws; } },
|
||
WorkstationCamera: {
|
||
async findOne() { return link; },
|
||
async update(fields, options) {
|
||
cleared.push({ fields, options });
|
||
return [ 1 ];
|
||
},
|
||
async findAll() { return [ link ]; },
|
||
},
|
||
WorkstationScanChannel: { async findAll() { return []; } },
|
||
}));
|
||
|
||
const res = await withSubject(app.httpRequest()
|
||
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
|
||
.send({ isLabelCamera: true }))
|
||
.expect(200);
|
||
|
||
assert(res.body.data.cameras[0].isLabelCamera === 1, '设置后详情里要带 isLabelCamera=1');
|
||
assert(link.toJSON().isLabelCamera === 1, '绑定记录要落库为 1');
|
||
assert(cleared.length === 1, '设新的之前要先清掉本工作台旧的面单标记');
|
||
assert(cleared[0].fields.isLabelCamera === 0);
|
||
assert(cleared[0].options.where.workstationId === WS_ID);
|
||
assert(cleared[0].options.where.isLabelCamera === 1);
|
||
});
|
||
|
||
it('PUT .../cameras/:cameraId/label:团购工作台也能设置(设备端按 wsType 决定记录规则)', async () => {
|
||
const ws = workstation(2);
|
||
const link = cameraLink();
|
||
const cleared = [];
|
||
|
||
mock(app, 'model', baseModels({
|
||
Workstation: { async findByPk() { return ws; } },
|
||
WorkstationCamera: {
|
||
async findOne() { return link; },
|
||
async update(fields, options) {
|
||
cleared.push({ fields, options });
|
||
return [ 1 ];
|
||
},
|
||
async findAll() { return [ link ]; },
|
||
},
|
||
WorkstationScanChannel: { async findAll() { return []; } },
|
||
}));
|
||
|
||
const res = await withSubject(app.httpRequest()
|
||
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
|
||
.send({ isLabelCamera: true }))
|
||
.expect(200);
|
||
|
||
assert(res.body.data.cameras[0].isLabelCamera === 1, '团购设置后详情里也要带 isLabelCamera=1');
|
||
assert(link.toJSON().isLabelCamera === 1);
|
||
assert(cleared.length === 1, '团购同样只保留一路');
|
||
});
|
||
|
||
it('PUT .../cameras/:cameraId/label:不传 isLabelCamera = 设置;false = 取消', async () => {
|
||
const ws = workstation(1);
|
||
const link = cameraLink();
|
||
const released = [];
|
||
|
||
mock(app, 'model', baseModels({
|
||
Workstation: { async findByPk() { return ws; } },
|
||
WorkstationCamera: {
|
||
async findOne() { return link; },
|
||
async update() { return [ 1 ]; },
|
||
async findAll() { return [ link ]; },
|
||
},
|
||
WorkstationScanChannel: {
|
||
async findAll() { return []; },
|
||
async destroy(options) {
|
||
released.push(options.where);
|
||
return 1;
|
||
},
|
||
async count() { return 0; },
|
||
},
|
||
ScanChannel: { async destroy() { return 1; } },
|
||
}));
|
||
|
||
await withSubject(app.httpRequest()
|
||
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`))
|
||
.expect(200);
|
||
assert(link.toJSON().isLabelCamera === 1, '不传按「设置」处理');
|
||
assert(released.length === 0, '设置时不动通道');
|
||
|
||
const res = await withSubject(app.httpRequest()
|
||
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
|
||
.send({ isLabelCamera: false }))
|
||
.expect(200);
|
||
assert(link.toJSON().isLabelCamera === 0, '传 false 要取消面单');
|
||
assert(res.body.data.cameras[0].isLabelCamera === 0);
|
||
assert(released.length === 1, '取消面单要顺手清掉虚拟通道');
|
||
assert(released[0].channelId === `CAM-${WS_ID}`, JSON.stringify(released[0]));
|
||
});
|
||
|
||
it('设置/取消面单摄像头 → 立刻叫醒设备重拉配置(不能等兜底轮询)', async () => {
|
||
const ws = workstation(1);
|
||
const link = cameraLink();
|
||
const notified = [];
|
||
|
||
app.mockService('labelScan', 'notifyChanged', async subjectId => {
|
||
notified.push(subjectId);
|
||
return 1;
|
||
});
|
||
|
||
mock(app, 'model', baseModels({
|
||
Workstation: { async findByPk() { return ws; } },
|
||
WorkstationCamera: {
|
||
async findOne() { return link; },
|
||
async update() { return [ 1 ]; },
|
||
async findAll() { return [ link ]; },
|
||
},
|
||
WorkstationScanChannel: {
|
||
async findAll() { return []; },
|
||
async destroy() { return 1; },
|
||
async count() { return 0; },
|
||
},
|
||
ScanChannel: { async destroy() { return 1; } },
|
||
}));
|
||
|
||
await withSubject(app.httpRequest()
|
||
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
|
||
.send({ isLabelCamera: true }))
|
||
.expect(200);
|
||
|
||
await withSubject(app.httpRequest()
|
||
.put(`/api/web/workstations/${WS_ID}/cameras/${CAMERA_ID}/label`)
|
||
.send({ isLabelCamera: false }))
|
||
.expect(200);
|
||
|
||
assert(notified.length === 2, `设置和取消都要通知,实际 ${notified.length} 次`);
|
||
assert(notified.every(id => id === SUBJECT_ID), JSON.stringify(notified));
|
||
});
|
||
});
|