@cuppet/core 1.3.1 → 1.3.2
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.
|
@@ -28,7 +28,8 @@ When('I unsubscribe from MQTT topic {string}', async function (topic) {
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
When('I prepare MQTT message as JSON', async function (docString) {
|
|
31
|
-
|
|
31
|
+
const message = JSON.stringify(docString);
|
|
32
|
+
await mqttFunctions.prepareMessage(message, true);
|
|
32
33
|
});
|
|
33
34
|
|
|
34
35
|
/**
|
|
@@ -57,7 +58,15 @@ When(
|
|
|
57
58
|
* @example When I publish JSON '{"temperature": 25, "humidity": 60}' to MQTT topic "sensors/room1"
|
|
58
59
|
*/
|
|
59
60
|
When('I publish JSON {string} to MQTT topic {string}', async function (jsonMessage, topic) {
|
|
60
|
-
await mqttFunctions.publishJsonMessage(this.mqttManager,
|
|
61
|
+
await mqttFunctions.publishJsonMessage(this.mqttManager, topic, jsonMessage);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Publish an already prepared JSON message to an MQTT topic
|
|
66
|
+
* @example When I publish the prepared JSON message to MQTT topic "sensors/room1"
|
|
67
|
+
*/
|
|
68
|
+
When('I publish the prepared JSON message to MQTT topic {string}', async function (topic) {
|
|
69
|
+
await mqttFunctions.publishJsonMessage(this.mqttManager, topic);
|
|
61
70
|
});
|
|
62
71
|
|
|
63
72
|
/**
|
package/package.json
CHANGED
package/src/dataStorage.js
CHANGED
|
@@ -126,6 +126,9 @@ module.exports = {
|
|
|
126
126
|
const regex = /%([^%]+)%/g;
|
|
127
127
|
const allVariables = this.getJsonFile();
|
|
128
128
|
// The convention dictates if function argument is not used, it can be replaced by "_".
|
|
129
|
+
if (typeof text !== 'string') {
|
|
130
|
+
throw new Error(`The value passed to checkForMultipleVariables is not a string: ${text}`);
|
|
131
|
+
}
|
|
129
132
|
const result = text.replace(regex, (_, group) => {
|
|
130
133
|
return allVariables[group];
|
|
131
134
|
});
|
package/src/mqttFunctions.js
CHANGED
|
@@ -71,33 +71,25 @@ module.exports = {
|
|
|
71
71
|
*/
|
|
72
72
|
publishMessage: async function (mqttManager, message, topic, qos = 0, retain = false) {
|
|
73
73
|
const resolvedTopic = await this.prepareTopic(topic);
|
|
74
|
-
const resolvedMessage =
|
|
74
|
+
const resolvedMessage = await this.prepareMessage(message);
|
|
75
75
|
await mqttManager.publish(resolvedTopic, resolvedMessage, { qos, retain });
|
|
76
|
-
delete this.messageObject;
|
|
77
76
|
},
|
|
78
77
|
|
|
79
78
|
/**
|
|
80
79
|
* Publish a JSON message to a topic
|
|
81
80
|
* @param {object} mqttManager - MQTT manager instance
|
|
82
|
-
* @param {string} jsonString - JSON string to publish
|
|
83
81
|
* @param {string} topic - Topic to publish to
|
|
82
|
+
* @param {string} jsonString - JSON string to publish
|
|
84
83
|
* @param {number} qos - Quality of Service level
|
|
85
84
|
* @param {boolean} retain - Whether to retain the message
|
|
86
85
|
* @returns {Promise<void>}
|
|
87
86
|
*/
|
|
88
|
-
publishJsonMessage: async function (mqttManager,
|
|
87
|
+
publishJsonMessage: async function (mqttManager, topic, jsonString = null, qos = 0, retain = false) {
|
|
89
88
|
const resolvedTopic = await this.prepareTopic(topic);
|
|
90
|
-
const resolvedJson =
|
|
91
|
-
|
|
92
|
-
// Validate JSON
|
|
93
|
-
try {
|
|
94
|
-
JSON.parse(resolvedJson);
|
|
95
|
-
} catch (error) {
|
|
96
|
-
throw new Error(`Invalid JSON message: ${error.message}`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
await mqttManager.publish(resolvedTopic, resolvedJson, { qos, retain });
|
|
89
|
+
const resolvedJson = jsonString ? await this.prepareMessage(jsonString, true) : this.messageObject;
|
|
90
|
+
// Delete the message object before the message is published to avoid conflicts if the request fails.
|
|
100
91
|
delete this.messageObject;
|
|
92
|
+
await mqttManager.publish(resolvedTopic, resolvedJson, { qos, retain });
|
|
101
93
|
},
|
|
102
94
|
|
|
103
95
|
/**
|