'use strict'; const { Controller } = require('egg'); const fs = require('fs'); const path = require('path'); /** * web 用户端:边缘能力(`/api/web/edge/*`) * * 查询机先在本机局域网找"设备管理服务"(UDP 询问),找不到才来这里问云端。 * 云端把问题转给打包仓库那台扫码服务(它出站领任务、真拉一段录像确认)。 * * 这样查询机得到的结论和"本机自己校验"是同一套语义,不会各说一套。 */ class WebEdgeController extends Controller { /** * POST /api/web/edge/check { code } * * 返回 `data.state`: * - `ok` 设备确认过:`hasVideo` 为准 * - `no-plan` 这个运单在本主体下算不出回放计划(没绑工作台/摄像头) * - `no-device` 同主体没有在线的扫码服务,或它们都连不上该 NVR * - `no-credential` 录像机没有保存密码 * * `hasVideo` 三态:true / false / null(未知,调用方自己决定怎么展示) */ async check() { const { ctx } = this; const body = ctx.request.body || {}; const code = String(body.code || ctx.query.code || '').trim(); if (!code) ctx.throw(422, '缺少运单号'); const data = await ctx.service.edgeTask.checkVideo({ subjectId: ctx.state.subjectId, code, }); ctx.body = { ok: true, data }; } /** * POST /api/web/edge/download { code } * * 跨网下载:云端让能访问 NVR 的扫码服务去拉流合并,传回云端中转, * 查询机拿到 `taskId` 后再取文件(不会占住这个请求)。 */ async download() { const { ctx } = this; const body = ctx.request.body || {}; const code = String(body.code || ctx.query.code || '').trim(); if (!code) ctx.throw(422, '缺少运单号'); const data = await ctx.service.edgeTask.createDownloadTask({ subjectId: ctx.state.subjectId, code, }); ctx.logger.info('[edge] 主体 %s 跨网下载 %s → %s', ctx.state.subjectId, code, data.state); ctx.body = { ok: true, data }; } /** * GET /api/web/edge/download/:taskId —— 查进度 * * 只能查自己主体创建的任务(元数据里记了 subjectId)。 */ async downloadStatus() { const { ctx } = this; const meta = await this.requireOwnTask(ctx.params.taskId); ctx.body = { ok: true, data: { state: meta.state, size: meta.size || 0, error: meta.error || null, scannerSn: meta.scannerSn || null, }, }; } /** * GET /api/web/edge/download/:taskId/file —— 取成片 * * 支持 `Range`(浏览器 `