2015-01-23 14:34:10 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
function RbeNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.func = n.func || "rbe";
|
2015-07-14 22:03:07 +02:00
|
|
|
this.gap = n.gap || "0";
|
2016-02-02 14:39:37 +01:00
|
|
|
this.start = n.start || '';
|
2016-03-21 01:01:50 +01:00
|
|
|
this.inout = n.inout || "out";
|
2015-07-14 22:03:07 +02:00
|
|
|
this.pc = false;
|
|
|
|
if (this.gap.substr(-1) === "%") {
|
|
|
|
this.pc = true;
|
|
|
|
this.gap = parseFloat(this.gap);
|
|
|
|
}
|
|
|
|
this.g = this.gap;
|
2015-01-23 14:34:10 +01:00
|
|
|
var node = this;
|
|
|
|
|
2015-07-14 22:03:07 +02:00
|
|
|
node.previous = {};
|
2015-01-23 14:34:10 +01:00
|
|
|
this.on("input",function(msg) {
|
2015-03-31 14:19:02 +02:00
|
|
|
if (msg.hasOwnProperty("payload")) {
|
2015-07-14 22:03:07 +02:00
|
|
|
var t = msg.topic || "_no_topic";
|
2015-03-31 14:19:02 +02:00
|
|
|
if (this.func === "rbe") {
|
2016-02-02 14:39:37 +01:00
|
|
|
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);
|
|
|
|
}
|
2015-01-23 14:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-03-31 14:19:02 +02:00
|
|
|
var n = parseFloat(msg.payload);
|
|
|
|
if (!isNaN(n)) {
|
2016-02-02 14:39:37 +01:00
|
|
|
if ((typeof node.previous[t] === 'undefined') && (this.func === "narrowband")) {
|
|
|
|
if (node.start === '') { node.previous[t] = n; }
|
|
|
|
else { node.previous[t] = node.start; }
|
|
|
|
}
|
2015-07-14 22:03:07 +02:00
|
|
|
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) {
|
2016-02-02 14:39:37 +01:00
|
|
|
if (this.func === "deadband") {
|
2016-03-21 01:01:50 +01:00
|
|
|
if (node.inout === "out") { node.previous[t] = n; }
|
2016-02-02 14:39:37 +01:00
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (this.func === "narrowband") {
|
2016-03-21 01:01:50 +01:00
|
|
|
if (node.inout === "out") { node.previous[t] = n; }
|
2016-02-02 14:39:37 +01:00
|
|
|
node.send(msg);
|
|
|
|
}
|
2015-03-31 14:19:02 +02:00
|
|
|
}
|
2016-03-21 01:01:50 +01:00
|
|
|
if (node.inout === "in") { node.previous[t] = n; }
|
2015-03-31 14:19:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-07-14 22:03:07 +02:00
|
|
|
node.warn(RED._("rbe.warn.nonumber"));
|
2015-03-31 14:19:02 +02:00
|
|
|
}
|
2015-01-23 14:34:10 +01:00
|
|
|
}
|
2015-03-31 14:19:02 +02:00
|
|
|
} // ignore msg with no payload property.
|
2015-01-23 14:34:10 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("rbe",RbeNode);
|
|
|
|
}
|