feat: POST /hikvision/forceRecord 强制录制推送
This commit is contained in:
parent
7f079c130f
commit
3662c82e26
27
app/controller/hikvision.js
Normal file
27
app/controller/hikvision.js
Normal file
@ -0,0 +1,27 @@
|
||||
const { Controller } = require('egg');
|
||||
|
||||
class HikvisionController extends Controller {
|
||||
/**
|
||||
* POST /hikvision/forceRecord
|
||||
* 强制推送录制任务,事务重置录像行并推送
|
||||
*/
|
||||
async forceRecord() {
|
||||
const { ctx } = this;
|
||||
const { pack_task_id } = ctx.request.body;
|
||||
|
||||
if (!pack_task_id) {
|
||||
ctx.body = { status: 'fail', message: '缺少 pack_task_id' };
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await ctx.service.hikvision.forceRecord({ pack_task_id });
|
||||
ctx.body = result;
|
||||
} catch (error) {
|
||||
console.error('[HikvisionController] forceRecord 错误:', error);
|
||||
ctx.body = { status: 'fail', message: error.message };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HikvisionController;
|
||||
@ -8,6 +8,7 @@ module.exports = app => {
|
||||
router.post('/auth/logout', controller.auth.logout);
|
||||
router.post('/auth/refreshInfo', controller.auth.refreshInfo);
|
||||
router.post('/frp/newProxy', controller.frp.newProxy);
|
||||
router.post('/hikvision/forceRecord', controller.hikvision.forceRecord);
|
||||
router.post('/sendToRoom', controller.home.sendToRoom);
|
||||
app.ws.route('/ws', controller.print.ws);
|
||||
};
|
||||
@ -262,6 +262,142 @@ class HikvisionService extends BaseService {
|
||||
return this.errorResponse('hikvision.taskComplete', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制录制:外部 HTTP 调用,按 pack_task_id 强制推送录制任务
|
||||
* 事务内重置已有录像行,推送成功后返回
|
||||
*/
|
||||
async forceRecord(data) {
|
||||
const { app } = this;
|
||||
const { pack_task_id } = data || {};
|
||||
|
||||
if (!pack_task_id) {
|
||||
return { status: 'fail', message: '缺少 pack_task_id' };
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 查询打包任务
|
||||
const task = await app.mysql.get('wms').get('wms_pack_task', {
|
||||
id: pack_task_id,
|
||||
deleted_at: null,
|
||||
});
|
||||
|
||||
if (!task) {
|
||||
return { status: 'fail', message: '打包任务不存在' };
|
||||
}
|
||||
|
||||
if (!task.end_time || !task.device_ids || task.device_ids === '[]') {
|
||||
return { status: 'fail', message: '打包任务未完成或无设备信息' };
|
||||
}
|
||||
|
||||
// 2. 查询摄像头和服务器
|
||||
const cameras = await app.mysql.get('wms').select('wms_camera', {
|
||||
where: { warehouse_id: task.warehouse_id, deleted_at: null, status: 1 },
|
||||
});
|
||||
const servers = await app.mysql.get('wms').select('wms_camera_server', {
|
||||
where: { warehouse_id: task.warehouse_id, deleted_at: null, status: 1 },
|
||||
});
|
||||
|
||||
// 3. 找到同仓库的录制电脑
|
||||
const warehousePattern = `warehouse:${task.warehouse_id}:printer:*`;
|
||||
const onlineKeys = [];
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [nextCursor, keys] = await app.redis.scan(cursor, 'MATCH', warehousePattern, 'COUNT', 100);
|
||||
cursor = nextCursor;
|
||||
onlineKeys.push(...keys);
|
||||
} while (cursor !== '0');
|
||||
|
||||
let recordingUniqueId = null;
|
||||
for (const key of onlineKeys) {
|
||||
const info = await app.redis.get(key);
|
||||
if (!info) continue;
|
||||
const parsed = JSON.parse(info);
|
||||
if (parsed.is_recording === 1) {
|
||||
recordingUniqueId = parsed.uniqueId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!recordingUniqueId) {
|
||||
return { status: 'fail', message: '该仓库无录制电脑在线' };
|
||||
}
|
||||
|
||||
// 4. 事务:重置录像行
|
||||
const deviceIds = typeof task.device_ids === 'string'
|
||||
? JSON.parse(task.device_ids) : (task.device_ids || []);
|
||||
|
||||
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 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',
|
||||
});
|
||||
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),
|
||||
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}`);
|
||||
}
|
||||
} catch (txError) {
|
||||
await conn.rollback();
|
||||
throw txError;
|
||||
}
|
||||
|
||||
return { status: 'success', message: '强制录制任务已推送', pack_task_id: task.id };
|
||||
} catch (error) {
|
||||
console.error('[Hikvision] forceRecord 错误:', error);
|
||||
return { status: 'fail', message: error.message };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HikvisionService;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user