node-red-contrib-looptimer-advanced 1.1.0 → 1.1.1
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.
- package/README.md +2 -2
- package/looptimer_advanced.html +2 -2
- package/looptimer_advanced.js +67 -20
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -89,7 +89,7 @@ A `status` command sends a message with payload `status` and [`msg.loopstate`](#
|
|
|
89
89
|
|
|
90
90
|
| Property | Type | Description |
|
|
91
91
|
|---|---|---|
|
|
92
|
-
| `msg.loop.duration` | number | Loop interval |
|
|
92
|
+
| `msg.loop.duration` | number | Loop interval, must be greater than 0 (intervals and timeouts longer than ~24.8 days work too) |
|
|
93
93
|
| `msg.loop.units` | string | `Millisecond`, `Second`, `Minute`, `Hour`, `Day` |
|
|
94
94
|
| `msg.loop.maxloops` | number | Maximum number of loops |
|
|
95
95
|
| `msg.loop.totalcount` | boolean | *Max Loops* counts the first message too |
|
|
@@ -150,7 +150,7 @@ The setting **New message** decides what a message does that arrives while a loo
|
|
|
150
150
|
|
|
151
151
|
## Typed settings
|
|
152
152
|
|
|
153
|
-
*Loop Every*, *Max Loops* and *Max Timeout* are typed inputs: next to a fixed number they can be read from `msg.`, `flow.`, `global.` or an environment variable (`env`). The value is read for
|
|
153
|
+
*Loop Every*, *Max Loops* and *Max Timeout* are typed inputs: next to a fixed number they can be read from `msg.`, `flow.`, `global.` or an environment variable (`env`). The value is read only for messages that start a loop (commands like `stop` do not need it), and has to be a number (the interval greater than 0), otherwise the node reports an error and does not start. Overrides in `msg.loop` are applied on top.
|
|
154
154
|
|
|
155
155
|
## Versioning
|
|
156
156
|
|
package/looptimer_advanced.html
CHANGED
|
@@ -158,7 +158,7 @@
|
|
|
158
158
|
<dt class="optional">loop.all <span class="property-type">boolean</span></dt>
|
|
159
159
|
<dd>With <code>true</code> a command applies to all loops of the node (see "New message" below).</dd>
|
|
160
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>
|
|
161
|
+
<dd>Override the loop interval, which must be greater than 0. Units: <code>Millisecond</code>, <code>Second</code>, <code>Minute</code>, <code>Hour</code>, <code>Day</code>.</dd>
|
|
162
162
|
<dt class="optional">loop.maxtimeout, loop.maxtimeoutunits <span class="property-type">number, string</span></dt>
|
|
163
163
|
<dd>Override the maximum timeout.</dd>
|
|
164
164
|
<dt class="optional">loop.maxloops <span class="property-type">number</span></dt>
|
|
@@ -182,6 +182,6 @@
|
|
|
182
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
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
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>
|
|
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. They are only read for messages that start a loop, commands like <code>stop</code> do not need them. The interval has to be greater than 0.</p>
|
|
186
186
|
<p>Overrides in <code>msg.loop</code> stay in effect for the following messages.</p>
|
|
187
187
|
</script>
|
package/looptimer_advanced.js
CHANGED
|
@@ -13,6 +13,9 @@ module.exports = function (RED) {
|
|
|
13
13
|
// Settings that can be a msg., flow., global. or env. value instead of a number (typed inputs).
|
|
14
14
|
const TYPED_FIELDS = ["duration", "maxloops", "maxtimeout"];
|
|
15
15
|
|
|
16
|
+
// setTimeout runs a delay above this after 1 ms (32-bit overflow), so longer waits are split into chunks.
|
|
17
|
+
const MAX_DELAY = 2147483647;
|
|
18
|
+
|
|
16
19
|
function toMs(unit, amount) {
|
|
17
20
|
if (!Object.prototype.hasOwnProperty.call(UNIT_MS, unit)) {
|
|
18
21
|
throw new Error(`Invalid unit: ${unit}`);
|
|
@@ -34,7 +37,9 @@ module.exports = function (RED) {
|
|
|
34
37
|
const hasTyped = TYPED_FIELDS.some((field) => types[field] !== "num");
|
|
35
38
|
|
|
36
39
|
node.units = config.units || "Second";
|
|
37
|
-
|
|
40
|
+
// The loop interval has to be a finite number > 0, anything else falls back to the default
|
|
41
|
+
const configDuration = Number(config.duration || 5);
|
|
42
|
+
node.duration = types.duration === "num" && isFinite(configDuration) && configDuration > 0 ? configDuration : 5;
|
|
38
43
|
node.maxloops = types.maxloops === "num" ? (config.maxloops || 100) : 100;
|
|
39
44
|
node.maxtimeoutunits = config.maxtimeoutunits || "Hour";
|
|
40
45
|
node.maxtimeout = types.maxtimeout === "num" ? (config.maxtimeout || 1) : 1;
|
|
@@ -49,7 +54,6 @@ module.exports = function (RED) {
|
|
|
49
54
|
node.pausetimeoutunits = Object.prototype.hasOwnProperty.call(UNIT_MS, config.pausetimeoutunits) ? config.pausetimeoutunits : "Minute";
|
|
50
55
|
node.pauseaction = config.pauseaction === "stop" ? "stop" : "resume";
|
|
51
56
|
|
|
52
|
-
if (node.duration <= 0) { node.duration = 0; }
|
|
53
57
|
if (node.maxtimeout <= 0) { node.maxtimeout = 0; }
|
|
54
58
|
|
|
55
59
|
// Runtime state of the running loops: one per node, or one per msg.topic in "pertopic" mode.
|
|
@@ -75,6 +79,16 @@ module.exports = function (RED) {
|
|
|
75
79
|
}
|
|
76
80
|
}
|
|
77
81
|
|
|
82
|
+
// Calls fn at the absolute time due, also when that is more than MAX_DELAY away. The timer is run[prop].
|
|
83
|
+
function armAt(run, prop, due, fn) {
|
|
84
|
+
const left = due - Date.now();
|
|
85
|
+
if (left > MAX_DELAY) {
|
|
86
|
+
run[prop] = setTimeout(() => armAt(run, prop, due, fn), MAX_DELAY);
|
|
87
|
+
} else {
|
|
88
|
+
run[prop] = setTimeout(fn, Math.max(0, left));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
78
92
|
function clearTimers(run) {
|
|
79
93
|
if (run.timer) { clearTimeout(run.timer); run.timer = null; }
|
|
80
94
|
if (run.pauseTimer) { clearTimeout(run.pauseTimer); run.pauseTimer = null; }
|
|
@@ -126,7 +140,7 @@ module.exports = function (RED) {
|
|
|
126
140
|
const deadline = run.t0 + run.maxtimeout;
|
|
127
141
|
const deadlineFirst = deadline < tickDue;
|
|
128
142
|
const due = deadlineFirst ? deadline : tickDue;
|
|
129
|
-
run
|
|
143
|
+
armAt(run, "timer", due, deadlineFirst ? () => onDeadline(run) : () => onTick(run));
|
|
130
144
|
}
|
|
131
145
|
|
|
132
146
|
function onDeadline(run) {
|
|
@@ -199,10 +213,10 @@ module.exports = function (RED) {
|
|
|
199
213
|
if (node.pausetimeout > 0) {
|
|
200
214
|
const wait = toMs(node.pausetimeoutunits, node.pausetimeout);
|
|
201
215
|
const action = node.pauseaction;
|
|
202
|
-
run
|
|
216
|
+
armAt(run, "pauseTimer", Date.now() + wait, () => {
|
|
203
217
|
run.pauseTimer = null;
|
|
204
218
|
if (action === "stop") { stop(run.msg, run, "pause timeout"); } else { resume(run, run.msg, "pause timeout"); }
|
|
205
|
-
}
|
|
219
|
+
});
|
|
206
220
|
}
|
|
207
221
|
}
|
|
208
222
|
|
|
@@ -220,24 +234,30 @@ module.exports = function (RED) {
|
|
|
220
234
|
schedule(run);
|
|
221
235
|
}
|
|
222
236
|
|
|
223
|
-
// Validates
|
|
237
|
+
// Validates the msg.loop overrides and applies them only when all of them are valid.
|
|
238
|
+
// Returns an error text, or null when everything is fine.
|
|
224
239
|
function applyOverrides(over) {
|
|
240
|
+
const pending = [];
|
|
225
241
|
const checkUnit = (key, prop) => {
|
|
226
242
|
if (over[key] === undefined) { return null; }
|
|
227
243
|
if (!Object.prototype.hasOwnProperty.call(UNIT_MS, over[key])) {
|
|
228
244
|
node.status({ fill: "red", shape: "ring", text: `Invalid Attribute msg.loop.${key}` });
|
|
229
245
|
return `Invalid Attribute: msg.loop.${key}, allowed values are ${UNIT_LIST}`;
|
|
230
246
|
}
|
|
231
|
-
node[prop] = over[key];
|
|
247
|
+
pending.push(() => { node[prop] = over[key]; });
|
|
232
248
|
return null;
|
|
233
249
|
};
|
|
234
|
-
const checkNumber = (key, prop) => {
|
|
250
|
+
const checkNumber = (key, prop, positive) => {
|
|
235
251
|
if (over[key] === undefined) { return null; }
|
|
236
|
-
if (typeof over[key] !== "number" ||
|
|
252
|
+
if (typeof over[key] !== "number" || !isFinite(over[key])) {
|
|
237
253
|
node.status({ fill: "red", shape: "ring", text: `msg.loop.${key} must be a number` });
|
|
238
254
|
return `Invalid Attribute: msg.loop.${key}, value must be a number`;
|
|
239
255
|
}
|
|
240
|
-
|
|
256
|
+
if (positive && over[key] <= 0) {
|
|
257
|
+
node.status({ fill: "red", shape: "ring", text: `msg.loop.${key} must be greater than 0` });
|
|
258
|
+
return `Invalid Attribute: msg.loop.${key}, value must be a positive number`;
|
|
259
|
+
}
|
|
260
|
+
pending.push(() => { node[prop] = over[key]; });
|
|
241
261
|
return null;
|
|
242
262
|
};
|
|
243
263
|
const checkBoolean = (key, prop) => {
|
|
@@ -246,7 +266,7 @@ module.exports = function (RED) {
|
|
|
246
266
|
node.status({ fill: "red", shape: "ring", text: `msg.loop.${key} must be a boolean` });
|
|
247
267
|
return `Invalid Attribute: msg.loop.${key}, value must be a boolean`;
|
|
248
268
|
}
|
|
249
|
-
node[prop] = over[key];
|
|
269
|
+
pending.push(() => { node[prop] = over[key]; });
|
|
250
270
|
return null;
|
|
251
271
|
};
|
|
252
272
|
const checkAction = () => {
|
|
@@ -255,12 +275,12 @@ module.exports = function (RED) {
|
|
|
255
275
|
node.status({ fill: "red", shape: "ring", text: "Invalid Attribute msg.loop.pauseaction" });
|
|
256
276
|
return "Invalid Attribute: msg.loop.pauseaction, allowed values are resume and stop";
|
|
257
277
|
}
|
|
258
|
-
node.pauseaction = over.pauseaction;
|
|
278
|
+
pending.push(() => { node.pauseaction = over.pauseaction; });
|
|
259
279
|
return null;
|
|
260
280
|
};
|
|
261
|
-
|
|
281
|
+
const problem = checkBoolean("totalcount", "totalcount") ||
|
|
262
282
|
checkUnit("units", "units") ||
|
|
263
|
-
checkNumber("duration", "duration") ||
|
|
283
|
+
checkNumber("duration", "duration", true) ||
|
|
264
284
|
checkUnit("maxtimeoutunits", "maxtimeoutunits") ||
|
|
265
285
|
checkNumber("maxtimeout", "maxtimeout") ||
|
|
266
286
|
checkNumber("maxloops", "maxloops") ||
|
|
@@ -269,6 +289,9 @@ module.exports = function (RED) {
|
|
|
269
289
|
checkNumber("pausetimeout", "pausetimeout") ||
|
|
270
290
|
checkUnit("pausetimeoutunits", "pausetimeoutunits") ||
|
|
271
291
|
checkAction();
|
|
292
|
+
if (problem) { return problem; }
|
|
293
|
+
pending.forEach((apply) => apply());
|
|
294
|
+
return null;
|
|
272
295
|
}
|
|
273
296
|
|
|
274
297
|
function start(msg) {
|
|
@@ -349,8 +372,13 @@ module.exports = function (RED) {
|
|
|
349
372
|
});
|
|
350
373
|
}
|
|
351
374
|
|
|
352
|
-
function
|
|
353
|
-
|
|
375
|
+
function loopOf(msg) {
|
|
376
|
+
return msg.loop !== null && typeof msg.loop === "object" ? msg.loop : undefined;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// What a message asks for: start, stop, pause, resume or status
|
|
380
|
+
function actionOf(msg) {
|
|
381
|
+
const over = loopOf(msg);
|
|
354
382
|
const command = over && typeof over.command === "string" ? over.command.toLowerCase() : null;
|
|
355
383
|
const byPayload = (word) => node.pausecontrol && isCommand(msg.payload, word);
|
|
356
384
|
|
|
@@ -364,9 +392,19 @@ module.exports = function (RED) {
|
|
|
364
392
|
} else if (command === "stop" || isCommand(msg.payload, "stop")) {
|
|
365
393
|
action = "stop";
|
|
366
394
|
}
|
|
395
|
+
return action;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// While a loop runs the new messages are not wanted, not even their settings
|
|
399
|
+
function isIgnored(msg, action) {
|
|
400
|
+
return action === "start" && node.concurrency === "ignore" && runs.has(keyOf(msg));
|
|
401
|
+
}
|
|
367
402
|
|
|
368
|
-
|
|
369
|
-
|
|
403
|
+
function handle(msg, done) {
|
|
404
|
+
const over = loopOf(msg);
|
|
405
|
+
const action = actionOf(msg);
|
|
406
|
+
|
|
407
|
+
if (isIgnored(msg, action)) {
|
|
370
408
|
done();
|
|
371
409
|
return;
|
|
372
410
|
}
|
|
@@ -420,7 +458,12 @@ module.exports = function (RED) {
|
|
|
420
458
|
reject(new Error(`Invalid value for ${field} (${types[field]}.${config[field]}), value must be a number`));
|
|
421
459
|
return;
|
|
422
460
|
}
|
|
423
|
-
|
|
461
|
+
if (field === "duration" && number <= 0) {
|
|
462
|
+
node.status({ fill: "red", shape: "ring", text: "duration must be greater than 0" });
|
|
463
|
+
reject(new Error(`Invalid value for ${field} (${types[field]}.${config[field]}), value must be a positive number`));
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
node[field] = field === "maxtimeout" ? Math.max(number, 0) : number;
|
|
424
467
|
resolve();
|
|
425
468
|
});
|
|
426
469
|
})));
|
|
@@ -436,8 +479,12 @@ module.exports = function (RED) {
|
|
|
436
479
|
handle(msg, done);
|
|
437
480
|
return;
|
|
438
481
|
}
|
|
482
|
+
// The settings are only read for a message that starts a loop, commands and ignored messages do not need them
|
|
439
483
|
queue = queue
|
|
440
|
-
.then(() =>
|
|
484
|
+
.then(() => {
|
|
485
|
+
const action = actionOf(msg);
|
|
486
|
+
return action === "start" && !isIgnored(msg, action) ? resolveTyped(msg) : null;
|
|
487
|
+
})
|
|
441
488
|
.then(() => handle(msg, done), (err) => {
|
|
442
489
|
node.error(err.message, msg);
|
|
443
490
|
done();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-looptimer-advanced",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Sends the msg through the first output, then continues to do so in a loop, once per the duration specified, until a payload of 'stop' or 'STOP' is received. Supports override with messages!",
|
|
5
5
|
"main": "looptimer_advanced.js",
|
|
6
6
|
"scripts": {
|