node-red-contrib-looptimer-advanced 1.0.0 โ 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.
- package/README.md +61 -9
- package/looptimer_advanced.html +187 -100
- package/looptimer_advanced.js +293 -105
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,12 @@
|
|
|
19
19
|
- ๐ **Repeat a message** once per interval (milliseconds to days) with an optional maximum loop count and maximum run time.
|
|
20
20
|
- ๐ **Stop** with a payload, **โธ pause and โถ resume** without losing the loop counter or the timeout budget.
|
|
21
21
|
- ๐งฉ **Reconfigure per message** with `msg.loop`: interval, limits and commands, no redeploy needed.
|
|
22
|
-
- ๐ฏ **Exact counts**: optionally make *Max Loops* mean the total number of messages, including a single call.
|
|
22
|
+
- ๐ฏ **Exact counts**: optionally make *Max Loops* mean the total number of messages, including a single call, and optionally skip the immediate first message.
|
|
23
|
+
- ๐ **Loop counters**: `msg.loopstate` with the count, the remaining loops and the elapsed time, and a `status` command to ask for them.
|
|
24
|
+
- ๐งต **One loop per topic**: run a separate loop for every `msg.topic` in one node, or ignore new messages while a loop runs.
|
|
25
|
+
- โฒ๏ธ **Pause timeout**: resume or stop automatically when a pause lasts too long.
|
|
26
|
+
- ๐ **Typed settings**: read the interval and the limits from `msg.`, `flow.`, `global.` or `env`.
|
|
27
|
+
- ๐ฏ **No drift**: every tick is due at a fixed time after the start, also after a pause.
|
|
23
28
|
- ๐งช **Tested and dependency free**: plain timers, an automated test suite and CI on Node.js 18 โ 24.
|
|
24
29
|
- ๐ **Backwards compatible**: flows built with 0.0.x keep working unchanged, every new feature is opt-in.
|
|
25
30
|
|
|
@@ -57,20 +62,29 @@ stateDiagram-v2
|
|
|
57
62
|
|
|
58
63
|
| Output | Sends |
|
|
59
64
|
|---|---|
|
|
60
|
-
| **1** | The original message immediately, then one copy per loop. The first copy after a resume carries `msg.pauseduration` (ms). |
|
|
61
|
-
| **2** | Status messages with these payloads: `stopped`, `max loops reached`, `timeout`, `paused`, `resumed`. |
|
|
65
|
+
| **1** | The original message immediately (unless switched off), then one copy per loop. The first copy after a resume carries `msg.pauseduration` (ms). With *Add the loop counters* every copy carries `msg.loopstate`. |
|
|
66
|
+
| **2** | Status messages with these payloads: `stopped`, `max loops reached`, `timeout`, `paused`, `resumed`, `status`. After an automatic resume or stop `msg.reason` is `pause timeout`. |
|
|
62
67
|
|
|
63
68
|
## Controlling the loop
|
|
64
69
|
|
|
65
70
|
| How | What it does |
|
|
66
71
|
|---|---|
|
|
67
72
|
| `msg.payload = "stop"` (or `"STOP"`) | Stops the loop |
|
|
68
|
-
| `msg.payload = "pause"` / `"resume"` | Pauses / resumes (enable *Pause / resume with payload* in the node) |
|
|
69
|
-
| `msg.loop.command = "stop"`, `"pause"` or `"
|
|
70
|
-
|
|
|
73
|
+
| `msg.payload = "pause"` / `"resume"` / `"status"` | Pauses / resumes / reports the state (enable *Pause / resume / status with payload* in the node) |
|
|
74
|
+
| `msg.loop.command = "stop"`, `"pause"`, `"resume"` or `"status"` | Same, but always available and independent of the payload |
|
|
75
|
+
| `msg.loop.all = true` | With a command: applies to all loops of the node (see [one loop per topic](#new-messages-while-a-loop-runs)) |
|
|
76
|
+
| Any other message | Starts the loop, or restarts it (see [New messages while a loop runs](#new-messages-while-a-loop-runs)) |
|
|
71
77
|
|
|
72
78
|
Pausing freezes the remaining time of the current interval, the loop counter and the timeout budget. Resuming continues from exactly there. Stopping while paused works too.
|
|
73
79
|
|
|
80
|
+
### Pause timeout
|
|
81
|
+
|
|
82
|
+
Set *Pause Timeout* (and *After Timeout*: resume or stop the loop) to get the loop going again, or end it, when a pause lasts too long. Output 2 then sends `resumed` / `stopped` with `msg.reason = "pause timeout"`. Resuming or stopping by hand cancels the timeout.
|
|
83
|
+
|
|
84
|
+
### Status command
|
|
85
|
+
|
|
86
|
+
A `status` command sends a message with payload `status` and [`msg.loopstate`](#loop-counters-msgloopstate) on output 2, for example to show the progress on a dashboard. When no loop is running `msg.loopstate.state` is `idle`.
|
|
87
|
+
|
|
74
88
|
## Overriding settings with `msg.loop`
|
|
75
89
|
|
|
76
90
|
| Property | Type | Description |
|
|
@@ -81,7 +95,12 @@ Pausing freezes the remaining time of the current interval, the loop counter and
|
|
|
81
95
|
| `msg.loop.totalcount` | boolean | *Max Loops* counts the first message too |
|
|
82
96
|
| `msg.loop.maxtimeout` | number | Maximum run time of the loop |
|
|
83
97
|
| `msg.loop.maxtimeoutunits` | string | Same units as above |
|
|
84
|
-
| `msg.loop.
|
|
98
|
+
| `msg.loop.sendfirst` | boolean | Send the first message immediately |
|
|
99
|
+
| `msg.loop.loopstate` | boolean | Add `msg.loopstate` to the messages |
|
|
100
|
+
| `msg.loop.pausetimeout` | number | Pause timeout (with `msg.loop.pausetimeoutunits`) |
|
|
101
|
+
| `msg.loop.pauseaction` | string | `resume` or `stop`, what happens after the pause timeout |
|
|
102
|
+
| `msg.loop.command` | string | `stop`, `pause`, `resume` or `status` |
|
|
103
|
+
| `msg.loop.all` | boolean | Apply a command to all loops |
|
|
85
104
|
|
|
86
105
|
```js
|
|
87
106
|
// Function node: loop every 30 seconds, at most 10 times
|
|
@@ -104,6 +123,35 @@ Tick **Max Loops counts the first message too** (or send `msg.loop.totalcount =
|
|
|
104
123
|
| `1` | 2 messages | **1 message** (a single call) |
|
|
105
124
|
| `5` | 6 messages | **5 messages** |
|
|
106
125
|
|
|
126
|
+
Without **Send the first message immediately** (or `msg.loop.sendfirst = false`) nothing goes out at the start, the first message follows after the first interval. *Max Loops* `5` then gives **5** messages, and with *counts the first message too* it is the same number.
|
|
127
|
+
|
|
128
|
+
## Loop counters (`msg.loopstate`)
|
|
129
|
+
|
|
130
|
+
Tick **Add the loop counters as msg.loopstate** (or send `msg.loop.loopstate = true`) and every message carries:
|
|
131
|
+
|
|
132
|
+
| Property | Meaning |
|
|
133
|
+
|---|---|
|
|
134
|
+
| `state` | `running`, `paused`, `stopped`, `finished` or `idle` |
|
|
135
|
+
| `count` | Loop messages sent, the first message not included |
|
|
136
|
+
| `sent` | All messages sent |
|
|
137
|
+
| `max`, `remaining` | Limit and loops left, `null` when unlimited |
|
|
138
|
+
| `elapsed` | Milliseconds the loop has been running, the time paused not included |
|
|
139
|
+
| `interval` | Loop interval in milliseconds |
|
|
140
|
+
|
|
141
|
+
## New messages while a loop runs
|
|
142
|
+
|
|
143
|
+
The setting **New message** decides what a message does that arrives while a loop is running:
|
|
144
|
+
|
|
145
|
+
| Setting | Behavior |
|
|
146
|
+
|---|---|
|
|
147
|
+
| **restarts the loop** (default) | The loop starts again from the beginning, as it always did |
|
|
148
|
+
| **is ignored while a loop runs** | The message and its `msg.loop` settings are dropped until the loop is over. `stop`, `pause`, `resume` and `status` still work |
|
|
149
|
+
| **starts a separate loop per msg.topic** | Every topic has its own loop, a new message restarts only the loop of its topic. Commands apply to the loop of the topic of the command message, or to all loops with `msg.loop.all = true`. The node status shows the number of loops |
|
|
150
|
+
|
|
151
|
+
## Typed settings
|
|
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 every message that starts a loop, and has to be a number, otherwise the node reports an error and does not start. Overrides in `msg.loop` are applied on top.
|
|
154
|
+
|
|
107
155
|
## Versioning
|
|
108
156
|
|
|
109
157
|
Since **1.0.0** the package follows [Semantic Versioning](https://semver.org/) and keeps a [changelog](CHANGELOG.md) in the [Keep a Changelog](https://keepachangelog.com/) format.
|
|
@@ -116,6 +164,10 @@ Since **1.0.0** the package follows [Semantic Versioning](https://semver.org/) a
|
|
|
116
164
|
|
|
117
165
|
Releases are tagged `vX.Y.Z` on GitHub. The old `0.0.x` numbering was one increment per change and carried no compatibility promise. `1.0.0` marks the first release with a stable, tested public interface: the node's properties, `msg.loop` and the output payloads.
|
|
118
166
|
|
|
167
|
+
## Upgrading
|
|
168
|
+
|
|
169
|
+
**From 1.0.x to 1.1:** nothing to change, every new setting is off by default and existing nodes keep their behavior. The only visible difference is that the ticks no longer drift: a long loop now sends its messages at fixed times after the start instead of a little later every time.
|
|
170
|
+
|
|
119
171
|
## Upgrading from 0.0.x
|
|
120
172
|
|
|
121
173
|
Nothing to change in your flows. What is different:
|
|
@@ -124,7 +176,7 @@ Nothing to change in your flows. What is different:
|
|
|
124
176
|
- A loop that hits its maximum timeout now always reports `timeout` on output 2 (before it sometimes ended silently). The old single-call workaround (`maxloops: 0` with a tiny `maxtimeout`) still works.
|
|
125
177
|
- Invalid `msg.loop` values are reported through normal error handling, so a **Catch** node sees them.
|
|
126
178
|
- Minimum versions: Node-RED 3.0 and Node.js 18.
|
|
127
|
-
- A payload of `pause` or `
|
|
179
|
+
- A payload of `pause`, `resume` or `status` is only treated as a command when the option is ticked. Existing nodes have it **off**, so such payloads loop like any other value.
|
|
128
180
|
|
|
129
181
|
## Contributing
|
|
130
182
|
|
|
@@ -139,4 +191,4 @@ npm test
|
|
|
139
191
|
|
|
140
192
|
## Credits and license
|
|
141
193
|
|
|
142
|
-
Based on **node-red-contrib-looptimer** and **node-red-contrib-looptimer2**, with the addition of per-message overrides, pause/resume and exact counting. Released under the [Apache-2.0 license](LICENSE).
|
|
194
|
+
Based on **node-red-contrib-looptimer** and **node-red-contrib-looptimer2**, with the addition of per-message overrides, pause/resume, loop counters, one loop per topic, typed settings and exact counting. Released under the [Apache-2.0 license](LICENSE).
|
package/looptimer_advanced.html
CHANGED
|
@@ -1,100 +1,187 @@
|
|
|
1
|
-
<script type="text/javascript">
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
</
|
|
70
|
-
|
|
71
|
-
<
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
<
|
|
82
|
-
<
|
|
83
|
-
<
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
<
|
|
87
|
-
<
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
<
|
|
91
|
-
|
|
92
|
-
<
|
|
93
|
-
<
|
|
94
|
-
</
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
</
|
|
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> </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> </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> </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> </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>
|
package/looptimer_advanced.js
CHANGED
|
@@ -10,6 +10,9 @@ module.exports = function (RED) {
|
|
|
10
10
|
};
|
|
11
11
|
const UNIT_LIST = Object.keys(UNIT_MS).join(", ");
|
|
12
12
|
|
|
13
|
+
// Settings that can be a msg., flow., global. or env. value instead of a number (typed inputs).
|
|
14
|
+
const TYPED_FIELDS = ["duration", "maxloops", "maxtimeout"];
|
|
15
|
+
|
|
13
16
|
function toMs(unit, amount) {
|
|
14
17
|
if (!Object.prototype.hasOwnProperty.call(UNIT_MS, unit)) {
|
|
15
18
|
throw new Error(`Invalid unit: ${unit}`);
|
|
@@ -25,77 +28,134 @@ module.exports = function (RED) {
|
|
|
25
28
|
RED.nodes.createNode(this, config);
|
|
26
29
|
const node = this;
|
|
27
30
|
|
|
31
|
+
// Which settings are not plain numbers, the value then is the name of the msg/flow/global/env property.
|
|
32
|
+
const types = {};
|
|
33
|
+
TYPED_FIELDS.forEach((field) => { types[field] = config[field + "Type"] || "num"; });
|
|
34
|
+
const hasTyped = TYPED_FIELDS.some((field) => types[field] !== "num");
|
|
35
|
+
|
|
28
36
|
node.units = config.units || "Second";
|
|
29
|
-
node.duration = config.duration || 5;
|
|
30
|
-
node.maxloops = config.maxloops || 100;
|
|
37
|
+
node.duration = types.duration === "num" ? (config.duration || 5) : 5;
|
|
38
|
+
node.maxloops = types.maxloops === "num" ? (config.maxloops || 100) : 100;
|
|
31
39
|
node.maxtimeoutunits = config.maxtimeoutunits || "Hour";
|
|
32
|
-
node.maxtimeout = config.maxtimeout || 1;
|
|
33
|
-
|
|
40
|
+
node.maxtimeout = types.maxtimeout === "num" ? (config.maxtimeout || 1) : 1;
|
|
41
|
+
|
|
42
|
+
// Opt-in features. Flows saved by 0.0.x / 1.0.x do not carry these properties, so they keep the old behaviour.
|
|
34
43
|
node.totalcount = config.totalcount === true || config.totalcount === "true";
|
|
35
44
|
node.pausecontrol = config.pausecontrol === true || config.pausecontrol === "true";
|
|
45
|
+
node.sendfirst = !(config.sendfirst === false || config.sendfirst === "false");
|
|
46
|
+
node.addstate = config.addstate === true || config.addstate === "true";
|
|
47
|
+
node.concurrency = config.concurrency === "ignore" || config.concurrency === "pertopic" ? config.concurrency : "restart";
|
|
48
|
+
node.pausetimeout = Number(config.pausetimeout) > 0 ? Number(config.pausetimeout) : 0;
|
|
49
|
+
node.pausetimeoutunits = Object.prototype.hasOwnProperty.call(UNIT_MS, config.pausetimeoutunits) ? config.pausetimeoutunits : "Minute";
|
|
50
|
+
node.pauseaction = config.pauseaction === "stop" ? "stop" : "resume";
|
|
36
51
|
|
|
37
52
|
if (node.duration <= 0) { node.duration = 0; }
|
|
38
53
|
if (node.maxtimeout <= 0) { node.maxtimeout = 0; }
|
|
39
54
|
|
|
40
|
-
// Runtime state of the running
|
|
41
|
-
|
|
55
|
+
// Runtime state of the running loops: one per node, or one per msg.topic in "pertopic" mode.
|
|
56
|
+
const runs = new Map();
|
|
57
|
+
|
|
58
|
+
function keyOf(msg) {
|
|
59
|
+
return node.concurrency === "pertopic" ? String(msg.topic) : "";
|
|
60
|
+
}
|
|
42
61
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
62
|
+
// In "pertopic" mode the status shows all loops, not the last event.
|
|
63
|
+
function setStatus(fill, shape, text) {
|
|
64
|
+
if (node.concurrency === "pertopic" && runs.size > 0) {
|
|
65
|
+
let paused = 0;
|
|
66
|
+
runs.forEach((r) => { if (r.paused) { paused++; } });
|
|
67
|
+
const running = runs.size - paused;
|
|
68
|
+
node.status({
|
|
69
|
+
fill: running > 0 ? "green" : "yellow",
|
|
70
|
+
shape: "dot",
|
|
71
|
+
text: paused > 0 ? `${running} running, ${paused} paused` : `${running} running`
|
|
72
|
+
});
|
|
73
|
+
} else {
|
|
74
|
+
node.status({ fill: fill, shape: shape, text: text });
|
|
47
75
|
}
|
|
48
76
|
}
|
|
49
77
|
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
run = null;
|
|
78
|
+
function clearTimers(run) {
|
|
79
|
+
if (run.timer) { clearTimeout(run.timer); run.timer = null; }
|
|
80
|
+
if (run.pauseTimer) { clearTimeout(run.pauseTimer); run.pauseTimer = null; }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function endLoop(run) {
|
|
84
|
+
clearTimers(run);
|
|
85
|
+
if (runs.get(run.key) === run) { runs.delete(run.key); }
|
|
53
86
|
}
|
|
54
87
|
|
|
55
|
-
|
|
56
|
-
|
|
88
|
+
// Counters of a loop, sent in msg.loopstate
|
|
89
|
+
function stateOf(run, state) {
|
|
90
|
+
const now = run.paused ? run.pausedAt : Date.now();
|
|
91
|
+
return {
|
|
92
|
+
state: state || (run.paused ? "paused" : "running"),
|
|
93
|
+
count: run.count,
|
|
94
|
+
sent: run.sent,
|
|
95
|
+
max: run.limit > 0 ? run.limit : null,
|
|
96
|
+
remaining: run.limit > 0 ? Math.max(0, run.limit - run.count) : null,
|
|
97
|
+
elapsed: now - run.t0,
|
|
98
|
+
interval: run.duration
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Copy of the message of the loop, with the loop counters when they are switched on
|
|
103
|
+
function copyOf(run, state) {
|
|
104
|
+
const m = RED.util.cloneMessage(run.msg);
|
|
105
|
+
if (run.addstate) { m.loopstate = stateOf(run, state); }
|
|
106
|
+
return m;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Status message for the second output. Based on the message of the loop, or on the incoming one when there is no loop.
|
|
110
|
+
function controlMsg(base, payload, run, state, reason) {
|
|
111
|
+
const m = RED.util.cloneMessage(base);
|
|
57
112
|
m.payload = payload;
|
|
58
|
-
|
|
113
|
+
if (run && run.addstate) { m.loopstate = stateOf(run, state); }
|
|
114
|
+
if (reason) { m.reason = reason; }
|
|
115
|
+
return m;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function sendControl(base, payload, run, state, reason) {
|
|
119
|
+
node.send([null, controlMsg(base, payload, run, state, reason)]);
|
|
59
120
|
}
|
|
60
121
|
|
|
61
122
|
// Arms the single timer: either the next loop tick or, when it comes first, the overall timeout.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const deadlineFirst = untilDeadline < untilTick;
|
|
70
|
-
const delay = Math.max(0, Math.min(untilTick, untilDeadline));
|
|
71
|
-
run.tickDue = now + (deadlineFirst ? untilTick : delay);
|
|
72
|
-
run.deadline = now + untilDeadline;
|
|
73
|
-
run.timer = setTimeout(deadlineFirst ? onDeadline : onTick, delay);
|
|
123
|
+
// Every tick is due at a fixed time after the start, so the loop does not drift.
|
|
124
|
+
function schedule(run) {
|
|
125
|
+
const tickDue = run.origin + (run.count + 1) * run.duration;
|
|
126
|
+
const deadline = run.t0 + run.maxtimeout;
|
|
127
|
+
const deadlineFirst = deadline < tickDue;
|
|
128
|
+
const due = deadlineFirst ? deadline : tickDue;
|
|
129
|
+
run.timer = setTimeout(deadlineFirst ? () => onDeadline(run) : () => onTick(run), Math.max(0, due - Date.now()));
|
|
74
130
|
}
|
|
75
131
|
|
|
76
|
-
function onDeadline() {
|
|
132
|
+
function onDeadline(run) {
|
|
77
133
|
run.timer = null;
|
|
78
|
-
|
|
79
|
-
sendControl(run.msg, "timeout");
|
|
80
|
-
endLoop();
|
|
134
|
+
setStatus("red", "ring", "timeout reached");
|
|
135
|
+
sendControl(run.msg, "timeout", run, "finished");
|
|
136
|
+
endLoop(run);
|
|
81
137
|
}
|
|
82
138
|
|
|
83
|
-
function onTick() {
|
|
139
|
+
function onTick(run) {
|
|
84
140
|
run.timer = null;
|
|
141
|
+
const now = Date.now();
|
|
142
|
+
const lateness = now - (run.origin + (run.count + 1) * run.duration);
|
|
85
143
|
run.count++;
|
|
144
|
+
run.sent++;
|
|
86
145
|
const count = run.count;
|
|
87
|
-
|
|
146
|
+
// After a long stall (e.g. a blocked event loop) the loop carries on from now instead of firing a burst
|
|
147
|
+
if (lateness > run.duration) { run.origin = now - count * run.duration; }
|
|
148
|
+
|
|
149
|
+
const cp = copyOf(run);
|
|
88
150
|
if (run.pausedFor) {
|
|
89
151
|
cp.pauseduration = run.pausedFor;
|
|
90
152
|
run.pausedFor = 0;
|
|
91
153
|
}
|
|
92
154
|
|
|
93
155
|
if (run.limit > 0 && count >= run.limit) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
node.status({ fill: "red", shape: "ring", text: "max loops reached" });
|
|
98
|
-
endLoop();
|
|
156
|
+
node.send([cp, controlMsg(run.msg, "max loops reached", run, "finished")]);
|
|
157
|
+
setStatus("red", "ring", "max loops reached");
|
|
158
|
+
endLoop(run);
|
|
99
159
|
return;
|
|
100
160
|
}
|
|
101
161
|
|
|
@@ -103,47 +163,61 @@ module.exports = function (RED) {
|
|
|
103
163
|
node.send([cp, null]);
|
|
104
164
|
if ((count + 1) * run.duration > run.maxtimeout) {
|
|
105
165
|
// Kept from 0.0.x: the last loop message is repeated together with the "timeout" message.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
node.status({ fill: "red", shape: "ring", text: "timeout reached" });
|
|
110
|
-
endLoop();
|
|
166
|
+
node.send([cp, controlMsg(run.msg, "timeout", run, "finished")]);
|
|
167
|
+
setStatus("red", "ring", "timeout reached");
|
|
168
|
+
endLoop(run);
|
|
111
169
|
return;
|
|
112
170
|
}
|
|
113
|
-
|
|
114
|
-
schedule();
|
|
171
|
+
setStatus("green", "dot", "running");
|
|
172
|
+
schedule(run);
|
|
115
173
|
} else {
|
|
116
|
-
|
|
117
|
-
sendControl(run.msg, "timeout");
|
|
118
|
-
endLoop();
|
|
174
|
+
setStatus("red", "ring", "timeout reached");
|
|
175
|
+
sendControl(run.msg, "timeout", run, "finished");
|
|
176
|
+
endLoop(run);
|
|
119
177
|
}
|
|
120
178
|
}
|
|
121
179
|
|
|
122
|
-
function stop(msg) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
180
|
+
function stop(msg, run, reason) {
|
|
181
|
+
let state = null;
|
|
182
|
+
if (run) {
|
|
183
|
+
state = stateOf(run, "stopped");
|
|
184
|
+
endLoop(run);
|
|
185
|
+
}
|
|
186
|
+
setStatus("red", "ring", "stopped");
|
|
187
|
+
const m = controlMsg(msg, "stopped", null, null, reason);
|
|
188
|
+
if (run && run.addstate) { m.loopstate = state; }
|
|
189
|
+
node.send([null, m]);
|
|
126
190
|
}
|
|
127
191
|
|
|
128
|
-
function pause(msg) {
|
|
129
|
-
if (
|
|
130
|
-
|
|
131
|
-
clearTimer();
|
|
192
|
+
function pause(run, msg) {
|
|
193
|
+
if (run.paused) { return; }
|
|
194
|
+
clearTimers(run);
|
|
132
195
|
run.paused = true;
|
|
133
|
-
run.pausedAt = now;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
node.
|
|
137
|
-
|
|
196
|
+
run.pausedAt = Date.now();
|
|
197
|
+
setStatus("yellow", "ring", "paused");
|
|
198
|
+
sendControl(msg, "paused", run);
|
|
199
|
+
if (node.pausetimeout > 0) {
|
|
200
|
+
const wait = toMs(node.pausetimeoutunits, node.pausetimeout);
|
|
201
|
+
const action = node.pauseaction;
|
|
202
|
+
run.pauseTimer = setTimeout(() => {
|
|
203
|
+
run.pauseTimer = null;
|
|
204
|
+
if (action === "stop") { stop(run.msg, run, "pause timeout"); } else { resume(run, run.msg, "pause timeout"); }
|
|
205
|
+
}, wait);
|
|
206
|
+
}
|
|
138
207
|
}
|
|
139
208
|
|
|
140
|
-
function resume(msg) {
|
|
141
|
-
if (!run
|
|
209
|
+
function resume(run, msg, reason) {
|
|
210
|
+
if (!run.paused) { return; }
|
|
211
|
+
if (run.pauseTimer) { clearTimeout(run.pauseTimer); run.pauseTimer = null; }
|
|
212
|
+
// The time spent paused does not count: the ticks and the timeout continue where they were left
|
|
213
|
+
const spent = Date.now() - run.pausedAt;
|
|
214
|
+
run.origin += spent;
|
|
215
|
+
run.t0 += spent;
|
|
216
|
+
run.pausedFor += spent;
|
|
142
217
|
run.paused = false;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
schedule();
|
|
218
|
+
setStatus("green", "dot", "running");
|
|
219
|
+
sendControl(msg, "resumed", run, null, reason);
|
|
220
|
+
schedule(run);
|
|
147
221
|
}
|
|
148
222
|
|
|
149
223
|
// Validates and applies msg.loop overrides. Returns an error text, or null when everything is fine.
|
|
@@ -166,22 +240,42 @@ module.exports = function (RED) {
|
|
|
166
240
|
node[prop] = over[key];
|
|
167
241
|
return null;
|
|
168
242
|
};
|
|
169
|
-
|
|
170
|
-
if (
|
|
171
|
-
|
|
172
|
-
|
|
243
|
+
const checkBoolean = (key, prop) => {
|
|
244
|
+
if (over[key] === undefined) { return null; }
|
|
245
|
+
if (typeof over[key] !== "boolean") {
|
|
246
|
+
node.status({ fill: "red", shape: "ring", text: `msg.loop.${key} must be a boolean` });
|
|
247
|
+
return `Invalid Attribute: msg.loop.${key}, value must be a boolean`;
|
|
248
|
+
}
|
|
249
|
+
node[prop] = over[key];
|
|
250
|
+
return null;
|
|
251
|
+
};
|
|
252
|
+
const checkAction = () => {
|
|
253
|
+
if (over.pauseaction === undefined) { return null; }
|
|
254
|
+
if (over.pauseaction !== "resume" && over.pauseaction !== "stop") {
|
|
255
|
+
node.status({ fill: "red", shape: "ring", text: "Invalid Attribute msg.loop.pauseaction" });
|
|
256
|
+
return "Invalid Attribute: msg.loop.pauseaction, allowed values are resume and stop";
|
|
173
257
|
}
|
|
174
|
-
node.
|
|
175
|
-
|
|
176
|
-
|
|
258
|
+
node.pauseaction = over.pauseaction;
|
|
259
|
+
return null;
|
|
260
|
+
};
|
|
261
|
+
return checkBoolean("totalcount", "totalcount") ||
|
|
262
|
+
checkUnit("units", "units") ||
|
|
177
263
|
checkNumber("duration", "duration") ||
|
|
178
264
|
checkUnit("maxtimeoutunits", "maxtimeoutunits") ||
|
|
179
265
|
checkNumber("maxtimeout", "maxtimeout") ||
|
|
180
|
-
checkNumber("maxloops", "maxloops")
|
|
266
|
+
checkNumber("maxloops", "maxloops") ||
|
|
267
|
+
checkBoolean("sendfirst", "sendfirst") ||
|
|
268
|
+
checkBoolean("loopstate", "addstate") ||
|
|
269
|
+
checkNumber("pausetimeout", "pausetimeout") ||
|
|
270
|
+
checkUnit("pausetimeoutunits", "pausetimeoutunits") ||
|
|
271
|
+
checkAction();
|
|
181
272
|
}
|
|
182
273
|
|
|
183
274
|
function start(msg) {
|
|
184
|
-
|
|
275
|
+
const key = keyOf(msg);
|
|
276
|
+
const previous = runs.get(key);
|
|
277
|
+
if (previous) { endLoop(previous); }
|
|
278
|
+
|
|
185
279
|
const duration = toMs(node.units, node.duration);
|
|
186
280
|
const maxtimeout = toMs(node.maxtimeoutunits, node.maxtimeout);
|
|
187
281
|
const maxloops = Number(node.maxloops);
|
|
@@ -190,45 +284,94 @@ module.exports = function (RED) {
|
|
|
190
284
|
let limit = maxloops;
|
|
191
285
|
if (node.totalcount) {
|
|
192
286
|
// totalcount: maxloops is the total number of messages, first one included; -1 = unlimited.
|
|
193
|
-
limit = maxloops < 0 ? 0 : maxloops - 1;
|
|
287
|
+
limit = maxloops < 0 ? 0 : (node.sendfirst ? maxloops - 1 : maxloops);
|
|
194
288
|
if (maxloops === 0) {
|
|
195
|
-
|
|
289
|
+
setStatus("red", "ring", "max loops reached");
|
|
196
290
|
sendControl(msg, "max loops reached");
|
|
197
291
|
return;
|
|
198
292
|
}
|
|
199
|
-
if (maxloops === 1) {
|
|
293
|
+
if (maxloops === 1 && node.sendfirst) {
|
|
200
294
|
node.send([RED.util.cloneMessage(msg), null]);
|
|
201
|
-
|
|
295
|
+
setStatus("red", "ring", "max loops reached");
|
|
202
296
|
sendControl(msg, "max loops reached");
|
|
203
297
|
return;
|
|
204
298
|
}
|
|
205
299
|
}
|
|
206
300
|
|
|
207
|
-
|
|
208
|
-
run = {
|
|
301
|
+
const now = Date.now();
|
|
302
|
+
const run = {
|
|
303
|
+
key: key,
|
|
209
304
|
msg: msg,
|
|
210
305
|
duration: duration,
|
|
211
306
|
maxtimeout: maxtimeout,
|
|
212
307
|
limit: limit,
|
|
308
|
+
addstate: node.addstate,
|
|
213
309
|
count: 0,
|
|
310
|
+
sent: 0,
|
|
214
311
|
timer: null,
|
|
312
|
+
pauseTimer: null,
|
|
215
313
|
paused: false,
|
|
216
314
|
pausedAt: 0,
|
|
217
315
|
pausedFor: 0,
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
remainingTick: null,
|
|
221
|
-
remainingDeadline: null
|
|
316
|
+
origin: now, // the ticks are due at origin + n * duration
|
|
317
|
+
t0: now // start of the loop, without the time spent paused; the timeout is counted from here
|
|
222
318
|
};
|
|
223
|
-
|
|
224
|
-
|
|
319
|
+
runs.set(key, run);
|
|
320
|
+
if (node.sendfirst) {
|
|
321
|
+
run.sent = 1;
|
|
322
|
+
node.send([copyOf(run), null]);
|
|
323
|
+
}
|
|
324
|
+
setStatus("green", "dot", "running");
|
|
325
|
+
schedule(run);
|
|
225
326
|
}
|
|
226
327
|
|
|
227
|
-
|
|
228
|
-
|
|
328
|
+
// Runs that a command is meant for: the one of this topic, or all of them with msg.loop.all
|
|
329
|
+
function targets(msg, over) {
|
|
330
|
+
if (over && over.all === true) { return Array.from(runs.values()); }
|
|
331
|
+
const run = runs.get(keyOf(msg));
|
|
332
|
+
return run ? [run] : [];
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function reportStatus(msg, over) {
|
|
336
|
+
const list = targets(msg, over);
|
|
337
|
+
if (list.length === 0) {
|
|
338
|
+
const m = RED.util.cloneMessage(msg);
|
|
339
|
+
m.payload = "status";
|
|
340
|
+
m.loopstate = { state: "idle" };
|
|
341
|
+
node.send([null, m]);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
list.forEach((run) => {
|
|
345
|
+
const m = RED.util.cloneMessage(run.msg);
|
|
346
|
+
m.payload = "status";
|
|
347
|
+
m.loopstate = stateOf(run);
|
|
348
|
+
node.send([null, m]);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function handle(msg, done) {
|
|
353
|
+
const over = msg.loop !== null && typeof msg.loop === "object" ? msg.loop : undefined;
|
|
354
|
+
const command = over && typeof over.command === "string" ? over.command.toLowerCase() : null;
|
|
355
|
+
const byPayload = (word) => node.pausecontrol && isCommand(msg.payload, word);
|
|
356
|
+
|
|
357
|
+
let action = "start";
|
|
358
|
+
if (command === "pause" || byPayload("pause")) {
|
|
359
|
+
action = "pause";
|
|
360
|
+
} else if (command === "resume" || byPayload("resume")) {
|
|
361
|
+
action = "resume";
|
|
362
|
+
} else if (command === "status" || byPayload("status")) {
|
|
363
|
+
action = "status";
|
|
364
|
+
} else if (command === "stop" || isCommand(msg.payload, "stop")) {
|
|
365
|
+
action = "stop";
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// While a loop runs the new messages are not wanted, not even their settings
|
|
369
|
+
if (action === "start" && node.concurrency === "ignore" && runs.has(keyOf(msg))) {
|
|
370
|
+
done();
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
229
373
|
|
|
230
|
-
|
|
231
|
-
if (over !== undefined && over !== null && typeof over === "object") {
|
|
374
|
+
if (over) {
|
|
232
375
|
const problem = applyOverrides(over);
|
|
233
376
|
if (problem) {
|
|
234
377
|
node.error(problem, msg);
|
|
@@ -237,28 +380,73 @@ module.exports = function (RED) {
|
|
|
237
380
|
}
|
|
238
381
|
}
|
|
239
382
|
|
|
240
|
-
const command = over && typeof over.command === "string" ? over.command.toLowerCase() : null;
|
|
241
|
-
const byPayload = (word) => node.pausecontrol && isCommand(msg.payload, word);
|
|
242
|
-
|
|
243
383
|
try {
|
|
244
|
-
|
|
245
|
-
pause
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
384
|
+
switch (action) {
|
|
385
|
+
case "pause":
|
|
386
|
+
targets(msg, over).forEach((run) => pause(run, msg));
|
|
387
|
+
break;
|
|
388
|
+
case "resume":
|
|
389
|
+
targets(msg, over).forEach((run) => resume(run, msg));
|
|
390
|
+
break;
|
|
391
|
+
case "status":
|
|
392
|
+
reportStatus(msg, over);
|
|
393
|
+
break;
|
|
394
|
+
case "stop": {
|
|
395
|
+
const list = targets(msg, over);
|
|
396
|
+
if (list.length === 0) {
|
|
397
|
+
stop(msg, null);
|
|
398
|
+
} else {
|
|
399
|
+
list.forEach((run) => stop(msg, run));
|
|
400
|
+
}
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
default:
|
|
404
|
+
start(msg);
|
|
252
405
|
}
|
|
253
406
|
done();
|
|
254
407
|
} catch (err) {
|
|
255
408
|
node.status({ fill: "red", shape: "ring", text: err.message });
|
|
256
409
|
done(err);
|
|
257
410
|
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Reads the settings that are msg., flow., global. or env. values
|
|
414
|
+
function resolveTyped(msg) {
|
|
415
|
+
return Promise.all(TYPED_FIELDS.filter((field) => types[field] !== "num").map((field) => new Promise((resolve, reject) => {
|
|
416
|
+
RED.util.evaluateNodeProperty(config[field], types[field], node, msg, (err, value) => {
|
|
417
|
+
const number = Number(value);
|
|
418
|
+
if (err || value === undefined || value === null || value === "" || !isFinite(number)) {
|
|
419
|
+
node.status({ fill: "red", shape: "ring", text: `${field} must be a number` });
|
|
420
|
+
reject(new Error(`Invalid value for ${field} (${types[field]}.${config[field]}), value must be a number`));
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
node[field] = field === "duration" || field === "maxtimeout" ? Math.max(number, 0) : number;
|
|
424
|
+
resolve();
|
|
425
|
+
});
|
|
426
|
+
})));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Messages are handled one after the other, also when a setting has to be read from the context first
|
|
430
|
+
let queue = Promise.resolve();
|
|
431
|
+
|
|
432
|
+
node.on("input", function (msg, send, done) {
|
|
433
|
+
done = done || function (err) { if (err) { node.error(err, msg); } };
|
|
434
|
+
|
|
435
|
+
if (!hasTyped) {
|
|
436
|
+
handle(msg, done);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
queue = queue
|
|
440
|
+
.then(() => resolveTyped(msg))
|
|
441
|
+
.then(() => handle(msg, done), (err) => {
|
|
442
|
+
node.error(err.message, msg);
|
|
443
|
+
done();
|
|
444
|
+
});
|
|
258
445
|
});
|
|
259
446
|
|
|
260
447
|
node.on("close", function () {
|
|
261
|
-
|
|
448
|
+
runs.forEach((run) => clearTimers(run));
|
|
449
|
+
runs.clear();
|
|
262
450
|
node.status({});
|
|
263
451
|
});
|
|
264
452
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-looptimer-advanced",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
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": {
|