node-red-contrib-interval-length
Version:
A Node Red node to measure the interval length between successive messages
191 lines (186 loc) • 10.9 kB
HTML
<!--
Copyright 2017, Bart Butenaers
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('interval-length',{
category: 'function',
color: '#a6bbcf',
defaults: {
format: {value:"mills"},
bytopic: {value:false},
minimum: {value:"", validator: function(v) { return !v || !isNaN(v)}},
maximum: {value:"", validator: function(v) { return !v || !isNaN(v)}},
window: {value:""},
timeout: {value:false}, // Window timeout (true/false) !
msgTimeout: {value:""},
minimumunit: {value:"msecs"},
maximumunit: {value:"msecs"},
windowunit: {value:"msecs"},
msgTimeoutUnit: {value:"msecs"},
reset: {value:false},
startup: {value:false},
msgField: {value:"payload", required:true},
timestampField: {value:"timestamp", required:true},
repeatTimeout: {value:false},
name: {value:""}
},
inputs:1,
outputs:2,
outputLabels: ["interval", "timeout"],
icon: "length.png",
label: function() {
return this.name||"Interval length";
},
oneditprepare: function() {
// Show the msgField value in a typedinput element (dropdown with only 'msg')
var value = $("#node-input-msgField").val() || 'payload';
$("#node-input-typed-msgField").typedInput({types:['msg']});
$("#node-input-typed-msgField").typedInput('type','msg');
$("#node-input-typed-msgField").typedInput('value',value);
// Show the timestampField value in a typedinput element (dropdown with only 'msg')
var value = $("#node-input-timestampField").val() || 'timestamp';
$("#node-input-typed-timestampField").typedInput({types:['msg']});
$("#node-input-typed-timestampField").typedInput('type','msg');
$("#node-input-typed-timestampField").typedInput('value',value);
},
oneditsave: function() {
// Copy the msgField value from the typedinput element to the msgField element
var value = $("#node-input-typed-msgField").typedInput('value');
$("#node-input-msgField").val(value);
// Copy the timestampField value from the typedinput element to the timestampField element
var value = $("#node-input-typed-timestampField").typedInput('value');
$("#node-input-timestampField").val(value);
}
});
</script>
<script type="text/x-red" data-template-name="interval-length">
<div class="form-row" id="node-set-pwm">
<label for="node-input-format"><i class="fa fa-envelope"></i> Format</label>
<select id="node-input-format">
<option value="mills">Milliseconds</option>
<option value="human">Human readable</option>
<option value="json">JSON object</option>
</select>
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-bytopic" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-bytopic" style="width:70%;">Topic dependent</label>
</div>
<div class="form-row">
<label for="node-input-minimum"><i class="fa fa-level-up"></i> Minimum</label>
<input type="number" id="node-input-minimum" style="width:180px;">
<select id="node-input-minimumunit" style="width:120px;">
<option value="msecs">MilliSeconds</option>
<option value="secs">Seconds</option>
<option value="mins">Minutes</option>
<option value="hours">Hours</option>
</select>
</div>
<div class="form-row">
<label for="node-input-maximum"><i class="fa fa-level-down"></i> Maximum</label>
<input type="number" id="node-input-maximum" style="width:180px;">
<select id="node-input-maximumunit" style="width:120px;">
<option value="msecs">MilliSeconds</option>
<option value="secs">Seconds</option>
<option value="mins">Minutes</option>
<option value="hours">Hours</option>
</select>
</div>
<div class="form-row">
<label for="node-input-window"><i class="fa fa-arrows-h"></i> Window</label>
<input type="number" id="node-input-window" style="width:180px;">
<select id="node-input-windowunit" style="width:120px;">
<option value="msecs">MilliSeconds</option>
<option value="secs">Seconds</option>
<option value="mins">Minutes</option>
<option value="hours">Hours</option>
</select>
</div>
<div class="form-row">
<label for="node-input-msgTimeout"><i class="fa fa-clock-o"></i> Timeout</label>
<input type="number" id="node-input-msgTimeout" style="width:180px;">
<select id="node-input-msgTimeoutUnit" style="width:120px;">
<option value="msecs">MilliSeconds</option>
<option value="secs">Seconds</option>
<option value="mins">Minutes</option>
<option value="hours">Hours</option>
</select>
</div>
<div class="form-row">
<label for="node-input-typed-msgField"><i class="fa fa-list"></i> Interval</label>
<input id="node-input-typed-msgField" type="text" style="width: 70%">
<input id="node-input-msgField" type="hidden">
</div>
<div class="form-row">
<label for="node-input-typed-timestampField"><i class="fa fa-list"></i> Timestamp</label>
<input id="node-input-typed-timestampField" type="text" style="width: 70%">
<input id="node-input-timestampField" type="hidden">
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-timeout" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-timeout" style="width:70%;"> Create 0-interval msg at window timeout</label>
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-startup" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-startup" style="width:70%;"> Start measurement at flow startup</label>
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-repeatTimeout" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-repeatTimeout" style="width:70%;"> Repeat timeout msg periodically</label>
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-reset" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-reset" style="width:70%;"> Allow measurements to be reset</label>
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="interval-length">
<p>A Node Red node to measure the interval length between messages.</p>
<p><strong>Format:</strong><br/>
The interval length (in the output message) can be obtained in a number of different formats:
<ul>
<li><code>Milliseconds</code> milliseconds as a number</li>
<li><code>Human readable</code> milliseconds formatted like '1d:7h:0m:0s'</li>
<li><code>JSON object</code> milliseconds formatted like { minutes: 23, seconds: 20, milliseconds: 1 } </li>
</ul></p>
<p><strong>Topic dependent:</strong><br/>
When selected, the intervals will be measured for each topic separately.</p>
<p><strong>Minimum:</strong><br/>
When value X is specified, intervals shorter than X will be ignored (i.e. won't result in an output message).</p>
<p><strong>Maximum:</strong><br/>
When value Y is specified, intervals longer than Y will be ignored (i.e. won't result in an output message).</p>
<p><strong>Window:</strong><br/>
When value Z is specified, all intervals within a window of length Z will be accumulated (and send as a single output message).</p>
<p><strong>Timeout:</strong><br/>
When value T is specified, a timeout message will be generated (on the second output port) when no new input message has arrived in the specified time interval T.</p>
<p><strong>Interval:</strong><br/>
By default the interval measurement value will be in the <code>msg.payload</code> field of the output message. However if the original message (and it's payload) needs to be preserved, another output message field can be specified here.</p>
<p><strong>Timestamp:</strong><br/>
By default the timestamp of the previous message will be in the <code>msg.timestamp</code> field of the output message. However if the original message (and it's timestamp field) needs to be preserved, another output message field can be specified here.</p>
<p><strong>Create 0-interval msg at window timeout:</strong><br/>
When selected, an extra message (containing a '0' interval) will be generated at the end of the window, when <b><u>no</u></b> input message has arrived during the window interval.</p>
<p><strong>Start measurement at startup:</strong><br/>
When selected, the node will start counting at flow startup. When deselected, the node will start counting when the first message arrives. In the latter case, <b><u>no</u></b> output message will be generated when the first message arrives.</p>
<p><strong>Repeat timeout msg periodically:</strong><br/>
When selected, the timout message will be repeated periodically (as long as the expected next input message doesn't arrive).</p>
<p><strong>Allow measurements to be reset:</strong><br/>
When selected, current measurement will be reset as soon as a <code>msg.reset=true</code> arrives. This reset message will not be taken into account for the interval measurement.</p>
</script>