node-red-contrib-looptimer-advanced 0.0.12 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,67 +1,187 @@
1
- <script type="text/javascript">
2
- RED.nodes.registerType('looptimer-advanced',{
3
- category: 'function',
4
- color:"#33B2FF",
5
- defaults: {
6
- duration: {value:"5",required:true,validate:RED.validators.number()},
7
- units: {value:"Second"},
8
- maxloops: {value:"100",required:false,validate:RED.validators.number()},
9
- maxtimeout: {value:"1",required:false,validate:RED.validators.number()},
10
- maxtimeoutunits: {value:"Hour"},
11
- name: {value:""}
12
- },
13
- inputs:1,
14
- outputs:2,
15
- icon: "looptimer_advanced.png",
16
- label: function() {
17
- return this.name || this.duration + " " + this.units + " Loop";
18
- },
19
- labelStyle: function() {
20
- return this.name?"node_label_italic":"";
21
- },
22
- outputLabels: ["original msg","stopped msg"]
23
- });
24
- </script>
25
-
26
- <script type="text/x-red" data-template-name="looptimer-advanced">
27
- <div class="form-row">
28
- <label for="node-input-duration"><i class="fa fa-clock-o"></i> Loop Every</label>
29
- <input type="text" id="node-input-duration" style="text-align:end; width:70px !important">
30
- <select id="node-input-units" style="width:140px !important">
31
- <option value="Millisecond">Miliseconds</option>
32
- <option value="Second">Seconds</option>
33
- <option value="Minute">Minutes</option>
34
- <option value="Hour">Hours</option>
35
- <option value="Day">Days</option>
36
- </select>
37
- </div>
38
- <div class="form-row">
39
- <label for="node-input-maxloops"><i class="fa fa-repeat"></i> Max Loops</label>
40
- <input type="text" id="node-input-maxloops" style="text-align:end; width:70px !important">
41
- </div>
42
- <div class="form-row">
43
- <label for="node-input-maxtimeout"><i class="fa fa-clock-o"></i> Max Timeout</label>
44
- <input type="text" id="node-input-maxtimeout" style="text-align:end; width:70px !important">
45
- <select id="node-input-maxtimeoutunits" style="width:140px !important">
46
- <option value="Millisecond">Miliseconds</option>
47
- <option value="Second">Seconds</option>
48
- <option value="Minute">Minutes</option>
49
- <option value="Hour">Hours</option>
50
- <option value="Day">Days</option>
51
- </select>
52
- </div>
53
- <div class="form-row">
54
- <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
55
- <input type="text" id="node-input-name" placeholder="Name"></input>
56
- </div>
57
- </script>
58
-
59
- <script type="text/x-red" data-help-name="looptimer-advanced">
60
- <p>Sends the <code>msg</code> through the first output, then continues to do so in a loop, once per the timer duration specified, until a payload of <code>stop</code> or <code>STOP</code> is received, at which time the second output will automatically send a payload of <code>stopped</code>.
61
- <p>You can also stop the loop by specifying a maximum number of loops, which when reached, will stop the loop and timer, as well as sending a payload of <code>max loops reached</code> through the second output. Keep in mind, the first <code>msg</code> simply passes through, and is therefore not part of the loop. So if you set the max loops to <code>5</code>, you will get <code>6 messages</code>, which is 1 original message, and 5 messages from the loop.</p>
62
- <p>Finally, to ensure you do not end up with an infinite loop, you can set a maximum timeout in seconds, minutes or hours, and when that time is reached, the loop and timer will also be stopped.</p>
63
- <p>Properties can be overridden via <code>msg.loop</code> messages supplemented with property name.</p>
64
- <p>Accepted messages for loop are <code>msg.loop.duration</code> and <code>msg.loop.units</code></p>
65
- <p>For timeout you can use <code>msg.loop.maxtimeout</code> and <code>msg.loop.maxtimeoutunits</code></p>
66
- <p><code>NOTE:</code> Setting the Max Loops and Max Timeout settings to high values can, for all intents, ensure that the loop can only be stopped by an incoming <code>stop</code> payload, however, the stability of the loop has not been tested over an extended number of hours.</p>
67
- </script>
1
+ <script type="text/javascript">
2
+ (function () {
3
+ // Settings can be a number, or a msg., flow., global. or env. value
4
+ function typedNumber(typeField) {
5
+ return function (value) {
6
+ if ((this[typeField] || 'num') !== 'num') { return !!value; }
7
+ return RED.validators.number()(value);
8
+ };
9
+ }
10
+ const numericTypes = ['num', 'msg', 'flow', 'global', 'env'];
11
+
12
+ RED.nodes.registerType('looptimer-advanced', {
13
+ category: 'function',
14
+ color: "#33B2FF",
15
+ defaults: {
16
+ duration: { value: "5", required: true, validate: typedNumber('durationType') },
17
+ durationType: { value: "num" },
18
+ units: { value: "Second" },
19
+ maxloops: { value: "100", required: false, validate: typedNumber('maxloopsType') },
20
+ maxloopsType: { value: "num" },
21
+ totalcount: { value: false },
22
+ sendfirst: { value: true },
23
+ maxtimeout: { value: "1", required: false, validate: typedNumber('maxtimeoutType') },
24
+ maxtimeoutType: { value: "num" },
25
+ maxtimeoutunits: { value: "Hour" },
26
+ concurrency: { value: "restart" },
27
+ pausecontrol: { value: true },
28
+ pausetimeout: { value: "", required: false, validate: function (v) { return v === undefined || v === "" || RED.validators.number()(v); } },
29
+ pausetimeoutunits: { value: "Minute" },
30
+ pauseaction: { value: "resume" },
31
+ addstate: { value: false },
32
+ name: { value: "" }
33
+ },
34
+ inputs: 1,
35
+ outputs: 2,
36
+ icon: "looptimer_advanced.png",
37
+ label: function () {
38
+ return this.name || this.duration + " " + this.units + " Loop";
39
+ },
40
+ labelStyle: function () {
41
+ return this.name ? "node_label_italic" : "";
42
+ },
43
+ outputLabels: ["original msg", "stopped msg"],
44
+ oneditprepare: function () {
45
+ // Nodes saved by earlier versions do not have the newer settings, show the defaults
46
+ const defaults = { durationType: 'num', maxloopsType: 'num', maxtimeoutType: 'num', concurrency: 'restart', pausetimeoutunits: 'Minute', pauseaction: 'resume' };
47
+ Object.keys(defaults).forEach((key) => {
48
+ if (!this[key]) { $('#node-input-' + key).val(defaults[key]); }
49
+ });
50
+ if (this.sendfirst === undefined) { $('#node-input-sendfirst').prop('checked', true); }
51
+
52
+ ['duration', 'maxloops', 'maxtimeout'].forEach((field) => {
53
+ $('#node-input-' + field).typedInput({
54
+ default: 'num',
55
+ types: numericTypes,
56
+ typeField: '#node-input-' + field + 'Type'
57
+ });
58
+ });
59
+ $('#node-input-duration').typedInput('width', '150px');
60
+ $('#node-input-maxloops').typedInput('width', '150px');
61
+ $('#node-input-maxtimeout').typedInput('width', '150px');
62
+ }
63
+ });
64
+ })();
65
+ </script>
66
+
67
+ <script type="text/html" data-template-name="looptimer-advanced">
68
+ <div class="form-row">
69
+ <label for="node-input-duration"><i class="fa fa-clock-o"></i> Loop Every</label>
70
+ <input type="text" id="node-input-duration">
71
+ <input type="hidden" id="node-input-durationType">
72
+ <select id="node-input-units" style="width:120px !important">
73
+ <option value="Millisecond">Milliseconds</option>
74
+ <option value="Second">Seconds</option>
75
+ <option value="Minute">Minutes</option>
76
+ <option value="Hour">Hours</option>
77
+ <option value="Day">Days</option>
78
+ </select>
79
+ </div>
80
+ <div class="form-row">
81
+ <label for="node-input-maxloops"><i class="fa fa-repeat"></i> Max Loops</label>
82
+ <input type="text" id="node-input-maxloops">
83
+ <input type="hidden" id="node-input-maxloopsType">
84
+ </div>
85
+ <div class="form-row">
86
+ <label>&nbsp;</label>
87
+ <input type="checkbox" id="node-input-totalcount" style="display:inline-block; width:auto; vertical-align:top;">
88
+ <label for="node-input-totalcount" style="width:auto"> Max Loops counts the first message too</label>
89
+ </div>
90
+ <div class="form-row">
91
+ <label>&nbsp;</label>
92
+ <input type="checkbox" id="node-input-sendfirst" style="display:inline-block; width:auto; vertical-align:top;">
93
+ <label for="node-input-sendfirst" style="width:auto"> Send the first message immediately</label>
94
+ </div>
95
+ <div class="form-row">
96
+ <label for="node-input-maxtimeout"><i class="fa fa-clock-o"></i> Max Timeout</label>
97
+ <input type="text" id="node-input-maxtimeout">
98
+ <input type="hidden" id="node-input-maxtimeoutType">
99
+ <select id="node-input-maxtimeoutunits" style="width:120px !important">
100
+ <option value="Millisecond">Milliseconds</option>
101
+ <option value="Second">Seconds</option>
102
+ <option value="Minute">Minutes</option>
103
+ <option value="Hour">Hours</option>
104
+ <option value="Day">Days</option>
105
+ </select>
106
+ </div>
107
+ <div class="form-row">
108
+ <label for="node-input-concurrency"><i class="fa fa-random"></i> New message</label>
109
+ <select id="node-input-concurrency" style="width:270px !important">
110
+ <option value="restart">restarts the loop</option>
111
+ <option value="ignore">is ignored while a loop runs</option>
112
+ <option value="pertopic">starts a separate loop per msg.topic</option>
113
+ </select>
114
+ </div>
115
+ <div class="form-row">
116
+ <label>&nbsp;</label>
117
+ <input type="checkbox" id="node-input-pausecontrol" style="display:inline-block; width:auto; vertical-align:top;">
118
+ <label for="node-input-pausecontrol" style="width:auto"> Pause / resume / status with payload</label>
119
+ </div>
120
+ <div class="form-row">
121
+ <label for="node-input-pausetimeout"><i class="fa fa-pause"></i> Pause Timeout</label>
122
+ <input type="text" id="node-input-pausetimeout" placeholder="none" style="text-align:end; width:70px !important">
123
+ <select id="node-input-pausetimeoutunits" style="width:120px !important">
124
+ <option value="Millisecond">Milliseconds</option>
125
+ <option value="Second">Seconds</option>
126
+ <option value="Minute">Minutes</option>
127
+ <option value="Hour">Hours</option>
128
+ <option value="Day">Days</option>
129
+ </select>
130
+ </div>
131
+ <div class="form-row">
132
+ <label for="node-input-pauseaction"><i class="fa fa-step-forward"></i> After Timeout</label>
133
+ <select id="node-input-pauseaction" style="width:140px !important">
134
+ <option value="resume">resume the loop</option>
135
+ <option value="stop">stop the loop</option>
136
+ </select>
137
+ </div>
138
+ <div class="form-row">
139
+ <label>&nbsp;</label>
140
+ <input type="checkbox" id="node-input-addstate" style="display:inline-block; width:auto; vertical-align:top;">
141
+ <label for="node-input-addstate" style="width:auto"> Add the loop counters as msg.loopstate</label>
142
+ </div>
143
+ <div class="form-row">
144
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
145
+ <input type="text" id="node-input-name" placeholder="Name">
146
+ </div>
147
+ </script>
148
+
149
+ <script type="text/html" data-help-name="looptimer-advanced">
150
+ <p>Sends the <code>msg</code> through the first output, then repeats it once per the configured duration until it is stopped, the maximum number of loops is reached or the maximum timeout expires.</p>
151
+
152
+ <h3>Inputs</h3>
153
+ <dl class="message-properties">
154
+ <dt>payload <span class="property-type">string</span></dt>
155
+ <dd><code>stop</code> / <code>STOP</code> stops the loop. If enabled in the node, <code>pause</code>, <code>resume</code> and <code>status</code> do what they say. Any other message (re)starts the loop.</dd>
156
+ <dt class="optional">loop.command <span class="property-type">string</span></dt>
157
+ <dd><code>stop</code>, <code>pause</code>, <code>resume</code> or <code>status</code>. Works regardless of the payload and of the "Pause / resume / status with payload" setting.</dd>
158
+ <dt class="optional">loop.all <span class="property-type">boolean</span></dt>
159
+ <dd>With <code>true</code> a command applies to all loops of the node (see "New message" below).</dd>
160
+ <dt class="optional">loop.duration, loop.units <span class="property-type">number, string</span></dt>
161
+ <dd>Override the loop interval. Units: <code>Millisecond</code>, <code>Second</code>, <code>Minute</code>, <code>Hour</code>, <code>Day</code>.</dd>
162
+ <dt class="optional">loop.maxtimeout, loop.maxtimeoutunits <span class="property-type">number, string</span></dt>
163
+ <dd>Override the maximum timeout.</dd>
164
+ <dt class="optional">loop.maxloops <span class="property-type">number</span></dt>
165
+ <dd>Override the maximum number of loops.</dd>
166
+ <dt class="optional">loop.totalcount, loop.sendfirst, loop.loopstate <span class="property-type">boolean</span></dt>
167
+ <dd>Override "Max Loops counts the first message too", "Send the first message immediately" and "Add the loop counters".</dd>
168
+ <dt class="optional">loop.pausetimeout, loop.pausetimeoutunits, loop.pauseaction <span class="property-type">number, string, string</span></dt>
169
+ <dd>Override the pause timeout and what happens after it (<code>resume</code> or <code>stop</code>).</dd>
170
+ </dl>
171
+
172
+ <h3>Outputs</h3>
173
+ <ol class="node-ports">
174
+ <li>The original message, then once per loop. After a pause, the first message carries <code>msg.pauseduration</code> (milliseconds spent paused). With "Add the loop counters" every message carries <code>msg.loopstate</code>.</li>
175
+ <li>Status messages with payload <code>stopped</code>, <code>max loops reached</code>, <code>timeout</code>, <code>paused</code>, <code>resumed</code> or <code>status</code>. After an automatic resume or stop <code>msg.reason</code> is <code>pause timeout</code>.</li>
176
+ </ol>
177
+
178
+ <h3>msg.loopstate</h3>
179
+ <p><code>state</code> (<code>running</code>, <code>paused</code>, <code>stopped</code>, <code>finished</code> or <code>idle</code>), <code>count</code> (loop messages sent, the first one not included), <code>sent</code> (all messages), <code>max</code> and <code>remaining</code> (<code>null</code> when unlimited), <code>elapsed</code> (milliseconds the loop has been running, the time paused not included) and <code>interval</code> (milliseconds).</p>
180
+
181
+ <h3>Details</h3>
182
+ <p><b>Counting.</b> By default the first message is not part of the loop, so Max Loops <code>5</code> gives <code>6</code> messages. Tick "Max Loops counts the first message too" to get exactly the configured number: <code>1</code> is a single message, <code>0</code> sends nothing and <code>-1</code> is unlimited. Without "Send the first message immediately" the first message goes out after the first interval, so Max Loops <code>5</code> gives <code>5</code> messages.</p>
183
+ <p><b>New message.</b> By default a new message restarts the loop. "is ignored while a loop runs" drops it (also its settings) until the loop is over. "starts a separate loop per msg.topic" runs one loop per topic, <code>stop</code>, <code>pause</code>, <code>resume</code> and <code>status</code> then apply to the loop of the topic of the command, or to all loops with <code>msg.loop.all = true</code>.</p>
184
+ <p><b>Pause.</b> Pausing freezes the remaining time of the current interval, the loop counter and the timeout budget. With a pause timeout the loop is resumed or stopped automatically after that time.</p>
185
+ <p><b>Typed settings.</b> Loop Every, Max Loops and Max Timeout can be read from <code>msg.</code>, <code>flow.</code>, <code>global.</code> or <code>env</code> instead of being a fixed number.</p>
186
+ <p>Overrides in <code>msg.loop</code> stay in effect for the following messages.</p>
187
+ </script>