mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add trigger node
This commit is contained in:
parent
479a02cc16
commit
1602d9496f
130
nodes/core/core/89-trigger.html
Normal file
130
nodes/core/core/89-trigger.html
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
<!--
|
||||||
|
Copyright 2014 IBM Corp.
|
||||||
|
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<script type="text/x-red" data-template-name="trigger">
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-op1type"><i class="fa fa-arrow-up"></i> Output</label>
|
||||||
|
<select id="node-input-op1type" style="width:73% !important">
|
||||||
|
<option value="val">the value below</option>
|
||||||
|
<option value="pay">the existing payload</option>
|
||||||
|
<option value="nul">nothing (no output)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row" id="node-op1">
|
||||||
|
<label for="node-input-op1"> </label>
|
||||||
|
<input type="text" id="node-input-op1" placeholder="1">
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-duration"><i class="fa fa-clock-o"></i> then wait</label>
|
||||||
|
<input type="text" id="node-input-duration" placeholder="250" style="direction:rtl; width:70px !important">
|
||||||
|
<select id="node-input-units" style="width:140px !important">
|
||||||
|
<option value="ms">Milliseconds</option>
|
||||||
|
<option value="s">Seconds</option>
|
||||||
|
<option value="min">Minutes</option>
|
||||||
|
<option value="hr">Hours</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-op2type"><i class="fa fa-arrow-down"></i> output</label>
|
||||||
|
<select id="node-input-op2type" style="width:73% !important">
|
||||||
|
<option value="val">the value below</option>
|
||||||
|
<option value="pay">the existing payload</option>
|
||||||
|
<option value="nul">nothing (no output)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row" id="node-op2">
|
||||||
|
<label for="node-input-op2"> </label>
|
||||||
|
<input type="text" id="node-input-op2" placeholder="0">
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-extend"><i class="fa fa-repeat"></i> and</label>
|
||||||
|
<select id="node-input-extend" style="width:73% !important">
|
||||||
|
<option value="false">don't extend the timer if retriggered</option>
|
||||||
|
<option value="true">extend the timer if retriggered</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
|
</div>
|
||||||
|
<!-- <div class="form-tips">Tip: Outputs can be values, null, {{templated}} or msg.payload<br/> -->
|
||||||
|
<div class="form-tips">Setting the timeout to 0 sets an infinite timeout = single shot.</div>
|
||||||
|
<script>
|
||||||
|
{
|
||||||
|
$("#node-input-op1type").change(function() {
|
||||||
|
if ($("#node-input-op1type").val() == "val") { $("#node-op1").show(); }
|
||||||
|
else { $("#node-op1").hide(); }
|
||||||
|
});
|
||||||
|
$("#node-input-op2type").change(function() {
|
||||||
|
if ($("#node-input-op2type").val() == "val") { $("#node-op2").show(); }
|
||||||
|
else { $("#node-op2").hide(); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/x-red" data-help-name="trigger">
|
||||||
|
<p>Creates two messages on the output separated by a timeout whenever ANY <b>msg</b> arrives on the input.</p>
|
||||||
|
<p>For example, this can be used to toggle a Raspberry PI GPIO pin on and off.</p>
|
||||||
|
<p>The two output states can be specified as can the duration of the timer.
|
||||||
|
Either output can be set to a value, or templated from the inbound
|
||||||
|
<b>msg</b> using mustache syntax. <pre>The payload is {{payload}}</pre></p>
|
||||||
|
<p>If the payload is an object then setting the output to <i>existing payload</i> will pass the complete payload object through.</p>
|
||||||
|
<p>Optionally the timer can be extended by being retriggered... or not.</p>
|
||||||
|
<p>By setting the first output to <i>nothing</i>, and selecting extend timer - a watchdog timer can be created.
|
||||||
|
No output will happen as long as repeated inputs occur within the timeout period.</p>
|
||||||
|
<p>Setting the timer to 0 creates an "infinite" timeout - the first output will happen but the second
|
||||||
|
never will, and neither can the first be retriggered - so a true one shot.</p>
|
||||||
|
<p>If a <b>msg.reset</b> property is present any timeout currently in progress
|
||||||
|
will be cleared and the second output will not happen.</p>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
RED.nodes.registerType('trigger',{
|
||||||
|
category: 'function',
|
||||||
|
color:"#E2D96E",
|
||||||
|
defaults: {
|
||||||
|
op1: {value:""},
|
||||||
|
op2: {value:""},
|
||||||
|
op1type: {value:""},
|
||||||
|
op2type: {value:""},
|
||||||
|
duration: {value:"250",required:true,validate:RED.validators.number()},
|
||||||
|
extend: {value:"false"},
|
||||||
|
units: {value: "ms"},
|
||||||
|
name: {value:""}
|
||||||
|
},
|
||||||
|
inputs:1,
|
||||||
|
outputs:1,
|
||||||
|
icon: "trigger.png",
|
||||||
|
label: function() {
|
||||||
|
if (this.duration > 0) {
|
||||||
|
return this.name||"trigger "+this.duration+this.units;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return this.name||"trigger once ∞";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
labelStyle: function() {
|
||||||
|
return this.name?"node_label_italic":"";
|
||||||
|
},
|
||||||
|
oneditprepare: function() {
|
||||||
|
$( "#node-input-duration" ).spinner({
|
||||||
|
min:1,
|
||||||
|
increment:25
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
91
nodes/core/core/89-trigger.js
Normal file
91
nodes/core/core/89-trigger.js
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2014 IBM Corp.
|
||||||
|
*
|
||||||
|
* 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);
|
||||||
|
this.op1 = n.op1 || "1";
|
||||||
|
this.op2 = n.op2 || "0";
|
||||||
|
this.op1type = n.op1type || "val";
|
||||||
|
this.op2type = n.op2type || "val";
|
||||||
|
this.extend = n.extend || false;
|
||||||
|
this.units = n.units || "ms";
|
||||||
|
this.duration = n.duration || 250;
|
||||||
|
if (this.duration <= 0) { this.duration = 0; }
|
||||||
|
else {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
this.op1Templated = this.op1.indexOf("{{") != -1;
|
||||||
|
this.op2Templated = this.op2.indexOf("{{") != -1;
|
||||||
|
if (!isNaN(this.op1)) { this.op1 = Number(this.op1); }
|
||||||
|
if (!isNaN(this.op2)) { this.op2 = Number(this.op2); }
|
||||||
|
if (this.op1 == "true") { this.op1 = true; }
|
||||||
|
if (this.op2 == "true") { this.op1 = true; }
|
||||||
|
if (this.op1 == "false") { this.op2 = false; }
|
||||||
|
if (this.op2 == "false") { this.op2 = false; }
|
||||||
|
if (this.op1 == "null") { this.op1 = null; }
|
||||||
|
if (this.op2 == "null") { this.op1 = null; }
|
||||||
|
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; }
|
||||||
|
|
||||||
|
var node = this;
|
||||||
|
var tout = null;
|
||||||
|
var m2;
|
||||||
|
this.on("input", function(msg) {
|
||||||
|
if (msg.hasOwnProperty("reset")) {
|
||||||
|
clearTimeout(tout);
|
||||||
|
tout = null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!tout) {
|
||||||
|
if (node.op2type === "pay") { m2 = msg.payload; }
|
||||||
|
else if (node.op2Templated) { m2 = mustache.render(node.op2,msg); }
|
||||||
|
else { m2 = node.op2; }
|
||||||
|
if (node.op1type === "pay") { }
|
||||||
|
else if (node.op1Templated) { msg.payload = mustache.render(node.op1,msg); }
|
||||||
|
else { msg.payload = node.op1; }
|
||||||
|
if (node.op1type !== "nul") { node.send(msg); }
|
||||||
|
if (node.duration == 0) { tout = "infinite"; }
|
||||||
|
else {
|
||||||
|
tout = setTimeout(function() {
|
||||||
|
msg.payload = m2;
|
||||||
|
if (node.op2type !== "nul") { node.send(msg); }
|
||||||
|
tout = null;
|
||||||
|
},node.duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((node.extend == "true") && (node.duration > 0)) {
|
||||||
|
clearTimeout(tout);
|
||||||
|
tout = setTimeout(function() {
|
||||||
|
msg.payload = m2;
|
||||||
|
if (node.op2type !== "nul") { node.send(msg); }
|
||||||
|
tout = null;
|
||||||
|
},node.duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.on("close", function() {
|
||||||
|
if (tout) { clearTimeout(tout); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
RED.nodes.registerType("trigger",TriggerNode);
|
||||||
|
}
|
BIN
public/icons/trigger.png
Normal file
BIN
public/icons/trigger.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 258 B |
Loading…
Reference in New Issue
Block a user