Compare commits
4 Commits
7923c3ea9a
...
2503aca841
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2503aca841 | ||
|
|
99091ac22d | ||
|
|
9626ba78e0 | ||
|
|
6b050a6865 |
@ -3,19 +3,19 @@ const { Controller } = require('egg');
|
||||
class HikvisionController extends Controller {
|
||||
/**
|
||||
* POST /hikvision/forceRecord
|
||||
* 强制推送录制任务,事务重置录像行并推送
|
||||
* 强制推送录制任务(支持打包 pack_task_id / 拆包 unpack_id+task_type),事务重置录像行并推送
|
||||
*/
|
||||
async forceRecord() {
|
||||
const { ctx } = this;
|
||||
const { pack_task_id } = ctx.request.body;
|
||||
const { pack_task_id, unpack_id, task_type } = ctx.request.body;
|
||||
|
||||
if (!pack_task_id) {
|
||||
ctx.body = { status: 'fail', message: '缺少 pack_task_id' };
|
||||
if (!pack_task_id && !unpack_id) {
|
||||
ctx.body = { status: 'fail', message: '缺少 pack_task_id 或 unpack_id' };
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await ctx.service.hikvision.forceRecord({ pack_task_id });
|
||||
const result = await ctx.service.hikvision.forceRecord({ pack_task_id, unpack_id, task_type });
|
||||
ctx.body = result;
|
||||
} catch (error) {
|
||||
console.error('[HikvisionController] forceRecord 错误:', error);
|
||||
|
||||
@ -22,7 +22,57 @@ class HikvisionService extends BaseService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装录像任务落参,并在事务内创建录像占位记录(防止并发重复)
|
||||
* 获取任务类型配置(打包/拆包共用)
|
||||
* @param {string} taskType - 'pack' | 'unpack'
|
||||
*/
|
||||
_getTaskCfg(taskType) {
|
||||
if (taskType === 'unpack') {
|
||||
return {
|
||||
taskType: 'unpack',
|
||||
taskTable: 'wms_return_unpack',
|
||||
videoTable: 'wms_return_unpack_video',
|
||||
fkColumn: 'unpack_id',
|
||||
cameraIdsField: 'camera_ids',
|
||||
codePrefix: 'unpack_',
|
||||
workbenchMatch: false,
|
||||
endOffsetMs: 5000, // 拆包录像结束时间往后延 5 秒
|
||||
buildFields: (task, cameraId) => ({
|
||||
task_type: 'unpack',
|
||||
unpack_id: task.id,
|
||||
device_id: cameraId,
|
||||
order_id: task.order_id,
|
||||
warehouse_id: task.warehouse_id,
|
||||
unpack_no: task.unpack_no,
|
||||
waybill_code: task.waybill_code,
|
||||
}),
|
||||
};
|
||||
}
|
||||
return {
|
||||
taskType: 'pack',
|
||||
taskTable: 'wms_pack_task',
|
||||
videoTable: 'wms_pack_task_video',
|
||||
fkColumn: 'pack_task_id',
|
||||
cameraIdsField: 'device_ids',
|
||||
codePrefix: '',
|
||||
workbenchMatch: true,
|
||||
endOffsetMs: 0, // 打包结束时间不延后
|
||||
buildFields: (task, cameraId) => ({
|
||||
task_type: 'pack',
|
||||
pack_task_id: task.id,
|
||||
device_id: cameraId,
|
||||
order_id: task.order_id,
|
||||
warehouse_id: task.warehouse_id,
|
||||
workbench_id: task.workbench_id,
|
||||
wave_no: task.wave_no,
|
||||
waybill_code: task.waybill_code,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装录像任务落参,并用原子 INSERT 创建录像占位记录(防止并发重复)
|
||||
* 注:不使用显式事务(egg-mysql 5 + mysql2 3.23 的事务 conn.query 存在挂起 bug),
|
||||
* 改用 INSERT ... SELECT ... WHERE NOT EXISTS 原子防重。
|
||||
* @param {Object} task 任务行(wms_pack_task 或 wms_return_unpack)
|
||||
* @param {Object} cfg 任务类型配置
|
||||
* - videoTable: 录像表名
|
||||
@ -47,72 +97,57 @@ class HikvisionService extends BaseService {
|
||||
}
|
||||
|
||||
const items = [];
|
||||
// 事务:原子化检查+插入,防止并发重复
|
||||
const conn = await app.mysql.get('wms').beginTransaction();
|
||||
try {
|
||||
for (const cameraId of cameraIds) {
|
||||
// 按设备ID匹配摄像头
|
||||
let matchedCamera = cfg.cameras.find(c => c.id === cameraId);
|
||||
if (!matchedCamera && cfg.workbenchMatch && task.workbench_id) {
|
||||
matchedCamera = cfg.cameras.find(
|
||||
c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)
|
||||
);
|
||||
}
|
||||
if (!matchedCamera) matchedCamera = cfg.cameras[0];
|
||||
if (!matchedCamera) continue;
|
||||
|
||||
const matchedServer = cfg.servers.find(s => s.id === matchedCamera.server_id) || cfg.servers[0];
|
||||
if (!matchedServer) continue;
|
||||
|
||||
// 事务内 SELECT ... FOR UPDATE 锁定检查,防止并发重复
|
||||
const [existing] = await conn.query(
|
||||
`SELECT id FROM ${cfg.videoTable}
|
||||
WHERE ${cfg.fkColumn} = ? AND camera_id = ? AND deleted_at IS NULL
|
||||
FOR UPDATE`,
|
||||
[task.id, matchedCamera.id]
|
||||
for (const cameraId of cameraIds) {
|
||||
// 按设备ID匹配摄像头
|
||||
let matchedCamera = cfg.cameras.find(c => c.id === cameraId);
|
||||
if (!matchedCamera && cfg.workbenchMatch && task.workbench_id) {
|
||||
matchedCamera = cfg.cameras.find(
|
||||
c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)
|
||||
);
|
||||
if (!existing || existing.length === 0) {
|
||||
await conn.insert(cfg.videoTable, {
|
||||
[cfg.fkColumn]: task.id,
|
||||
camera_id: matchedCamera.id,
|
||||
video_url: '',
|
||||
status: 'created',
|
||||
created_at: app.mysql.literals.now,
|
||||
updated_at: app.mysql.literals.now,
|
||||
});
|
||||
console.log(`[Hikvision] 创建录像记录: ${cfg.videoTable}=${task.id}, camera=${matchedCamera.id}`);
|
||||
}
|
||||
|
||||
items.push({
|
||||
...cfg.buildFields(task, cameraId),
|
||||
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
||||
task_status: task.task_status,
|
||||
start_time: this._toISO(task.start_time, -5000),
|
||||
end_time: this._toISO(task.end_time),
|
||||
operator: task.operator_text,
|
||||
camera: {
|
||||
id: matchedCamera.id,
|
||||
channel_id: matchedCamera.channel_id,
|
||||
channel_name: matchedCamera.channel_name,
|
||||
ip: matchedCamera.ip,
|
||||
port: matchedCamera.port,
|
||||
model: matchedCamera.model,
|
||||
custom_name: matchedCamera.custom_name,
|
||||
},
|
||||
server: {
|
||||
id: matchedServer.id,
|
||||
brand: matchedServer.brand,
|
||||
internal_ip: matchedServer.internal_ip,
|
||||
internal_port: matchedServer.internal_port || 80,
|
||||
username: matchedServer.username,
|
||||
password: matchedServer.password,
|
||||
},
|
||||
});
|
||||
}
|
||||
await conn.commit();
|
||||
} catch (txError) {
|
||||
await conn.rollback();
|
||||
throw txError;
|
||||
if (!matchedCamera) matchedCamera = cfg.cameras[0];
|
||||
if (!matchedCamera) continue;
|
||||
|
||||
const matchedServer = cfg.servers.find(s => s.id === matchedCamera.server_id) || cfg.servers[0];
|
||||
if (!matchedServer) continue;
|
||||
|
||||
// 原子插入防重:仅当该任务+摄像头没有未删除录像行时插入
|
||||
await app.mysql.get('wms').query(
|
||||
`INSERT INTO ${cfg.videoTable} (${cfg.fkColumn}, camera_id, video_url, status, created_at, updated_at)
|
||||
SELECT ?, ?, '', 'created', NOW(), NOW()
|
||||
FROM DUAL
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM ${cfg.videoTable}
|
||||
WHERE ${cfg.fkColumn} = ? AND camera_id = ? AND deleted_at IS NULL
|
||||
)`,
|
||||
[task.id, matchedCamera.id, task.id, matchedCamera.id]
|
||||
);
|
||||
|
||||
items.push({
|
||||
...cfg.buildFields(task, cameraId),
|
||||
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
||||
task_status: task.task_status,
|
||||
start_time: this._toISO(task.start_time, -5000),
|
||||
end_time: this._toISO(task.end_time, cfg.endOffsetMs || 0),
|
||||
operator: task.operator_text,
|
||||
camera: {
|
||||
id: matchedCamera.id,
|
||||
channel_id: matchedCamera.channel_id,
|
||||
channel_name: matchedCamera.channel_name,
|
||||
ip: matchedCamera.ip,
|
||||
port: matchedCamera.port,
|
||||
model: matchedCamera.model,
|
||||
custom_name: matchedCamera.custom_name,
|
||||
},
|
||||
server: {
|
||||
id: matchedServer.id,
|
||||
brand: matchedServer.brand,
|
||||
internal_ip: matchedServer.internal_ip,
|
||||
internal_port: matchedServer.internal_port || 80,
|
||||
username: matchedServer.username,
|
||||
password: matchedServer.password,
|
||||
},
|
||||
});
|
||||
}
|
||||
return items;
|
||||
}
|
||||
@ -183,39 +218,8 @@ class HikvisionService extends BaseService {
|
||||
});
|
||||
|
||||
// 4. 组装返回数据:每个任务的每个摄像头返回一条(先打包后拆包)
|
||||
const packCfg = {
|
||||
videoTable: 'wms_pack_task_video',
|
||||
fkColumn: 'pack_task_id',
|
||||
cameraIdsField: 'device_ids',
|
||||
codePrefix: '',
|
||||
workbenchMatch: true,
|
||||
buildFields: (task, cameraId) => ({
|
||||
task_type: 'pack',
|
||||
pack_task_id: task.id,
|
||||
device_id: cameraId,
|
||||
order_id: task.order_id,
|
||||
warehouse_id: task.warehouse_id,
|
||||
workbench_id: task.workbench_id,
|
||||
wave_no: task.wave_no,
|
||||
waybill_code: task.waybill_code,
|
||||
}),
|
||||
};
|
||||
const unpackCfg = {
|
||||
videoTable: 'wms_return_unpack_video',
|
||||
fkColumn: 'unpack_id',
|
||||
cameraIdsField: 'camera_ids',
|
||||
codePrefix: 'unpack_',
|
||||
workbenchMatch: false,
|
||||
buildFields: (task, cameraId) => ({
|
||||
task_type: 'unpack',
|
||||
unpack_id: task.id,
|
||||
device_id: cameraId,
|
||||
order_id: task.order_id,
|
||||
warehouse_id: task.warehouse_id,
|
||||
unpack_no: task.unpack_no,
|
||||
waybill_code: task.waybill_code,
|
||||
}),
|
||||
};
|
||||
const packCfg = this._getTaskCfg('pack');
|
||||
const unpackCfg = this._getTaskCfg('unpack');
|
||||
|
||||
const result = [];
|
||||
for (const task of tasks || []) {
|
||||
@ -257,6 +261,7 @@ class HikvisionService extends BaseService {
|
||||
}
|
||||
const parts = uniqueCode.split('_');
|
||||
const taskIdNum = parseInt(isUnpack ? parts[1] : parts[0]);
|
||||
const cameraIdNum = parseInt(parts[parts.length - 1]);
|
||||
|
||||
if (!taskIdNum) {
|
||||
return this.errorResponse('hikvision.receiveTask', `无法解析任务ID: ${uniqueCode}`);
|
||||
@ -266,25 +271,30 @@ class HikvisionService extends BaseService {
|
||||
const fkColumn = isUnpack ? 'unpack_id' : 'pack_task_id';
|
||||
|
||||
if (ok) {
|
||||
// 任务接收成功 → 创建录像记录,状态 recording
|
||||
const existingVideo = await app.mysql.get('wms').get(videoTable, {
|
||||
[fkColumn]: taskIdNum,
|
||||
deleted_at: null,
|
||||
});
|
||||
// 任务接收成功 → 定位该摄像头录像行,状态改为 recording
|
||||
const whereCond = { [fkColumn]: taskIdNum, deleted_at: null };
|
||||
if (!isNaN(cameraIdNum)) whereCond.camera_id = cameraIdNum;
|
||||
|
||||
const existingVideo = await app.mysql.get('wms').get(videoTable, whereCond);
|
||||
|
||||
if (!existingVideo) {
|
||||
await app.mysql.get('wms').insert(videoTable, {
|
||||
[fkColumn]: taskIdNum,
|
||||
video_url: '',
|
||||
status: 'recording',
|
||||
remark: message || '任务已接收',
|
||||
created_at: app.mysql.literals.now,
|
||||
updated_at: app.mysql.literals.now,
|
||||
});
|
||||
if (!isNaN(cameraIdNum)) {
|
||||
await app.mysql.get('wms').query(
|
||||
`INSERT INTO ${videoTable} (${fkColumn}, camera_id, video_url, status, remark, created_at, updated_at)
|
||||
VALUES (?, ?, '', 'recording', ?, NOW(), NOW())`,
|
||||
[taskIdNum, cameraIdNum, message || '任务已接收']
|
||||
);
|
||||
} else {
|
||||
await app.mysql.get('wms').query(
|
||||
`INSERT INTO ${videoTable} (${fkColumn}, video_url, status, remark, created_at, updated_at)
|
||||
VALUES (?, '', 'recording', ?, NOW(), NOW())`,
|
||||
[taskIdNum, message || '任务已接收']
|
||||
);
|
||||
}
|
||||
} else if (existingVideo.status === 'created') {
|
||||
await app.mysql.get('wms').update(videoTable,
|
||||
{ status: 'recording', remark: message || '任务已接收', updated_at: app.mysql.literals.now },
|
||||
{ where: { id: existingVideo.id } }
|
||||
await app.mysql.get('wms').query(
|
||||
`UPDATE ${videoTable} SET status = 'recording', remark = ?, updated_at = NOW() WHERE id = ?`,
|
||||
[message || '任务已接收', existingVideo.id]
|
||||
);
|
||||
}
|
||||
|
||||
@ -325,6 +335,7 @@ class HikvisionService extends BaseService {
|
||||
}
|
||||
const parts = uniqueCode.split('_');
|
||||
const taskIdNum = parseInt(isUnpack ? parts[1] : parts[0]);
|
||||
const cameraIdNum = parseInt(parts[parts.length - 1]);
|
||||
|
||||
if (!taskIdNum) {
|
||||
return this.errorResponse('hikvision.taskComplete', `无法解析任务ID: ${uniqueCode}`);
|
||||
@ -333,22 +344,22 @@ class HikvisionService extends BaseService {
|
||||
const videoTable = isUnpack ? 'wms_return_unpack_video' : 'wms_pack_task_video';
|
||||
const fkColumn = isUnpack ? 'unpack_id' : 'pack_task_id';
|
||||
|
||||
// 按任务ID+摄像头ID精确定位录像行,避免多摄像头/forceRecord 并发错位
|
||||
const whereCond = { [fkColumn]: taskIdNum, deleted_at: null };
|
||||
if (!isNaN(cameraIdNum)) whereCond.camera_id = cameraIdNum;
|
||||
|
||||
if (success && status === 'completed') {
|
||||
// 取最新的录像记录 ID 精准更新,避免 forceRecord 并发覆盖
|
||||
// 取该摄像头最新的录像记录 ID 精准更新
|
||||
const [latestVideo] = await app.mysql.get('wms').select(videoTable, {
|
||||
where: { [fkColumn]: taskIdNum, deleted_at: null },
|
||||
where: whereCond,
|
||||
orders: [['id', 'desc']],
|
||||
limit: 1,
|
||||
});
|
||||
if (latestVideo) {
|
||||
await app.mysql.get('wms').update(videoTable,
|
||||
{
|
||||
video_url: fileUrl || output || '',
|
||||
status: 'completed',
|
||||
remark: `大小:${(size / 1024 / 1024).toFixed(2)}MB, 时长:${duration}s, 倍速:${speed}x`,
|
||||
updated_at: app.mysql.literals.now,
|
||||
},
|
||||
{ where: { id: latestVideo.id } }
|
||||
const remarkText = `大小:${(size / 1024 / 1024).toFixed(2)}MB, 时长:${duration}s, 倍速:${speed}x`;
|
||||
await app.mysql.get('wms').query(
|
||||
`UPDATE ${videoTable} SET video_url = ?, status = 'completed', remark = ?, updated_at = NOW() WHERE id = ?`,
|
||||
[fileUrl || output || '', remarkText, latestVideo.id]
|
||||
);
|
||||
}
|
||||
|
||||
@ -356,16 +367,16 @@ class HikvisionService extends BaseService {
|
||||
return this.successResponse('hikvision.taskComplete',
|
||||
{ [fkColumn]: taskIdNum, status: 'completed' }, '录像完成');
|
||||
} else {
|
||||
// 录像失败 - 同样精准更新最新记录
|
||||
// 录像失败 - 同样按摄像头精准更新最新记录
|
||||
const [latestVideo] = await app.mysql.get('wms').select(videoTable, {
|
||||
where: { [fkColumn]: taskIdNum, deleted_at: null },
|
||||
where: whereCond,
|
||||
orders: [['id', 'desc']],
|
||||
limit: 1,
|
||||
});
|
||||
if (latestVideo) {
|
||||
await app.mysql.get('wms').update(videoTable,
|
||||
{ status: 'failed', remark: errorMsg || '录像失败', updated_at: app.mysql.literals.now },
|
||||
{ where: { id: latestVideo.id } }
|
||||
await app.mysql.get('wms').query(
|
||||
`UPDATE ${videoTable} SET status = 'failed', remark = ?, updated_at = NOW() WHERE id = ?`,
|
||||
[errorMsg || '录像失败', latestVideo.id]
|
||||
);
|
||||
}
|
||||
|
||||
@ -380,30 +391,36 @@ class HikvisionService extends BaseService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制录制:外部 HTTP 调用,按 pack_task_id 强制推送录制任务
|
||||
* 事务内重置已有录像行,推送成功后返回
|
||||
* 强制录制:外部 HTTP 调用,按任务ID强制推送录制任务
|
||||
* 支持打包(pack_task_id)与拆包(unpack_id + task_type='unpack')
|
||||
* 事务内重置已有录像行,推送成功后提交
|
||||
*/
|
||||
async forceRecord(data) {
|
||||
const { app } = this;
|
||||
const { pack_task_id } = data || {};
|
||||
const { pack_task_id, unpack_id, task_type } = data || {};
|
||||
|
||||
if (!pack_task_id) {
|
||||
return { status: 'fail', message: '缺少 pack_task_id' };
|
||||
// 任务类型判断:task_type 优先;否则按入参 ID 判断
|
||||
const isUnpack = task_type === 'unpack' || (!pack_task_id && !!unpack_id);
|
||||
const taskId = isUnpack ? unpack_id : pack_task_id;
|
||||
|
||||
if (!taskId) {
|
||||
return { status: 'fail', message: isUnpack ? '缺少 unpack_id' : '缺少 pack_task_id' };
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 查询打包任务
|
||||
const task = await app.mysql.get('wms').get('wms_pack_task', {
|
||||
id: pack_task_id,
|
||||
deleted_at: null,
|
||||
});
|
||||
const cfg = this._getTaskCfg(isUnpack ? 'unpack' : 'pack');
|
||||
|
||||
// 1. 查询任务(拆包主表无 deleted_at 列)
|
||||
const getCond = { id: taskId };
|
||||
if (!isUnpack) getCond.deleted_at = null;
|
||||
const task = await app.mysql.get('wms').get(cfg.taskTable, getCond);
|
||||
|
||||
if (!task) {
|
||||
return { status: 'fail', message: '打包任务不存在' };
|
||||
return { status: 'fail', message: isUnpack ? '拆包任务不存在' : '打包任务不存在' };
|
||||
}
|
||||
|
||||
if (!task.end_time || !task.device_ids || task.device_ids === '[]') {
|
||||
return { status: 'fail', message: '打包任务未完成或无设备信息' };
|
||||
if (!task.end_time || !task[cfg.cameraIdsField] || task[cfg.cameraIdsField] === '[]') {
|
||||
return { status: 'fail', message: '任务未完成或无摄像头信息' };
|
||||
}
|
||||
|
||||
// 2. 查询摄像头和服务器
|
||||
@ -440,78 +457,75 @@ class HikvisionService extends BaseService {
|
||||
return { status: 'fail', message: '该仓库无录制电脑在线' };
|
||||
}
|
||||
|
||||
// 4. 事务:重置录像行
|
||||
const deviceIds = typeof task.device_ids === 'string'
|
||||
? JSON.parse(task.device_ids) : (task.device_ids || []);
|
||||
// 4. 重置录像行(无显式事务):先删旧行,再逐摄像头插入 created 行
|
||||
const cameraIds = typeof task[cfg.cameraIdsField] === 'string'
|
||||
? JSON.parse(task[cfg.cameraIdsField]) : (task[cfg.cameraIdsField] || []);
|
||||
|
||||
const conn = await app.mysql.get('wms').beginTransaction();
|
||||
try {
|
||||
for (const deviceId of deviceIds) {
|
||||
const matchedCamera = cameras.find(c => c.id === deviceId) || cameras[0];
|
||||
if (!matchedCamera) continue;
|
||||
const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0];
|
||||
if (!matchedServer) continue;
|
||||
await app.mysql.get('wms').delete(cfg.videoTable, { [cfg.fkColumn]: task.id });
|
||||
|
||||
// 删除已有录像行,重新创建
|
||||
await conn.delete('wms_pack_task_video', { pack_task_id: task.id });
|
||||
await conn.insert('wms_pack_task_video', {
|
||||
pack_task_id: task.id,
|
||||
camera_id: matchedCamera.id,
|
||||
video_url: '',
|
||||
status: 'created',
|
||||
created_at: app.mysql.literals.now,
|
||||
updated_at: app.mysql.literals.now,
|
||||
});
|
||||
await conn.commit();
|
||||
console.log(`[Hikvision] 强制录制-重置录像行: pack_task=${task.id}`);
|
||||
|
||||
// 5. 推送录制任务
|
||||
const payload = {
|
||||
pack_task_id: task.id,
|
||||
device_id: deviceId,
|
||||
uniqueCode: `${task.id}_${deviceId}`,
|
||||
order_id: task.order_id,
|
||||
warehouse_id: task.warehouse_id,
|
||||
workbench_id: task.workbench_id,
|
||||
wave_no: task.wave_no,
|
||||
waybill_code: task.waybill_code,
|
||||
start_time: this._toISO(task.start_time, -5000),
|
||||
end_time: this._toISO(task.end_time),
|
||||
camera: {
|
||||
id: matchedCamera.id,
|
||||
channel_id: matchedCamera.channel_id,
|
||||
channel_name: matchedCamera.channel_name,
|
||||
ip: matchedCamera.ip,
|
||||
port: matchedCamera.port,
|
||||
model: matchedCamera.model,
|
||||
},
|
||||
server: {
|
||||
id: matchedServer.id,
|
||||
brand: matchedServer.brand,
|
||||
internal_ip: matchedServer.internal_ip,
|
||||
internal_port: matchedServer.internal_port || 80,
|
||||
username: matchedServer.username,
|
||||
password: matchedServer.password,
|
||||
},
|
||||
};
|
||||
|
||||
const sendRes = await app.ws.sendTo(recordingUniqueId, JSON.stringify({
|
||||
method: 'hikvision.receiveTask',
|
||||
data: payload,
|
||||
}));
|
||||
|
||||
if (sendRes !== 1) {
|
||||
return { status: 'fail', message: '推送失败,录制电脑可能已断开' };
|
||||
}
|
||||
|
||||
console.log(`[Hikvision] 强制录制-已推送: pack_task=${task.id}, device=${deviceId}`);
|
||||
const pushedItems = [];
|
||||
for (const cameraId of cameraIds) {
|
||||
let matchedCamera = cameras.find(c => c.id === cameraId);
|
||||
if (!matchedCamera && cfg.workbenchMatch && task.workbench_id) {
|
||||
matchedCamera = cameras.find(
|
||||
c => c.custom_name && c.custom_name.includes(`${task.workbench_id}号`)
|
||||
);
|
||||
}
|
||||
} catch (txError) {
|
||||
await conn.rollback();
|
||||
throw txError;
|
||||
if (!matchedCamera) matchedCamera = cameras[0];
|
||||
if (!matchedCamera) continue;
|
||||
const matchedServer = servers.find(s => s.id === matchedCamera.server_id) || servers[0];
|
||||
if (!matchedServer) continue;
|
||||
|
||||
await app.mysql.get('wms').query(
|
||||
`INSERT INTO ${cfg.videoTable} (${cfg.fkColumn}, camera_id, video_url, status, created_at, updated_at)
|
||||
VALUES (?, ?, '', 'created', NOW(), NOW())`,
|
||||
[task.id, matchedCamera.id]
|
||||
);
|
||||
pushedItems.push({ cameraId, matchedCamera, matchedServer });
|
||||
}
|
||||
console.log(`[Hikvision] 强制录制-重置录像行: ${cfg.fkColumn}=${task.id}`);
|
||||
|
||||
// 5. 推送录制任务(推送失败则删除刚插入的行,恢复原状)
|
||||
for (const { cameraId, matchedCamera, matchedServer } of pushedItems) {
|
||||
const payload = {
|
||||
...cfg.buildFields(task, cameraId),
|
||||
uniqueCode: `${cfg.codePrefix}${task.id}_${cameraId}`,
|
||||
start_time: this._toISO(task.start_time, -5000),
|
||||
end_time: this._toISO(task.end_time, cfg.endOffsetMs || 0),
|
||||
operator: task.operator_text,
|
||||
camera: {
|
||||
id: matchedCamera.id,
|
||||
channel_id: matchedCamera.channel_id,
|
||||
channel_name: matchedCamera.channel_name,
|
||||
ip: matchedCamera.ip,
|
||||
port: matchedCamera.port,
|
||||
model: matchedCamera.model,
|
||||
},
|
||||
server: {
|
||||
id: matchedServer.id,
|
||||
brand: matchedServer.brand,
|
||||
internal_ip: matchedServer.internal_ip,
|
||||
internal_port: matchedServer.internal_port || 80,
|
||||
username: matchedServer.username,
|
||||
password: matchedServer.password,
|
||||
},
|
||||
};
|
||||
|
||||
const sendRes = await app.ws.sendTo(recordingUniqueId, JSON.stringify({
|
||||
method: 'hikvision.receiveTask',
|
||||
data: payload,
|
||||
}));
|
||||
|
||||
if (sendRes !== 1) {
|
||||
// 推送失败:删除刚插入的行,避免任务卡在 created 状态
|
||||
await app.mysql.get('wms').delete(cfg.videoTable, { [cfg.fkColumn]: task.id });
|
||||
return { status: 'fail', message: '推送失败,录制电脑可能已断开' };
|
||||
}
|
||||
|
||||
console.log(`[Hikvision] 强制录制-已推送: ${cfg.fkColumn}=${task.id}, camera=${cameraId}`);
|
||||
}
|
||||
|
||||
return { status: 'success', message: '强制录制任务已推送', pack_task_id: task.id };
|
||||
return { status: 'success', message: '强制录制任务已推送', [cfg.fkColumn]: task.id };
|
||||
} catch (error) {
|
||||
console.error('[Hikvision] forceRecord 错误:', error);
|
||||
return { status: 'fail', message: error.message };
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user