Commit 9a2c6e24 authored by 李纪文's avatar 李纪文

feat: mqttview推送数据非json容错处理

parent 4955a6f3
...@@ -74,7 +74,7 @@ class MqttView { ...@@ -74,7 +74,7 @@ class MqttView {
this.flag = true; this.flag = true;
this.devices.forEach((item) => { this.devices.forEach((item) => {
if (item) { if (item) {
var saveWaTopic = this.siteCode + '/' + item.replace(/[#+]/g, '@'); const saveWaTopic = this.siteCode + '/' + item.replace(/[#+]/g, '@');
this.saveWaClient.subscribe(saveWaTopic); this.saveWaClient.subscribe(saveWaTopic);
} }
}); });
...@@ -88,7 +88,7 @@ class MqttView { ...@@ -88,7 +88,7 @@ class MqttView {
this.isAlarm && this.isAlarm &&
this.devices.forEach((item) => { this.devices.forEach((item) => {
if (item) { if (item) {
var saveWaTopic = this.siteCode + '/' + item.replace(/[#+]/g, '@') + '/alarm' + '/#'; const saveWaTopic = this.siteCode + '/' + item.replace(/[#+]/g, '@') + '/alarm' + '/#';
this.saveWaClient.subscribe(saveWaTopic); this.saveWaClient.subscribe(saveWaTopic);
} }
}); });
...@@ -122,18 +122,22 @@ class MqttView { ...@@ -122,18 +122,22 @@ class MqttView {
//消息接收(控制和订阅) //消息接收(控制和订阅)
onMessageArrived(message, infoType) { onMessageArrived(message, infoType) {
var topic = message.topic; try {
var code = topic.split('/')[topic.split('/').length - 1]; const topic = message.topic;
if (topic.indexOf('callback/control/' + this.siteCode) > -1) { const code = topic.split('/')[topic.split('/').length - 1];
this.controlback(message.payloadString, code, topic); if(!message?.payloadString) return false;
return false; const payloadString = JSON.stringify(JSON.parse(message.payloadString));
} if (topic.indexOf('callback/control/' + this.siteCode) > -1) {
if (topic.indexOf('alarm') > -1) { this.controlback(payloadString, code, topic);
var alarmCode = topic.split('/')[1]; return false;
this.alarmback(message.payloadString, alarmCode, topic); }
return false; if (topic.indexOf('alarm') > -1) {
} const alarmCode = topic.split('/')[1];
this.callback(message.payloadString, code, topic); this.alarmback(payloadString, alarmCode, topic);
return false;
}
this.callback(payloadString, code, topic);
} catch (err) {}
} }
//消息发送 //消息发送
...@@ -147,7 +151,7 @@ class MqttView { ...@@ -147,7 +151,7 @@ class MqttView {
val, val,
controlMode, controlMode,
) { ) {
var info = { const info = {
userName: userName, userName: userName,
password: password, password: password,
callbackGuid: callbackGuid, callbackGuid: callbackGuid,
...@@ -159,7 +163,7 @@ class MqttView { ...@@ -159,7 +163,7 @@ class MqttView {
controlMode: controlMode ? controlMode : '', controlMode: controlMode ? controlMode : '',
tag: tag, tag: tag,
}; };
var message = new MqttClient.Message(JSON.stringify(info)); let message = new MqttClient.Message(JSON.stringify(info));
message.destinationName = 'setdata/' + this.siteCode + '/' + devicecode; message.destinationName = 'setdata/' + this.siteCode + '/' + devicecode;
this.saveWaClient.send(message); this.saveWaClient.send(message);
} }
...@@ -168,7 +172,7 @@ class MqttView { ...@@ -168,7 +172,7 @@ class MqttView {
createGuid() { createGuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.replace(/[xy]/g, function (c) { .replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0, const r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8; v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16); return v.toString(16);
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment