node-red-contrib-looptimer-advanced 0.0.1 → 0.0.4
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 +73 -72
- package/looptimer_advanced.html +67 -67
- package/looptimer_advanced.js +187 -179
- package/package.json +32 -32
package/README.md
CHANGED
|
@@ -1,72 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Advanced Looptimer for node-red
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
----------------------------
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
This project is based on **node-red-contrib-looptimer** and **node-red-contrib-looptimer2** with the need of overwrite properties via messages.
|
|
13
|
-
|
|
14
|
-
**Installation:**
|
|
15
|
-
> npm install node-red-contrib-looptimer-advanced
|
|
16
|
-
|
|
17
|
-
The main functionality is not changed (for now), however fix from looptimer2 has been implemented, because it did not work with other timers for example **node-red-contrib-stoptimer** and I've did some code rework for better functionality and cleaner code.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
Sends the `msg` through the first output, then continues to do so in a loop, once per the timer duration specified, until a payload of `stop` or `STOP` is received, at which time the second output will automatically send a payload of `stopped`.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
You can also stop the loop by specifying a maximum number of loops, which when reached, will stop the loop and timer, as well as sending a payload of `max loops reached` through the second output. Keep in mind, the first `msg` simply passes through, and is therefore not part of the loop. So if you set the max loops to `5`, you will get `6 messages`, which is 1 original message, and 5 messages from the loop.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
Finally, to ensure you do not end up with an infinite loop, you can set a maximum timeout in seconds, minutes or hours, and when that time is reached, the loop and timer will also be stopped.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
The properties can be overridden via `msg.loop` messages supplemented with property name. Accepted messages for loop are `msg.loop.duration` and `msg.loop.units`, for timeout are `msg.loop.maxtimeout` and `msg.loop.maxtimeoutunits`.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
You can define maximum number of loops with `msg.loop.maxloops`
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
Setting the Max Loops and Max Timeout settings to high values can, for all intents, ensure that the loop can only be stopped by an incoming `stop` payload, however, the stability of the loop has not been tested over an extended number of hours.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
**0.0.1** - Initial Release
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
Advanced Looptimer for node-red
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
----------------------------
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
This project is based on **node-red-contrib-looptimer** and **node-red-contrib-looptimer2** with the need of overwrite properties via messages.
|
|
13
|
+
|
|
14
|
+
**Installation:**
|
|
15
|
+
> npm install node-red-contrib-looptimer-advanced
|
|
16
|
+
|
|
17
|
+
The main functionality is not changed (for now), however fix from looptimer2 has been implemented, because it did not work with other timers for example **node-red-contrib-stoptimer** and I've did some code rework for better functionality and cleaner code.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
Sends the `msg` through the first output, then continues to do so in a loop, once per the timer duration specified, until a payload of `stop` or `STOP` is received, at which time the second output will automatically send a payload of `stopped`.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
You can also stop the loop by specifying a maximum number of loops, which when reached, will stop the loop and timer, as well as sending a payload of `max loops reached` through the second output. Keep in mind, the first `msg` simply passes through, and is therefore not part of the loop. So if you set the max loops to `5`, you will get `6 messages`, which is 1 original message, and 5 messages from the loop.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
Finally, to ensure you do not end up with an infinite loop, you can set a maximum timeout in seconds, minutes or hours, and when that time is reached, the loop and timer will also be stopped.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
The properties can be overridden via `msg.loop` messages supplemented with property name. Accepted messages for loop are `msg.loop.duration` and `msg.loop.units`, for timeout are `msg.loop.maxtimeout` and `msg.loop.maxtimeoutunits`.
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
You can define maximum number of loops with `msg.loop.maxloops`
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
Setting the Max Loops and Max Timeout settings to high values can, for all intents, ensure that the loop can only be stopped by an incoming `stop` payload, however, the stability of the loop has not been tested over an extended number of hours.
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
**0.0.4** - Issue #4 -> Added message when timeout reached
|
|
57
|
+
**0.0.1** - Initial Release
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
**To Do:**
|
|
65
|
+
|
|
66
|
+
- [ ] Mustache Template Support
|
|
67
|
+
|
|
68
|
+
- [ ] Example Flows
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
[](https://www.buymeacoffee.com/haxiboy)
|
package/looptimer_advanced.html
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
<script type="text/javascript">
|
|
2
|
-
RED.nodes.registerType('looptimer-advanced',{
|
|
3
|
-
category: 'function',
|
|
4
|
-
color:"#33B2FF",
|
|
5
|
-
defaults: {
|
|
6
|
-
duration: {value:"5",required:true,validate:RED.validators.number()},
|
|
7
|
-
units: {value:"Second"},
|
|
8
|
-
maxloops: {value:"100",required:false,validate:RED.validators.number()},
|
|
9
|
-
maxtimeout: {value:"1",required:false,validate:RED.validators.number()},
|
|
10
|
-
maxtimeoutunits: {value:"Hour"},
|
|
11
|
-
name: {value:""}
|
|
12
|
-
},
|
|
13
|
-
inputs:1,
|
|
14
|
-
outputs:2,
|
|
15
|
-
icon: "looptimer_advanced.png",
|
|
16
|
-
label: function() {
|
|
17
|
-
return this.name || this.duration + " " + this.units + " Loop";
|
|
18
|
-
},
|
|
19
|
-
labelStyle: function() {
|
|
20
|
-
return this.name?"node_label_italic":"";
|
|
21
|
-
},
|
|
22
|
-
outputLabels: ["original msg","stopped msg"]
|
|
23
|
-
});
|
|
24
|
-
</script>
|
|
25
|
-
|
|
26
|
-
<script type="text/x-red" data-template-name="looptimer-advanced">
|
|
27
|
-
<div class="form-row">
|
|
28
|
-
<label for="node-input-duration"><i class="fa fa-clock-o"></i> Loop Every</label>
|
|
29
|
-
<input type="text" id="node-input-duration" style="text-align:end; width:70px !important">
|
|
30
|
-
<select id="node-input-units" style="width:140px !important">
|
|
31
|
-
<option value="
|
|
32
|
-
<option value="Second">Seconds</option>
|
|
33
|
-
<option value="Minute">Minutes</option>
|
|
34
|
-
<option value="Hour">Hours</option>
|
|
35
|
-
<option value="Day">Days</option>
|
|
36
|
-
</select>
|
|
37
|
-
</div>
|
|
38
|
-
<div class="form-row">
|
|
39
|
-
<label for="node-input-maxloops"><i class="fa fa-repeat"></i> Max Loops</label>
|
|
40
|
-
<input type="text" id="node-input-maxloops" style="text-align:end; width:70px !important">
|
|
41
|
-
</div>
|
|
42
|
-
<div class="form-row">
|
|
43
|
-
<label for="node-input-maxtimeout"><i class="fa fa-clock-o"></i> Max Timeout</label>
|
|
44
|
-
<input type="text" id="node-input-maxtimeout" style="text-align:end; width:70px !important">
|
|
45
|
-
<select id="node-input-maxtimeoutunits" style="width:140px !important">
|
|
46
|
-
<option value="
|
|
47
|
-
<option value="Second">Seconds</option>
|
|
48
|
-
<option value="Minute">Minutes</option>
|
|
49
|
-
<option value="Hour">Hours</option>
|
|
50
|
-
<option value="Day">Days</option>
|
|
51
|
-
</select>
|
|
52
|
-
</div>
|
|
53
|
-
<div class="form-row">
|
|
54
|
-
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
55
|
-
<input type="text" id="node-input-name" placeholder="Name"></input>
|
|
56
|
-
</div>
|
|
57
|
-
</script>
|
|
58
|
-
|
|
59
|
-
<script type="text/x-red" data-help-name="looptimer-advanced">
|
|
60
|
-
<p>Sends the <code>msg</code> through the first output, then continues to do so in a loop, once per the timer duration specified, until a payload of <code>stop</code> or <code>STOP</code> is received, at which time the second output will automatically send a payload of <code>stopped</code>.
|
|
61
|
-
<p>You can also stop the loop by specifying a maximum number of loops, which when reached, will stop the loop and timer, as well as sending a payload of <code>max loops reached</code> through the second output. Keep in mind, the first <code>msg</code> simply passes through, and is therefore not part of the loop. So if you set the max loops to <code>5</code>, you will get <code>6 messages</code>, which is 1 original message, and 5 messages from the loop.</p>
|
|
62
|
-
<p>Finally, to ensure you do not end up with an infinite loop, you can set a maximum timeout in seconds, minutes or hours, and when that time is reached, the loop and timer will also be stopped.</p>
|
|
63
|
-
<p>Properties can be overridden via <code>msg.loop</code> messages supplemented with property name.</p>
|
|
64
|
-
<p>Accepted messages for loop are <code>msg.loop.duration</code> and <code>msg.loop.units</code></p>
|
|
65
|
-
<p>For timeout you can use <code>msg.loop.maxtimeout</code> and <code>msg.loop.maxtimeoutunits</code></p>
|
|
66
|
-
<p><code>NOTE:</code> Setting the Max Loops and Max Timeout settings to high values can, for all intents, ensure that the loop can only be stopped by an incoming <code>stop</code> payload, however, the stability of the loop has not been tested over an extended number of hours.</p>
|
|
67
|
-
</script>
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('looptimer-advanced',{
|
|
3
|
+
category: 'function',
|
|
4
|
+
color:"#33B2FF",
|
|
5
|
+
defaults: {
|
|
6
|
+
duration: {value:"5",required:true,validate:RED.validators.number()},
|
|
7
|
+
units: {value:"Second"},
|
|
8
|
+
maxloops: {value:"100",required:false,validate:RED.validators.number()},
|
|
9
|
+
maxtimeout: {value:"1",required:false,validate:RED.validators.number()},
|
|
10
|
+
maxtimeoutunits: {value:"Hour"},
|
|
11
|
+
name: {value:""}
|
|
12
|
+
},
|
|
13
|
+
inputs:1,
|
|
14
|
+
outputs:2,
|
|
15
|
+
icon: "looptimer_advanced.png",
|
|
16
|
+
label: function() {
|
|
17
|
+
return this.name || this.duration + " " + this.units + " Loop";
|
|
18
|
+
},
|
|
19
|
+
labelStyle: function() {
|
|
20
|
+
return this.name?"node_label_italic":"";
|
|
21
|
+
},
|
|
22
|
+
outputLabels: ["original msg","stopped msg"]
|
|
23
|
+
});
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<script type="text/x-red" data-template-name="looptimer-advanced">
|
|
27
|
+
<div class="form-row">
|
|
28
|
+
<label for="node-input-duration"><i class="fa fa-clock-o"></i> Loop Every</label>
|
|
29
|
+
<input type="text" id="node-input-duration" style="text-align:end; width:70px !important">
|
|
30
|
+
<select id="node-input-units" style="width:140px !important">
|
|
31
|
+
<option value="Millisecond">Miliseconds</option>
|
|
32
|
+
<option value="Second">Seconds</option>
|
|
33
|
+
<option value="Minute">Minutes</option>
|
|
34
|
+
<option value="Hour">Hours</option>
|
|
35
|
+
<option value="Day">Days</option>
|
|
36
|
+
</select>
|
|
37
|
+
</div>
|
|
38
|
+
<div class="form-row">
|
|
39
|
+
<label for="node-input-maxloops"><i class="fa fa-repeat"></i> Max Loops</label>
|
|
40
|
+
<input type="text" id="node-input-maxloops" style="text-align:end; width:70px !important">
|
|
41
|
+
</div>
|
|
42
|
+
<div class="form-row">
|
|
43
|
+
<label for="node-input-maxtimeout"><i class="fa fa-clock-o"></i> Max Timeout</label>
|
|
44
|
+
<input type="text" id="node-input-maxtimeout" style="text-align:end; width:70px !important">
|
|
45
|
+
<select id="node-input-maxtimeoutunits" style="width:140px !important">
|
|
46
|
+
<option value="Millisecond">Miliseconds</option>
|
|
47
|
+
<option value="Second">Seconds</option>
|
|
48
|
+
<option value="Minute">Minutes</option>
|
|
49
|
+
<option value="Hour">Hours</option>
|
|
50
|
+
<option value="Day">Days</option>
|
|
51
|
+
</select>
|
|
52
|
+
</div>
|
|
53
|
+
<div class="form-row">
|
|
54
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
55
|
+
<input type="text" id="node-input-name" placeholder="Name"></input>
|
|
56
|
+
</div>
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<script type="text/x-red" data-help-name="looptimer-advanced">
|
|
60
|
+
<p>Sends the <code>msg</code> through the first output, then continues to do so in a loop, once per the timer duration specified, until a payload of <code>stop</code> or <code>STOP</code> is received, at which time the second output will automatically send a payload of <code>stopped</code>.
|
|
61
|
+
<p>You can also stop the loop by specifying a maximum number of loops, which when reached, will stop the loop and timer, as well as sending a payload of <code>max loops reached</code> through the second output. Keep in mind, the first <code>msg</code> simply passes through, and is therefore not part of the loop. So if you set the max loops to <code>5</code>, you will get <code>6 messages</code>, which is 1 original message, and 5 messages from the loop.</p>
|
|
62
|
+
<p>Finally, to ensure you do not end up with an infinite loop, you can set a maximum timeout in seconds, minutes or hours, and when that time is reached, the loop and timer will also be stopped.</p>
|
|
63
|
+
<p>Properties can be overridden via <code>msg.loop</code> messages supplemented with property name.</p>
|
|
64
|
+
<p>Accepted messages for loop are <code>msg.loop.duration</code> and <code>msg.loop.units</code></p>
|
|
65
|
+
<p>For timeout you can use <code>msg.loop.maxtimeout</code> and <code>msg.loop.maxtimeoutunits</code></p>
|
|
66
|
+
<p><code>NOTE:</code> Setting the Max Loops and Max Timeout settings to high values can, for all intents, ensure that the loop can only be stopped by an incoming <code>stop</code> payload, however, the stability of the loop has not been tested over an extended number of hours.</p>
|
|
67
|
+
</script>
|
package/looptimer_advanced.js
CHANGED
|
@@ -1,179 +1,187 @@
|
|
|
1
|
-
module.exports = function (RED) {
|
|
2
|
-
"use strict";
|
|
3
|
-
var Looper = require('loop');
|
|
4
|
-
|
|
5
|
-
var unitMap = [
|
|
6
|
-
{ unit: "Millisecond", multiplier: 1, duration: 1 },
|
|
7
|
-
{ unit: "Second", multiplier: 1000, get duration() { return unitMap[0].duration * unitMap[1].multiplier } },
|
|
8
|
-
{ unit: "Minute", multiplier: 60, get duration() { return unitMap[1].duration * unitMap[2].multiplier } },
|
|
9
|
-
{ unit: "Hour", multiplier: 60, get duration() { return unitMap[2].duration * unitMap[3].multiplier } },
|
|
10
|
-
{ unit: "Day", multiplier: 24, get duration() { return unitMap[3].duration * unitMap[4].multiplier } }
|
|
11
|
-
]
|
|
12
|
-
|
|
13
|
-
var CalcDuration = function (unit, duration) { return (unitMap.find(o => o.unit === unit).duration * duration); }
|
|
14
|
-
function LoopTimer(n) {
|
|
15
|
-
RED.nodes.createNode(this, n);
|
|
16
|
-
|
|
17
|
-
this.units = n.units || "Second";
|
|
18
|
-
this.duration = n.duration || 5;
|
|
19
|
-
this.maxloops = n.maxloops || 100;
|
|
20
|
-
this.maxtimeoutunits = n.maxtimeoutunits || "Hour";
|
|
21
|
-
this.maxtimeout = n.maxtimeout || 1;
|
|
22
|
-
this.nodeduration = n.nodeduration || 1;
|
|
23
|
-
this.nodemaxtimeout = n.nodemaxtimeout || 1;
|
|
24
|
-
|
|
25
|
-
if (this.duration <= 0) {
|
|
26
|
-
this.duration = 0;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (this.maxtimeout <= 0) {
|
|
30
|
-
this.maxtimeout = 0;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
var node = this;
|
|
34
|
-
var loop = null;
|
|
35
|
-
var timeout = null;
|
|
36
|
-
var stopped = false;
|
|
37
|
-
var cpmsg = null;
|
|
38
|
-
var stopmsg = null;
|
|
39
|
-
var mlmsg = null;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
node.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
node.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
node.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
node.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
node.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
node.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
loop.
|
|
104
|
-
node.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
timeout
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
stopmsg
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
mlmsg
|
|
138
|
-
|
|
139
|
-
node.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
timeout = null;
|
|
151
|
-
if (((data + 1) * node.nodeduration) > node.nodemaxtimeout) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
1
|
+
module.exports = function (RED) {
|
|
2
|
+
"use strict";
|
|
3
|
+
var Looper = require('loop');
|
|
4
|
+
|
|
5
|
+
var unitMap = [
|
|
6
|
+
{ unit: "Millisecond", multiplier: 1, duration: 1 },
|
|
7
|
+
{ unit: "Second", multiplier: 1000, get duration() { return unitMap[0].duration * unitMap[1].multiplier } },
|
|
8
|
+
{ unit: "Minute", multiplier: 60, get duration() { return unitMap[1].duration * unitMap[2].multiplier } },
|
|
9
|
+
{ unit: "Hour", multiplier: 60, get duration() { return unitMap[2].duration * unitMap[3].multiplier } },
|
|
10
|
+
{ unit: "Day", multiplier: 24, get duration() { return unitMap[3].duration * unitMap[4].multiplier } }
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
var CalcDuration = function (unit, duration) { return (unitMap.find(o => o.unit === unit).duration * duration); }
|
|
14
|
+
function LoopTimer(n) {
|
|
15
|
+
RED.nodes.createNode(this, n);
|
|
16
|
+
|
|
17
|
+
this.units = n.units || "Second";
|
|
18
|
+
this.duration = n.duration || 5;
|
|
19
|
+
this.maxloops = n.maxloops || 100;
|
|
20
|
+
this.maxtimeoutunits = n.maxtimeoutunits || "Hour";
|
|
21
|
+
this.maxtimeout = n.maxtimeout || 1;
|
|
22
|
+
this.nodeduration = n.nodeduration || 1;
|
|
23
|
+
this.nodemaxtimeout = n.nodemaxtimeout || 1;
|
|
24
|
+
|
|
25
|
+
if (this.duration <= 0) {
|
|
26
|
+
this.duration = 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (this.maxtimeout <= 0) {
|
|
30
|
+
this.maxtimeout = 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var node = this;
|
|
34
|
+
var loop = null;
|
|
35
|
+
var timeout = null;
|
|
36
|
+
var stopped = false;
|
|
37
|
+
var cpmsg = null;
|
|
38
|
+
var stopmsg = null;
|
|
39
|
+
var mlmsg = null;
|
|
40
|
+
var tomsg = null;
|
|
41
|
+
|
|
42
|
+
this.on("input", function (msg) {
|
|
43
|
+
loop = Looper();
|
|
44
|
+
if (msg.loop != undefined) {
|
|
45
|
+
|
|
46
|
+
if (msg.loop.units != undefined) {
|
|
47
|
+
if ((msg.loop.units === "Millisecond") || (msg.loop.units === "Second") || (msg.loop.units === "Hour") || (msg.loop.units === "Day")) {
|
|
48
|
+
node.units = msg.loop.units;
|
|
49
|
+
} else {
|
|
50
|
+
node.status({ fill: "red", shape: "ring", text: "Invalid attribbute msg.loop.units" });
|
|
51
|
+
node.error('Invalid Attribbute: msg.loop.units, allowed values are Millisecond, Second, Minute, Hour, and Day');
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if (msg.loop.duration != undefined) {
|
|
58
|
+
if (typeof msg.loop.duration === 'number') {
|
|
59
|
+
node.duration = msg.loop.duration;
|
|
60
|
+
} else {
|
|
61
|
+
node.status({ fill: "red", shape: "ring", text: "msg.loop.units must be an Integer" });
|
|
62
|
+
node.error('Invalid Attribbute: msg.loop.duration, value must be an Integer');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (msg.loop.maxtimeoutunits != undefined) {
|
|
68
|
+
if ((msg.loop.maxtimeoutunits === "Millisecond") || (msg.loop.maxtimeoutunits === "Second") || (msg.loop.maxtimeoutunits === "Hour") || (msg.loop.maxtimeoutunits === "Day")) {
|
|
69
|
+
node.maxtimeoutunits = msg.loop.maxtimeoutunits;
|
|
70
|
+
} else {
|
|
71
|
+
node.status({ fill: "red", shape: "ring", text: "Invalid attribbute msg.loop.maxtimeoutunits" });
|
|
72
|
+
node.error('Invalid Attribbute: msg.loop.maxtimeoutunits, allowed values are Millisecond, Second, Minute, Hour, and Day');
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
if (msg.loop.maxtimeout != undefined) {
|
|
79
|
+
if (typeof msg.loop.maxtimeout === 'number') {
|
|
80
|
+
node.maxtimeout = msg.loop.maxtimeout;
|
|
81
|
+
} else {
|
|
82
|
+
node.status({ fill: "red", shape: "ring", text: "msg.loop.maxtimeout must be an Integer" });
|
|
83
|
+
node.error('Invalid Attribbute: msg.loop.maxtimeout, value must be an Integer');
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (msg.loop.maxloops != undefined) {
|
|
88
|
+
if (typeof msg.loop.maxloops === 'number') {
|
|
89
|
+
node.maxloops = msg.loop.maxloops;
|
|
90
|
+
} else {
|
|
91
|
+
node.status({ fill: "red", shape: "ring", text: "msg.loop.maxloops must be an Integer" });
|
|
92
|
+
node.error('Invalid Attribbute: msg.loop.maxloops, value must be an Integer');
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
}
|
|
100
|
+
node.nodeduration = CalcDuration(node.units, node.duration);
|
|
101
|
+
node.nodemaxtimeout = CalcDuration(node.maxtimeoutunits, node.maxtimeout);
|
|
102
|
+
|
|
103
|
+
loop.setTimeout(node.nodemaxtimeout);
|
|
104
|
+
loop.setMaxLoop(node.maxloops);
|
|
105
|
+
node.status({});
|
|
106
|
+
|
|
107
|
+
if (stopped === false || msg._timerpass !== true) {
|
|
108
|
+
stopped = false;
|
|
109
|
+
clearTimeout(timeout);
|
|
110
|
+
timeout = null;
|
|
111
|
+
if (msg.payload == "stop" || msg.payload == "STOP") {
|
|
112
|
+
node.status({
|
|
113
|
+
fill: "red",
|
|
114
|
+
shape: "ring",
|
|
115
|
+
text: "stopped"
|
|
116
|
+
});
|
|
117
|
+
stopped = true;
|
|
118
|
+
stopmsg = RED.util.cloneMessage(msg);
|
|
119
|
+
stopmsg.payload = "stopped";
|
|
120
|
+
node.send([null, stopmsg]);
|
|
121
|
+
stopped = false;
|
|
122
|
+
} else {
|
|
123
|
+
cpmsg = RED.util.cloneMessage(msg);
|
|
124
|
+
node.send([cpmsg, null]);
|
|
125
|
+
msg._timerpass = true;
|
|
126
|
+
loop.run(function (next, err, data) {
|
|
127
|
+
node.status({
|
|
128
|
+
fill: "green",
|
|
129
|
+
shape: "dot",
|
|
130
|
+
text: "running"
|
|
131
|
+
});
|
|
132
|
+
data++;
|
|
133
|
+
timeout = setTimeout(function () {
|
|
134
|
+
if (stopped === false) {
|
|
135
|
+
cpmsg = RED.util.cloneMessage(msg);
|
|
136
|
+
if (data == node.maxloops) {
|
|
137
|
+
mlmsg = RED.util.cloneMessage(msg);
|
|
138
|
+
mlmsg.payload = "max loops reached";
|
|
139
|
+
node.send([cpmsg, mlmsg]);
|
|
140
|
+
node.status({
|
|
141
|
+
fill: "red",
|
|
142
|
+
shape: "ring",
|
|
143
|
+
text: "max loops reached"
|
|
144
|
+
});
|
|
145
|
+
timeout = null;
|
|
146
|
+
next("break");
|
|
147
|
+
} else {
|
|
148
|
+
node.status({});
|
|
149
|
+
if ((data * node.nodeduration) <= node.nodemaxtimeout) {
|
|
150
|
+
timeout = null;
|
|
151
|
+
if (((data + 1) * node.nodeduration) > node.nodemaxtimeout) {
|
|
152
|
+
stopmsg = RED.util.cloneMessage(msg);
|
|
153
|
+
stopmsg.payload = 'timeout';
|
|
154
|
+
node.send([cpmsg, stopmsg]);
|
|
155
|
+
node.status({
|
|
156
|
+
fill: "red",
|
|
157
|
+
shape: "ring",
|
|
158
|
+
text: "timeout reached"
|
|
159
|
+
});
|
|
160
|
+
next("break");
|
|
161
|
+
} else {
|
|
162
|
+
next(undefined, data);
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
timeout = null;
|
|
166
|
+
next("break");
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
timeout = null;
|
|
171
|
+
next("break");
|
|
172
|
+
}
|
|
173
|
+
}, node.nodeduration);
|
|
174
|
+
}, 0);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
this.on("close", function () {
|
|
179
|
+
stopped = false;
|
|
180
|
+
if (timeout) {
|
|
181
|
+
clearTimeout(timeout);
|
|
182
|
+
}
|
|
183
|
+
node.status({});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
RED.nodes.registerType("looptimer-advanced", LoopTimer);
|
|
187
|
+
}
|
package/package.json
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "node-red-contrib-looptimer-advanced",
|
|
3
|
-
"version": "0.0.
|
|
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
|
-
"main": "looptimer_advanced.js",
|
|
6
|
-
"scripts": {},
|
|
7
|
-
"dependencies": {
|
|
8
|
-
"loop": "2.1.2"
|
|
9
|
-
},
|
|
10
|
-
"directories": {},
|
|
11
|
-
"keywords": [
|
|
12
|
-
"node-red",
|
|
13
|
-
"loop",
|
|
14
|
-
"timer",
|
|
15
|
-
"advanced"
|
|
16
|
-
],
|
|
17
|
-
"author": "Haxiboy",
|
|
18
|
-
"license": "Apache-2.0",
|
|
19
|
-
"node-red": {
|
|
20
|
-
"nodes": {
|
|
21
|
-
"looptimer-advanced": "looptimer_advanced.js"
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
"repository": {
|
|
25
|
-
"type": "git",
|
|
26
|
-
"url": "git+https://github.com/Haxiboy/node-red-contrib-looptimer-advanced.git"
|
|
27
|
-
},
|
|
28
|
-
"bugs": {
|
|
29
|
-
"url": "https://github.com/Haxiboy/node-red-contrib-looptimer-advanced/issues"
|
|
30
|
-
},
|
|
31
|
-
"homepage": "https://github.com/Haxiboy/node-red-contrib-looptimer-advanced#readme"
|
|
32
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "node-red-contrib-looptimer-advanced",
|
|
3
|
+
"version": "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
|
+
"main": "looptimer_advanced.js",
|
|
6
|
+
"scripts": {},
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"loop": "2.1.2"
|
|
9
|
+
},
|
|
10
|
+
"directories": {},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"node-red",
|
|
13
|
+
"loop",
|
|
14
|
+
"timer",
|
|
15
|
+
"advanced"
|
|
16
|
+
],
|
|
17
|
+
"author": "Haxiboy",
|
|
18
|
+
"license": "Apache-2.0",
|
|
19
|
+
"node-red": {
|
|
20
|
+
"nodes": {
|
|
21
|
+
"looptimer-advanced": "looptimer_advanced.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/Haxiboy/node-red-contrib-looptimer-advanced.git"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/Haxiboy/node-red-contrib-looptimer-advanced/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/Haxiboy/node-red-contrib-looptimer-advanced#readme"
|
|
32
|
+
}
|