From 84a6a5235ea60f0f8bf7c75b86ddae22738678db Mon Sep 17 00:00:00 2001 From: Arnaud Durand Date: Thu, 11 Oct 2018 21:22:44 +0200 Subject: [PATCH 1/2] Add output type to mqtt node Fixes #1912 --- nodes/core/io/10-mqtt.html | 11 +++++++++++ nodes/core/io/10-mqtt.js | 3 ++- nodes/core/locales/en-US/messages.json | 5 +++++ nodes/core/locales/ja/messages.json | 5 +++++ nodes/core/locales/zh-CN/messages.json | 5 +++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/nodes/core/io/10-mqtt.html b/nodes/core/io/10-mqtt.html index afe710472..612c3b159 100644 --- a/nodes/core/io/10-mqtt.html +++ b/nodes/core/io/10-mqtt.html @@ -28,6 +28,13 @@ +
+ + +
@@ -61,6 +68,7 @@ name: {value:""}, topic: {value:"",required:true,validate: RED.validators.regex(/^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/)}, qos: {value: "2"}, + datatype: {value:"utf8",required:true}, broker: {type:"mqtt-broker", required:true} }, color:"#d8bfd8", @@ -77,6 +85,9 @@ if (this.qos === undefined) { $("#node-input-qos").val("2"); } + if (this.datatype === undefined) { + $("#node-input-datatype").val("utf8"); + } } }); diff --git a/nodes/core/io/10-mqtt.js b/nodes/core/io/10-mqtt.js index 233bef1b6..e9d87ad43 100644 --- a/nodes/core/io/10-mqtt.js +++ b/nodes/core/io/10-mqtt.js @@ -392,13 +392,14 @@ module.exports = function(RED) { if (!/^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/.test(this.topic)) { return this.warn(RED._("mqtt.errors.invalid-topic")); } + this.datatype = n.datatype || "utf8"; var node = this; if (this.brokerConn) { this.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"}); if (this.topic) { node.brokerConn.register(this); this.brokerConn.subscribe(this.topic,this.qos,function(topic,payload,packet) { - if (isUtf8(payload)) { payload = payload.toString(); } + if (this.datatype == "utf8" && 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; diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index a8f9490f0..d4c995ac1 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -324,6 +324,7 @@ "label": { "broker": "Server", "example": "e.g. localhost", + "output": "Output", "qos": "QoS", "retain": "Retain", "clientid": "Client ID", @@ -358,6 +359,10 @@ "connect-failed": "Connection failed to broker: __broker__" }, "retain": "Retain", + "output": { + "buffer": "a Buffer", + "string": "a String" + }, "true": "true", "false": "false", "tip": "Tip: Leave topic, qos or retain blank if you want to set them via msg properties.", diff --git a/nodes/core/locales/ja/messages.json b/nodes/core/locales/ja/messages.json index f160164d3..3b42454a3 100644 --- a/nodes/core/locales/ja/messages.json +++ b/nodes/core/locales/ja/messages.json @@ -324,6 +324,7 @@ "label": { "broker": "サーバ", "example": "例) localhost", + "output": "出力", "qos": "QoS", "retain": "保持", "clientid": "クライアント", @@ -358,6 +359,10 @@ "connect-failed": "ブローカへの接続に失敗しました: __broker__" }, "retain": "保持", + "output": { + "buffer": "バイナリバッファ", + "string": "文字列" + }, "true": "する", "false": "しない", "tip": "注釈: トピックやQoSをメッセージのプロパティを用いて設定する場合は、無記入にしてください。", diff --git a/nodes/core/locales/zh-CN/messages.json b/nodes/core/locales/zh-CN/messages.json index f674e5417..aa4bc2113 100644 --- a/nodes/core/locales/zh-CN/messages.json +++ b/nodes/core/locales/zh-CN/messages.json @@ -314,6 +314,7 @@ "label": { "broker": "服务端", "example": "e.g. localhost", + "output": "输出", "qos": "QoS", "clientid": "客户端ID", "port": "端口", @@ -342,6 +343,10 @@ "connect-failed": "与服务端 __broker__ 的连接失败" }, "retain": "保留", + "output": { + "buffer": "Buffer", + "string": "字符串" + }, "true": "是", "false": "否", "tip": "提示: 若希望通过msg属性对topic(信息), qos及retain(保留)进行设置, 则将上述项留白", From e6466c3c3a31f6c698a19572f1ecdb335ce13785 Mon Sep 17 00:00:00 2001 From: DurandA Date: Mon, 29 Oct 2018 15:10:04 +0100 Subject: [PATCH 2/2] Add auto-detect and base64 output to mqtt node Fixes #1912 --- nodes/core/io/10-mqtt.html | 6 ++++-- nodes/core/io/10-mqtt.js | 10 +++++++++- nodes/core/locales/en-US/messages.json | 4 +++- nodes/core/locales/ja/messages.json | 3 ++- nodes/core/locales/zh-CN/messages.json | 3 ++- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/nodes/core/io/10-mqtt.html b/nodes/core/io/10-mqtt.html index 612c3b159..586d02686 100644 --- a/nodes/core/io/10-mqtt.html +++ b/nodes/core/io/10-mqtt.html @@ -31,8 +31,10 @@
@@ -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"); } } }); diff --git a/nodes/core/io/10-mqtt.js b/nodes/core/io/10-mqtt.js index e9d87ad43..ff152c7b8 100644 --- a/nodes/core/io/10-mqtt.js +++ b/nodes/core/io/10-mqtt.js @@ -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; diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index d4c995ac1..74885cfcb 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -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", diff --git a/nodes/core/locales/ja/messages.json b/nodes/core/locales/ja/messages.json index 3b42454a3..546394682 100644 --- a/nodes/core/locales/ja/messages.json +++ b/nodes/core/locales/ja/messages.json @@ -361,7 +361,8 @@ "retain": "保持", "output": { "buffer": "バイナリバッファ", - "string": "文字列" + "string": "文字列", + "base64": "Base64文字列" }, "true": "する", "false": "しない", diff --git a/nodes/core/locales/zh-CN/messages.json b/nodes/core/locales/zh-CN/messages.json index aa4bc2113..16afd6707 100644 --- a/nodes/core/locales/zh-CN/messages.json +++ b/nodes/core/locales/zh-CN/messages.json @@ -345,7 +345,8 @@ "retain": "保留", "output": { "buffer": "Buffer", - "string": "字符串" + "string": "字符串", + "base64": "Base64编码字符串" }, "true": "是", "false": "否",