add msg.property option to rbe, randon, smooth and base64 nodes

This commit is contained in:
Dave Conway-Jones 2018-01-30 21:42:14 +00:00
parent 4ed7ab590d
commit be79b6a1c6
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
17 changed files with 527 additions and 565 deletions

View File

@ -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" : {
},

View File

@ -1,27 +1,31 @@
<script type="text/x-red" data-template-name="random">
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-inte"><i class="fa fa-random"></i> Generate</label>
<select type="text" id="node-input-inte" style="width: 300px;">
<select type="text" id="node-input-inte" style="width:70%;">
<option value="true">a whole number - integer</option>
<option value="false">a real number - floating point</option>
</select>
</div>
<div class="form-row">
<label for="node-input-low"><i class="fa fa-arrow-down"></i> From</label>
<input type="text" id="node-input-low" placeholder="lowest number" style="width: 300px;">
<input type="text" id="node-input-low" placeholder="lowest number" style="width:70%;">
</div>
<div class="form-row">
<label for="node-input-high"><i class="fa fa-arrow-up"></i> To</label>
<input type="text" id="node-input-high" placeholder="highest number" style="width: 300px;">
<input type="text" id="node-input-high" placeholder="highest number" style="width:70%;">
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" style="width: 300px;">
<input type="text" id="node-input-name" placeholder="Name" style="width:70%;">
</div>
</script>
70%
<script type="text/x-red" data-help-name="random">
<p>Generates a random number between a low and high value.</p>
<p>If you return an integer it can <i>include</i> both the low and high values.</p>
@ -36,7 +40,8 @@
name: {value:""},
low: {value:"1"},
high: {value:"10"},
inte: {value:"true"}
inte: {value:"true"},
property: {value:"payload",required:true}
},
inputs:1,
outputs:1,
@ -46,6 +51,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']});
}
});
</script>

View File

@ -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);
});
}

View File

@ -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" : {
},

View File

@ -2,7 +2,7 @@
<script type="text/x-red" data-template-name="rbe">
<div class="form-row">
<label for="node-input-func"><i class="fa fa-wrench"></i> <span data-i18n="rbe.label.func"></span></label>
<select type="text" id="node-input-func" style="width:74%;">
<select type="text" id="node-input-func" style="width:70%;">
<option value="rbe" data-i18n="rbe.opts.rbe"></option>
<option value="rbei" data-i18n="rbe.opts.rbei"></option>
<option value="deadbandEq" data-i18n="rbe.opts.deadbandEq"></option>
@ -21,11 +21,15 @@
</div>
<div class="form-row" id="node-startvalue">
<label for="node-input-start"><i class="fa fa-thumb-tack"/> <span data-i18n="rbe.label.start"></span></label>
<input type="text" id="node-input-start" data-i18n="[placeholder]rbe.placeholder.start" style="width:71%;">
<input type="text" id="node-input-start" data-i18n="[placeholder]rbe.placeholder.start" style="width:70%;">
</div>
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"/> <span data-i18n="rbe.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]rbe.label.name" style="width:71%;">
<input type="text" id="node-input-name" data-i18n="[placeholder]rbe.label.name" style="width:70%;">
</div>
</script>
@ -43,7 +47,7 @@
<dd>if specified the function will work on a per topic basis.</dd>
<dt class="optional">reset<span class="property-type">any</span></dt>
<dd>if set clears the stored value for the specified msg.topic, or
all topics if msg.topic is not specified.</dd>
all topics if msg.topic is not specified.</dd>
</dl>
<h3>Outputs</h3>
<dl class="message-properties">
@ -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");

View File

@ -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; }

View File

@ -1,7 +1,11 @@
<script type="text/x-red" data-template-name="smooth">
<div class="form-row">
<label for="node-input-action"><i class="fa fa-bolt"></i> Action</label>
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-action"><i class="fa fa-bolt"></i> Action</label>
<select id="node-input-action" style="width:60%; margin-right:5px;">
<option value="max">Return the maximum value seen</option>
<option value="min">Return the minimum value seen</option>
@ -53,6 +57,7 @@
category: 'function',
defaults: {
name: {value:""},
property: {value:"payload",required:true},
action: {value:"mean"},
count: {value:"10",required:true,validate:RED.validators.number()},
round: {value:""},
@ -68,6 +73,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-count").spinner({
min:1
});

View File

@ -9,10 +9,12 @@ module.exports = function(RED) {
if (this.round == "true") { this.round = 0; }
this.count = Number(n.count);
this.mult = n.mult || "single";
this.property = n.property || "payload";
var node = this;
var v = {};
this.on('input', function (msg) {
var value = RED.util.getMessageProperty(msg,node.property);
var top = msg.topic || "_my_default_topic";
if (this.mult === "single") { top = "a"; }
@ -25,43 +27,44 @@ module.exports = function(RED) {
v[top].old = null;
v[top].count = this.count;
}
if (msg.hasOwnProperty("payload")) {
var n = Number(msg.payload);
if (value !== undefined) {
var n = Number(value);
if (!isNaN(n)) {
if ((node.action === "low") || (node.action === "high")) {
if (v[top].old == null) { v[top].old = n; }
v[top].old = v[top].old + (n - v[top].old) / v[top].count;
if (node.action === "low") { msg.payload = v[top].old; }
else { msg.payload = n - v[top].old; }
if (node.action === "low") { value = v[top].old; }
else { value = n - v[top].old; }
}
else {
v[top].a.push(n);
if (v[top].a.length > v[top].count) { v[top].pop = v[top].a.shift(); }
if (node.action === "max") {
msg.payload = Math.max.apply(Math, v[top].a);
value = Math.max.apply(Math, v[top].a);
}
if (node.action === "min") {
msg.payload = Math.min.apply(Math, v[top].a);
value = Math.min.apply(Math, v[top].a);
}
if (node.action === "mean") {
v[top].tot = v[top].tot + n - v[top].pop;
msg.payload = v[top].tot / v[top].a.length;
value = v[top].tot / v[top].a.length;
}
if (node.action === "sd") {
v[top].tot = v[top].tot + n - v[top].pop;
v[top].tot2 = v[top].tot2 + (n*n) - (v[top].pop * v[top].pop);
if (v[top].a.length > 1) {
msg.payload = Math.sqrt((v[top].a.length * v[top].tot2 - v[top].tot * v[top].tot)/(v[top].a.length * (v[top].a.length - 1)));
value = Math.sqrt((v[top].a.length * v[top].tot2 - v[top].tot * v[top].tot)/(v[top].a.length * (v[top].a.length - 1)));
}
else { msg.payload = 0; }
else { value = 0; }
}
}
if (node.round !== false) {
msg.payload = Math.round(msg.payload * Math.pow(10, node.round)) / Math.pow(10, node.round);
value = Math.round(value * Math.pow(10, node.round)) / Math.pow(10, node.round);
}
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else { node.log("Not a number: "+msg.payload); }
else { node.log("Not a number: "+value); }
} // ignore msg with no payload property.
});
}

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-smooth",
"version" : "0.0.11",
"version" : "0.1.0",
"description" : "A Node-RED node that provides several simple smoothing algorithms for incoming data values.",
"dependencies" : {
},

817
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,9 @@
<script type="text/x-red" data-template-name="base64">
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
@ -7,7 +11,7 @@
</script>
<script type="text/x-red" data-help-name="base64">
<p>A function that converts the <code>msg.payload</code> to and from base64 format.</p>
<p>A function that converts the chosen proerty (default <code>msg.payload</code>) to and from base64 format.</p>
<p>If the input is a buffer it converts it to a Base64 encoded string.</p>
<p>If the input is a Base64 string it converts it back to a binary buffer.</p>
</script>
@ -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']});
}
});
</script>

View File

@ -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);

View File

@ -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" : {
},

View File

@ -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"});
});
});
});

View File

@ -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"} ];

View File

@ -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"} ];

View File

@ -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"} ];