From 3662c82e26a30f77d9166baba847be0ba39037ec Mon Sep 17 00:00:00 2001 From: liufumingren Date: Mon, 27 Jul 2026 03:31:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20POST=20/hikvision/forceRecord=20?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E5=BD=95=E5=88=B6=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/hikvision.js | 27 +++++++ app/router.js | 1 + app/service/hikvision.js | 136 ++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 app/controller/hikvision.js diff --git a/app/controller/hikvision.js b/app/controller/hikvision.js new file mode 100644 index 0000000..a5b06bc --- /dev/null +++ b/app/controller/hikvision.js @@ -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; diff --git a/app/router.js b/app/router.js index d4727d7..68acdc4 100644 --- a/app/router.js +++ b/app/router.js @@ -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); }; \ No newline at end of file diff --git a/app/service/hikvision.js b/app/service/hikvision.js index 1ce68dd..53ff880 100644 --- a/app/service/hikvision.js +++ b/app/service/hikvision.js @@ -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;