@machhub-dev/sdk-ts 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -90,20 +90,20 @@ class MQTTService {
|
|
|
90
90
|
this.client.on('message', (topic, message) => {
|
|
91
91
|
for (const subscribedTopic of this.subscribedTopics) {
|
|
92
92
|
if (topic === subscribedTopic.topic) {
|
|
93
|
-
const parsedMessage = this.parseMessage(message);
|
|
93
|
+
const parsedMessage = this.parseMessage(message, topic);
|
|
94
94
|
subscribedTopic.handler(parsedMessage);
|
|
95
95
|
break;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
});
|
|
99
99
|
}
|
|
100
|
-
parseMessage(message) {
|
|
100
|
+
parseMessage(message, topic) {
|
|
101
101
|
try {
|
|
102
102
|
return JSON.parse(message.toString());
|
|
103
103
|
}
|
|
104
104
|
catch (error) {
|
|
105
|
-
console.
|
|
106
|
-
return
|
|
105
|
+
console.warn("Failed to parse message as JSON '" + topic + "' : ", message.toString());
|
|
106
|
+
return message.toString();
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
}
|
|
@@ -84,20 +84,20 @@ export class MQTTService {
|
|
|
84
84
|
this.client.on('message', (topic, message) => {
|
|
85
85
|
for (const subscribedTopic of this.subscribedTopics) {
|
|
86
86
|
if (topic === subscribedTopic.topic) {
|
|
87
|
-
const parsedMessage = this.parseMessage(message);
|
|
87
|
+
const parsedMessage = this.parseMessage(message, topic);
|
|
88
88
|
subscribedTopic.handler(parsedMessage);
|
|
89
89
|
break;
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
});
|
|
93
93
|
}
|
|
94
|
-
parseMessage(message) {
|
|
94
|
+
parseMessage(message, topic) {
|
|
95
95
|
try {
|
|
96
96
|
return JSON.parse(message.toString());
|
|
97
97
|
}
|
|
98
98
|
catch (error) {
|
|
99
|
-
console.
|
|
100
|
-
return
|
|
99
|
+
console.warn("Failed to parse message as JSON '" + topic + "' : ", message.toString());
|
|
100
|
+
return message.toString();
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
}
|
package/package.json
CHANGED
|
@@ -102,7 +102,7 @@ export class MQTTService {
|
|
|
102
102
|
this.client.on('message', (topic: string, message: Buffer) => {
|
|
103
103
|
for (const subscribedTopic of this.subscribedTopics) {
|
|
104
104
|
if (topic === subscribedTopic.topic) {
|
|
105
|
-
const parsedMessage = this.parseMessage(message);
|
|
105
|
+
const parsedMessage = this.parseMessage(message, topic);
|
|
106
106
|
subscribedTopic.handler(parsedMessage);
|
|
107
107
|
break;
|
|
108
108
|
}
|
|
@@ -110,12 +110,12 @@ export class MQTTService {
|
|
|
110
110
|
});
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
private parseMessage(message: Buffer): unknown {
|
|
113
|
+
private parseMessage(message: Buffer, topic: string): unknown {
|
|
114
114
|
try {
|
|
115
115
|
return JSON.parse(message.toString());
|
|
116
116
|
} catch (error) {
|
|
117
|
-
console.
|
|
118
|
-
return
|
|
117
|
+
console.warn("Failed to parse message as JSON '" + topic + "' : ", message.toString());
|
|
118
|
+
return message.toString();
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
}
|