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
17 changed files with 527 additions and 565 deletions

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