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

@@ -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 {