1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Allow Smooth node to be reset by msg.reset

to close #273
This commit is contained in:
Dave Conway-Jones 2017-02-07 21:41:18 +00:00
parent 7e421db642
commit 93c85ca7cd
5 changed files with 35 additions and 2 deletions

View File

@ -32,7 +32,10 @@
<script type="text/x-red" data-help-name="smooth">
<p>A simple node to provide various functions across several previous values, including max, min, mean, high and low pass filters.</p>
<p>Max, Min and Mean work over a specified number of previous values.</p>
<p>The High and Low pass filters use a smoothing factor. The higher the number the more the smoothing. E.g. a value of 10 is similar to an &alpha; of 0.1. It is analagous to an RC time constant - but there is no time component to this as the time is based on events arriving.</p>
<p>The High and Low pass filters use a smoothing factor. The higher the number the more the smoothing. E.g. a value of 10 is
similar to an &alpha; of 0.1. It is analagous to an RC time constant - but there is no time component to this as the
time is based on events arriving.</p>
<p>If <code>msg.reset</code> is received (with any value), all the counters and intermediate values are reset to an initial state.</p>
<p><b>Note:</b> This only operates on <b>numbers</b>. Anything else will try to be made into a number and rejected if that fails.</p>
</script>

View File

@ -16,6 +16,13 @@ module.exports = function(RED) {
var old = null;
this.on('input', function (msg) {
if (msg.hasOwnProperty("reset")) {
a = [];
tot = 0;
tot2 = 0;
pop = 0;
old = null;
}
if (msg.hasOwnProperty("payload")) {
var n = Number(msg.payload);
if (!isNaN(n)) {

View File

@ -33,5 +33,7 @@ the more the smoothing. E.g. a value of 10 is similar to an &alpha; of 0.1.
It is analogous to an RC time constant - but there is no time component to
this as the code is based on events arriving.
If `msg.reset` is received (with any value), all the counters and intermediate values are reset to an initial state.
**Note:** This node only operates on **numbers**. Anything else will try to be
made into a number and rejected if that fails.

View File

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

View File

@ -48,6 +48,27 @@ describe('smooth node', function() {
});
});
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"} ];
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("payload", 2); }
if (c === 6) { msg.should.have.a.property("payload", 5); done(); }
});
n1.emit("input", {payload:1});
n1.emit("input", {payload:2});
n1.emit("input", {payload:3});
n1.emit("input", {reset:true, payload:4});
n1.emit("input", {payload:5});
n1.emit("input", {payload: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"} ];