This commit is contained in:
chengdandan 2023-04-21 18:19:06 +08:00
parent 83c46fe83f
commit 5da48a181e
3 changed files with 80 additions and 81 deletions

View File

@ -102,6 +102,7 @@
<script>
import { GetLevelAlarm } from "../api/index";
import mqtt from "mqtt"; // mqtt
export default {
name: "court",
data() {
@ -142,8 +143,85 @@ export default {
this.getSecondAlarm();
//
this.getThirdAlarm();
this.getMqttData()
},
methods: {
getMqttData(){
this.getDataByMqtt(
// "ws:ht.mqtt.umayle.com:2022/mqtt",
"ws:172.16.1.253:1883/mqtt",
"FirstLevel",
);
},
// mqtt
getDataByMqtt(url, topic) {
const clientId = "test_id_" + String(new Date().getTime()); //
const host = url; // urlws://broker.emqx.io:8083/mqtt
const options = {
//
// IP
// host: host,
// port: port,
keepalive: 60, // 60s0
username: 'test', //
password: 1234, //
clientId: clientId, // ID
protocolId: "MQTT",
protocolVersion: 4,
clean: true, // false线QoS12
reconnectPeriod: 2000, // 1000
connectTimeout: 30 * 1000, // CONNACK
will: {
// Broker
topic: "WillMsg", //
payload: "[MQTT-TEST] 遗嘱消息:连接异常断开!", //
qos: 0, // QoS(Quality of Service)QoS0QoS11QoS21
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("FirstLevel") != -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);
} else if (topic.indexOf("InConvoyor") != -1) {
}
});
}
//
this.mqttClient.subscribe(topic, { qos: 1 });
},
//
getFirstAlarm() {
GetLevelAlarm({

View File

@ -387,7 +387,7 @@ export default {
}
this.calculateEcharts();
this.calculateEchartsTwo();
window.createMqtt = this.createMqtt();
// window.createMqtt = this.createMqtt();
this.changeWarn(0)
},
methods: {
@ -399,86 +399,7 @@ export default {
},
/** 实时数据分类 */
// mqtt
getDataByMqtt(url, topic, cIdNum) {
const clientId = "test_id_" + String(new Date().getTime()); //
const host = url; // urlws://broker.emqx.io:8083/mqtt
const options = {
//
// IP
// host: host,
// port: port,
keepalive: 60, // 60s0
// username: 'test', //
// password: 1234, //
clientId: clientId, // ID
protocolId: "MQTT",
protocolVersion: 4,
clean: true, // false线QoS12
reconnectPeriod: 2000, // 1000
connectTimeout: 30 * 1000, // CONNACK
will: {
// Broker
topic: "WillMsg", //
payload: "[MQTT-TEST] 遗嘱消息:连接异常断开!", //
qos: 1, // QoS(Quality of Service)QoS0QoS11QoS21
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) {

View File

@ -238,7 +238,7 @@
</div>
</div>
</div>
</div>
<!-- </div> -->
</template>
<script>