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

Add auto-detect and base64 output to mqtt node

Fixes #1912
This commit is contained in:
DurandA 2018-10-29 15:10:04 +01:00
parent 84a6a5235e
commit e6466c3c3a
5 changed files with 20 additions and 6 deletions

View File

@ -31,8 +31,10 @@
<div class="form-row">
<label for="node-input-datatype"><i class="fa fa-sign-out"></i> <span data-i18n="mqtt.label.output"></span></label>
<select id="node-input-datatype" style="width:70%;">
<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="base64" data-i18n="mqtt.output.base64"></option>
</select>
</div>
<div class="form-row">
@ -68,7 +70,7 @@
name: {value:""},
topic: {value:"",required:true,validate: RED.validators.regex(/^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/)},
qos: {value: "2"},
datatype: {value:"utf8",required:true},
datatype: {value:"auto",required:true},
broker: {type:"mqtt-broker", required:true}
},
color:"#d8bfd8",
@ -86,7 +88,7 @@
$("#node-input-qos").val("2");
}
if (this.datatype === undefined) {
$("#node-input-datatype").val("utf8");
$("#node-input-datatype").val("auto");
}
}
});

View File

@ -399,7 +399,15 @@ module.exports = function(RED) {
if (this.topic) {
node.brokerConn.register(this);
this.brokerConn.subscribe(this.topic,this.qos,function(topic,payload,packet) {
if (this.datatype == "utf8" && isUtf8(payload)) { payload = payload.toString(); }
if (node.datatype =="buffer") {
// payload = payload;
} else if (node.datatype =="base64") {
payload = payload.toString('base64');
} else if (node.datatype =="utf8") {
payload = payload.toString('utf8');
} else {
if (isUtf8(payload)) { payload = payload.toString(); }
}
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

@ -361,7 +361,9 @@
"retain": "Retain",
"output": {
"buffer": "a Buffer",
"string": "a String"
"string": "a String",
"base64": "a Base64 encoded string",
"auto": "auto-detect"
},
"true": "true",
"false": "false",

View File

@ -361,7 +361,8 @@
"retain": "保持",
"output": {
"buffer": "バイナリバッファ",
"string": "文字列"
"string": "文字列",
"base64": "Base64文字列"
},
"true": "する",
"false": "しない",

View File

@ -345,7 +345,8 @@
"retain": "保留",
"output": {
"buffer": "Buffer",
"string": "字符串"
"string": "字符串",
"base64": "Base64编码字符串"
},
"true": "是",
"false": "否",