mqtt完善

This commit is contained in:
kerwincui
2022-04-16 21:31:09 +08:00
parent a8ab4bf2e2
commit 5b277fa130
4 changed files with 24 additions and 23 deletions

View File

@@ -80,7 +80,7 @@
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8"> <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="8">
<div style="border:1px solid #dfe4ed;border-radius:5px;padding:5px;text-align:center;line-height:400px;"> <div style="border:1px solid #dfe4ed;border-radius:5px;padding:5px;text-align:center;line-height:400px;">
<div id="map" style="height:435px;width:100%;">地图展示区域新增后显示</div> <div id="map" style="height:435px;width:100%;">地图展示区域新增后显示</div>
</div> </div>

View File

@@ -197,7 +197,9 @@ import {
export default { export default {
name: "Device", name: "Device",
dicts: ['iot_device_status', 'iot_is_enable'], dicts: ['iot_device_status', 'iot_is_enable'],
components: {mqttClient}, components: {
mqttClient
},
data() { data() {
return { return {
// 实时监测间隔 // 实时监测间隔
@@ -337,11 +339,9 @@ export default {
// 发布 // 发布
this.publish = { this.publish = {
topic: topic, topic: topic,
message: message message: message,
name: model.name
}; };
if (model.name != "") {
this.$modal.notifySuccess("[ " + model.name + " ] 指令发送成功");
}
} }
}, },
/** 接收到Mqtt回调 */ /** 接收到Mqtt回调 */

View File

@@ -4,9 +4,6 @@
<script> <script>
import mqtt from 'mqtt' import mqtt from 'mqtt'
import {
cacheJsonThingsModel
} from "@/api/iot/model";
import { import {
getToken getToken
} from "@/utils/auth"; } from "@/utils/auth";
@@ -26,7 +23,7 @@ export default {
watch: { watch: {
// 获取到父组件传递的值 // 获取到父组件传递的值
publish: function (val, oldVal) { publish: function (val, oldVal) {
this.mqttPublish(val.topic, val.message); this.mqttPublish(val.topic, val.message,val.name);
}, },
subscribes: function (val, oldVal) { subscribes: function (val, oldVal) {
this.connectMqtt(val); this.connectMqtt(val);
@@ -80,9 +77,10 @@ export default {
}, },
/** 发布消息 */ /** 发布消息 */
mqttPublish(topic, message) { mqttPublish(topic, message, name) {
if (!this.client.connected) { if (!this.client.connected) {
console.log('客户端未连接') console.log('客户端未连接')
this.$modal.notifyError("Mqtt客户端未连接");
return return
} }
this.client.publish(topic, message, { this.client.publish(topic, message, {
@@ -91,6 +89,7 @@ export default {
if (!err) { if (!err) {
console.log('成功发布主题:' + topic) console.log('成功发布主题:' + topic)
console.log('主题内容:' + message); console.log('主题内容:' + message);
this.$modal.notifySuccess("[ " + name + " ] 指令发送成功");
} }
}) })
}, },
@@ -110,7 +109,8 @@ export default {
mqttError() { mqttError() {
this.client.on('error', (error) => { this.client.on('error', (error) => {
console.log('连接失败:', error) console.log('连接失败:', error)
this.client.end() this.$modal.notifyError("Mqtt客户端连接失败");
this.client.end();
}) })
}, },
/** 取消订阅 */ /** 取消订阅 */
@@ -123,7 +123,8 @@ export default {
unconnectMqtt() { unconnectMqtt() {
this.client.end() this.client.end()
this.client = null this.client = null
console.log('服务器已断开连接!') console.log('服务器已断开连接!');
this.$modal.notifyError("Mqtt服务器已断开连接");
}, },
/** 监听服务器重新连接 */ /** 监听服务器重新连接 */
reconnectMqtt() { reconnectMqtt() {

View File

@@ -181,7 +181,9 @@ import mqttClient from './mqtt-client.vue'
export default { export default {
name: "running-status", name: "running-status",
dicts: ['iot_yes_no'], dicts: ['iot_yes_no'],
components: {mqttClient}, components: {
mqttClient
},
props: { props: {
device: { device: {
type: Object, type: Object,
@@ -302,10 +304,8 @@ export default {
// 发布 // 发布
this.publish = { this.publish = {
topic: topic, topic: topic,
message: message message: message,
} name: model.name
if (model) {
this.$modal.notifySuccess("[ " + model.name + " ] 指令发送成功");
} }
} }
}, },