设备状态、信号、属性、功能实时订阅

This commit is contained in:
kerwincui
2022-07-29 18:34:52 +08:00
parent c887c9b011
commit fd7f186ccc
3 changed files with 140 additions and 10 deletions

View File

@@ -250,7 +250,6 @@
</table> </table>
</el-col> </el-col>
</el-row> </el-row>
</el-col> </el-col>
<el-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6" style="padding:20px;"> <el-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6" style="padding:20px;">
<div style="padding:20px;"> <div style="padding:20px;">
@@ -262,7 +261,6 @@
<div style="padding:20px;"> <div style="padding:20px;">
<div ref="pieDisk" style="height:161px;margin-bottom:-20px;"></div> <div ref="pieDisk" style="height:161px;margin-bottom:-20px;"></div>
</div> </div>
</el-col> </el-col>
</el-row> </el-row>

View File

@@ -405,14 +405,18 @@ export default {
if (this.deviceList[i].serialNumber == deviceNum) { if (this.deviceList[i].serialNumber == deviceNum) {
this.deviceList[i].status = data.message.status; this.deviceList[i].status = data.message.status;
this.deviceList[i].isShadow = data.message.isShadow; this.deviceList[i].isShadow = data.message.isShadow;
this.deviceList[i].rssi = data.message.rssi;
return;
} }
} }
// 更新实时监测模型的状态 // 更新实时监测模型的状态
if (this.monitorDevice.serialNumber == deviceNum) { if (this.monitorDevice.serialNumber == deviceNum) {
this.monitorDevice.status = data.message.status; this.monitorDevice.status = data.message.status;
this.monitorDevice.isShadow = data.message.isShadow; this.monitorDevice.isShadow = data.message.isShadow;
this.monitorDevice.rssi = data.message.rssi;
} }
} else if (topics[3] == "monitor") { }
if (topics[3] == "monitor") {
// 实时监测 // 实时监测
this.chartLoading = false; this.chartLoading = false;
for (let k = 0; k < data.message.length; k++) { for (let k = 0; k < data.message.length; k++) {
@@ -432,21 +436,84 @@ export default {
data: this.dataList[i].data data: this.dataList[i].data
}] }]
}); });
return;
} }
} }
} }
} }
if (topics[3] == 'property' || topics[3] == 'function') {
// 更新列表中设备的属性
for (let i = 0; i < this.deviceList.length; i++) {
if (this.deviceList[i].serialNumber == deviceNum) {
for (let j = 0; j < data.message.length; j++) {
let isComplete = false;
// 布尔类型
for (let k = 0; k < this.deviceList[i].boolList.length && !isComplete; k++) {
if (this.deviceList[i].boolList[k].id == data.message[j].id) {
this.deviceList[i].boolList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 枚举类型
for (let k = 0; k < this.deviceList[i].enumList.length && !isComplete; k++) {
if (this.deviceList[i].enumList[k].id == data.message[j].id) {
this.deviceList[i].enumList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 字符串类型
for (let k = 0; k < this.deviceList[i].stringList.length && !isComplete; k++) {
if (this.deviceList[i].stringList[k].id == data.message[j].id) {
this.deviceList[i].stringList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 数组类型
for (let k = 0; k < this.deviceList[i].arrayList.length && !isComplete; k++) {
if (this.deviceList[i].arrayList[k].id == data.message[j].id) {
this.deviceList[i].arrayList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 整数类型
for (let k = 0; k < this.deviceList[i].integerList.length && !isComplete; k++) {
if (this.deviceList[i].integerList[k].id == data.message[j].id) {
this.deviceList[i].integerList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 小数类型
for (let k = 0; k < this.deviceList[i].decimalList.length && !isComplete; k++) {
if (this.deviceList[i].decimalList[k].id == data.message[j].id) {
this.deviceList[i].decimalList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
}
return;
}
}
}
}, },
/** Mqtt订阅主题 */ /** Mqtt订阅主题 */
mqttSubscribe(list) { mqttSubscribe(list) {
// 订阅当前页面设备状态和实时监测 // 订阅当前页面设备状态和实时监测
let topics = []; let topics = [];
// 订阅数太多会导致emqx连接中断或者订阅缓慢
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
let topicStatus = "/" + list[i].productId + "/" + list[i].serialNumber + "/status/post"; let topicStatus = "/" + list[i].productId + "/" + list[i].serialNumber + "/status/post";
let topicMonitor = "/" + list[i].productId + "/" + list[i].serialNumber + "/monitor/post"; let topicMonitor = "/" + list[i].productId + "/" + list[i].serialNumber + "/monitor/post";
let topicProperty = '/' + list[i].productId + '/' + list[i].serialNumber + '/property/post';
let topicFunction = '/' + list[i].productId + '/' + list[i].serialNumber + '/function/post';
topics.push(topicStatus); topics.push(topicStatus);
topics.push(topicMonitor); topics.push(topicMonitor);
topics.push(topicProperty);
topics.push(topicFunction);
} }
this.subscribes = topics; this.subscribes = topics;
}, },

View File

@@ -344,14 +344,79 @@ export default {
// 更新列表中设备的状态 // 更新列表中设备的状态
this.deviceInfo.status = data.message.status; this.deviceInfo.status = data.message.status;
this.deviceInfo.isShadow = data.message.isShadow; this.deviceInfo.isShadow = data.message.isShadow;
this.deviceInfo.rssi = data.message.rssi;
this.updateDeviceStatus(this.deviceInfo); this.updateDeviceStatus(this.deviceInfo);
return;
}
if (topics[3] == 'property' || topics[3] == 'function') {
// 更新列表中设备的属性
if (this.deviceInfo.serialNumber == deviceNum) {
for (let j = 0; j < data.message.length; j++) {
let isComplete = false;
// 布尔类型
for (let k = 0; k < this.deviceInfo.boolList.length && !isComplete; k++) {
if (this.deviceInfo.boolList[k].id == data.message[j].id) {
this.deviceInfo.boolList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 枚举类型
for (let k = 0; k < this.deviceInfo.enumList.length && !isComplete; k++) {
if (this.deviceInfo.enumList[k].id == data.message[j].id) {
this.deviceInfo.enumList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 字符串类型
for (let k = 0; k < this.deviceInfo.stringList.length && !isComplete; k++) {
if (this.deviceInfo.stringList[k].id == data.message[j].id) {
this.deviceInfo.stringList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 数组类型
for (let k = 0; k < this.deviceInfo.arrayList.length && !isComplete; k++) {
if (this.deviceInfo.arrayList[k].id == data.message[j].id) {
this.deviceInfo.arrayList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 整数类型
for (let k = 0; k < this.deviceInfo.integerList.length && !isComplete; k++) {
if (this.deviceInfo.integerList[k].id == data.message[j].id) {
this.deviceInfo.integerList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
// 小数类型
for (let k = 0; k < this.deviceInfo.decimalList.length && !isComplete; k++) {
if (this.deviceInfo.decimalList[k].id == data.message[j].id) {
this.deviceInfo.decimalList[k].shadow = data.message[j].value;
isComplete = true;
break;
}
}
}
return;
}
} }
}, },
/** Mqtt订阅主题 */ /** Mqtt订阅主题 */
mqttSubscribe(device) { mqttSubscribe(device) {
// 订阅当前设备状态 // 订阅当前设备状态和无模型
let topic = "/" + device.productId + "/" + device.serialNumber + "/status/post"; let topicStatus = '/' + device.productId + '/' + device.serialNumber + '/status/post';
this.subscribes = [topic]; let topicProperty = '/' + device.productId + '/' + device.serialNumber + '/property/post';
let topicFunction = '/' + device.productId + '/' + device.serialNumber + '/function/post';
let topics = [];
topics.push(topicStatus);
topics.push(topicProperty);
topics.push(topicFunction);
this.subscribes = topics;
}, },
/** 更新设备状态 */ /** 更新设备状态 */
updateDeviceStatus(device) { updateDeviceStatus(device) {