From be79b6a1c603fb331f28a806c51d99bdcd33cd40 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Tue, 30 Jan 2018 21:42:14 +0000 Subject: [PATCH] add msg.property option to rbe, randon, smooth and base64 nodes --- function/random/package.json | 2 +- function/random/random.html | 23 +- function/random/random.js | 7 +- function/rbe/package.json | 2 +- function/rbe/rbe.html | 19 +- function/rbe/rbe.js | 16 +- function/smooth/17-smooth.html | 11 +- function/smooth/17-smooth.js | 25 +- function/smooth/package.json | 2 +- package-lock.json | 817 +++++++++---------------- parsers/base64/70-base64.html | 15 +- parsers/base64/70-base64.js | 22 +- parsers/base64/package.json | 2 +- test/function/random/random_spec.js | 19 + test/function/rbe/rbe_spec.js | 35 ++ test/function/smooth/17-smooth_spec.js | 46 ++ test/parsers/base64/70-base64_spec.js | 29 + 17 files changed, 527 insertions(+), 565 deletions(-) diff --git a/function/random/package.json b/function/random/package.json index f6e48cac..4898c021 100644 --- a/function/random/package.json +++ b/function/random/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-random", - "version" : "0.0.8", + "version" : "0.1.0", "description" : "A Node-RED node that when triggered generates a random number between two values.", "dependencies" : { }, diff --git a/function/random/random.html b/function/random/random.html index 1a9cb7c7..0f2c3824 100644 --- a/function/random/random.html +++ b/function/random/random.html @@ -1,27 +1,31 @@ - +70% diff --git a/function/random/random.js b/function/random/random.js index 69da1d65..a6fb1a47 100644 --- a/function/random/random.js +++ b/function/random/random.js @@ -6,14 +6,17 @@ module.exports = function(RED) { this.low = Number(n.low || 1); this.high = Number(n.high || 10); this.inte = n.inte || false; + this.property = n.property||"payload"; var node = this; this.on("input", function(msg) { + var value; if (node.inte == "true" || node.inte === true) { - msg.payload = Math.round(Number(Math.random()) * (node.high - node.low + 1) + node.low - 0.5); + value = Math.round(Number(Math.random()) * (node.high - node.low + 1) + node.low - 0.5); } else { - msg.payload = Number(Math.random()) * (node.high - node.low) + node.low; + value = Number(Math.random()) * (node.high - node.low) + node.low; } + RED.util.setMessageProperty(msg,node.property,value); node.send(msg); }); } diff --git a/function/rbe/package.json b/function/rbe/package.json index 6b1af48d..a48b84fc 100644 --- a/function/rbe/package.json +++ b/function/rbe/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-rbe", - "version" : "0.1.14", + "version" : "0.2.0", "description" : "A Node-RED node that provides report-by-exception (RBE) and deadband capability.", "dependencies" : { }, diff --git a/function/rbe/rbe.html b/function/rbe/rbe.html index ecf951c2..759c7c06 100644 --- a/function/rbe/rbe.html +++ b/function/rbe/rbe.html @@ -2,7 +2,7 @@ @@ -43,7 +47,7 @@
if specified the function will work on a per topic basis.
resetany
if set clears the stored value for the specified msg.topic, or - all topics if msg.topic is not specified.
+ all topics if msg.topic is not specified.

Outputs

@@ -76,7 +80,8 @@ func: {value:"rbe"}, gap: {value:"",validate:RED.validators.regex(/^(\d*[.]*\d*|)(%|)$/)}, start: {value:""}, - inout: {value:"out"} + inout: {value:"out"}, + property: {value:"payload",required:true} }, inputs:1, outputs:1, @@ -89,6 +94,10 @@ return this.name?"node_label_italic":""; }, oneditprepare: function() { + if (this.property === undefined) { + $("#node-input-property").val("payload"); + } + $("#node-input-property").typedInput({default:'msg',types:['msg']}); //$( "#node-input-gap" ).spinner({min:0}); if ($("#node-input-inout").val() === null) { $("#node-input-inout").val("out"); diff --git a/function/rbe/rbe.js b/function/rbe/rbe.js index 50bfa1e8..49e2a208 100644 --- a/function/rbe/rbe.js +++ b/function/rbe/rbe.js @@ -13,6 +13,7 @@ module.exports = function(RED) { this.gap = parseFloat(this.gap); } this.g = this.gap; + this.property = n.property||"payload"; var node = this; @@ -24,26 +25,27 @@ module.exports = function(RED) { } else { node.previous = {}; } } - if (msg.hasOwnProperty("payload")) { + var value = RED.util.getMessageProperty(msg,node.property); + if (value !== undefined) { var t = msg.topic || "_no_topic"; if ((this.func === "rbe") || (this.func === "rbei")) { var doSend = (this.func !== "rbei") || (node.previous.hasOwnProperty(t)) || false; - if (typeof(msg.payload) === "object") { + if (typeof(value) === "object") { if (typeof(node.previous[t]) !== "object") { node.previous[t] = {}; } - if (!RED.util.compareObjects(msg.payload, node.previous[t])) { - node.previous[t] = RED.util.cloneMessage(msg.payload); + if (!RED.util.compareObjects(value, node.previous[t])) { + node.previous[t] = RED.util.cloneMessage(value); if (doSend) { node.send(msg); } } } else { - if (msg.payload !== node.previous[t]) { - node.previous[t] = RED.util.cloneMessage(msg.payload); + if (value !== node.previous[t]) { + node.previous[t] = RED.util.cloneMessage(value); if (doSend) { node.send(msg); } } } } else { - var n = parseFloat(msg.payload); + var n = parseFloat(value); if (!isNaN(n)) { if ((typeof node.previous[t] === 'undefined') && (this.func === "narrowband")) { if (node.start === '') { node.previous[t] = n; } diff --git a/function/smooth/17-smooth.html b/function/smooth/17-smooth.html index 5de5af16..d7340849 100644 --- a/function/smooth/17-smooth.html +++ b/function/smooth/17-smooth.html @@ -1,7 +1,11 @@ @@ -17,7 +21,8 @@ category: 'function', color:"#DEBD5C", defaults: { - name: {value:""} + name: {value:""}, + property: {value:"payload",required:true} }, inputs:1, outputs:1, @@ -27,6 +32,12 @@ }, labelStyle: function() { return this.name?"node_label_italic":""; + }, + oneditprepare: function() { + if (this.property === undefined) { + $("#node-input-property").val("payload"); + } + $("#node-input-property").typedInput({default:'msg',types:['msg']}); } }); diff --git a/parsers/base64/70-base64.js b/parsers/base64/70-base64.js index 741fa5dc..764895df 100644 --- a/parsers/base64/70-base64.js +++ b/parsers/base64/70-base64.js @@ -1,28 +1,32 @@ module.exports = function(RED) { "use strict"; - function Base64Node(n) { RED.nodes.createNode(this,n); + this.property = n.property||"payload"; var node = this; this.on("input", function(msg) { - if (msg.hasOwnProperty("payload")) { - if (Buffer.isBuffer(msg.payload)) { + var value = RED.util.getMessageProperty(msg,node.property); + if (value !== undefined) { + if (Buffer.isBuffer(value)) { // Take binary buffer and make into a base64 string - msg.payload = msg.payload.toString('base64'); + value = value.toString('base64'); + RED.util.setMessageProperty(msg,node.property,value); node.send(msg); } - else if (typeof msg.payload === "string") { + else if (typeof value === "string") { // Take base64 string and make into binary buffer - var load = msg.payload.replace(/\s+/g,''); // remove any whitespace + var load = value.replace(/\s+/g,''); // remove any whitespace var regexp = new RegExp('^[A-Za-z0-9+\/=]*$'); // check it only contains valid characters if ( regexp.test(load) && (load.length % 4 === 0) ) { - msg.payload = new Buffer(load,'base64'); + value = new Buffer(load,'base64'); + RED.util.setMessageProperty(msg,node.property,value); node.send(msg); } else { //node.log("Not a Base64 string - maybe we should encode it..."); - msg.payload = (new Buffer(msg.payload,"binary")).toString('base64'); + value = (new Buffer(value,"binary")).toString('base64'); + RED.util.setMessageProperty(msg,node.property,value); node.send(msg); } } @@ -30,7 +34,7 @@ module.exports = function(RED) { node.warn("This node only handles strings or buffers."); } } - else { node.warn("No payload found to process"); } + else { node.warn("No property found to process"); } }); } RED.nodes.registerType("base64",Base64Node); diff --git a/parsers/base64/package.json b/parsers/base64/package.json index 99197caf..1805f7ef 100644 --- a/parsers/base64/package.json +++ b/parsers/base64/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-base64", - "version" : "0.0.8", + "version" : "0.1.0", "description" : "A Node-RED node to pack and unpack objects to base64 format", "dependencies" : { }, diff --git a/test/function/random/random_spec.js b/test/function/random/random_spec.js index e1a7d963..e1b9939f 100644 --- a/test/function/random/random_spec.js +++ b/test/function/random/random_spec.js @@ -66,4 +66,23 @@ describe('random node', function() { }); }); + it('should output an integer between -3 and 3 on chosen property - foo', function(done) { + var flow = [{"id":"n1", "type":"random", property:"foo", low:-3, high:3, inte:true, wires:[["n2"]] }, + {id:"n2", type:"helper"} ]; + helper.load(testNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + var c = 0; + n2.on("input", function(msg) { + if (c === 0) { + msg.should.have.a.property("foo"); + msg.foo.should.be.approximately(0,3); + msg.foo.toString().indexOf(".").should.equal(-1); + done(); + } + }); + n1.emit("input", {payload:"a"}); + }); + }); + }); diff --git a/test/function/rbe/rbe_spec.js b/test/function/rbe/rbe_spec.js index e19249ab..f41517d7 100644 --- a/test/function/rbe/rbe_spec.js +++ b/test/function/rbe/rbe_spec.js @@ -62,6 +62,41 @@ describe('rbe node', function() { }); }); + it('should only send output if another chosen property changes - foo (rbe)', function(done) { + var flow = [{"id":"n1", "type":"rbe", func:"rbe", gap:"0", property:"foo", wires:[["n2"]] }, + {id:"n2", type:"helper"} ]; + helper.load(testNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + var c = 0; + n2.on("input", function(msg) { + if (c === 0) { + msg.should.have.a.property("foo", "a"); + c+=1; + } + else if (c === 1) { + msg.should.have.a.property("foo", "b"); + c+=1; + } + else { + msg.should.have.a.property("foo"); + msg.foo.should.have.a.property("b",1); + msg.foo.should.have.a.property("c",2); + done(); + } + }); + n1.emit("input", {foo:"a"}); + n1.emit("input", {payload:"a"}); + n1.emit("input", {foo:"a"}); + n1.emit("input", {payload:"a"}); + n1.emit("input", {foo:"a"}); + n1.emit("input", {foo:"b"}); + n1.emit("input", {foo:{b:1,c:2}}); + n1.emit("input", {foo:{c:2,b:1}}); + n1.emit("input", {payload:{c:2,b:1}}); + }); + }); + it('should only send output if payload changes - ignoring first value (rbei)', function(done) { var flow = [{"id":"n1", "type":"rbe", func:"rbei", gap:"0", wires:[["n2"]] }, {id:"n2", type:"helper"} ]; diff --git a/test/function/smooth/17-smooth_spec.js b/test/function/smooth/17-smooth_spec.js index 6145a94e..4a9d2b83 100644 --- a/test/function/smooth/17-smooth_spec.js +++ b/test/function/smooth/17-smooth_spec.js @@ -48,6 +48,29 @@ describe('smooth node', function() { }); }); + it('should average over a number of inputs - another property - foo', function(done) { + var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", round:"true", property:"foo", wires:[["n2"]] }, + {id:"n2", type:"helper"} ]; + helper.load(testNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + var c = 0; + n2.on("input", function(msg) { + c += 1; + if (c === 4) { msg.should.have.a.property("foo", 1.8); } + if (c === 6) { msg.should.have.a.property("foo", 3); done(); } + }); + n1.emit("input", {foo:1}); + n1.emit("input", {foo:1}); + n1.emit("input", {foo:2}); + n1.emit("input", {payload:2}); + n1.emit("input", {payload:2}); + n1.emit("input", {foo:3}); + n1.emit("input", {foo:4}); + n1.emit("input", {foo:4.786}); + }); + }); + it('should be able to be reset', function(done) { var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", round:"true", wires:[["n2"]] }, {id:"n2", type:"helper"} ]; @@ -69,6 +92,29 @@ describe('smooth node', function() { }); }); + it('should be able to be reset - while using another property - foo', function(done) { + var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", round:"true", property:"foo", wires:[["n2"]] }, + {id:"n2", type:"helper"} ]; + helper.load(testNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + var c = 0; + n2.on("input", function(msg) { + c += 1; + if (c === 3) { msg.should.have.a.property("foo", 2); } + if (c === 6) { msg.should.have.a.property("foo", 5); done(); } + }); + n1.emit("input", {foo:1}); + n1.emit("input", {foo:2}); + n1.emit("input", {payload:2}); + n1.emit("input", {foo:3}); + n1.emit("input", {reset:true, foo:4}); + n1.emit("input", {foo:5}); + n1.emit("input", {payload:2}); + n1.emit("input", {foo:6}); + }); + }); + it('should output max over a number of inputs', function(done) { var flow = [{"id":"n1", "type":"smooth", action:"max", count:"5", wires:[["n2"]] }, {id:"n2", type:"helper"} ]; diff --git a/test/parsers/base64/70-base64_spec.js b/test/parsers/base64/70-base64_spec.js index 1fe5ddda..b7324248 100644 --- a/test/parsers/base64/70-base64_spec.js +++ b/test/parsers/base64/70-base64_spec.js @@ -39,6 +39,20 @@ describe('base64 node', function() { }); }); + it('should convert a Buffer to base64 using another property - foo', function(done) { + var flow = [{id:"n1", type:"base64", property:"foo", wires:[["n2"]] }, + {id:"n2", type:"helper"} ]; + helper.load(testNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + msg.should.have.a.property("foo","QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo="); + done(); + }); + n1.emit("input", {foo: Buffer.from("ABCDEFGHIJKLMNOPQRSTUVWXYZ")}); + }); + }); + it('should convert base64 to a Buffer', function(done) { var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] }, {id:"n2", type:"helper"} ]; @@ -54,6 +68,21 @@ describe('base64 node', function() { }); }); + it('should convert base64 to a Buffer using another property - foo', function(done) { + var flow = [{id:"n1", type:"base64", property:"foo", wires:[["n2"]] }, + {id:"n2", type:"helper"} ]; + helper.load(testNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + msg.should.have.a.property("foo"); + msg.foo.toString().should.equal("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); + done(); + }); + n1.emit("input", {foo:"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVo="}); + }); + }); + it('should try to encode a non base64 string', function(done) { var flow = [{"id":"n1", "type":"base64", wires:[["n2"]] }, {id:"n2", type:"helper"} ];