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

Update RBE to add object compare

and narrowband mode (thanks @nlecaude for the idea)
This commit is contained in:
Dave Conway-Jones 2016-02-02 13:39:37 +00:00
parent ed4b6b18ec
commit 6c3dbbbfbb
5 changed files with 101 additions and 22 deletions

View File

@ -2,14 +2,17 @@
"rbe": {
"label": {
"func": "Mode",
"start": "Start value",
"name": "Name"
},
"placeholder":{
"bandgap": "e.g. 10 or 5%"
"bandgap": "e.g. 10 or 5%",
"start": "leave blank to use first data received"
},
"opts": {
"rbe": "block unless value changes",
"deadband": "block unless changes by more than"
"deadband": "block unless value changes by more than",
"narrowband": "block if value changes by more than"
},
"warn": {
"nonumber": "no number found in payload"

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-rbe",
"version" : "0.1.1",
"version" : "0.1.2",
"description" : "A Node-RED node that provides report-by-exception (RBE) and deadband capability.",
"dependencies" : {
},

View File

@ -20,12 +20,17 @@
<select type="text" id="node-input-func" style="width:74%;">
<option value="rbe" data-i18n="rbe.opts.rbe"></option>
<option value="deadband" data-i18n="rbe.opts.deadband"></option>
<option value="narrowband" data-i18n="rbe.opts.narrowband"></option>
</select>
</div>
<div class="form-row" id="node-bandgap">
<label for="node-input-gap">&nbsp;</label>
<input type="text" id="node-input-gap" data-i18n="[placeholder]rbe.placeholder.bandgap" style="width:71%;">
</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%;">
</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%;">
@ -35,11 +40,14 @@
<script type="text/x-red" data-help-name="rbe">
<p>Report by Exception node - only passes on data if it has changed.</p>
<p>The node can either block until the <b>msg.payload</b> is
different to the previous one - <b>rbe</b> mode. Works on numbers and strings.</p>
different to the previous one - <b>rbe</b> mode. This works on numbers, strings, and simple objects.</p>
<p>Or it can block until the value changes by a specified amount - <b>deadband</b> mode.</p>
<p>In deadband mode the incoming payload should contain a parseable <i>number</i> and is
<p>In deadband mode the incoming payload must contain a parseable <i>number</i> and is
output only if greater than + or - the <i>band gap</i> away from the previous output.</p>
<p>Deadband also supports % - only sends if the input differs by more than x% of the original value.</p>
<p>It can also ignore outlier values - <b>narrowband</b> mode.</p>
<p>In narrowband mode the incoming payload is blocked if it is more than + or - the band gap
away from the previous value. Useful for ignoring outliers from a faulty sensor for example.</p>
<p><b>Note:</b> This works on a per <b>msg.topic</b> basis. This means that a single rbe node can
handle multiple topics at the same time.</p>
</script>
@ -51,7 +59,8 @@
defaults: {
name: {value:""},
func: {value:"rbe"},
gap: {value:"",validate:RED.validators.regex(/^(\d*[.]*\d*|)(%|)$/)}
gap: {value:"",validate:RED.validators.regex(/^(\d*[.]*\d*|)(%|)$/)},
start: {value:""}
},
inputs:1,
outputs:1,
@ -70,6 +79,11 @@
} else {
$("#node-bandgap").show();
}
if ($("#node-input-func").val() === "narrowband") {
$("#node-startvalue").show();
} else {
$("#node-startvalue").hide();
}
});
}
});

View File

@ -1,5 +1,5 @@
/**
* Copyright 2014, 2015 IBM Corp.
* Copyright 2014, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
this.func = n.func || "rbe";
this.gap = n.gap || "0";
this.start = n.start || '';
this.pc = false;
if (this.gap.substr(-1) === "%") {
this.pc = true;
@ -33,19 +34,40 @@ module.exports = function(RED) {
if (msg.hasOwnProperty("payload")) {
var t = msg.topic || "_no_topic";
if (this.func === "rbe") {
if (msg.payload != node.previous[t]) {
node.previous[t] = msg.payload;
node.send(msg);
if (typeof(msg.payload) === "object") {
if (typeof(node.previous[t]) !== "object") { node.previous[t] = {}; }
if (!RED.util.compareObjects(msg.payload, node.previous[t])) {
node.previous[t] = msg.payload;
node.send(msg);
}
}
else {
if (msg.payload !== node.previous[t]) {
node.previous[t] = msg.payload;
node.send(msg);
}
}
}
else {
var n = parseFloat(msg.payload);
if (!isNaN(n)) {
if ((typeof node.previous[t] === 'undefined') && (this.func === "narrowband")) {
if (node.start === '') { node.previous[t] = n; }
else { node.previous[t] = node.start; }
}
if (node.pc) { node.gap = (node.previous[t] * node.g / 100) || 0; }
if (!node.previous.hasOwnProperty(t)) { node.previous[t] = n - node.gap; }
if (Math.abs(n - node.previous[t]) >= node.gap) {
node.previous[t] = n;
node.send(msg);
if (this.func === "deadband") {
node.previous[t] = n;
node.send(msg);
}
}
else {
if (this.func === "narrowband") {
node.previous[t] = n;
node.send(msg);
}
}
}
else {

View File

@ -1,5 +1,5 @@
/**
* Copyright 2015 IBM Corp.
* Copyright 2015,2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -42,7 +42,7 @@ describe('rbe node', function() {
});
});
it('should only send output if payload changes', function(done) {
it('should only send output if payload changes (rbe)', function(done) {
var flow = [{"id":"n1", "type":"rbe", func:"rbe", gap:"0", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
@ -54,8 +54,14 @@ describe('rbe node', function() {
msg.should.have.a.property("payload", "a");
c+=1;
}
else {
else if (c === 1) {
msg.should.have.a.property("payload", "b");
c+=1;
}
else {
msg.should.have.a.property("payload");
msg.payload.should.have.a.property("b",1);
msg.payload.should.have.a.property("c",2);
done();
}
});
@ -65,12 +71,14 @@ describe('rbe node', function() {
n1.emit("input", {payload:"a"});
n1.emit("input", {payload:"a"});
n1.emit("input", {payload:"b"});
n1.emit("input", {payload:"b"});
n1.emit("input", {payload:{b:1,c:2}});
n1.emit("input", {payload:{c:2,b:1}});
n1.emit("input", {payload:{c:2,b:1}});
});
});
it('should only send output if more than x away from original value', function(done) {
var flow = [{"id":"n1", "type":"rbe", func:"gap", gap:"10", wires:[["n2"]] },
it('should only send output if more than x away from original value (deadband)', function(done) {
var flow = [{"id":"n1", "type":"rbe", func:"deadband", gap:"10", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
@ -100,8 +108,8 @@ describe('rbe node', function() {
});
});
it('should only send output if more than x% away from original value', function(done) {
var flow = [{"id":"n1", "type":"rbe", func:"gap", gap:"10%", wires:[["n2"]] },
it('should only send output if more than x% away from original value (deadband)', function(done) {
var flow = [{"id":"n1", "type":"rbe", func:"deadband", gap:"10%", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
@ -129,8 +137,8 @@ describe('rbe node', function() {
});
});
it('should warn if no number found in gap mode', function(done) {
var flow = [{"id":"n1", "type":"rbe", func:"gap", gap:"10", wires:[["n2"]] },
it('should warn if no number found in deadband mode', function(done) {
var flow = [{"id":"n1", "type":"rbe", func:"deadband", gap:"10", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
@ -157,4 +165,36 @@ describe('rbe node', function() {
});
});
it('should not send output if more than x away from original value (narrowband)', function(done) {
var flow = [{"id":"n1", "type":"rbe", func:"narrowband", gap:"10", 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("payload", 0);
}
else if (c === 1) {
msg.should.have.a.property("payload","6 deg");
}
else {
msg.should.have.a.property("payload", "5 deg");
done();
}
c += 1;
});
n1.emit("input", {payload:0});
n1.emit("input", {payload:20});
n1.emit("input", {payload:40});
n1.emit("input", {payload:"6 deg"});
n1.emit("input", {payload:18});
n1.emit("input", {payload:20});
n1.emit("input", {payload:50});
n1.emit("input", {payload:"5 deg"});
});
});
});