node-red-contrib-timeouttrigger
Version:
This node will pipe any received `msg` to its output. At the same time a time-out interval is set. Upon time-out a defined payload is sent. Each new message will reset the time-out.
146 lines (130 loc) • 6.17 kB
HTML
<!--
Copyright Sineos
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/javascript">
RED.nodes.registerType('timeouttrigger', {
category: 'function',
color: "#E6E0F8",
defaults: {
OnTimeoutType: { value: 'str' },
OnTimeoutVal: { value: "", required: true },
duration: { value: "", required: true, validate: RED.validators.number() },
units: { value: "s" },
passthrough: { value: true },
sendTimeoutValue: { value: "once" },
interval: { value: "", validate: function(value) {
return ($("#node-input-interval-row").is(":visible") && value === "") ? false : true; }},
IntervalUnits: { value: "s" },
name: { value: "" }
},
inputs: 1,
outputs: 1,
icon: "timeouttrigger.png",
label: function () {
var label = this.name + " " + this.duration + this.units || "timeouttrigger" + " " + this.duration + this.units;
if (this.sendTimeoutValue === "continuously" && this.interval) {
label += " / " + this.interval + this.IntervalUnits;
}
return label;
},
labelStyle: function () {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function () {
const intervalRow = $("#node-input-interval-row");
const inputSendTimeoutValue = $("#node-input-sendTimeoutValue");
const inputOnTimeoutVal = $("#node-input-OnTimeoutVal");
const inputUnits = $("#node-input-units");
const inputIntervalUnits = $("#node-input-IntervalUnits");
inputUnits.typedInput({type:"units", types:[{
value: "units",
options: [
{ value: "ms", label: "Miliseconds"},
{ value: "s", label: "Seconds"},
{ value: "min", label: "Minutes"},
{ value: "hr", label: "Hours"},
]
}]})
inputIntervalUnits.typedInput({type:"IntervalUnits", types:[{
value: "IntervalUnits",
options: [
{ value: "ms", label: "Miliseconds"},
{ value: "s", label: "Seconds"},
{ value: "min", label: "Minutes"},
{ value: "hr", label: "Hours"},
]
}]})
inputSendTimeoutValue.typedInput({type:"sendTimeoutValue", types:[{
value: "sendTimeoutValue",
options: [
{ value: "once", label: "Once"},
{ value: "continuously", label: "Continuously"},
]
}]})
inputOnTimeoutVal.typedInput({
default: "str",
typeField: $("#node-input-OnTimeoutType"),
types: ["str", "num", "bool"]
});
if (typeof this.passthrough === 'undefined') {
this.passthrough = true;
$("#node-input-passthrough").prop("checked", true);
}
if (typeof this.sendTimeoutValue === 'undefined') {
this.sendTimeoutValue = "once";
inputSendTimeoutValue.val("once");
}
// show/hide the interval field based on the selection
function toggleIntervalField() {
intervalRow.toggle(inputSendTimeoutValue.val() === "continuously");
}
// Call the toggleIntervalField initially
toggleIntervalField();
// Event listener for the dropdown change event
inputSendTimeoutValue.on("change", toggleIntervalField);
}
});
</script>
<script type="text/x-red" data-template-name="timeouttrigger">
<div class="form-row">
<label style="width: 180px !important" for="node-input-duration"><i class="fa fa-clock-o"></i> Time-Out</label>
<input type="text" id="node-input-duration" placeholder="Time-Out" style="text-align:end; width:100px !important">
<input type="text" id="node-input-units" style="width:140px !important">
</div>
<div class="form-row">
<label style="width: 180px !important" for="node-input-OnTimeoutType"><i class="fa fa-feed"></i> Send on Time-Out</label>
<input type="hidden" id="node-input-OnTimeoutType">
<input type="text" id="node-input-OnTimeoutVal" placeholder="Time-Out Value" style="text-align:end; width:245px !important">
</div>
<div class="form-row">
<label style="width: 180px !important" for="node-input-passthrough"><i class="fa fa-sign-out"></i> Pass-through messages</label>
<input type="checkbox" id="node-input-passthrough" style="margin-left: 0px; vertical-align: middle; width: auto !important;">
</div>
<div class="form-row">
<label style="width: 180px !important" for="node-input-sendTimeoutValue"><i class="fa fa-refresh"></i> Send Time-Out Value</label>
<input type="text" id="node-input-sendTimeoutValue" style="text-align:end; width:245px !important">
</div>
<div class="form-row" id="node-input-interval-row" style="display: none;">
<label style="width: 180px !important" for="node-input-interval"><i class="fa fa-clock-o"></i> Interval</label>
<input type="text" id="node-input-interval" placeholder="Interval" style="text-align:end; width:100px !important">
<input type="text" id="node-input-IntervalUnits" style="width:140px !important">
</div>
<div class="form-row">
<label style="width: 180px !important" for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" style="width:245px !important"></input>
</div>
</script>
<script type="text/x-red" data-help-name="timeouttrigger">
<p>Pipes <i>any</i> <code>msg</code> while resetting a time-out. Upon time-out a defined value is send.
Turning off <code>Pass-through messages</code> will stop piping the incoming messages, so only the time-out value is send.
Setting <code>Send Time-Out Value</code> to <code>Continuously</code> will repeat the time-out value at the given interval.</p>
</script>