2014-06-25 22:36:28 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-06-25 22:36:28 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var mustache = require("mustache");
|
|
|
|
function TriggerNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2017-11-17 18:35:18 +01:00
|
|
|
this.bytopic = n.bytopic || "all";
|
2014-06-25 22:36:28 +02:00
|
|
|
this.op1 = n.op1 || "1";
|
|
|
|
this.op2 = n.op2 || "0";
|
2016-07-05 16:38:43 +02:00
|
|
|
this.op1type = n.op1type || "str";
|
|
|
|
this.op2type = n.op2type || "str";
|
|
|
|
|
|
|
|
if (this.op1type === 'val') {
|
|
|
|
if (this.op1 === 'true' || this.op1 === 'false') {
|
|
|
|
this.op1type = 'bool'
|
|
|
|
} else if (this.op1 === 'null') {
|
|
|
|
this.op1type = 'null';
|
|
|
|
this.op1 = null;
|
|
|
|
} else {
|
|
|
|
this.op1type = 'str';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.op2type === 'val') {
|
|
|
|
if (this.op2 === 'true' || this.op2 === 'false') {
|
|
|
|
this.op2type = 'bool'
|
|
|
|
} else if (this.op2 === 'null') {
|
|
|
|
this.op2type = 'null';
|
|
|
|
this.op2 = null;
|
|
|
|
} else {
|
|
|
|
this.op2type = 'str';
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 16:01:55 +01:00
|
|
|
this.extend = n.extend || "false";
|
2014-06-25 22:36:28 +02:00
|
|
|
this.units = n.units || "ms";
|
2016-02-04 22:05:15 +01:00
|
|
|
this.reset = n.reset || '';
|
2017-11-17 18:35:18 +01:00
|
|
|
this.duration = parseFloat(n.duration);
|
2017-07-03 16:17:17 +02:00
|
|
|
if (isNaN(this.duration)) {
|
|
|
|
this.duration = 250;
|
|
|
|
}
|
2017-02-07 21:11:34 +01:00
|
|
|
if (this.duration < 0) {
|
|
|
|
this.loop = true;
|
|
|
|
this.duration = this.duration * -1;
|
|
|
|
this.extend = false;
|
2014-06-25 22:36:28 +02:00
|
|
|
}
|
2017-02-07 21:11:34 +01:00
|
|
|
|
|
|
|
if (this.units == "s") { this.duration = this.duration * 1000; }
|
|
|
|
if (this.units == "min") { this.duration = this.duration * 1000 * 60; }
|
|
|
|
if (this.units == "hr") { this.duration = this.duration * 1000 *60 * 60; }
|
|
|
|
|
2016-07-05 16:38:43 +02:00
|
|
|
this.op1Templated = (this.op1type === 'str' && this.op1.indexOf("{{") != -1);
|
|
|
|
this.op2Templated = (this.op2type === 'str' && this.op2.indexOf("{{") != -1);
|
2016-02-04 22:05:15 +01:00
|
|
|
if ((this.op1type === "num") && (!isNaN(this.op1))) { this.op1 = Number(this.op1); }
|
|
|
|
if ((this.op2type === "num") && (!isNaN(this.op2))) { this.op2 = Number(this.op2); }
|
2017-11-17 18:35:18 +01:00
|
|
|
//if (this.op1 == "null") { this.op1 = null; }
|
|
|
|
//if (this.op2 == "null") { this.op2 = null; }
|
2016-02-04 22:05:15 +01:00
|
|
|
//try { this.op1 = JSON.parse(this.op1); }
|
|
|
|
//catch(e) { this.op1 = this.op1; }
|
|
|
|
//try { this.op2 = JSON.parse(this.op2); }
|
|
|
|
//catch(e) { this.op2 = this.op2; }
|
2014-06-25 22:36:28 +02:00
|
|
|
|
|
|
|
var node = this;
|
2017-11-17 18:35:18 +01:00
|
|
|
node.topics = {};
|
|
|
|
|
2014-06-25 22:36:28 +02:00
|
|
|
this.on("input", function(msg) {
|
2017-11-17 18:35:18 +01:00
|
|
|
var topic = msg.topic || "_none";
|
|
|
|
if (node.bytopic === "all") { topic = "_none"; }
|
|
|
|
node.topics[topic] = node.topics[topic] || {};
|
2018-01-17 10:51:53 +01:00
|
|
|
if (msg.hasOwnProperty("reset") || ((node.reset !== '') && msg.hasOwnProperty("payload") && (msg.payload !== null) && msg.payload.toString && (msg.payload.toString() == node.reset)) ) {
|
2017-11-17 18:35:18 +01:00
|
|
|
if (node.loop === true) { clearInterval(node.topics[topic].tout); }
|
|
|
|
else { clearTimeout(node.topics[topic].tout); }
|
|
|
|
delete node.topics[topic];
|
2015-10-22 17:27:07 +02:00
|
|
|
node.status({});
|
2014-06-25 22:36:28 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-11-17 18:35:18 +01:00
|
|
|
if (((!node.topics[topic].tout) && (node.topics[topic].tout !== 0)) || (node.loop === true)) {
|
|
|
|
if (node.op2type === "pay" || node.op2type === "payl") { node.topics[topic].m2 = RED.util.cloneMessage(msg.payload); }
|
|
|
|
else if (node.op2Templated) { node.topics[topic].m2 = mustache.render(node.op2,msg); }
|
2016-07-05 16:38:43 +02:00
|
|
|
else if (node.op2type !== "nul") {
|
2017-11-17 18:35:18 +01:00
|
|
|
node.topics[topic].m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
|
2016-07-05 16:38:43 +02:00
|
|
|
}
|
|
|
|
|
2014-06-25 22:36:28 +02:00
|
|
|
if (node.op1type === "pay") { }
|
|
|
|
else if (node.op1Templated) { msg.payload = mustache.render(node.op1,msg); }
|
2016-07-05 16:38:43 +02:00
|
|
|
else if (node.op1type !== "nul") {
|
|
|
|
msg.payload = RED.util.evaluateNodeProperty(node.op1,node.op1type,node,msg);
|
|
|
|
}
|
|
|
|
|
2017-11-17 18:35:18 +01:00
|
|
|
if (node.op1type !== "nul") { node.send(RED.util.cloneMessage(msg)); }
|
2016-07-05 16:38:43 +02:00
|
|
|
|
2017-11-17 18:35:18 +01:00
|
|
|
if (node.duration === 0) { node.topics[topic].tout = 0; }
|
2017-02-07 21:11:34 +01:00
|
|
|
else if (node.loop === true) {
|
2018-01-17 10:51:53 +01:00
|
|
|
/* istanbul ignore else */
|
2017-11-17 18:35:18 +01:00
|
|
|
if (node.topics[topic].tout) { clearInterval(node.topics[topic].tout); }
|
2018-01-17 10:51:53 +01:00
|
|
|
/* istanbul ignore else */
|
2017-02-07 21:11:34 +01:00
|
|
|
if (node.op1type !== "nul") {
|
|
|
|
var msg2 = RED.util.cloneMessage(msg);
|
2017-11-17 18:35:18 +01:00
|
|
|
node.topics[topic].tout = setInterval(function() { node.send(RED.util.cloneMessage(msg2)); }, node.duration);
|
2017-02-07 21:11:34 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-25 22:36:28 +02:00
|
|
|
else {
|
2017-11-17 18:35:18 +01:00
|
|
|
node.topics[topic].tout = setTimeout(function() {
|
2018-01-02 09:28:08 +01:00
|
|
|
var msg2 = null;
|
2016-07-05 16:38:43 +02:00
|
|
|
if (node.op2type !== "nul") {
|
2018-01-02 09:28:08 +01:00
|
|
|
msg2 = RED.util.cloneMessage(msg);
|
2016-07-05 16:38:43 +02:00
|
|
|
if (node.op2type === "flow" || node.op2type === "global") {
|
2017-11-17 18:35:18 +01:00
|
|
|
node.topics[topic].m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
|
2016-07-05 16:38:43 +02:00
|
|
|
}
|
2017-11-17 18:35:18 +01:00
|
|
|
msg2.payload = node.topics[topic].m2;
|
2016-07-05 16:38:43 +02:00
|
|
|
}
|
2017-11-17 18:35:18 +01:00
|
|
|
delete node.topics[topic];
|
2015-10-22 17:27:07 +02:00
|
|
|
node.status({});
|
2018-01-02 09:28:08 +01:00
|
|
|
node.send(msg2);
|
2017-11-17 18:35:18 +01:00
|
|
|
}, node.duration);
|
2014-06-25 22:36:28 +02:00
|
|
|
}
|
2015-10-22 17:27:07 +02:00
|
|
|
node.status({fill:"blue",shape:"dot",text:" "});
|
2014-06-25 22:36:28 +02:00
|
|
|
}
|
2015-06-25 22:59:26 +02:00
|
|
|
else if ((node.extend === "true" || node.extend === true) && (node.duration > 0)) {
|
2018-01-17 10:51:53 +01:00
|
|
|
/* istanbul ignore else */
|
2017-11-17 18:35:18 +01:00
|
|
|
if (node.op2type === "payl") { node.topics[topic].m2 = RED.util.cloneMessage(msg.payload); }
|
2018-01-17 10:51:53 +01:00
|
|
|
/* istanbul ignore else */
|
2017-12-19 18:52:35 +01:00
|
|
|
if (node.topics[topic].tout) { clearTimeout(node.topics[topic].tout); }
|
2017-11-17 18:35:18 +01:00
|
|
|
node.topics[topic].tout = setTimeout(function() {
|
2018-01-02 09:28:08 +01:00
|
|
|
var msg2 = null;
|
2016-07-05 16:38:43 +02:00
|
|
|
if (node.op2type !== "nul") {
|
|
|
|
if (node.op2type === "flow" || node.op2type === "global") {
|
2017-11-17 18:35:18 +01:00
|
|
|
node.topics[topic].m2 = RED.util.evaluateNodeProperty(node.op2,node.op2type,node,msg);
|
2016-07-05 16:38:43 +02:00
|
|
|
}
|
2017-12-19 18:52:35 +01:00
|
|
|
if (node.topics[topic] !== undefined) {
|
2018-01-02 09:28:08 +01:00
|
|
|
msg2 = RED.util.cloneMessage(msg);
|
2017-12-19 18:52:35 +01:00
|
|
|
msg2.payload = node.topics[topic].m2;
|
|
|
|
}
|
2016-07-05 16:38:43 +02:00
|
|
|
}
|
2017-11-17 18:35:18 +01:00
|
|
|
delete node.topics[topic];
|
2015-10-22 17:27:07 +02:00
|
|
|
node.status({});
|
2018-01-02 09:28:08 +01:00
|
|
|
node.send(msg2);
|
2017-11-17 18:35:18 +01:00
|
|
|
}, node.duration);
|
2017-02-07 21:11:34 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-11-17 18:35:18 +01:00
|
|
|
if (node.op2type === "payl") { node.topics[topic].m2 = RED.util.cloneMessage(msg.payload); }
|
2014-06-25 22:36:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.on("close", function() {
|
2017-11-17 18:35:18 +01:00
|
|
|
for (var t in node.topics) {
|
2018-01-17 10:51:53 +01:00
|
|
|
/* istanbul ignore else */
|
2017-11-17 18:35:18 +01:00
|
|
|
if (node.topics[t]) {
|
|
|
|
if (node.loop === true) { clearInterval(node.topics[t].tout); }
|
|
|
|
else { clearTimeout(node.topics[t].tout); }
|
|
|
|
delete node.topics[t];
|
|
|
|
}
|
2015-10-22 17:27:07 +02:00
|
|
|
}
|
2016-02-04 23:13:08 +01:00
|
|
|
node.status({});
|
2014-06-25 22:36:28 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("trigger",TriggerNode);
|
|
|
|
}
|