2014-09-20 10:26:09 +02:00
|
|
|
/**
|
2016-02-14 14:47:04 +01:00
|
|
|
* Copyright 2014,2016 IBM Corp.
|
2014-09-20 10:26:09 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
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);
|
|
|
|
var node = this;
|
|
|
|
var a = [];
|
|
|
|
var tot = 0;
|
2015-10-11 18:47:08 +02:00
|
|
|
var tot2 = 0;
|
2015-02-05 14:52:20 +01:00
|
|
|
var pop = 0;
|
|
|
|
var old = null;
|
2014-09-20 10:26:09 +02:00
|
|
|
|
2015-02-05 14:52:20 +01:00
|
|
|
this.on('input', function (msg) {
|
2015-03-31 14:25:59 +02:00
|
|
|
if (msg.hasOwnProperty("payload")) {
|
2015-03-31 14:18:25 +02:00
|
|
|
var n = Number(msg.payload);
|
|
|
|
if (!isNaN(n)) {
|
|
|
|
if ((node.action === "low") || (node.action === "high")) {
|
|
|
|
if (old == null) { old = n; }
|
|
|
|
old = old + (n - old) / node.count;
|
|
|
|
if (node.action === "low") { msg.payload = old; }
|
|
|
|
else { msg.payload = n - old; }
|
2015-02-05 14:52:20 +01:00
|
|
|
}
|
2015-03-31 14:18:25 +02:00
|
|
|
else {
|
|
|
|
a.push(n);
|
|
|
|
if (a.length > node.count) { pop = a.shift(); }
|
|
|
|
if (node.action === "max") {
|
|
|
|
msg.payload = Math.max.apply(Math, a);
|
|
|
|
}
|
|
|
|
if (node.action === "min") {
|
|
|
|
msg.payload = Math.min.apply(Math, a);
|
|
|
|
}
|
|
|
|
if (node.action === "mean") {
|
|
|
|
tot = tot + n - pop;
|
|
|
|
msg.payload = tot / a.length;
|
|
|
|
}
|
2015-10-11 18:47:08 +02:00
|
|
|
if (node.action === "sd") {
|
|
|
|
tot = tot + n - pop;
|
|
|
|
tot2 = tot2 + (n*n) - (pop * pop);
|
|
|
|
if (a.length > 1) {
|
|
|
|
msg.payload = Math.sqrt((a.length * tot2 - tot * tot)/(a.length * (a.length - 1)));
|
|
|
|
}
|
|
|
|
else { msg.payload = 0; }
|
|
|
|
}
|
2015-02-05 14:52:20 +01:00
|
|
|
}
|
2016-02-14 14:47:04 +01:00
|
|
|
if (node.round !== false) {
|
|
|
|
msg.payload = Math.round(msg.payload * Math.pow(10, node.round)) / Math.pow(10, node.round);
|
|
|
|
}
|
2015-03-31 14:18:25 +02:00
|
|
|
node.send(msg);
|
2014-09-20 10:26:09 +02:00
|
|
|
}
|
2015-03-31 14:18:25 +02:00
|
|
|
else { node.log("Not a number: "+msg.payload); }
|
|
|
|
} // 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
|
|
|
}
|