node-red-contrib-looptimer-advanced 0.0.12 → 1.0.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/LICENSE +202 -0
- package/README.md +142 -56
- package/looptimer_advanced.html +59 -26
- package/looptimer_advanced.js +236 -163
- package/package.json +26 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,56 +1,142 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# ⏱️ Advanced Looptimer for Node-RED
|
|
4
|
+
|
|
5
|
+
**A message loop with brains: repeat a `msg` on an interval, then stop, pause or resume it, and reconfigure everything per message.**
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/node-red-contrib-looptimer-advanced)
|
|
8
|
+
[](https://www.npmjs.com/package/node-red-contrib-looptimer-advanced)
|
|
9
|
+
[](https://github.com/Haxiboy/node-red-contrib-looptimer-advanced/actions/workflows/ci.yml)
|
|
10
|
+
[](https://flows.nodered.org/node/node-red-contrib-looptimer-advanced)
|
|
11
|
+
[](LICENSE)
|
|
12
|
+
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Highlights
|
|
18
|
+
|
|
19
|
+
- 🔁 **Repeat a message** once per interval (milliseconds to days) with an optional maximum loop count and maximum run time.
|
|
20
|
+
- 🛑 **Stop** with a payload, **⏸ pause and ▶ resume** without losing the loop counter or the timeout budget.
|
|
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.
|
|
23
|
+
- 🧪 **Tested and dependency free**: plain timers, an automated test suite and CI on Node.js 18 – 24.
|
|
24
|
+
- 🔒 **Backwards compatible**: flows built with 0.0.x keep working unchanged, every new feature is opt-in.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Use **Menu → Manage palette → Install** and search for `node-red-contrib-looptimer-advanced`, or:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
cd ~/.node-red
|
|
32
|
+
npm install node-red-contrib-looptimer-advanced
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Requires **Node-RED 3.0+** and **Node.js 18+**. Older runtimes can stay on `0.0.x`.
|
|
36
|
+
|
|
37
|
+
## Quick start
|
|
38
|
+
|
|
39
|
+
An [example flow](examples/Example%20Flow.json) is included (*Import → Examples* in the editor, or paste the file). In short:
|
|
40
|
+
|
|
41
|
+
1. Wire an **inject** node into *looptimer-advanced*, set *Loop Every* to `1 Second` and *Max Loops* to `10`.
|
|
42
|
+
2. Output 1 emits your message immediately and then once per second; output 2 reports `max loops reached`.
|
|
43
|
+
3. Send a message with payload `stop` to the same node to end the loop early.
|
|
44
|
+
|
|
45
|
+
```mermaid
|
|
46
|
+
stateDiagram-v2
|
|
47
|
+
[*] --> Running: any message
|
|
48
|
+
Running --> Running: tick (output 1)
|
|
49
|
+
Running --> Paused: pause
|
|
50
|
+
Paused --> Running: resume
|
|
51
|
+
Running --> [*]: stop / max loops / timeout (output 2)
|
|
52
|
+
Paused --> [*]: stop
|
|
53
|
+
Running --> Running: new message restarts the loop
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Outputs
|
|
57
|
+
|
|
58
|
+
| Output | Sends |
|
|
59
|
+
|---|---|
|
|
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`. |
|
|
62
|
+
|
|
63
|
+
## Controlling the loop
|
|
64
|
+
|
|
65
|
+
| How | What it does |
|
|
66
|
+
|---|---|
|
|
67
|
+
| `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 `"resume"` | Same, but always available and independent of the payload |
|
|
70
|
+
| Any other message | Starts the loop, or restarts it. Two loops never run in parallel in one node |
|
|
71
|
+
|
|
72
|
+
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
|
+
|
|
74
|
+
## Overriding settings with `msg.loop`
|
|
75
|
+
|
|
76
|
+
| Property | Type | Description |
|
|
77
|
+
|---|---|---|
|
|
78
|
+
| `msg.loop.duration` | number | Loop interval |
|
|
79
|
+
| `msg.loop.units` | string | `Millisecond`, `Second`, `Minute`, `Hour`, `Day` |
|
|
80
|
+
| `msg.loop.maxloops` | number | Maximum number of loops |
|
|
81
|
+
| `msg.loop.totalcount` | boolean | *Max Loops* counts the first message too |
|
|
82
|
+
| `msg.loop.maxtimeout` | number | Maximum run time of the loop |
|
|
83
|
+
| `msg.loop.maxtimeoutunits` | string | Same units as above |
|
|
84
|
+
| `msg.loop.command` | string | `stop`, `pause` or `resume` |
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
// Function node: loop every 30 seconds, at most 10 times
|
|
88
|
+
msg.loop = { duration: 30, units: "Second", maxloops: 10 };
|
|
89
|
+
return msg;
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Overrides stay in effect for the following messages (as they always did). In a function node create the object first: `msg.loop = { maxloops: 5 };`, never `msg.loop.maxloops = 5` on its own.
|
|
93
|
+
|
|
94
|
+
## How loops are counted
|
|
95
|
+
|
|
96
|
+
By default the first message is not part of the loop, so *Max Loops* `5` gives **6** messages: 1 original + 5 loops. This keeps compatibility with `node-red-contrib-looptimer`.
|
|
97
|
+
|
|
98
|
+
Tick **Max Loops counts the first message too** (or send `msg.loop.totalcount = true`) for exact counts:
|
|
99
|
+
|
|
100
|
+
| Max Loops | Default (legacy) | Exact count |
|
|
101
|
+
|---|---|---|
|
|
102
|
+
| `-1` | loops until timeout or stop (the legacy way is `0`) | unlimited |
|
|
103
|
+
| `0` | unlimited | no message at all |
|
|
104
|
+
| `1` | 2 messages | **1 message** (a single call) |
|
|
105
|
+
| `5` | 6 messages | **5 messages** |
|
|
106
|
+
|
|
107
|
+
## Versioning
|
|
108
|
+
|
|
109
|
+
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.
|
|
110
|
+
|
|
111
|
+
| Bump | Meaning |
|
|
112
|
+
|---|---|
|
|
113
|
+
| **patch** `1.0.x` | Bug fixes only |
|
|
114
|
+
| **minor** `1.x.0` | New, backwards compatible features |
|
|
115
|
+
| **major** `x.0.0` | Breaking changes (including raising the minimum Node-RED / Node.js version), always called out in the changelog |
|
|
116
|
+
|
|
117
|
+
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
|
+
|
|
119
|
+
## Upgrading from 0.0.x
|
|
120
|
+
|
|
121
|
+
Nothing to change in your flows. What is different:
|
|
122
|
+
|
|
123
|
+
- The `loop` dependency is gone and the node runs on plain timers. A `stop` that arrived between two iterations is no longer lost, and no timer is left running after stop or redeploy.
|
|
124
|
+
- 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
|
+
- Invalid `msg.loop` values are reported through normal error handling, so a **Catch** node sees them.
|
|
126
|
+
- Minimum versions: Node-RED 3.0 and Node.js 18.
|
|
127
|
+
- A payload of `pause` or `resume` 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
|
+
|
|
129
|
+
## Contributing
|
|
130
|
+
|
|
131
|
+
Issues and pull requests are welcome.
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
git clone https://github.com/Haxiboy/node-red-contrib-looptimer-advanced.git
|
|
135
|
+
cd node-red-contrib-looptimer-advanced
|
|
136
|
+
npm install
|
|
137
|
+
npm test
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Credits and license
|
|
141
|
+
|
|
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).
|
package/looptimer_advanced.html
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
<script type="text/javascript">
|
|
2
|
-
RED.nodes.registerType('looptimer-advanced',{
|
|
2
|
+
RED.nodes.registerType('looptimer-advanced', {
|
|
3
3
|
category: 'function',
|
|
4
|
-
color:"#33B2FF",
|
|
4
|
+
color: "#33B2FF",
|
|
5
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
|
-
|
|
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
|
+
totalcount: { value: false },
|
|
12
|
+
pausecontrol: { value: true },
|
|
13
|
+
name: { value: "" }
|
|
12
14
|
},
|
|
13
|
-
inputs:1,
|
|
14
|
-
outputs:2,
|
|
15
|
+
inputs: 1,
|
|
16
|
+
outputs: 2,
|
|
15
17
|
icon: "looptimer_advanced.png",
|
|
16
|
-
label: function() {
|
|
18
|
+
label: function () {
|
|
17
19
|
return this.name || this.duration + " " + this.units + " Loop";
|
|
18
20
|
},
|
|
19
|
-
labelStyle: function() {
|
|
20
|
-
return this.name?"node_label_italic":"";
|
|
21
|
+
labelStyle: function () {
|
|
22
|
+
return this.name ? "node_label_italic" : "";
|
|
21
23
|
},
|
|
22
|
-
outputLabels: ["original msg","stopped msg"]
|
|
24
|
+
outputLabels: ["original msg", "stopped msg"]
|
|
23
25
|
});
|
|
24
26
|
</script>
|
|
25
27
|
|
|
26
|
-
<script type="text/
|
|
28
|
+
<script type="text/html" data-template-name="looptimer-advanced">
|
|
27
29
|
<div class="form-row">
|
|
28
30
|
<label for="node-input-duration"><i class="fa fa-clock-o"></i> Loop Every</label>
|
|
29
31
|
<input type="text" id="node-input-duration" style="text-align:end; width:70px !important">
|
|
30
32
|
<select id="node-input-units" style="width:140px !important">
|
|
31
|
-
<option value="Millisecond">
|
|
33
|
+
<option value="Millisecond">Milliseconds</option>
|
|
32
34
|
<option value="Second">Seconds</option>
|
|
33
35
|
<option value="Minute">Minutes</option>
|
|
34
36
|
<option value="Hour">Hours</option>
|
|
@@ -39,29 +41,60 @@
|
|
|
39
41
|
<label for="node-input-maxloops"><i class="fa fa-repeat"></i> Max Loops</label>
|
|
40
42
|
<input type="text" id="node-input-maxloops" style="text-align:end; width:70px !important">
|
|
41
43
|
</div>
|
|
44
|
+
<div class="form-row">
|
|
45
|
+
<label> </label>
|
|
46
|
+
<input type="checkbox" id="node-input-totalcount" style="display:inline-block; width:auto; vertical-align:top;">
|
|
47
|
+
<label for="node-input-totalcount" style="width:auto"> Max Loops counts the first message too</label>
|
|
48
|
+
</div>
|
|
42
49
|
<div class="form-row">
|
|
43
50
|
<label for="node-input-maxtimeout"><i class="fa fa-clock-o"></i> Max Timeout</label>
|
|
44
51
|
<input type="text" id="node-input-maxtimeout" style="text-align:end; width:70px !important">
|
|
45
52
|
<select id="node-input-maxtimeoutunits" style="width:140px !important">
|
|
46
|
-
<option value="Millisecond">
|
|
53
|
+
<option value="Millisecond">Milliseconds</option>
|
|
47
54
|
<option value="Second">Seconds</option>
|
|
48
55
|
<option value="Minute">Minutes</option>
|
|
49
56
|
<option value="Hour">Hours</option>
|
|
50
57
|
<option value="Day">Days</option>
|
|
51
58
|
</select>
|
|
52
59
|
</div>
|
|
60
|
+
<div class="form-row">
|
|
61
|
+
<label> </label>
|
|
62
|
+
<input type="checkbox" id="node-input-pausecontrol" style="display:inline-block; width:auto; vertical-align:top;">
|
|
63
|
+
<label for="node-input-pausecontrol" style="width:auto"> Pause / resume with payload</label>
|
|
64
|
+
</div>
|
|
53
65
|
<div class="form-row">
|
|
54
66
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
55
|
-
<input type="text" id="node-input-name" placeholder="Name"
|
|
67
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
56
68
|
</div>
|
|
57
69
|
</script>
|
|
58
70
|
|
|
59
|
-
<script type="text/
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
<
|
|
63
|
-
<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
<script type="text/html" data-help-name="looptimer-advanced">
|
|
72
|
+
<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>
|
|
73
|
+
|
|
74
|
+
<h3>Inputs</h3>
|
|
75
|
+
<dl class="message-properties">
|
|
76
|
+
<dt>payload <span class="property-type">string</span></dt>
|
|
77
|
+
<dd><code>stop</code> / <code>STOP</code> stops the loop. If enabled in the node, <code>pause</code> and <code>resume</code> do what they say. Any other message (re)starts the loop.</dd>
|
|
78
|
+
<dt class="optional">loop.command <span class="property-type">string</span></dt>
|
|
79
|
+
<dd><code>stop</code>, <code>pause</code> or <code>resume</code>. Works regardless of the payload and of the "Pause / resume with payload" setting.</dd>
|
|
80
|
+
<dt class="optional">loop.duration, loop.units <span class="property-type">number, string</span></dt>
|
|
81
|
+
<dd>Override the loop interval. Units: <code>Millisecond</code>, <code>Second</code>, <code>Minute</code>, <code>Hour</code>, <code>Day</code>.</dd>
|
|
82
|
+
<dt class="optional">loop.maxtimeout, loop.maxtimeoutunits <span class="property-type">number, string</span></dt>
|
|
83
|
+
<dd>Override the maximum timeout.</dd>
|
|
84
|
+
<dt class="optional">loop.maxloops <span class="property-type">number</span></dt>
|
|
85
|
+
<dd>Override the maximum number of loops.</dd>
|
|
86
|
+
<dt class="optional">loop.totalcount <span class="property-type">boolean</span></dt>
|
|
87
|
+
<dd>Override whether Max Loops counts the first message too.</dd>
|
|
88
|
+
</dl>
|
|
89
|
+
|
|
90
|
+
<h3>Outputs</h3>
|
|
91
|
+
<ol class="node-ports">
|
|
92
|
+
<li>The original message, then once per loop. After a pause, the first message carries <code>msg.pauseduration</code> (milliseconds spent paused).</li>
|
|
93
|
+
<li>Status messages with payload <code>stopped</code>, <code>max loops reached</code>, <code>timeout</code>, <code>paused</code> or <code>resumed</code>.</li>
|
|
94
|
+
</ol>
|
|
95
|
+
|
|
96
|
+
<h3>Details</h3>
|
|
97
|
+
<p>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 of messages: <code>1</code> is a single message, <code>0</code> sends nothing and <code>-1</code> is unlimited.</p>
|
|
98
|
+
<p>Pausing freezes the remaining time of the current interval, the loop counter and the timeout budget; resuming continues from there.</p>
|
|
99
|
+
<p>Overrides in <code>msg.loop</code> stay in effect for the following messages.</p>
|
|
67
100
|
</script>
|
package/looptimer_advanced.js
CHANGED
|
@@ -1,194 +1,267 @@
|
|
|
1
1
|
module.exports = function (RED) {
|
|
2
2
|
"use strict";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (!unitObj) {
|
|
3
|
+
|
|
4
|
+
const UNIT_MS = {
|
|
5
|
+
Millisecond: 1,
|
|
6
|
+
Second: 1000,
|
|
7
|
+
Minute: 60 * 1000,
|
|
8
|
+
Hour: 60 * 60 * 1000,
|
|
9
|
+
Day: 24 * 60 * 60 * 1000
|
|
10
|
+
};
|
|
11
|
+
const UNIT_LIST = Object.keys(UNIT_MS).join(", ");
|
|
12
|
+
|
|
13
|
+
function toMs(unit, amount) {
|
|
14
|
+
if (!Object.prototype.hasOwnProperty.call(UNIT_MS, unit)) {
|
|
16
15
|
throw new Error(`Invalid unit: ${unit}`);
|
|
17
16
|
}
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
17
|
+
return UNIT_MS[unit] * amount;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isCommand(value, word) {
|
|
21
|
+
return typeof value === "string" && value.toLowerCase() === word;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function LoopTimer(config) {
|
|
25
|
+
RED.nodes.createNode(this, config);
|
|
26
|
+
const node = this;
|
|
27
|
+
|
|
28
|
+
node.units = config.units || "Second";
|
|
29
|
+
node.duration = config.duration || 5;
|
|
30
|
+
node.maxloops = config.maxloops || 100;
|
|
31
|
+
node.maxtimeoutunits = config.maxtimeoutunits || "Hour";
|
|
32
|
+
node.maxtimeout = config.maxtimeout || 1;
|
|
33
|
+
// Opt-in features. Flows saved by 0.0.x do not carry these properties, so they keep the old behaviour.
|
|
34
|
+
node.totalcount = config.totalcount === true || config.totalcount === "true";
|
|
35
|
+
node.pausecontrol = config.pausecontrol === true || config.pausecontrol === "true";
|
|
36
|
+
|
|
37
|
+
if (node.duration <= 0) { node.duration = 0; }
|
|
38
|
+
if (node.maxtimeout <= 0) { node.maxtimeout = 0; }
|
|
39
|
+
|
|
40
|
+
// Runtime state of the running loop (null when idle).
|
|
41
|
+
let run = null;
|
|
42
|
+
|
|
43
|
+
function clearTimer() {
|
|
44
|
+
if (run && run.timer) {
|
|
45
|
+
clearTimeout(run.timer);
|
|
46
|
+
run.timer = null;
|
|
47
|
+
}
|
|
33
48
|
}
|
|
34
49
|
|
|
35
|
-
|
|
36
|
-
|
|
50
|
+
function endLoop() {
|
|
51
|
+
clearTimer();
|
|
52
|
+
run = null;
|
|
37
53
|
}
|
|
38
54
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
function sendControl(msg, payload) {
|
|
56
|
+
const m = RED.util.cloneMessage(msg);
|
|
57
|
+
m.payload = payload;
|
|
58
|
+
node.send([null, m]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Arms the single timer: either the next loop tick or, when it comes first, the overall timeout.
|
|
62
|
+
function schedule() {
|
|
63
|
+
const now = Date.now();
|
|
64
|
+
const untilTick = run.remainingTick !== null ? run.remainingTick : run.duration;
|
|
65
|
+
const untilDeadline = run.remainingDeadline !== null ? run.remainingDeadline : run.deadline - now;
|
|
66
|
+
run.remainingTick = null;
|
|
67
|
+
run.remainingDeadline = null;
|
|
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);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function onDeadline() {
|
|
77
|
+
run.timer = null;
|
|
78
|
+
node.status({ fill: "red", shape: "ring", text: "timeout reached" });
|
|
79
|
+
sendControl(run.msg, "timeout");
|
|
80
|
+
endLoop();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function onTick() {
|
|
84
|
+
run.timer = null;
|
|
85
|
+
run.count++;
|
|
86
|
+
const count = run.count;
|
|
87
|
+
const cp = RED.util.cloneMessage(run.msg);
|
|
88
|
+
if (run.pausedFor) {
|
|
89
|
+
cp.pauseduration = run.pausedFor;
|
|
90
|
+
run.pausedFor = 0;
|
|
91
|
+
}
|
|
61
92
|
|
|
93
|
+
if (run.limit > 0 && count >= run.limit) {
|
|
94
|
+
const ml = RED.util.cloneMessage(run.msg);
|
|
95
|
+
ml.payload = "max loops reached";
|
|
96
|
+
node.send([cp, ml]);
|
|
97
|
+
node.status({ fill: "red", shape: "ring", text: "max loops reached" });
|
|
98
|
+
endLoop();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
62
101
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
102
|
+
if (count * run.duration <= run.maxtimeout) {
|
|
103
|
+
node.send([cp, null]);
|
|
104
|
+
if ((count + 1) * run.duration > run.maxtimeout) {
|
|
105
|
+
// Kept from 0.0.x: the last loop message is repeated together with the "timeout" message.
|
|
106
|
+
const to = RED.util.cloneMessage(run.msg);
|
|
107
|
+
to.payload = "timeout";
|
|
108
|
+
node.send([cp, to]);
|
|
109
|
+
node.status({ fill: "red", shape: "ring", text: "timeout reached" });
|
|
110
|
+
endLoop();
|
|
111
|
+
return;
|
|
71
112
|
}
|
|
113
|
+
node.status({ fill: "green", shape: "dot", text: "running" });
|
|
114
|
+
schedule();
|
|
115
|
+
} else {
|
|
116
|
+
node.status({ fill: "red", shape: "ring", text: "timeout reached" });
|
|
117
|
+
sendControl(run.msg, "timeout");
|
|
118
|
+
endLoop();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function stop(msg) {
|
|
123
|
+
endLoop();
|
|
124
|
+
node.status({ fill: "red", shape: "ring", text: "stopped" });
|
|
125
|
+
sendControl(msg, "stopped");
|
|
126
|
+
}
|
|
72
127
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
128
|
+
function pause(msg) {
|
|
129
|
+
if (!run || run.paused) { return; }
|
|
130
|
+
const now = Date.now();
|
|
131
|
+
clearTimer();
|
|
132
|
+
run.paused = true;
|
|
133
|
+
run.pausedAt = now;
|
|
134
|
+
run.remainingTick = Math.max(0, run.tickDue - now);
|
|
135
|
+
run.remainingDeadline = Math.max(0, run.deadline - now);
|
|
136
|
+
node.status({ fill: "yellow", shape: "ring", text: "paused" });
|
|
137
|
+
sendControl(msg, "paused");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function resume(msg) {
|
|
141
|
+
if (!run || !run.paused) { return; }
|
|
142
|
+
run.paused = false;
|
|
143
|
+
run.pausedFor = (run.pausedFor || 0) + (Date.now() - run.pausedAt);
|
|
144
|
+
node.status({ fill: "green", shape: "dot", text: "running" });
|
|
145
|
+
sendControl(msg, "resumed");
|
|
146
|
+
schedule();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Validates and applies msg.loop overrides. Returns an error text, or null when everything is fine.
|
|
150
|
+
function applyOverrides(over) {
|
|
151
|
+
const checkUnit = (key, prop) => {
|
|
152
|
+
if (over[key] === undefined) { return null; }
|
|
153
|
+
if (!Object.prototype.hasOwnProperty.call(UNIT_MS, over[key])) {
|
|
154
|
+
node.status({ fill: "red", shape: "ring", text: `Invalid Attribute msg.loop.${key}` });
|
|
155
|
+
return `Invalid Attribute: msg.loop.${key}, allowed values are ${UNIT_LIST}`;
|
|
156
|
+
}
|
|
157
|
+
node[prop] = over[key];
|
|
158
|
+
return null;
|
|
159
|
+
};
|
|
160
|
+
const checkNumber = (key, prop) => {
|
|
161
|
+
if (over[key] === undefined) { return null; }
|
|
162
|
+
if (typeof over[key] !== "number" || isNaN(over[key])) {
|
|
163
|
+
node.status({ fill: "red", shape: "ring", text: `msg.loop.${key} must be a number` });
|
|
164
|
+
return `Invalid Attribute: msg.loop.${key}, value must be a number`;
|
|
81
165
|
}
|
|
166
|
+
node[prop] = over[key];
|
|
167
|
+
return null;
|
|
168
|
+
};
|
|
169
|
+
if (over.totalcount !== undefined) {
|
|
170
|
+
if (typeof over.totalcount !== "boolean") {
|
|
171
|
+
node.status({ fill: "red", shape: "ring", text: "msg.loop.totalcount must be a boolean" });
|
|
172
|
+
return "Invalid Attribute: msg.loop.totalcount, value must be a boolean";
|
|
173
|
+
}
|
|
174
|
+
node.totalcount = over.totalcount;
|
|
175
|
+
}
|
|
176
|
+
return checkUnit("units", "units") ||
|
|
177
|
+
checkNumber("duration", "duration") ||
|
|
178
|
+
checkUnit("maxtimeoutunits", "maxtimeoutunits") ||
|
|
179
|
+
checkNumber("maxtimeout", "maxtimeout") ||
|
|
180
|
+
checkNumber("maxloops", "maxloops");
|
|
181
|
+
}
|
|
82
182
|
|
|
183
|
+
function start(msg) {
|
|
184
|
+
endLoop();
|
|
185
|
+
const duration = toMs(node.units, node.duration);
|
|
186
|
+
const maxtimeout = toMs(node.maxtimeoutunits, node.maxtimeout);
|
|
187
|
+
const maxloops = Number(node.maxloops);
|
|
83
188
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
189
|
+
// limit = number of loop messages after the first one; 0 means unlimited (until timeout or stop).
|
|
190
|
+
let limit = maxloops;
|
|
191
|
+
if (node.totalcount) {
|
|
192
|
+
// totalcount: maxloops is the total number of messages, first one included; -1 = unlimited.
|
|
193
|
+
limit = maxloops < 0 ? 0 : maxloops - 1;
|
|
194
|
+
if (maxloops === 0) {
|
|
195
|
+
node.status({ fill: "red", shape: "ring", text: "max loops reached" });
|
|
196
|
+
sendControl(msg, "max loops reached");
|
|
197
|
+
return;
|
|
92
198
|
}
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
node.error('Invalid Attribute: msg.loop.maxloops, value must be an Integer');
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
199
|
+
if (maxloops === 1) {
|
|
200
|
+
node.send([RED.util.cloneMessage(msg), null]);
|
|
201
|
+
node.status({ fill: "red", shape: "ring", text: "max loops reached" });
|
|
202
|
+
sendControl(msg, "max loops reached");
|
|
203
|
+
return;
|
|
101
204
|
}
|
|
205
|
+
}
|
|
102
206
|
|
|
207
|
+
node.send([RED.util.cloneMessage(msg), null]);
|
|
208
|
+
run = {
|
|
209
|
+
msg: msg,
|
|
210
|
+
duration: duration,
|
|
211
|
+
maxtimeout: maxtimeout,
|
|
212
|
+
limit: limit,
|
|
213
|
+
count: 0,
|
|
214
|
+
timer: null,
|
|
215
|
+
paused: false,
|
|
216
|
+
pausedAt: 0,
|
|
217
|
+
pausedFor: 0,
|
|
218
|
+
tickDue: 0,
|
|
219
|
+
deadline: Date.now() + maxtimeout,
|
|
220
|
+
remainingTick: null,
|
|
221
|
+
remainingDeadline: null
|
|
222
|
+
};
|
|
223
|
+
node.status({ fill: "green", shape: "dot", text: "running" });
|
|
224
|
+
schedule();
|
|
225
|
+
}
|
|
103
226
|
|
|
227
|
+
node.on("input", function (msg, send, done) {
|
|
228
|
+
done = done || function (err) { if (err) { node.error(err, msg); } };
|
|
104
229
|
|
|
230
|
+
const over = msg.loop;
|
|
231
|
+
if (over !== undefined && over !== null && typeof over === "object") {
|
|
232
|
+
const problem = applyOverrides(over);
|
|
233
|
+
if (problem) {
|
|
234
|
+
node.error(problem, msg);
|
|
235
|
+
done();
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
105
238
|
}
|
|
106
|
-
node.nodeduration = CalcDuration(node.units, node.duration);
|
|
107
|
-
node.nodemaxtimeout = CalcDuration(node.maxtimeoutunits, node.maxtimeout);
|
|
108
239
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
node.status({});
|
|
240
|
+
const command = over && typeof over.command === "string" ? over.command.toLowerCase() : null;
|
|
241
|
+
const byPayload = (word) => node.pausecontrol && isCommand(msg.payload, word);
|
|
112
242
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
shape: "ring",
|
|
121
|
-
text: "stopped"
|
|
122
|
-
});
|
|
123
|
-
stopped = true;
|
|
124
|
-
stopmsg = RED.util.cloneMessage(msg);
|
|
125
|
-
stopmsg.payload = "stopped";
|
|
126
|
-
node.send([null, stopmsg]);
|
|
127
|
-
stopped = false;
|
|
243
|
+
try {
|
|
244
|
+
if (command === "pause" || byPayload("pause")) {
|
|
245
|
+
pause(msg);
|
|
246
|
+
} else if (command === "resume" || byPayload("resume")) {
|
|
247
|
+
resume(msg);
|
|
248
|
+
} else if (command === "stop" || isCommand(msg.payload, "stop")) {
|
|
249
|
+
stop(msg);
|
|
128
250
|
} else {
|
|
129
|
-
|
|
130
|
-
node.send([cpmsg, null]);
|
|
131
|
-
msg._timerpass = true;
|
|
132
|
-
loop.run(function (next, err, data) {
|
|
133
|
-
node.status({
|
|
134
|
-
fill: "green",
|
|
135
|
-
shape: "dot",
|
|
136
|
-
text: "running"
|
|
137
|
-
});
|
|
138
|
-
data++;
|
|
139
|
-
timeout = setTimeout(function () {
|
|
140
|
-
if (stopped === false) {
|
|
141
|
-
cpmsg = RED.util.cloneMessage(msg);
|
|
142
|
-
if (data == node.maxloops) {
|
|
143
|
-
mlmsg = RED.util.cloneMessage(msg);
|
|
144
|
-
mlmsg.payload = "max loops reached";
|
|
145
|
-
node.send([cpmsg, mlmsg]);
|
|
146
|
-
node.status({
|
|
147
|
-
fill: "red",
|
|
148
|
-
shape: "ring",
|
|
149
|
-
text: "max loops reached"
|
|
150
|
-
});
|
|
151
|
-
timeout = null;
|
|
152
|
-
next("break");
|
|
153
|
-
} else {
|
|
154
|
-
node.status({});
|
|
155
|
-
if ((data * node.nodeduration) <= node.nodemaxtimeout) {
|
|
156
|
-
node.send([cpmsg, null]);
|
|
157
|
-
timeout = null;
|
|
158
|
-
if (((data + 1) * node.nodeduration) > node.nodemaxtimeout) {
|
|
159
|
-
stopmsg = RED.util.cloneMessage(msg);
|
|
160
|
-
stopmsg.payload = 'timeout';
|
|
161
|
-
node.send([cpmsg, stopmsg]);
|
|
162
|
-
node.status({
|
|
163
|
-
fill: "red",
|
|
164
|
-
shape: "ring",
|
|
165
|
-
text: "timeout reached"
|
|
166
|
-
});
|
|
167
|
-
next("break");
|
|
168
|
-
} else {
|
|
169
|
-
next(undefined, data);
|
|
170
|
-
}
|
|
171
|
-
} else {
|
|
172
|
-
timeout = null;
|
|
173
|
-
next("break");
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
} else {
|
|
177
|
-
timeout = null;
|
|
178
|
-
next("break");
|
|
179
|
-
}
|
|
180
|
-
}, node.nodeduration);
|
|
181
|
-
}, 0);
|
|
251
|
+
start(msg);
|
|
182
252
|
}
|
|
253
|
+
done();
|
|
254
|
+
} catch (err) {
|
|
255
|
+
node.status({ fill: "red", shape: "ring", text: err.message });
|
|
256
|
+
done(err);
|
|
183
257
|
}
|
|
184
258
|
});
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
clearTimeout(timeout);
|
|
189
|
-
}
|
|
259
|
+
|
|
260
|
+
node.on("close", function () {
|
|
261
|
+
endLoop();
|
|
190
262
|
node.status({});
|
|
191
263
|
});
|
|
192
264
|
}
|
|
265
|
+
|
|
193
266
|
RED.nodes.registerType("looptimer-advanced", LoopTimer);
|
|
194
|
-
}
|
|
267
|
+
};
|
package/package.json
CHANGED
|
@@ -1,30 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-looptimer-advanced",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.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
|
-
"scripts": {
|
|
7
|
-
|
|
8
|
-
"loop": "2.1.2"
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "mocha \"test/**/*.test.js\""
|
|
9
8
|
},
|
|
10
|
-
"directories": {},
|
|
11
9
|
"keywords": [
|
|
12
10
|
"node-red",
|
|
13
11
|
"loop",
|
|
14
12
|
"timer",
|
|
15
13
|
"advanced",
|
|
16
|
-
"looptimer"
|
|
14
|
+
"looptimer",
|
|
15
|
+
"node-red-contrib",
|
|
16
|
+
"pause",
|
|
17
|
+
"resume",
|
|
18
|
+
"repeat",
|
|
19
|
+
"interval"
|
|
17
20
|
],
|
|
18
21
|
"author": "Haxiboy",
|
|
19
22
|
"license": "Apache-2.0",
|
|
20
23
|
"node-red": {
|
|
21
|
-
"version": ">=
|
|
24
|
+
"version": ">=3.0.0",
|
|
22
25
|
"nodes": {
|
|
23
26
|
"looptimer-advanced": "looptimer_advanced.js"
|
|
24
27
|
}
|
|
25
28
|
},
|
|
26
29
|
"engines": {
|
|
27
|
-
"node": ">=
|
|
30
|
+
"node": ">=18"
|
|
28
31
|
},
|
|
29
32
|
"repository": {
|
|
30
33
|
"type": "git",
|
|
@@ -33,5 +36,19 @@
|
|
|
33
36
|
"bugs": {
|
|
34
37
|
"url": "https://github.com/Haxiboy/node-red-contrib-looptimer-advanced/issues"
|
|
35
38
|
},
|
|
36
|
-
"homepage": "https://github.com/Haxiboy/node-red-contrib-looptimer-advanced#readme"
|
|
39
|
+
"homepage": "https://github.com/Haxiboy/node-red-contrib-looptimer-advanced#readme",
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"mocha": "^12.0.3",
|
|
42
|
+
"node-red": "^5.0.8",
|
|
43
|
+
"node-red-node-test-helper": "^0.3.6",
|
|
44
|
+
"should": "^13.2.3"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"looptimer_advanced.js",
|
|
48
|
+
"looptimer_advanced.html",
|
|
49
|
+
"icons",
|
|
50
|
+
"examples",
|
|
51
|
+
"README.md",
|
|
52
|
+
"LICENSE"
|
|
53
|
+
]
|
|
37
54
|
}
|