From 12815a3ceea75c3fc2ba876627780f18a8c39100 Mon Sep 17 00:00:00 2001 From: yuan_yi <1650192445@qq.com> Date: Tue, 7 Jun 2022 17:27:41 +0800 Subject: [PATCH] =?UTF-8?q?mqtt=20=E5=BC=95=E5=85=A5=2040%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/VideoAccess-VCMP/api/.vscode/launch.json | 3 +- code/VideoAccess-VCMP/api/app/lib/index.js | 6 +- .../api/app/lib/service/mqttVideoServer.js | 22 +++++++ .../api/app/lib/service/redis.js | 36 +++++------ code/VideoAccess-VCMP/api/config.js | 60 +++++++++++-------- code/VideoAccess-VCMP/api/package.json | 1 + .../web/client/src/layout/index.jsx | 2 +- 7 files changed, 82 insertions(+), 48 deletions(-) create mode 100644 code/VideoAccess-VCMP/api/app/lib/service/mqttVideoServer.js diff --git a/code/VideoAccess-VCMP/api/.vscode/launch.json b/code/VideoAccess-VCMP/api/.vscode/launch.json index 5b54c7b..d4341c7 100644 --- a/code/VideoAccess-VCMP/api/.vscode/launch.json +++ b/code/VideoAccess-VCMP/api/.vscode/launch.json @@ -22,7 +22,8 @@ "--iotAuthApi http://127.0.0.1:4200", "--iotVideoServerUrl http://221.230.55.27:8081", "--godUrl https://restapi.amap.com/v3", - "--godKey 21c2d970e1646bb9a795900dd00093ce" + "--godKey 21c2d970e1646bb9a795900dd00093ce", + "--mqttVideoServer mqtt://127.0.0.1" ] }, { diff --git a/code/VideoAccess-VCMP/api/app/lib/index.js b/code/VideoAccess-VCMP/api/app/lib/index.js index dcb5bc3..09ccd95 100644 --- a/code/VideoAccess-VCMP/api/app/lib/index.js +++ b/code/VideoAccess-VCMP/api/app/lib/index.js @@ -4,6 +4,7 @@ const utils = require('./utils') const routes = require('./routes'); const redisConnect = require('./service/redis') const socketConect = require('./service/socket') +const mqttVideoServer = require('./service/mqttVideoServer') const paasRequest = require('./service/paasRequest'); const authenticator = require('./middlewares/authenticator'); // const apiLog = require('./middlewares/api-log'); @@ -16,10 +17,11 @@ module.exports.entry = function (app, router, opts) { app.fs.api.authAttr = app.fs.api.authAttr || {}; app.fs.api.logAttr = app.fs.api.logAttr || {}; - // 顺序固定 ↓ + // 顺序固定 ↓ redisConnect(app, opts) socketConect(app, opts) - + mqttVideoServer(app, opts) + // 实例其他平台请求方法 paasRequest(app, opts) diff --git a/code/VideoAccess-VCMP/api/app/lib/service/mqttVideoServer.js b/code/VideoAccess-VCMP/api/app/lib/service/mqttVideoServer.js new file mode 100644 index 0000000..6f72bc3 --- /dev/null +++ b/code/VideoAccess-VCMP/api/app/lib/service/mqttVideoServer.js @@ -0,0 +1,22 @@ +'use strict'; +const mqtt = require('mqtt'); + +module.exports = async function factory (app, opts) { + // const client = mqtt.connect(opts.mqtt.mqttVideoServer); + + // client.on('connect', function () { + // console.info(`mqtt connect success ${opts.mqtt.mqttVideoServer}`); + // }) + // client.on('error', function (e) { + // console.error(`mqtt connect failed ${opts.mqtt.mqttVideoServer}`); + // app.fs.logger.error('info', '[FS-AUTH-MQTT]', `mqtt connect failed ${opts.mqtt.mqttVideoServer}`); + // }) + + // client.subscribe('test', { qos: 2 });//订阅主题为test的消息 + + // client.on('message', function (top, message) { + // console.log(message.toString()); + // }); + + // app.mqttVideoServer = client +} diff --git a/code/VideoAccess-VCMP/api/app/lib/service/redis.js b/code/VideoAccess-VCMP/api/app/lib/service/redis.js index 4fc0ff4..48810c0 100644 --- a/code/VideoAccess-VCMP/api/app/lib/service/redis.js +++ b/code/VideoAccess-VCMP/api/app/lib/service/redis.js @@ -3,26 +3,26 @@ const redis = require("ioredis") module.exports = async function factory (app, opts) { - let client = new redis(opts.redis.port, opts.redis.host); + let client = new redis(opts.redis.port, opts.redis.host); - client.on("error", function (err) { - app.fs.logger.error('info', '[FS-AUTH-REDIS]', 'redis connect error.'); - console.error("Error :", err); - process.exit(-1); - }); + client.on("error", function (err) { + app.fs.logger.error('info', '[FS-AUTH-REDIS]', `redis connect error. ${opts.redis.host + ':' + opts.redis.port}` ); + // console.error("Error :", err); + // process.exit(-1); + }); - client.on('connect', function () { - console.log(`redis connect success ${opts.redis.host + ':' + opts.redis.port}`); - }) + client.on('connect', function () { + console.info(`redis connect success ${opts.redis.host + ':' + opts.redis.port}`); + }) - // 自定义方法 - async function hdelall (key) { - const obj = await client.hgetall(key); - await client.hdel(key, Object.keys(obj)) - } + // 自定义方法 + async function hdelall (key) { + const obj = await client.hgetall(key); + await client.hdel(key, Object.keys(obj)) + } - app.redis = client - app.redisTools = { - hdelall, - } + app.redis = client + app.redisTools = { + hdelall, + } } diff --git a/code/VideoAccess-VCMP/api/config.js b/code/VideoAccess-VCMP/api/config.js index 4b88f2b..87aa071 100644 --- a/code/VideoAccess-VCMP/api/config.js +++ b/code/VideoAccess-VCMP/api/config.js @@ -19,6 +19,7 @@ args.option('iotAuthApi', 'IOT 鉴权 api'); args.option('iotVideoServerUrl', '视频后端服务地址'); args.option('godUrl', '高德地图API请求地址'); args.option('godKey', '高德地图API key'); +args.option('mqttVideoServer', '视频后台 mqtt 服务 URL'); const flags = args.parse(process.argv); @@ -29,6 +30,8 @@ const IOTA_REDIS_SERVER_HOST = process.env.IOTA_REDIS_SERVER_HOST || flags.redis const IOTA_REDIS_SERVER_PORT = process.env.IOTA_REDIS_SERVER_PORT || flags.redisPort || "6379";//redis 端口 const IOTA_REDIS_SERVER_PWD = process.env.IOTA_REDIS_SERVER_PWD || flags.redisPswd || "";//redis 密码 +const MQTT_VIDEO_SERVER = process.env.MQTT_VIDEO_SERVER || flags.mqttVideoServer; + const IOT_AUTH_API = process.env.IOT_AUTH_API || flags.iotAuthApi; const IOT_VIDEO_SERVER_URL = process.env.IOT_VIDEO_SERVER_URL || flags.iotVideoServerUrl const AXY_API_URL = process.env.AXY_API_URL || flags.axyApiUrl; @@ -36,7 +39,7 @@ const GOD_URL = process.env.GOD_URL || flags.godUrl || 'https://restapi.amap.com const GOD_KEY = process.env.GOD_KEY || flags.godKey; const YINGSHI_URL = process.env.YINGSHI_URL || flags.yingshiUrl || 'https://open.ys7.com/api'; -if (!IOT_VIDEO_ACCESS_DB || !IOTA_REDIS_SERVER_HOST || !IOTA_REDIS_SERVER_PORT || !GOD_KEY) { +if (!IOT_VIDEO_ACCESS_DB || !IOTA_REDIS_SERVER_HOST || !IOTA_REDIS_SERVER_PORT || !GOD_KEY || !MQTT_VIDEO_SERVER) { console.log('缺少启动参数,异常退出'); args.showHelp(); process.exit(-1); @@ -65,33 +68,38 @@ const product = { port: IOTA_REDIS_SERVER_PORT, pwd: IOTA_REDIS_SERVER_PWD }, - pssaRequest: [{// name 会作为一个 request 出现在 ctx.app.fs - name: 'authRequest', - root: IOT_AUTH_API - }, { - name: 'axyRequest', - root: AXY_API_URL - }, { - name: 'godRequest', - root: GOD_URL, - params: { - query: { - key: GOD_KEY + mqtt: { + mqttVideoServer: MQTT_VIDEO_SERVER, + }, + pssaRequest: [ + {// name 会作为一个 request 出现在 ctx.app.fs + name: 'authRequest', + root: IOT_AUTH_API + }, { + name: 'axyRequest', + root: AXY_API_URL + }, { + name: 'godRequest', + root: GOD_URL, + params: { + query: { + key: GOD_KEY + } } - } - }, { - name: 'yingshiRequest', - root: YINGSHI_URL, - params: { - query: { - key: GOD_KEY + }, { + name: 'yingshiRequest', + root: YINGSHI_URL, + params: { + query: { + key: GOD_KEY + } } - } - }, { - name: 'videoServerRequest', - root: IOT_VIDEO_SERVER_URL + '/api', - dataWord: 'text' - },] + }, { + name: 'videoServerRequest', + root: IOT_VIDEO_SERVER_URL + '/api', + dataWord: 'text' + }, + ] } } ], diff --git a/code/VideoAccess-VCMP/api/package.json b/code/VideoAccess-VCMP/api/package.json index c71edec..834d9ff 100644 --- a/code/VideoAccess-VCMP/api/package.json +++ b/code/VideoAccess-VCMP/api/package.json @@ -23,6 +23,7 @@ "koa-convert": "^1.2.0", "koa-proxy": "^0.9.0", "moment": "^2.24.0", + "mqtt": "^4.3.7", "path": "^0.12.7", "path-to-regexp": "^3.0.0", "pg": "^7.9.0", diff --git a/code/VideoAccess-VCMP/web/client/src/layout/index.jsx b/code/VideoAccess-VCMP/web/client/src/layout/index.jsx index e3109ac..7918169 100644 --- a/code/VideoAccess-VCMP/web/client/src/layout/index.jsx +++ b/code/VideoAccess-VCMP/web/client/src/layout/index.jsx @@ -162,7 +162,7 @@ const Root = props => { setAuthCrossLoading(false) }); - setAuthCrossLoading(false) + // setAuthCrossLoading(false) }, []) return (