From e0dd6bb8fe3fb04923dcd7bc2d0915f6bfd3a212 Mon Sep 17 00:00:00 2001 From: chengdandan <2549057906@qq.com> Date: Wed, 19 Apr 2023 17:50:09 +0800 Subject: [PATCH] 20230419 --- src/utils/mqttConfig.js | 1 + src/views/energySaving.vue | 83 +++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/utils/mqttConfig.js b/src/utils/mqttConfig.js index 2600510..7563e2f 100644 --- a/src/utils/mqttConfig.js +++ b/src/utils/mqttConfig.js @@ -33,6 +33,7 @@ class mqttHandle { */ createConnect(onConnected) { //配置链接 + // console.log("connectUrl",connectUrl) const { host, port, endpoint, ...options } = this.connect; console.log("this.connect.host", this.connect.host) const connectUrl = 'ws://138.227.111.141:8083/mqtt'; diff --git a/src/views/energySaving.vue b/src/views/energySaving.vue index 2883c85..187ad05 100644 --- a/src/views/energySaving.vue +++ b/src/views/energySaving.vue @@ -398,7 +398,88 @@ export default { window.PubScribe(topicSends, this.realInfo); }, /** 实时数据分类 */ - realInfo(topic, message) { + + // mqtt订阅(独立) + getDataByMqtt(url, topic, cIdNum) { + const clientId = "test_id_" + String(new Date().getTime()); // 用户名 + const host = url; // 一个测试用url,改成给的,ws://broker.emqx.io:8083/mqtt + const options = { + // 配置 + // 测试:订阅本机IP + // host: host, + // port: port, + + keepalive: 60, // 心跳时间,默认60s,设置为0禁用 + // username: 'test', // 用户名(可选) + // password: 1234, // 密码(可选) + clientId: clientId, // 客户端ID,默认随机生成 + protocolId: "MQTT", + protocolVersion: 4, + clean: true, // false在离线时接收QoS1和2的消息 + reconnectPeriod: 2000, // 重连间隔,默认1000毫秒 + connectTimeout: 30 * 1000, // 收到CONNACK之前的等待时间 + will: { + // 遗嘱消息(客户端严重断开连接时Broker将自动发送的消息) + topic: "WillMsg", // 要发布的主题 + payload: "[MQTT-TEST] 遗嘱消息:连接异常断开!", // 要发布的消息 + qos: 1, // QoS(Quality of Service),QoS0:只负责发,QoS1:保证消息至少送达1次,QoS2:保证消息到且仅到1次 + retain: false, // 保留标志 + }, + }; + if (this.mqttClient == undefined) { + this.mqttClient = mqtt.connect(host, options); // 连接 + // const client = mqtt.connect(host, options); // 连接 + // const client = mqtt.connect(host) // 连接 + // 错误回调 + this.mqttClient.on("error", (err) => { + // console.log("[MQTT-TEST] 连接错误:", this.models[cIdNum].cName); + this.mqttClient.end(); + }); + // 重连回调 + this.mqttClient.on("reconnect", () => { + console.log("[MQTT-TEST] 重连中……"); + }); + // 连接回调 + this.mqttClient.on("connect", () => { + // console.log( + // "[MQTT-TEST] 已连接的客户端ID: " + this.models[cIdNum].cName + // ); + }); + // 接收回调 + this.mqttClient.on("message", (topic, message, packet) => { + console.log( + `[MQTT-TEST] 从主题 "${topic}" 收到的内容: ${message.toString()}`, + new Date(), + new Date().getMilliseconds() + ); + // 解析后端数据 + let that = this; + if (topic.indexOf("device/publish/FB80") != -1) { + const utf8decoder = new TextDecoder(); + const u8arr = new Uint8Array(message); + const temp = utf8decoder.decode(u8arr); // 将二进制数据转为字符串 + const msg = JSON.parse(temp); //这一步报错则返回的是二进制流图片,不报错则返回的是JSON的错误提示数据 + console.log("msg", msg); + if (msg.params.设备运行状态 == 1) { + msg.params.设备运行状态 = "正常"; + } else { + msg.params.设备运行状态 = "异常"; + } + that.sunList[0].count = msg.params.光伏逆变器日发电量; + that.sunList[1].count = msg.params.光伏逆变器总发电量; + that.sunList[2].count = msg.params.有功功率; + that.sunList[3].count = msg.params.设备运行状态; + that.sunList[4].count = msg.params.机器总运行时间; + that.sunList[5].count = msg.params.警告信息; + } else if (topic.indexOf("InConvoyor") != -1) { + cId = topic.replace("InConvoyor", ""); + } + }); + } + // 订阅 + this.mqttClient.subscribe(topic, { qos: 1 }); + }, + realInfo(topic, message) { let that = this; switch (topic) { // 接收托片