2014-09-20 10:26:09 +02:00
|
|
|
|
2015-02-05 14:52:20 +01:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2014-09-20 10:26:09 +02:00
|
|
|
|
2015-02-05 14:52:20 +01:00
|
|
|
function SmoothNode(n) {
|
|
|
|
RED.nodes.createNode(this, n);
|
|
|
|
this.action = n.action;
|
|
|
|
this.round = n.round || false;
|
2016-02-14 14:47:04 +01:00
|
|
|
if (this.round == "true") { this.round = 0; }
|
2015-02-05 14:52:20 +01:00
|
|
|
this.count = Number(n.count);
|
2017-05-24 20:45:31 +02:00
|
|
|
this.mult = n.mult || "single";
|
2018-01-30 22:42:14 +01:00
|
|
|
this.property = n.property || "payload";
|
2015-02-05 14:52:20 +01:00
|
|
|
var node = this;
|
2017-05-24 20:45:31 +02:00
|
|
|
var v = {};
|
2014-09-20 10:26:09 +02:00
|
|
|
|
2015-02-05 14:52:20 +01:00
|
|
|
this.on('input', function (msg) {
|
2018-01-30 22:42:14 +01:00
|
|
|
var value = RED.util.getMessageProperty(msg,node.property);
|
2017-05-24 20:45:31 +02:00
|
|
|
var top = msg.topic || "_my_default_topic";
|
|
|
|
if (this.mult === "single") { top = "a"; }
|
|
|
|
|
|
|
|
if ((v.hasOwnProperty(top) !== true) || msg.hasOwnProperty("reset")) {
|
|
|
|
v[top] = {};
|
|
|
|
v[top].a = [];
|
|
|
|
v[top].tot = 0;
|
|
|
|
v[top].tot2 = 0;
|
|
|
|
v[top].pop = 0;
|
|
|
|
v[top].old = null;
|
|
|
|
v[top].count = this.count;
|
2017-02-07 22:41:18 +01:00
|
|
|
}
|
2018-01-30 22:42:14 +01:00
|
|
|
if (value !== undefined) {
|
|
|
|
var n = Number(value);
|
2015-03-31 14:18:25 +02:00
|
|
|
if (!isNaN(n)) {
|
|
|
|
if ((node.action === "low") || (node.action === "high")) {
|
2017-05-24 20:45:31 +02:00
|
|
|
if (v[top].old == null) { v[top].old = n; }
|
|
|
|
v[top].old = v[top].old + (n - v[top].old) / v[top].count;
|
2018-01-30 22:42:14 +01:00
|
|
|
if (node.action === "low") { value = v[top].old; }
|
|
|
|
else { value = n - v[top].old; }
|
2015-02-05 14:52:20 +01:00
|
|
|
}
|
2015-03-31 14:18:25 +02:00
|
|
|
else {
|
2017-05-24 20:45:31 +02:00
|
|
|
v[top].a.push(n);
|
|
|
|
if (v[top].a.length > v[top].count) { v[top].pop = v[top].a.shift(); }
|
2015-03-31 14:18:25 +02:00
|
|
|
if (node.action === "max") {
|
2018-01-30 22:42:14 +01:00
|
|
|
value = Math.max.apply(Math, v[top].a);
|
2015-03-31 14:18:25 +02:00
|
|
|
}
|
|
|
|
if (node.action === "min") {
|
2018-01-30 22:42:14 +01:00
|
|
|
value = Math.min.apply(Math, v[top].a);
|
2015-03-31 14:18:25 +02:00
|
|
|
}
|
|
|
|
if (node.action === "mean") {
|
2017-05-24 20:45:31 +02:00
|
|
|
v[top].tot = v[top].tot + n - v[top].pop;
|
2018-01-30 22:42:14 +01:00
|
|
|
value = v[top].tot / v[top].a.length;
|
2015-03-31 14:18:25 +02:00
|
|
|
}
|
2015-10-11 18:47:08 +02:00
|
|
|
if (node.action === "sd") {
|
2017-05-24 20:45:31 +02:00
|
|
|
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) {
|
2018-01-30 22:42:14 +01:00
|
|
|
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)));
|
2015-10-11 18:47:08 +02:00
|
|
|
}
|
2018-01-30 22:42:14 +01:00
|
|
|
else { value = 0; }
|
2015-10-11 18:47:08 +02:00
|
|
|
}
|
2015-02-05 14:52:20 +01:00
|
|
|
}
|
2016-02-14 14:47:04 +01:00
|
|
|
if (node.round !== false) {
|
2018-01-30 22:42:14 +01:00
|
|
|
value = Math.round(value * Math.pow(10, node.round)) / Math.pow(10, node.round);
|
2016-02-14 14:47:04 +01:00
|
|
|
}
|
2018-01-30 22:42:14 +01:00
|
|
|
RED.util.setMessageProperty(msg,node.property,value);
|
2015-03-31 14:18:25 +02:00
|
|
|
node.send(msg);
|
2014-09-20 10:26:09 +02:00
|
|
|
}
|
2018-01-30 22:42:14 +01:00
|
|
|
else { node.log("Not a number: "+value); }
|
2015-03-31 14:18:25 +02:00
|
|
|
} // ignore msg with no payload property.
|
2015-02-05 14:52:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("smooth", SmoothNode);
|
2014-09-20 10:26:09 +02:00
|
|
|
}
|