diff --git a/.env.development b/.env.development index ed7723d..0463e0d 100644 --- a/.env.development +++ b/.env.development @@ -12,6 +12,12 @@ VUE_APP_ST_MQTT_PORT = 61614 VUE_APP_CT_MQTT_URL = vip.shuziweidao.com VUE_APP_CT_MQTT_PORT = 61614 VUE_APP_LED_MQTT_URL = wss.dm.yixiong-tech.com -VUE_APP_LED_MQTT_PORT = 8443 -VUE_APP_LED_MQTT_USERNAME = test -VUE_APP_LED_MQTT_PASSWORD = Yx123456 \ No newline at end of file +VUE_APP_LED_MQTT_PORT = 80 +VUE_APP_LED_MQTT_USERNAME = admin +VUE_APP_LED_MQTT_PASSWORD = 7dzkZU1GOnYgNP0Y + +# dataBoard 排行大屏 mqtt 通信地址 +VUE_APP_DB_MQTT_URL = wss.dm.yixiong-tech.com +VUE_APP_DB_MQTT_PORT = 80 +VUE_APP_DB_MQTT_USERNAME = admin +VUE_APP_DB_MQTT_PASSWORD = 7dzkZU1GOnYgNP0Y \ No newline at end of file diff --git a/.env.production b/.env.production index 4f68afc..9c7382d 100644 --- a/.env.production +++ b/.env.production @@ -11,6 +11,12 @@ VUE_APP_ST_MQTT_PORT = 61614 VUE_APP_CT_MQTT_URL = vip.shuziweidao.com VUE_APP_CT_MQTT_PORT = 61614 VUE_APP_LED_MQTT_URL = wss.dm.yixiong-tech.com -VUE_APP_LED_MQTT_PORT = 8443 -VUE_APP_LED_MQTT_USERNAME = test -VUE_APP_LED_MQTT_PASSWORD = Yx123456 +VUE_APP_LED_MQTT_PORT = 80 +VUE_APP_LED_MQTT_USERNAME = admin +VUE_APP_LED_MQTT_PASSWORD = 7dzkZU1GOnYgNP0Y + +# dataBoard 排行大屏 mqtt 通信地址 +VUE_APP_DB_MQTT_URL = wss.dm.yixiong-tech.com +VUE_APP_DB_MQTT_PORT = 80 +VUE_APP_DB_MQTT_USERNAME = admin +VUE_APP_DB_MQTT_PASSWORD = 7dzkZU1GOnYgNP0Y diff --git a/src/utils/mqtt.js b/src/utils/mqtt.js index 432a12f..650ff6d 100644 --- a/src/utils/mqtt.js +++ b/src/utils/mqtt.js @@ -1,54 +1,82 @@ import mqtt from "mqtt"; import {getServeUrl} from "@/utils/serveUrl"; -let mqttClient = null; +const clients = {}; export const initMqtt = (clientId, topic, type, {username='sw_mqtt', password='150326sw@', head='ws'}) => { return new Promise((resolve, reject) => { + // 同 type 已有连接则先断开 + if (clients[type]) { + clients[type].client.end(true); + delete clients[type]; + } + const url = { - head: head, // 必须是 ws 或 wss (mqtt:// 或 mqtts:// 必须让后端开放websocket服务) - host: getServeUrl(type + 'MqttUrl'), // 服务地址 - port: getServeUrl(type + 'MqttPORT'), // 服务端口 - tailPath: "mqtt", // 服务路径 + head: head, + host: getServeUrl(type + 'MqttUrl'), + port: getServeUrl(type + 'MqttPORT'), + tailPath: "mqtt", }; const options = { clientId: clientId, username: username, password: password, - // keepalive: 60, clean: true, cleanSession: true, - reconnectPeriod: 5000, // 断线后重连间隔 (ms) - connectTimeout: 30 * 1000, // 连接超时时间 + reconnectPeriod: 5000, + connectTimeout: 30 * 1000, }; - // 创建MQTT连接 - mqttClient = mqtt.connect(`${url.head}://${url.host}:${url.port}/${url.tailPath}`, options); - // 事件处理 - mqttClient.on("connect", () => { - console.info("连接成功_clientId:" + clientId); + const client = mqtt.connect(`${url.head}://${url.host}:${url.port}/${url.tailPath}`, options); + const entry = { client, callback: null }; + clients[type] = entry; - mqttClient.subscribe(topic, (err) => { + client.on("connect", () => { + console.info("MQTT 连接成功_clientId:" + clientId); + + client.subscribe(topic, (err) => { if (err) { reject("订阅失败_clientId:" + clientId); } else { + if (entry.callback) { + client.on('message', (topic, message) => { + entry.callback({ topic, message: JSON.parse(message.toString()) }); + }); + } resolve("订阅成功_clientId:" + clientId); } }); }); - mqttClient.on("error", (error) => { + client.on("error", (error) => { reject("连接失败_clientId:" + clientId); }); - }) - + }); }; -export const callBackMqttMessage = (callback) => { - if(mqttClient) { - mqttClient.on('message', (topic, message) => { - callback({ topic, message: JSON.parse(message.toString()) }); +export const callBackMqttMessage = (type, callback) => { + const entry = clients[type]; + if (entry) { + entry.callback = callback; + if (entry.client) { + entry.client.on('message', (topic, message) => { + callback({ topic, message: JSON.parse(message.toString()) }); + }); + } + } +}; + +export const disconnectMqtt = (type) => { + if (type) { + if (clients[type]) { + clients[type].client.end(true); + delete clients[type]; + } + } else { + Object.keys(clients).forEach(k => { + clients[k].client.end(true); + delete clients[k]; }); } -} +}; diff --git a/src/utils/serveUrl.js b/src/utils/serveUrl.js index 51c5b61..3a1dd41 100644 --- a/src/utils/serveUrl.js +++ b/src/utils/serveUrl.js @@ -25,6 +25,12 @@ const getServeUrl = function(type) { case 'ledMqttPORT': serveUrl = process.env.VUE_APP_LED_MQTT_PORT; break; + case 'dbMqttUrl': + serveUrl = process.env.VUE_APP_DB_MQTT_URL; + break; + case 'dbMqttPORT': + serveUrl = process.env.VUE_APP_DB_MQTT_PORT; + break; } return serveUrl; }; diff --git a/src/views/dataBoard/dataBoard.vue b/src/views/dataBoard/dataBoard.vue index c73f24d..df56ae9 100644 --- a/src/views/dataBoard/dataBoard.vue +++ b/src/views/dataBoard/dataBoard.vue @@ -46,7 +46,7 @@
人均营养分
-
{{dinnerType}} {{mealTimeStart && mealTimeEnd ? `(${mealTimeStart} ~ ${mealTimeEnd})` : ''}}
+
{{mealType}} {{mealTimeStart && mealTimeEnd ? `(${mealTimeStart} ~ ${mealTimeEnd})` : ''}}
@@ -59,8 +59,8 @@
- - {{preName(item.name)}} {{item.userScore}}分 第{{item?.sort}}名; + + {{preName(item.userName)}} {{item.score}}分 第{{item?.rank}}名;
@@ -95,12 +95,12 @@ {{index+1}}
- +
-
{{preName(item.name)}}
-
{{ item.userScore}}
-
{{preDate(item.orderDate)}}
+
{{preName(item.userName)}}
+
{{ item.score}}
+
{{preDate(item.pickupTime)}}
@@ -141,14 +141,14 @@
轮播图片
-
16.8
+
{{ deviceData.price }}
{{ parseInt(deviceData.calorie) }}kcal
diff --git a/src/views/led/led.vue b/src/views/led/led.vue index 7901e8c..cdbf27c 100644 --- a/src/views/led/led.vue +++ b/src/views/led/led.vue @@ -30,7 +30,7 @@ export default { }, data() { return { - url: "http://192.168.10.101:24801/nutrition/", + url: "https://dev.yixiong-tech.com:8081/nutrition/", screenWidth: 1920, deviceDataList: [{ deviceCode: '1', @@ -81,9 +81,6 @@ export default { } }, mounted() { - callBackMqttMessage((data) => { - this.getInfoByDevice(data?.message || {}); - }) }, created() { this.initData(); @@ -117,6 +114,7 @@ export default { }).then(function (res) { if (res?.code === '00000') { _this.deviceDataList = res?.data?.foodList.map((item, index) => { + item.calorie = item.calorie ? item.calorie : 0; if (!item.deviceCode) { item.deviceCode = index.toString(); } @@ -129,12 +127,16 @@ export default { }) }, initMQ() { - initMqtt('deviceId-' + Math.random().toString(16).substring(2, 8), 'yx/device/foodRecord/needUpdate', 'led', { + const clientId = 'deviceId-' + Math.random().toString(16).substring(2, 8); + initMqtt(clientId, 'yx/device/foodRecord/needUpdate', 'led', { username: process.env.VUE_APP_LED_MQTT_USERNAME, password: process.env.VUE_APP_LED_MQTT_PASSWORD, - head: 'wss' + head: 'ws' }).then((res) => { - console.log(res) + console.log(res); + callBackMqttMessage('led', (data) => { + this.getInfoByDevice(data?.message || {}); + }); }).catch((err) => { console.log(err) })