forked from ontje/node-red-contrib-countdown
-
Notifications
You must be signed in to change notification settings - Fork 4
/
countdown.js
206 lines (172 loc) · 6.36 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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 = getTimeout();
var stopMsg = {};
this.status({ fill: "red", shape: "dot", text: "Stopped: " + timeout });
function getTimeout() {
if(node.config.minuteCounter) {
return parseInt(node.config.timer) * 60;
} else {
return parseInt(node.config.timer);
}
}
function runningMessage(ticks) {
var t = ticks;
var m = false;
if(t / 60 > 1) {
t = t / 60;
t = t.toFixed(2);
//node.warn("minute running: " + t);
m = true;
}
var ret = "Running: " + t;
if(m) {
ret = ret + " minute(s)";
} else {
ret = ret + " second(s)";
}
return ret;
}
function startTimer() {
timeout = timeout || getTimeout();
ticks = timeout;
// running status message
node.status({
fill: "green", shape: "dot", text: runningMessage(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(output=true) {
var tOutMsg = timeout + " sec";
if(node.config.minuteCounter) {
tOutMsg = parseInt(node.config.timer) + " min";
}
node.status({
fill: "red", shape: "dot", text: "Stopped: " + tOutMsg
});
// Timer Message
var msg = {}
var cancel = false;
if(output) {
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;
}
} else {
msg = null;
cancel = true;
}
var remainingTicksMsg = { "payload": ticks, "cancled": cancel };
// only send stop msg if type is not equal "send nothing" option
if (node.config.payloadTimerStopType == "nul") {
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]);
// update Running status message
node.status({
fill: "green", shape: "dot", text: runningMessage(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 {
if(msg.payload === "cancel") {
stopTimer(false);
}
if(msg.payload === "reset") {
startTimer();
}
}
} else {
if(node.config.payloadTimerStopType === 'msg') {
var prop = RED.util.evaluateNodeProperty(node.config.payloadTimerStop, node.config.payloadTimerStopType, node);
stopMsg = {
"payload": RED.util.getMessageProperty(msg, node.config.payloadTimerStop)
}
if(typeof stopMsg.payload === 'undefined' ) {
node.warn("Message Attribute not defined");
}
}
if (ticker) {
if (node.config.resetWhileRunning) {
endTicker();
startTimer();
}
} else {
startTimer();
}
}
});
node.on("close", function() {
if (ticker) {
clearInterval(ticker);
}
});
}
RED.nodes.registerType("countdown", countdown);
}