diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html index 747281ad0..fd39c9a9c 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.html @@ -65,6 +65,11 @@ /* opacity: 0.3; pointer-events: none; */ } + .form-row.form-row-mqtt-datatype-tip > .form-tips { + width: calc(70% - 18px); + display: inline-block; + margin-top: -8px; + } @@ -121,6 +126,7 @@
+
+ +
+
@@ -750,7 +760,7 @@ } }, qos: {value: "2"}, - datatype: {value:"auto",required:true}, + datatype: {value:"auto-detect",required:true}, broker: {type:"mqtt-broker", required:true}, // subscriptionIdentifier: {value:0}, nl: {value:false}, @@ -787,6 +797,16 @@ $("div.form-row-mqtt5").toggleClass("form-row-mqtt5-active",!!v5); $("div.form-row.form-row-mqtt-static").toggleClass("form-row-mqtt-static-disabled", !!dynamic) } + + $("#node-input-datatype").on("change", function() { + if($(this).val() === "auto") { + $(".form-row.form-row-mqtt-datatype-tip").show(); + } else { + $(".form-row.form-row-mqtt-datatype-tip").hide(); + } + }) + $("#node-input-datatype").trigger("change"); + $("#node-input-broker").on("change",function(d){ updateVisibility(); }); @@ -807,7 +827,7 @@ $("#node-input-qos").val("2"); } if (this.datatype === undefined) { - $("#node-input-datatype").val("auto"); + $("#node-input-datatype").val("auto-detect"); } }, oneditsave: function() { diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js index d3016e5c6..9b365585b 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js @@ -25,7 +25,7 @@ module.exports = function(RED) { "text/html":"string", "text/plain":"string", "text/html":"string", - "application/json":"object", + "application/json":"json", "application/octet-stream":"buffer", "application/pdf":"buffer", "application/x-gtar":"buffer", @@ -234,15 +234,16 @@ module.exports = function(RED) { } else if (datatype === "json") { if (v5isUtf8 || isUtf8(payload)) { try { - payload = JSON.parse(payload.toString()); - } catch(e) { - node.error(RED._("mqtt.errors.invalid-json-parse"),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return; + payload = JSON.parse(payload.toString()); + } 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 { node.error((RED._("mqtt.errors.invalid-json-string")),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return; } } else { - //auto - if(v5isUtf8 || v5HasMediaType) { + //"auto" (legacy) or "auto-detect" (new default) + if (v5isUtf8 || v5HasMediaType) { const outputType = knownMediaTypes[v5MediaTypeLC] switch (outputType) { case "string": @@ -251,25 +252,39 @@ module.exports = function(RED) { case "buffer": //no change break; - case "object": + case "json": try { - payload = JSON.parse(payload.toString()); - } catch(e) { - node.error(RED._("mqtt.errors.invalid-json-parse"),{payload:payload, topic:topic, qos:packet.qos, retain:packet.retain}); return; + //since v5 type states this should be JSON, parse it & error out if NOT JSON + payload = payload.toString() + const obj = JSON.parse(payload); + if (datatype === "auto-detect") { + payload = obj; //as mode is "auto-detect", return the parsed JSON + } + } catch (e) { + node.error(RED._("mqtt.errors.invalid-json-parse"), { payload: payload, topic: topic, qos: packet.qos, retain: packet.retain }); return; } break; default: if (v5isUtf8 || isUtf8(payload)) { payload = payload.toString(); //auto String + if (datatype === "auto-detect") { + try { + payload = JSON.parse(payload); //auto to parsed object (attempt) + } catch (e) { + /* mute error - it simply isnt JSON, just leave payload as a string */ + } + } } break; } - } else if (isUtf8(payload)) { + } else if (isUtf8(payload)) { payload = payload.toString(); //auto String - try { - payload = JSON.parse(payload); - } catch(e) { - /* mute error - it simply isnt JSON, just leave payload as a string */ + if (datatype === "auto-detect") { + try { + payload = JSON.parse(payload); + } catch (e) { + /* mute error - it simply isnt JSON, just leave payload as a string */ + } } } //else { //leave as buffer diff --git a/packages/node_modules/@node-red/nodes/locales/de/messages.json b/packages/node_modules/@node-red/nodes/locales/de/messages.json index 35ce50f4d..65f251e98 100755 --- a/packages/node_modules/@node-red/nodes/locales/de/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/de/messages.json @@ -422,7 +422,8 @@ "buffer": "Einen binären Buffer", "string": "Ein String", "base64": "Ein Base64-kodierter String", - "auto": "Auto-Erkennung (parsed JSON-Objekt, string oder buffer)", + "auto": "Auto-Erkennung (string oder buffer)", + "auto-detect": "Auto-Erkennung (parsed JSON-Objekt, string oder buffer)", "json": "Ein analysiertes (parsed) JSON-Objekt" }, "true": "wahr", diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index 486764d18..d68fff71b 100755 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -424,7 +424,8 @@ "action": "Action", "staticTopic": "Subscribe to single topic", "dynamicTopic": "Dynamic subscription", - "auto-connect": "Connect automatically" + "auto-connect": "Connect automatically", + "auto-mode-depreciated": "This option is depreciated. Please use the new auto-detect mode." }, "sections-label":{ "birth-message": "Message sent on connection (birth message)", @@ -454,7 +455,8 @@ "buffer": "a Buffer", "string": "a String", "base64": "a Base64 encoded string", - "auto": "auto-detect (parsed JSON object, string or buffer)", + "auto": "auto-detect (string or buffer)", + "auto-detect": "auto-detect (parsed JSON object, string or buffer)", "json": "a parsed JSON object" }, "true": "true", diff --git a/packages/node_modules/@node-red/nodes/locales/ja/messages.json b/packages/node_modules/@node-red/nodes/locales/ja/messages.json index 613871771..f49a973e5 100644 --- a/packages/node_modules/@node-red/nodes/locales/ja/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/ja/messages.json @@ -454,7 +454,8 @@ "buffer": "バイナリバッファ", "string": "文字列", "base64": "Base64文字列", - "auto": "自動判定(JSONオブジェクト、文字列もしくはバイナリバッファ)", + "auto": "自動判定(文字列もしくはバイナリバッファ)", + "auto-detect": "自動判定(JSONオブジェクト、文字列もしくはバイナリバッファ)", "json": "JSONオブジェクト" }, "true": "する", diff --git a/packages/node_modules/@node-red/nodes/locales/ko/messages.json b/packages/node_modules/@node-red/nodes/locales/ko/messages.json index b543bba80..15b7bee8e 100755 --- a/packages/node_modules/@node-red/nodes/locales/ko/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/ko/messages.json @@ -361,7 +361,8 @@ "buffer": "바이너리 버퍼", "string": "문자열", "base64": "Base64문자열", - "auto": "자동판정(JSON오브젝트, 문자열혹은 바이너리버퍼)", + "auto": "자동판정(문자열혹은 바이너리버퍼)", + "auto-detect": "자동판정(JSON오브젝트, 문자열혹은 바이너리버퍼)", "json": "JSON오브젝트" }, "true": "한다", diff --git a/packages/node_modules/@node-red/nodes/locales/ru/messages.json b/packages/node_modules/@node-red/nodes/locales/ru/messages.json index 33d750950..32364b458 100755 --- a/packages/node_modules/@node-red/nodes/locales/ru/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/ru/messages.json @@ -384,7 +384,8 @@ "buffer": "буфер", "string": "строка", "base64": "строка в кодировке Base64", - "auto": "автоопределение (разобрать объект JSON, строка или буфер)", + "auto": "автоопределение (строка или буфер)", + "auto-detect": "автоопределение (разобрать объект JSON, строка или буфер)", "json": "объект JSON" }, "true": "да", diff --git a/packages/node_modules/@node-red/nodes/locales/zh-CN/messages.json b/packages/node_modules/@node-red/nodes/locales/zh-CN/messages.json index 9b972a3bb..b649163b7 100644 --- a/packages/node_modules/@node-red/nodes/locales/zh-CN/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/zh-CN/messages.json @@ -381,7 +381,8 @@ "buffer": "Buffer", "string": "字符串", "base64": "Base64编码字符串", - "auto": "自动检测 (已解析的JSON对象、字符串或buffer)", + "auto": "自动检测 (字符串或buffer)", + "auto-detect": "自动检测 (已解析的JSON对象、字符串或buffer)", "json": "解析的JSON对象" }, "true": "是", diff --git a/packages/node_modules/@node-red/nodes/locales/zh-TW/messages.json b/packages/node_modules/@node-red/nodes/locales/zh-TW/messages.json index 03db9d0c4..d63c47865 100644 --- a/packages/node_modules/@node-red/nodes/locales/zh-TW/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/zh-TW/messages.json @@ -385,7 +385,8 @@ "buffer": "Buffer", "string": "字串", "base64": "Base64編碼字串", - "auto": "自动检测 (已解析的JSON对象、字符串或buffer)", + "auto": "自動檢測 (字符串或buffer)", + "auto-detect": "自动检测 (已解析的JSON对象、字符串或buffer)", "json": "解析的JSON對象" }, "true": "是",