-
Notifications
You must be signed in to change notification settings - Fork 13
/
countdown.js
179 lines (148 loc) · 5.67 KB
/
countdown.js
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
module.exports = function(RED) {
"use strict";
const isNumber = require('is-number');
function countdown(config) {
RED.nodes.createNode(this, config);
var node = this;
node.config = config;
// Local variables
var ticker = null;
var ticks = -1;
var timeout = parseInt(node.config.timer);
var stopMsg = {};
this.status({ fill: "red", shape: "dot", text: "Stopped: " + timeout });
function startTimer() {
timeout = timeout || parseInt(node.config.timer);
ticks = timeout;
node.status({
fill: "green", shape: "dot", text: "Running: " + ticks
});
// Timer Message
var msg = {}
msg.payload = RED.util.evaluateNodeProperty(node.config.payloadTimerStart, node.config.payloadTimerStartType, node);
if (node.config.topic !== '') {
msg.topic = node.config.topic;
}
// only send stop msg if type is not equal "send nothing" option
if (node.config.payloadTimerStartType !== "nul") {
node.send([msg, null]);
}
if (!ticker) {
ticker = setInterval(function() { node.emit("TIX"); }, 1000);
}
}
function stopTimer(onReset = false) {
node.status({
fill: "red", shape: "dot", text: "Stopped: " + timeout
});
// Timer Message
var msg = {}
if (node.config.payloadTimerStopType === 'msg') {
msg = stopMsg;
} else {
msg.payload = RED.util.evaluateNodeProperty(node.config.payloadTimerStop, node.config.payloadTimerStopType, node);
}
if (node.config.topic !== '') {
msg.topic = node.config.topic;
}
var remainingTicksMsg = { "payload": 0 };
// only send stop msg if type is not equal "send nothing" option
if (node.config.payloadTimerStopType == "nul") {
node.send([null, remainingTicksMsg]);
} else {
if (node.config.outputOnReset) {
node.send([msg, remainingTicksMsg]);
} else {
if (onReset) {
node.send([null, remainingTicksMsg]);
} else {
node.send([msg, remainingTicksMsg]);
}
}
}
endTicker();
}
function endTicker() {
if (ticker) {
clearInterval(ticker);
ticker = null;
}
ticks = -1;
}
node.on("TIX", function() {
if (ticks > 1) {
ticks--;
var remainingTicksMsg = { "payload": ticks };
node.send([null, remainingTicksMsg]);
node.status({
fill: "green", shape: "dot", text: "Running: " + ticks
});
} else if (ticks == 1){
stopTimer();
ticks = 0;
} else {
// Do nothing
}
});
node.on("input", function (msg) {
if (msg.topic === "control") {
if (isNumber(msg.payload) && msg.payload > 1) {
timeout = Math.ceil(msg.payload);
if (ticker) {
// countdown is running
if (node.config.setTimeToNewWhileRunning) {
ticks = msg.payload;
node.status({
fill: "green", shape: "dot", text: "Running: "+ timeout
});
}
} else {
// countdown is stopped
if (node.config.startCountdownOnControlMessage) {
startTimer();
} else {
node.status({
fill: "red", shape: "dot", text: "Stopped: "+ timeout
});
}
}
} else {
// do nothing
}
} else {
// if payload type of stop message is msg, store it in stopMsg var
if (node.config.payloadTimerStopType === 'msg') {
var prop = RED.util.evaluateNodeProperty(node.config.payloadTimerStop, node.config.payloadTimerStopType, node);
if(msg.hasOwnProperty(prop)) {
stopMsg = {
"payload": msg[prop]
};
} else {
node.warn("Property not set correctly! Input Message does not have property: " + prop);
stopMsg = {
"payload": prop
};
}
}
if (msg.payload === false || msg.payload === 0) {
stopTimer(true);
} else {
if (ticker) {
if (node.config.resetWhileRunning) {
endTicker();
startTimer();
}
} else {
startTimer();
}
}
}
});
node.on("close", function() {
if (ticker) {
clearInterval(ticker);
}
});
}
RED.nodes.registerType("countdown", countdown);
}