diff --git a/app/controller/hikvision.js b/app/controller/hikvision.js index a5b06bc..24ce389 100644 --- a/app/controller/hikvision.js +++ b/app/controller/hikvision.js @@ -22,6 +22,95 @@ class HikvisionController extends Controller { ctx.body = { status: 'fail', message: error.message }; } } + + /** + * POST /camera/setOSD + * 主程序调用,下发 OSD 叠加信息到仓库中开启录制服务的客户端 + * 入参: warehouse_id, rtspUrl, text, x?, y? + */ + async setCameraOSD() { + const { ctx, app } = this; + const { warehouse_id, rtspUrl, text } = ctx.request.body; + const x = ctx.request.body.x !== undefined ? ctx.request.body.x : -0.9; + const y = ctx.request.body.y !== undefined ? ctx.request.body.y : 0.8; + + if (!warehouse_id) { + ctx.body = { status: 'fail', message: '缺少必要参数: warehouse_id' }; + return; + } + if (!rtspUrl) { + ctx.body = { status: 'fail', message: '缺少必要参数: rtspUrl' }; + return; + } + if (!text) { + ctx.body = { status: 'fail', message: '缺少必要参数: text' }; + return; + } + + try { + // 1. 扫描仓库下所有在线电脑,找到录制中的那台 + const warehousePattern = `warehouse:${warehouse_id}:printer:*`; + let recordingClient = null; + let cursor = '0'; + + do { + const [nextCursor, keys] = await app.redis.scan(cursor, 'MATCH', warehousePattern, 'COUNT', 100); + cursor = nextCursor; + for (const key of keys) { + const raw = await app.redis.get(key); + if (!raw) continue; + const info = JSON.parse(raw); + if (info.is_recording === 1) { + recordingClient = info; + break; + } + } + if (recordingClient) break; + } while (cursor !== '0'); + + if (!recordingClient || !recordingClient.uniqueId) { + ctx.body = { + status: 'fail', + message: `仓库 ${warehouse_id} 中没有在线的录制电脑`, + }; + return; + } + + const roomId = recordingClient.uniqueId; + console.log(`[setCameraOSD] 发送 OSD 到房间 ${roomId}, rtspUrl=${rtspUrl}, text=${text}`); + + // 2. 下发 setting.setCameraOSD 消息 + const res = await app.ws.sendTo(roomId, JSON.stringify({ + method: 'setting.setCameraOSD', + data: { rtspUrl, text, x, y }, + })); + + if (res !== 1) { + ctx.body = { + status: 'fail', + message: '录制电脑不在线,消息发送失败', + }; + return; + } + + console.log('[setCameraOSD] 消息发送成功'); + ctx.body = { + status: 'success', + message: 'OSD 设置指令已下发', + result: { + warehouse_id, + computer_id: recordingClient.computerId, + rtspUrl, + text, + x, + y, + }, + }; + } catch (error) { + console.error('[setCameraOSD] 错误:', error); + ctx.body = { status: 'fail', message: error.message }; + } + } } module.exports = HikvisionController; diff --git a/app/controller/print.js b/app/controller/print.js index f096b9d..75f7340 100644 --- a/app/controller/print.js +++ b/app/controller/print.js @@ -61,6 +61,13 @@ class PrintController extends Controller { if(msg=="ping"){ ws.send("pong"); + // 维护 Redis 在线状态 + if (ctx.print_computer_info) { + const { warehouse, computerId, uniqueId } = ctx.print_computer_info; + const key = `warehouse:${warehouse}:printer:${computerId}`; + await app.redis.set(key, JSON.stringify(ctx.print_computer_info), 'EX', 300); + await app.redis.expire(uniqueId, 300); + } return; } // method过滤 diff --git a/app/router.js b/app/router.js index c165372..bb10032 100644 --- a/app/router.js +++ b/app/router.js @@ -10,6 +10,7 @@ module.exports = app => { router.post('/frp/newProxy', controller.frp.newProxy); router.post('/hikvision/forceRecord', controller.hikvision.forceRecord); router.post('/record/force', controller.hikvision.forceRecord); + router.post('/camera/setOSD', controller.hikvision.setCameraOSD); router.post('/sendToRoom', controller.home.sendToRoom); app.ws.route('/ws', controller.print.ws); }; \ No newline at end of file diff --git a/app/service/auth.js b/app/service/auth.js index 5e82552..04618ef 100644 --- a/app/service/auth.js +++ b/app/service/auth.js @@ -62,10 +62,8 @@ class AuthService extends BaseService { } } - this.app.redis.del(key); // 删除旧的打印机信息 - this.app.redis.set(key, JSON.stringify(data), 'EX', 300); // 设置过期时间为 5分钟 data._source = 'getInfo'; // 标记来源 - console.log(`[setInfo] 来源=getInfo, computer=${computerId}, warehouse=${warehouse}`); + console.log(`[getInfo] 认证成功, computer=${computerId}, warehouse=${warehouse} (在线状态由 ping 维护)`); return this.successResponse('setting.setInfo', data, 'Token 验证成功'); } catch (error) { console.log(error); @@ -73,6 +71,29 @@ class AuthService extends BaseService { } } + /** + * 心跳 ping:客户端定期调用,维护 Redis 在线状态 + * 写入/刷新 warehouse:{warehouse}:printer:{computerId},TTL 300 秒 + */ + async ping() { + const { ctx, app } = this; + const info = ctx.print_computer_info; + if (!info) { + // 未登录,仅回 pong 不写 Redis + return this.successResponse('auth.ping', { timestamp: Date.now() }, 'pong'); + } + const { warehouse, computerId, uniqueId } = info; + const key = `warehouse:${warehouse}:printer:${computerId}`; + + // 写入/刷新电脑在线状态(含完整数据),TTL 5 分钟 + await app.redis.set(key, JSON.stringify(info), 'EX', 300); + // 同时刷新 uniqueId 房间 key + await app.redis.expire(uniqueId, 300); + + console.log(`[ping] 在线状态已刷新, computer=${computerId}, warehouse=${warehouse}`); + return this.successResponse('auth.ping', { timestamp: Date.now() }, 'pong'); + } + async refreshToken(data) { const { token } = data; console.log('AuthService.refreshToken called with oldToken:', token); diff --git a/app/service/hikvision.js b/app/service/hikvision.js index 1515ab0..8aa7f42 100644 --- a/app/service/hikvision.js +++ b/app/service/hikvision.js @@ -44,7 +44,6 @@ class HikvisionService extends BaseService { t.start_time, t.end_time, t.operator_text FROM wms_pack_task t WHERE t.warehouse_id = ? - AND t.pack_mode = 1 AND t.deleted_at IS NULL AND t.end_time IS NOT NULL AND t.device_ids IS NOT NULL diff --git a/config/config.default.js b/config/config.default.js index 72992cb..bb9ed36 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -78,7 +78,7 @@ module.exports = appInfo => { config.frp = { server_addr: 'o.gztbyc.com', // frps 地址 server_port: 6005, // frps bindPort - auth_token: 'a,13579,bmingming))*^', // frps auth.token + auth_token: 'frpPrintToken2024', // frps auth.token subdomain_host: 'o.gztbyc.com', // 子域名后缀 vhost_http_port: 18080, // frps HTTP 代理端口(内部) local_port: 8080, // 客户端本地 web 服务端口