20230421
This commit is contained in:
parent
83c46fe83f
commit
5da48a181e
|
@ -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; // 一个测试用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: 0, // 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("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({
|
||||
|
|
|
@ -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; // 一个测试用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) {
|
||||
|
|
|
@ -238,7 +238,7 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- </div> -->
|
||||
</template>
|
||||
|
||||
<script>
|
||||
|
|
Loading…
Reference in New Issue