2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 module.exports = function(RED) {
19 var mustache = require("mustache");
20 function TriggerNode(n) {
21 RED.nodes.createNode(this,n);
22 this.op1 = n.op1 || "1";
23 this.op2 = n.op2 || "0";
24 this.op1type = n.op1type || "val";
25 this.op2type = n.op2type || "val";
26 this.extend = n.extend || false;
27 this.units = n.units || "ms";
28 this.duration = n.duration || 250;
29 if (this.duration <= 0) { this.duration = 0; }
31 if (this.units == "s") { this.duration = this.duration * 1000; }
32 if (this.units == "min") { this.duration = this.duration * 1000 * 60; }
33 if (this.units == "hr") { this.duration = this.duration * 1000 *60 * 60; }
35 this.op1Templated = this.op1.indexOf("{{") != -1;
36 this.op2Templated = this.op2.indexOf("{{") != -1;
37 if (!isNaN(this.op1)) { this.op1 = Number(this.op1); }
38 if (!isNaN(this.op2)) { this.op2 = Number(this.op2); }
39 if (this.op1 == "true") { this.op1 = true; }
40 if (this.op2 == "true") { this.op1 = true; }
41 if (this.op1 == "false") { this.op2 = false; }
42 if (this.op2 == "false") { this.op2 = false; }
43 if (this.op1 == "null") { this.op1 = null; }
44 if (this.op2 == "null") { this.op1 = null; }
45 try { this.op1 = JSON.parse(this.op1); }
46 catch(e) { this.op1 = this.op1; }
47 try { this.op2 = JSON.parse(this.op2); }
48 catch(e) { this.op2 = this.op2; }
53 this.on("input", function(msg) {
54 if (msg.hasOwnProperty("reset")) {
60 if (node.op2type === "pay") { m2 = msg.payload; }
61 else if (node.op2Templated) { m2 = mustache.render(node.op2,msg); }
62 else { m2 = node.op2; }
63 if (node.op1type === "pay") { }
64 else if (node.op1Templated) { msg.payload = mustache.render(node.op1,msg); }
65 else { msg.payload = node.op1; }
66 if (node.op1type !== "nul") { node.send(msg); }
67 if (node.duration === 0) { tout = "infinite"; }
69 tout = setTimeout(function() {
71 if (node.op2type !== "nul") { node.send(msg); }
76 else if ((node.extend == "true") && (node.duration > 0)) {
78 tout = setTimeout(function() {
80 if (node.op2type !== "nul") { node.send(msg); }
86 this.on("close", function() {
87 if (tout) { clearTimeout(tout); }
90 RED.nodes.registerType("trigger",TriggerNode);