协议修改
This commit is contained in:
@ -41,7 +41,6 @@
|
|||||||
disabled
|
disabled
|
||||||
@click="selectShow(item, index)"
|
@click="selectShow(item, index)"
|
||||||
/>
|
/>
|
||||||
<div></div>
|
|
||||||
</view>
|
</view>
|
||||||
<view class="">
|
<view class="">
|
||||||
{{ item.selectLabel || $t("homePage.alarm.placeSelect") }}
|
{{ item.selectLabel || $t("homePage.alarm.placeSelect") }}
|
||||||
@ -78,7 +77,7 @@
|
|||||||
import { formList } from "@/common/form.js";
|
import { formList } from "@/common/form.js";
|
||||||
import { Langlist } from "@/common/lang";
|
import { Langlist } from "@/common/lang";
|
||||||
import pako from "pako";
|
import pako from "pako";
|
||||||
import mqtt from "mqtt";
|
import * as Paho from "paho-mqtt";
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@ -115,7 +114,7 @@ export default {
|
|||||||
},
|
},
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
if (this.mqttClient) {
|
if (this.mqttClient) {
|
||||||
this.mqttClient.end()
|
this.mqttClient.disconnect()
|
||||||
this.mqttClient = null
|
this.mqttClient = null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -194,72 +193,66 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
initMQTT() {
|
initMQTT() {
|
||||||
// 使用mqtt.js库创建MQTT连接
|
// 创建MQTT客户端实例
|
||||||
|
const clientId = 'client_' + Math.random().toString(36).substr(2, 9);
|
||||||
|
// 使用WebSocket连接方式
|
||||||
|
this.mqttClient = new Paho.Client('13.39.200.14', 8083, clientId);
|
||||||
|
|
||||||
|
// 设置回调函数
|
||||||
|
this.mqttClient.onConnectionLost = this.onConnectionLost;
|
||||||
|
this.mqttClient.onMessageArrived = this.onMessageArrived;
|
||||||
|
|
||||||
|
// 连接选项
|
||||||
const options = {
|
const options = {
|
||||||
clientId: 'client_' + Math.random().toString(36).substr(2, 9),
|
onSuccess: this.onConnect,
|
||||||
username: 'admin',
|
onFailure: this.onFail,
|
||||||
|
userName: 'admin',
|
||||||
password: 'zzkj@688737',
|
password: 'zzkj@688737',
|
||||||
keepalive: 60,
|
useSSL: false,
|
||||||
clean: true,
|
timeout: 30,
|
||||||
reconnectPeriod: 5000,
|
keepAliveInterval: 60,
|
||||||
connectTimeout: 30 * 1000
|
cleanSession: true
|
||||||
};
|
};
|
||||||
|
|
||||||
// 使用WebSocket连接到MQTT broker
|
// 连接到MQTT broker
|
||||||
const brokerURL = 'ws://13.39.200.14:8083/mqtt';
|
this.mqttClient.connect(options);
|
||||||
this.mqttClient = mqtt.connect(brokerURL, options);
|
},
|
||||||
|
onConnect() {
|
||||||
// 连接成功
|
console.log('Connected to MQTT broker!');
|
||||||
this.mqttClient.on('connect', () => {
|
|
||||||
console.log('MQTT连接成功!');
|
|
||||||
// 订阅主题
|
// 订阅主题
|
||||||
this.mqttClient.subscribe('WJ_Get_NewControlSystem', (err) => {
|
this.mqttClient.subscribe('WJ_Get_NewControlSystem');
|
||||||
if (err) {
|
|
||||||
console.log('订阅主题失败:', err);
|
|
||||||
} else {
|
|
||||||
console.log('订阅主题成功: WJ_Get_NewControlSystem');
|
|
||||||
// 发送获取数据的消息
|
// 发送获取数据的消息
|
||||||
this.sendMQTTMessage('WJ_Get_NewControlSystem', JSON.stringify({
|
this.sendMQTTMessage('WJ_Get_NewControlSystem', JSON.stringify({
|
||||||
fun: 'GET',
|
fun: 'GET',
|
||||||
type: 'WJ_Get_NewControlSystem',
|
type: 'WJ_Get_NewControlSystem',
|
||||||
content: 0
|
content: 0
|
||||||
}));
|
}));
|
||||||
|
},
|
||||||
|
onFail(error) {
|
||||||
|
console.error('Failed to connect:', error);
|
||||||
|
},
|
||||||
|
onConnectionLost(responseObject) {
|
||||||
|
if (responseObject.errorCode !== 0) {
|
||||||
|
console.log('Connection lost: ' + responseObject.errorMessage);
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
});
|
onMessageArrived(message) {
|
||||||
|
console.log('Message arrived:', message.payloadString);
|
||||||
// 连接错误
|
|
||||||
this.mqttClient.on('error', (err) => {
|
|
||||||
console.error('MQTT连接错误:', err);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 连接关闭
|
|
||||||
this.mqttClient.on('close', () => {
|
|
||||||
console.log('MQTT连接关闭');
|
|
||||||
});
|
|
||||||
|
|
||||||
// 重连
|
|
||||||
this.mqttClient.on('reconnect', () => {
|
|
||||||
console.log('MQTT正在重连...');
|
|
||||||
});
|
|
||||||
|
|
||||||
// 收到消息
|
|
||||||
this.mqttClient.on('message', (topic, message) => {
|
|
||||||
console.log('收到MQTT消息:', topic, message.toString());
|
|
||||||
try {
|
try {
|
||||||
// let data = JSON.parse(message.toString());
|
// let data = JSON.parse(message.payloadString);
|
||||||
// if (data.type == 'WJ_Get_NewControlSystem') {
|
// if (data.type == 'WJ_Get_NewControlSystem') {
|
||||||
// this.control(data.content);
|
// this.control(data.content);
|
||||||
// }
|
// }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('解析MQTT消息错误:', error);
|
console.log('解析MQTT消息错误:', error);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
},
|
},
|
||||||
sendMQTTMessage(topic, payload) {
|
sendMQTTMessage(topic, payload) {
|
||||||
if (this.mqttClient && this.mqttClient.connected) {
|
if (this.mqttClient && this.mqttClient.isConnected()) {
|
||||||
this.mqttClient.publish(topic, payload);
|
const message = new Paho.Message(payload);
|
||||||
console.log('MQTT消息发送成功:', topic, payload);
|
message.destinationName = topic;
|
||||||
|
this.mqttClient.send(message);
|
||||||
|
console.log('MQTT消息发送成功:', payload);
|
||||||
} else {
|
} else {
|
||||||
console.log('MQTT客户端未连接');
|
console.log('MQTT客户端未连接');
|
||||||
if (!this.mqttClient) {
|
if (!this.mqttClient) {
|
||||||
|
|||||||
Reference in New Issue
Block a user