From a11a279c007b32a68c5f5fd9b2527ad2b0af5c17 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Tue, 16 Jan 2018 10:59:44 +0000 Subject: [PATCH] add msg. select to range and yaml nodes, re-order son node (name to bottom) add common.label.property to messages list --- nodes/core/locales/en-US/messages.json | 5 +++-- nodes/core/logic/16-range.html | 11 +++++++++++ nodes/core/logic/16-range.js | 13 ++++++++----- nodes/core/parsers/70-JSON.html | 17 +++++++---------- nodes/core/parsers/70-YAML.html | 11 +++++++++++ nodes/core/parsers/70-YAML.js | 16 ++++++++++------ 6 files changed, 50 insertions(+), 23 deletions(-) diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json index 6665387ae..62339d81a 100644 --- a/nodes/core/locales/en-US/messages.json +++ b/nodes/core/locales/en-US/messages.json @@ -5,7 +5,8 @@ "topic": "Topic", "name": "Name", "username": "Username", - "password": "Password" + "password": "Password", + "property": "Property" }, "status": { "connected": "connected", @@ -602,7 +603,7 @@ "maxout": "e.g. 255" }, "scale": { - "payload": "Scale msg.payload", + "payload": "Scale the message property", "limit": "Scale and limit to the target range", "wrap": "Scale and wrap within the target range" }, diff --git a/nodes/core/logic/16-range.html b/nodes/core/logic/16-range.html index 321bec7e4..37d35fb85 100644 --- a/nodes/core/logic/16-range.html +++ b/nodes/core/logic/16-range.html @@ -1,5 +1,9 @@ diff --git a/nodes/core/logic/16-range.js b/nodes/core/logic/16-range.js index 1de073fee..b92b155ed 100644 --- a/nodes/core/logic/16-range.js +++ b/nodes/core/logic/16-range.js @@ -24,11 +24,13 @@ module.exports = function(RED) { this.maxin = Number(n.maxin); this.minout = Number(n.minout); this.maxout = Number(n.maxout); + this.property = n.property||"payload"; var node = this; this.on('input', function (msg) { - if (msg.hasOwnProperty("payload")) { - var n = Number(msg.payload); + var value = RED.util.getMessageProperty(msg,node.property); + if (value !== undefined) { + var n = Number(value); if (!isNaN(n)) { if (node.action == "clamp") { if (n < node.minin) { n = node.minin; } @@ -38,11 +40,12 @@ module.exports = function(RED) { var divisor = node.maxin - node.minin; n = ((n - node.minin) % divisor + divisor) % divisor + node.minin; } - msg.payload = ((n - node.minin) / (node.maxin - node.minin) * (node.maxout - node.minout)) + node.minout; - if (node.round) { msg.payload = Math.round(msg.payload); } + value = ((n - node.minin) / (node.maxin - node.minin) * (node.maxout - node.minout)) + node.minout; + if (node.round) { value = Math.round(value); } + RED.util.setMessageProperty(msg,node.property,value); node.send(msg); } - else { node.log(RED._("range.errors.notnumber")+": "+msg.payload); } + else { node.log(RED._("range.errors.notnumber")+": "+value); } } else { node.send(msg); } // If no payload - just pass it on. }); diff --git a/nodes/core/parsers/70-JSON.html b/nodes/core/parsers/70-JSON.html index f473c6039..01ffe42b3 100644 --- a/nodes/core/parsers/70-JSON.html +++ b/nodes/core/parsers/70-JSON.html @@ -1,9 +1,5 @@ diff --git a/nodes/core/parsers/70-YAML.js b/nodes/core/parsers/70-YAML.js index ae4ca1a8c..1a34fdd57 100644 --- a/nodes/core/parsers/70-YAML.js +++ b/nodes/core/parsers/70-YAML.js @@ -4,20 +4,24 @@ module.exports = function(RED) { var yaml = require('js-yaml'); function YAMLNode(n) { RED.nodes.createNode(this,n); + this.property = n.property||"payload"; var node = this; this.on("input", function(msg) { - if (msg.hasOwnProperty("payload")) { - if (typeof msg.payload === "string") { + var value = RED.util.getMessageProperty(msg,node.property); + if (value !== undefined) { + if (typeof value === "string") { try { - msg.payload = yaml.load(msg.payload); + value = yaml.load(value); + RED.util.setMessageProperty(msg,node.property,value); node.send(msg); } catch(e) { node.error(e.message,msg); } } - else if (typeof msg.payload === "object") { - if (!Buffer.isBuffer(msg.payload)) { + else if (typeof value === "object") { + if (!Buffer.isBuffer(value)) { try { - msg.payload = yaml.dump(msg.payload); + value = yaml.dump(value); + RED.util.setMessageProperty(msg,node.property,value); node.send(msg); } catch(e) {