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;
|
2021-04-27 21:25:30 +02:00
|
|
|
this.property = n.property || "payload";
|
|
|
|
this.topi = n.topi || "topic";
|
2021-02-26 15:51:55 +01:00
|
|
|
this.septopics = true;
|
|
|
|
if (n.septopics !== undefined && n.septopics === false) {
|
|
|
|
this.septopics = false;
|
|
|
|
}
|
2017-05-24 14:14:55 +02:00
|
|
|
|
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) {
|
2021-04-27 21:25:30 +02:00
|
|
|
var topic;
|
|
|
|
try {
|
|
|
|
topic = RED.util.getMessageProperty(msg,node.topi);
|
|
|
|
}
|
|
|
|
catch(e) { }
|
2017-12-20 17:28:32 +01:00
|
|
|
if (msg.hasOwnProperty("reset")) {
|
2021-04-27 21:25:30 +02:00
|
|
|
if (node.septopics && topic && (typeof topic === "string") && (topic !== "")) {
|
2019-08-11 14:37:15 +02:00
|
|
|
delete node.previous[msg.topic];
|
2017-12-20 17:28:32 +01:00
|
|
|
}
|
|
|
|
else { node.previous = {}; }
|
|
|
|
}
|
2018-01-30 22:42:14 +01:00
|
|
|
var value = RED.util.getMessageProperty(msg,node.property);
|
|
|
|
if (value !== undefined) {
|
2021-02-26 15:51:55 +01:00
|
|
|
var t = "_no_topic";
|
2021-04-27 21:25:30 +02:00
|
|
|
if (node.septopics) { t = topic || t; }
|
2017-05-24 14:14:55 +02:00
|
|
|
if ((this.func === "rbe") || (this.func === "rbei")) {
|
|
|
|
var doSend = (this.func !== "rbei") || (node.previous.hasOwnProperty(t)) || false;
|
2018-01-30 22:42:14 +01:00
|
|
|
if (typeof(value) === "object") {
|
2016-02-02 14:39:37 +01:00
|
|
|
if (typeof(node.previous[t]) !== "object") { node.previous[t] = {}; }
|
2018-01-30 22:42:14 +01:00
|
|
|
if (!RED.util.compareObjects(value, node.previous[t])) {
|
|
|
|
node.previous[t] = RED.util.cloneMessage(value);
|
2017-05-24 14:14:55 +02:00
|
|
|
if (doSend) { node.send(msg); }
|
2016-02-02 14:39:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-01-30 22:42:14 +01:00
|
|
|
if (value !== node.previous[t]) {
|
|
|
|
node.previous[t] = RED.util.cloneMessage(value);
|
2017-05-24 14:14:55 +02:00
|
|
|
if (doSend) { node.send(msg); }
|
2016-02-02 14:39:37 +01:00
|
|
|
}
|
2015-01-23 14:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-01-30 22:42:14 +01:00
|
|
|
var n = parseFloat(value);
|
2015-03-31 14:19:02 +02:00
|
|
|
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; }
|
|
|
|
}
|
2018-02-08 16:51:57 +01:00
|
|
|
if (node.pc) { node.gap = Math.abs(node.previous[t] * node.g / 100) || 0; }
|
2017-04-14 22:38:42 +02:00
|
|
|
else { node.gap = Number(node.gap); }
|
|
|
|
if ((node.previous[t] === undefined) && (node.func === "narrowbandEq")) { node.previous[t] = n; }
|
2020-05-31 22:52:37 +02:00
|
|
|
if (node.previous[t] === undefined) { node.previous[t] = n - node.gap - 1; }
|
2017-04-14 22:38:42 +02:00
|
|
|
if (Math.abs(n - node.previous[t]) === node.gap) {
|
2019-08-14 22:35:45 +02:00
|
|
|
if ((this.func === "deadbandEq")||(this.func === "narrowband")) {
|
2017-04-14 22:38:42 +02:00
|
|
|
if (node.inout === "out") { node.previous[t] = n; }
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Math.abs(n - node.previous[t]) > node.gap) {
|
2017-09-11 18:45:45 +02:00
|
|
|
if (this.func === "deadband" || this.func === "deadbandEq") {
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-04-14 22:38:42 +02:00
|
|
|
else if (Math.abs(n - node.previous[t]) < node.gap) {
|
|
|
|
if ((this.func === "narrowband")||(this.func === "narrowbandEq")) {
|
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);
|
|
|
|
}
|