-
Notifications
You must be signed in to change notification settings - Fork 1
/
suncron.js
349 lines (303 loc) · 8.74 KB
/
suncron.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const CronJob = require('cron').CronJob
const dayjs = require('dayjs')
const SunCalc = require('suncalc')
module.exports = function (RED) {
function SuncronNode(config) {
RED.nodes.createNode(this, config)
const node = this
let schedule
const eventTypes = [
'sunrise',
'sunriseEnd',
'goldenHourEnd',
'solarNoon',
'goldenHour',
'sunsetStart',
'sunset',
'dusk',
'nauticalDusk',
'night',
'nadir',
'nightEnd',
'nauticalDawn',
'dawn',
]
let msgCrons = []
let dailyCron = []
const letsGo = function () {
schedule = calcScheduleForToday()
if (config.replay === true) {
try {
const mostRecentEvent = findMostRecentEvent(schedule)
setTimeout(() => {
ejectMsg(mostRecentEvent, schedule)
}, 500)
} catch (e) {
debug(e)
}
}
installMsgCronjobs(schedule)
setTimeout(() => {
ejectSchedule(schedule)
}, 500)
debug(schedule)
dailyCron = installDailyCronjob()
}
const calcScheduleForToday = function () {
const today = new Date()
const midday = new Date(
today.getFullYear(),
today.getMonth(),
today.getDate(),
12,
0,
0,
0,
0
)
const sunTimes = SunCalc.getTimes(midday, config.lat, config.lon)
return eventTypes.reduce((result, eventType) => {
const payload = config[`${eventType}Payload`]
if (payload !== '') {
const payloadType = config[`${eventType}PayloadType`]
const topic = config[`${eventType}Topic`]
const sunEventTime = dayjs(sunTimes[eventType])
const offsetSec = config[`${eventType}Offset`]
const offsetType = config[`${eventType}OffsetType`]
const offset = offsetSec * offsetType
let cronTime
if (offset > 0) {
cronTime = dayjs(sunEventTime).add(offsetSec, 'second')
} else {
cronTime = dayjs(sunEventTime).subtract(offsetSec, 'second')
}
try {
result[eventType] = {
event: eventType,
sunEventTimeUTC: sunEventTime.toISOString(),
sunEventTimeLocal: sunEventTime.format('YYYY-MM-DDTHH:mm:ss'),
offset: offsetSec * offsetType,
cronTime,
cronTimeUTC: cronTime.toISOString(),
cronTimeLocal: cronTime.format('YYYY-MM-DDTHH:mm:ss'),
payload,
payloadType,
topic,
}
} catch (e) {
console.log(
`ignoring event type '${eventType}' as no event time could be determined for current day.`
)
}
}
return result
}, {})
}
const formatSchedule = function (schedule) {
let result = {}
for (let eventType in schedule) {
let event = schedule[eventType]
result[eventType] = {
event: eventType,
sunEventTime: event.sunEventTimeLocal,
cronTime: event.cronTimeLocal,
offset: event.offset,
}
}
return result
}
function findNextEvent(schedule) {
let futureEvents = Object.keys(schedule)
.map((eventType) => ({
eventName: eventType,
eventTime: schedule[eventType].cronTime,
}))
.sort((e1, e2) => e1.eventTime.unix() - e2.eventTime.unix())
.filter((event) => event.eventTime.isAfter(dayjs()))
if (futureEvents.length > 0) {
return futureEvents.shift()
} else {
throw new Error('done for today')
}
}
const installMsgCronjobs = function (schedule) {
stopMsgCrons()
let i = 0
for (let eventType in schedule) {
i++
let event = schedule[eventType]
// override cronTimes for debugging purpose
if (RED.settings.suncronMockTimes) {
event.cronTime = dayjs().add(i * 5, 'second')
}
let cron = new CronJob({
cronTime: event.cronTime.toDate(),
onTick: () => {
ejectMsg(event, schedule)
setNodeStatus(
`now: ${event.event} @ ${event.cronTime.format('HH:mm')}`,
'green'
)
setTimeout(() => {
setNodeStatusToNextEvent(schedule)
}, 2000)
},
})
try {
cron.start()
msgCrons.push(cron)
} catch (err) {
debug(`${event.event}: ${err.message}`)
}
}
debug(`${i} msg crons installed`)
setNodeStatus(`${i} crons active`)
setTimeout(() => {
setNodeStatusToNextEvent(schedule)
}, 2000)
}
const installDailyCronjob = function () {
// run daily cron 5 seconds past midnight (except for debugging: 5 seconds past the full minute)
const cronTime = RED.settings.suncronMockTimes
? '5 * * * * *'
: '5 0 0 * * *'
const cron = new CronJob({
cronTime,
onTick: () => {
schedule = calcScheduleForToday()
installMsgCronjobs(schedule)
ejectSchedule(schedule)
setNodeStatusToNextEvent(schedule)
},
})
cron.start()
return cron
}
const debug = function (debugMsg) {
if (RED.settings.suncronDebug) {
node.warn(debugMsg)
}
}
const setNodeStatus = function (text, color = 'grey') {
node.status({
fill: color,
shape: 'dot',
text: text,
})
}
const setNodeStatusToNextEvent = function (schedule) {
try {
const nextEvent = findNextEvent(schedule)
setNodeStatus(
`next: ${nextEvent.eventName} @ ${nextEvent.eventTime.format(
'HH:mm'
)}`
)
} catch (err) {
setNodeStatus(err.message)
}
}
const findMostRecentEvent = function (schedule) {
let pastEvents = Object.keys(schedule)
.map((eventType) => schedule[eventType])
.sort((e1, e2) => e2.cronTime.unix() - e1.cronTime.unix())
.filter((event) => event.cronTime.isBefore(dayjs()))
if (pastEvents.length > 0) {
return pastEvents.shift()
} else {
throw new Error('no past events')
}
}
const stopMsgCrons = function () {
if (msgCrons.length > 0) {
msgCrons.forEach((cron) => {
cron.stop()
})
debug(`${msgCrons.length} msg crons deleted`)
msgCrons = []
}
}
const ejectMsg = function ({ payload, payloadType, topic }, schedule) {
const castPayload = (payload, payloadType) => {
if (payloadType === 'num') {
return Number(payload)
} else if (payloadType === 'bool') {
return payload === 'true'
} else if (payloadType === 'json') {
return JSON.parse(payload)
} else {
return payload
}
}
let formatedSchedule = formatSchedule(schedule)
let next
try {
next = formatedSchedule[findNextEvent(schedule).eventName]
} catch (e) {
next = null
}
node.send({
topic,
payload: castPayload(payload, payloadType),
schedule: formatedSchedule,
next,
})
}
const ejectSchedule = function (schedule) {
if (!config.ejectScheduleOnUpdate) {
return
}
node.send({
topic: 'suncron:schedule',
payload: formatSchedule(schedule),
})
}
node.on('input', function (msg, send, done) {
send =
send ||
function () {
node.send.apply(node, arguments)
}
if (typeof msg.payload === 'object') {
// config object received as msg.payload
debug(`!!!! CONFIG OBJECT RECEIVED !!!`)
// debug(msg.payload)
eventTypes.forEach((eventType) => {
if (
msg.payload.hasOwnProperty(eventType) &&
Number.isInteger(msg.payload[eventType])
) {
debug(`new offset for ${eventType}: ${msg.payload[eventType]}`)
config[`${eventType}Offset`] = Math.abs(msg.payload[eventType])
config[`${eventType}OffsetType`] =
msg.payload[eventType] < 0 ? -1 : 1
}
})
letsGo()
} else {
try {
const mostRecentEvent = findMostRecentEvent(schedule)
ejectMsg(mostRecentEvent, schedule)
} catch (e) {
debug(e)
}
}
if (done) {
done()
}
})
node.on('close', function () {
stopMsgCrons()
dailyCron.stop()
})
;(function () {
// on startup:
try {
letsGo()
} catch (e) {
console.log(e)
}
})()
}
RED.nodes.registerType('suncron', SuncronNode)
}