-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrashschedule.js
303 lines (262 loc) · 10.3 KB
/
trashschedule.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* eslint-disable no-shadow */
/* eslint-disable prefer-destructuring */
// eslint-disable-next-line func-names
const validateEvent = require('./src/validateEvent');
const sortTrashschedule = require('./src/sortTrashschedule');
// eslint-disable-next-line func-names
module.exports = function (RED) {
function trashschedule(config) {
RED.nodes.createNode(this, config);
const node = this;
let trashschedule = config.trashschedule;
const skipHour = config.skipHour;
let currentDay; // current day 1 - 31
let currentMonth; // current month 0 - 11
let currentYear; // current year yyyy
let currentHour; // current hour hh
let currentMinute; // current minute mm
// refresh time vars
function setCurrentDate() {
const date = new Date();
currentDay = date.getDate();
currentMonth = date.getMonth();
currentYear = date.getFullYear();
currentHour = date.getHours();
currentMinute = date.getMinutes();
}
// calculate daysLeft
function calcDaysLeft(trashscheduleElement) {
const daysLeft = Math.round((new Date(
trashscheduleElement.year,
trashscheduleElement.month - 1,
trashscheduleElement.day,
).valueOf() - new Date(
currentYear,
currentMonth,
currentDay,
).valueOf()) / 86400000);
return daysLeft;
}
// send all trashschedule events related to current date
function sendAllEvents() {
// sort trashschedule array by yyyy, mm, dd
trashschedule = sortTrashschedule(trashschedule);
node.send([null, null, { payload: trashschedule }]);
}
// send next three trashschedule events related to current date
function sendNextThreeTrashEvents() {
// sort trashschedule array by yyyy, mm, dd
trashschedule = sortTrashschedule(trashschedule);
// outputArr bundles the 3 events
const outputArr = [];
// check wether trashschedule array contains trashschedule events
if (trashschedule.length > 0) {
for (let index = 0; index < trashschedule.length; index += 1) {
const trashscheduleElement = trashschedule[index];
const trashscheduleYear = trashscheduleElement.year;
const trashscheduleMonth = trashscheduleElement.month - 1;
const trashscheduleDay = trashscheduleElement.day;
if (new Date(
trashscheduleYear,
trashscheduleMonth,
trashscheduleDay,
).valueOf() - new Date(
currentYear,
currentMonth,
currentDay,
).valueOf() >= 0) {
if (trashscheduleYear === currentYear
&& trashscheduleMonth === currentMonth
&& trashscheduleDay === currentDay) {
if (currentHour < skipHour) {
// first event
if (validateEvent(trashscheduleElement)) {
// set days left
trashscheduleElement.daysLeft = 0;
outputArr[0] = trashscheduleElement;
} else {
outputArr[0] = 'Trashschedule events outdated';
}
// second event
if (validateEvent(trashschedule[index + 1])) {
// calculate days left
trashschedule[index + 1].daysLeft = calcDaysLeft(trashschedule[index + 1]);
outputArr[1] = trashschedule[index + 1];
} else {
outputArr[1] = 'Trashschedule events outdated';
}
// third event
if (validateEvent(trashschedule[index + 2])) {
// calculate days left
trashschedule[index + 2].daysLeft = calcDaysLeft(trashschedule[index + 2]);
outputArr[2] = trashschedule[index + 2];
} else {
outputArr[2] = 'Trashschedule events outdated';
}
break;
} else {
// first event
if (validateEvent(trashschedule[index + 1])) {
// calculate days left
trashschedule[index + 1].daysLeft = calcDaysLeft(trashschedule[index + 1]);
outputArr[0] = trashschedule[index + 1];
} else {
outputArr[0] = 'Trashschedule events outdated';
}
// second event
if (validateEvent(trashschedule[index + 2])) {
// calculate days left
trashschedule[index + 2].daysLeft = calcDaysLeft(trashschedule[index + 2]);
outputArr[1] = trashschedule[index + 2];
} else {
outputArr[1] = 'Trashschedule events outdated';
}
// third event
if (validateEvent(trashschedule[index + 3])) {
// calculate days left
trashschedule[index + 3].daysLeft = calcDaysLeft(trashschedule[index + 3]);
outputArr[2] = trashschedule[index + 3];
} else {
outputArr[2] = 'Trashschedule events outdated';
}
break;
}
} else {
// first event
if (validateEvent(trashscheduleElement)) {
// calculate days left
trashscheduleElement.daysLeft = calcDaysLeft(trashscheduleElement);
outputArr[0] = trashscheduleElement;
} else {
outputArr[0] = 'Trashschedule events outdated';
}
// second event
if (validateEvent(trashschedule[index + 1])) {
// calculate days left
trashschedule[index + 1].daysLeft = calcDaysLeft(trashschedule[index + 1]);
outputArr[1] = trashschedule[index + 1];
} else {
outputArr[1] = 'Trashschedule events outdated';
}
// third event
if (validateEvent(trashschedule[index + 2])) {
// calculate days left
trashschedule[index + 2].daysLeft = calcDaysLeft(trashschedule[index + 2]);
outputArr[2] = trashschedule[index + 2];
} else {
outputArr[2] = 'Trashschedule events outdated';
}
break;
}
}
}
} else {
// trashschedule array is empty
outputArr[0] = 'Trashschedule events outdated';
outputArr[1] = 'Trashschedule events outdated';
outputArr[2] = 'Trashschedule events outdated';
}
node.send([null, { payload: outputArr }, null]);
}
// check wether today is trashschedule event
function checkTrashschedule() {
// sort trashschedule array by yyyy, mm, dd
trashschedule = sortTrashschedule(trashschedule);
// check wether trashschedule array contains trashschedule events
if (trashschedule.length > 0) {
for (let index = 0; index < trashschedule.length; index += 1) {
const trashscheduleElement = trashschedule[index];
const trashscheduleYear = trashscheduleElement.year;
const trashscheduleMonth = trashscheduleElement.month - 1;
const trashscheduleDay = trashscheduleElement.day;
// search for latest trashschedule event in trashschedule array
if (new Date(
trashscheduleYear,
trashscheduleMonth,
trashscheduleDay,
).valueOf() - new Date(
currentYear,
currentMonth,
currentDay,
).valueOf() >= 0) {
// check wether trashschedule event is today
if (trashscheduleYear === currentYear
&& trashscheduleMonth === currentMonth
&& trashscheduleDay === currentDay) {
// check wether skiphour is already over
if (currentHour < skipHour) {
// validate trashschedule event
if (validateEvent(trashscheduleElement)) {
// set days left
trashscheduleElement.daysLeft = 0;
node.send([{ payload: trashscheduleElement }, null, null]);
} else {
node.send([{ payload: 'Trashschedule events outdated' }, null, null]);
}
break;
} else {
if (validateEvent(trashschedule[index + 1])) {
// calculate days left
trashschedule[index + 1].daysLeft = calcDaysLeft(trashschedule[index + 1]);
node.send([{ payload: trashschedule[index + 1] }, null, null]);
} else {
node.send([{ payload: 'Trashschedule events outdated' }, null, null]);
}
break;
}
} else {
if (validateEvent(trashscheduleElement)) {
// calculate days left
trashscheduleElement.daysLeft = calcDaysLeft(trashscheduleElement);
node.send([{ payload: trashscheduleElement }, null, null]);
} else {
node.send([{ payload: 'Trashschedule events outdated' }, null, null]);
}
break;
}
}
}
} else {
// trashschedule array is empty
node.send([{ payload: 'Trashschedule events outdated' }, null, null]);
}
}
// listen to node's input
this.on('input', (msg) => {
const payload = msg.payload;
switch (payload) {
case 'checkTrashschedule':
checkTrashschedule();
break;
case 'checkNextThree':
sendNextThreeTrashEvents();
break;
case 'all':
sendAllEvents();
break;
default:
checkTrashschedule();
sendNextThreeTrashEvents();
sendAllEvents();
break;
}
});
const dailyInterval = setInterval(() => {
setCurrentDate();
if (currentMinute === 0) {
checkTrashschedule();
sendNextThreeTrashEvents();
sendAllEvents();
}
}, 60000);
const setTimeInterval = setInterval(() => {
setCurrentDate();
});
// listen wether node has been closed
this.on('close', () => {
clearInterval(dailyInterval);
clearInterval(setTimeInterval);
});
}
RED.nodes.registerType('trashschedule', trashschedule);
};