1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add parsed JSON output option to MQTT subscribe node

This commit is contained in:
Dave Conway-Jones 2019-02-04 16:35:42 +00:00
parent c409af0ea8
commit 79fe7d684c
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
3 changed files with 17 additions and 6 deletions

View File

@ -34,6 +34,7 @@
<option value="auto" data-i18n="mqtt.output.auto"></option>
<option value="buffer" data-i18n="mqtt.output.buffer"></option>
<option value="utf8" data-i18n="mqtt.output.string"></option>
<option value="json" data-i18n="mqtt.output.json"></option>
<option value="base64" data-i18n="mqtt.output.base64"></option>
</select>
</div>

View File

@ -399,16 +399,23 @@ module.exports = function(RED) {
if (this.topic) {
node.brokerConn.register(this);
this.brokerConn.subscribe(this.topic,this.qos,function(topic,payload,packet) {
if (node.datatype =="buffer") {
if (node.datatype === "buffer") {
// payload = payload;
} else if (node.datatype =="base64") {
} else if (node.datatype === "base64") {
payload = payload.toString('base64');
} else if (node.datatype =="utf8") {
} else if (node.datatype === "utf8") {
payload = payload.toString('utf8');
} else if (node.datatype === "json") {
if (isUtf8(payload)) {
payload = payload.toString();
try { payload = JSON.parse(payload); }
catch(e) { node.error(RED._("mqtt.errors.invalid-json-parse"),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return; }
}
else { node.error((RED._("mqtt.errors.invalid-json-string")),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return; }
} else {
if (isUtf8(payload)) { payload = payload.toString(); }
}
var msg = {topic:topic,payload:payload, qos: packet.qos, retain: packet.retain};
var msg = {topic:topic, payload:payload, qos:packet.qos, retain:packet.retain};
if ((node.brokerConn.broker === "localhost")||(node.brokerConn.broker === "127.0.0.1")) {
msg._topic = topic;
}

View File

@ -353,7 +353,8 @@
"buffer": "a Buffer",
"string": "a String",
"base64": "a Base64 encoded string",
"auto": "auto-detect"
"auto": "auto-detect (string or buffer)",
"json": "a parsed JSON object"
},
"true": "true",
"false": "false",
@ -362,7 +363,9 @@
"not-defined": "topic not defined",
"missing-config": "missing broker configuration",
"invalid-topic": "Invalid topic specified",
"nonclean-missingclientid": "No client ID set, using clean session"
"nonclean-missingclientid": "No client ID set, using clean session",
"invalid-json-string": "Invalid JSON string",
"invalid-json-parse": "Failed to parse JSON string"
}
},
"httpin": {