-
Notifications
You must be signed in to change notification settings - Fork 592
/
Copy pathtimeswitch.js
265 lines (235 loc) · 11.5 KB
/
timeswitch.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
module.exports = function (RED) {
"use strict";
var SunCalc = require('suncalc');
const spacetime = require("spacetime")
const SUNRISE_KEY = "sunrise";
const SUNSET_KEY = "sunset";
function TimeswitchNode(n) {
RED.nodes.createNode(this, n);
this.lat = n.lat;
this.lon = n.lon;
this.startt = n.starttime;
this.endt = n.endtime;
this.sunriseOffset = n.dawnoff;
this.sunsetOffset = n.duskoff;
this.mytopic = n.mytopic;
this.timezone = n.timezone || "UTC";
this.sun = n.sun;
this.mon = n.mon;
this.tue = n.tue;
this.wed = n.wed;
this.thu = n.thu;
this.fri = n.fri;
this.sat = n.sat;
this.jan = n.jan;
this.feb = n.feb;
this.mar = n.mar;
this.apr = n.apr;
this.may = n.may;
this.jun = n.jun;
this.jul = n.jul;
this.aug = n.aug;
this.sep = n.sep;
this.oct = n.oct;
this.nov = n.nov;
this.dec = n.dec;
var node = this;
this.on("input", function () {
// current global time
const now = spacetime.now();
const nowNative = now.toNativeDate();
// all sun events for the given lat/long
const sunEvents = SunCalc.getTimes(nowNative, node.lat, node.lon);
const sunAlt = SunCalc.getPosition(nowNative, node.lat, node.lon).altitude;
var sunriseDateTime = spacetime(sunEvents[SUNRISE_KEY]).nearest("minute");
var sunsetDateTime = spacetime(sunEvents[SUNSET_KEY]).nearest("minute");
if (sunEvents[SUNRISE_KEY] == "Invalid Date") {
if (sunAlt >= 0) { sunriseDateTime = now.startOf("day"); }
else { sunriseDateTime = now.endOf("day"); }
}
if (sunEvents[SUNSET_KEY] == "Invalid Date") {
if (sunAlt >= 0) { sunsetDateTime = now.endOf("day"); }
else { sunsetDateTime = now.startOf("day"); }
}
// add optional sun event offset, if specified
sunriseDateTime = sunriseDateTime.add(Number(node.sunriseOffset), "minutes");
sunsetDateTime = sunsetDateTime.add(Number(node.sunsetOffset), "minutes");
// check if sun event has already occurred today
if (now.isAfter(sunriseDateTime)) {
// get tomorrow's sunrise, since it'll be different
// sunriseDateTime = spacetime(SunCalc.getTimes(now.add(1, "day").toNativeDate(), node.lat, node.lon)[SUNRISE_KEY]).nearest("minute");
sunriseDateTime = sunriseDateTime.add(1, "day");
// add optional sun event offset, if specified (again)
sunriseDateTime = sunriseDateTime.add(Number(node.sunriseOffset), "minutes");
}
if (now.isAfter(sunsetDateTime)) {
// get tomorrow's sunset, since it'll be different
// sunsetDateTime = spacetime(SunCalc.getTimes(now.add(1, "day").toNativeDate(), node.lat, node.lon)[SUNSET_KEY]).nearest("minute");
sunsetDateTime = sunsetDateTime.add(1, "day");
// add optional sun event offset, if specified (again)
sunsetDateTime = sunsetDateTime.add(Number(node.sunsetOffset), "minutes");
}
// log sun events
if (RED.settings.verbose) {
node.log(`Sunrise ${sunriseDateTime.format("time")} - Sunset ${sunsetDateTime.format("time")} `);
}
// apply selected timezone to selected times (not to sunrise/sunset-- those are based on lat/long)
const currentTimeZone = now.timezone();
const selectedTimeZone = spacetime(now.epoch, this.timezone.toLowerCase()).timezone();
// handler function to convert minute strings (from <option> tags) to spacetime objects, called below
let getSelectedTimeFromMinuteString = minuteString => {
const selectedTimeInMinutesAfterMidnight = Number(minuteString);
let selectedTime = spacetime.now();
// if less than 1440, what are the time values for the next start and stop time?
if (selectedTimeInMinutesAfterMidnight < 1440) {
// determine offset to get from selected time zone to current timezone
// e.g. current (EDT) is -4, selected (PDT) is -7
// in order to get from PDT to EDT, you must add 3
// (-4) - (-7) = +3
const offset = currentTimeZone.current.offset - selectedTimeZone.current.offset;
const selectedHourValue = Math.floor(selectedTimeInMinutesAfterMidnight / 60);
const selectedMinuteValue = Math.floor(selectedTimeInMinutesAfterMidnight % 60);
selectedTime = selectedTime.hour(selectedHourValue).minute(selectedMinuteValue).second(0).millisecond(0);
selectedTime = selectedTime.add(offset, "hours");
// select the next time if it's in the past
if (now.isAfter(selectedTime)) {
selectedTime = selectedTime.add(1, "day");
}
} else if (selectedTimeInMinutesAfterMidnight == 5000) { // sunrise
selectedTime = sunriseDateTime;
} else if (selectedTimeInMinutesAfterMidnight == 6000) { // sunset
selectedTime = sunsetDateTime;
}
return selectedTime;
};
// our definitive next ON time
let selectedOnTime = getSelectedTimeFromMinuteString(node.startt);
// our definitive next OFF time
let selectedOffTime = getSelectedTimeFromMinuteString(node.endt);
// handle the "Start + X Minutes" cases
if (node.endt >= 10000) {
// even though the next start time might be tomorrow,
// the start time + X minutes might still be coming today,
// so we need to go back a day first
const selectedOnTimeMinus1Day = selectedOnTime.subtract(1, "day");
selectedOffTime = selectedOnTimeMinus1Day.add(node.endt - 10000, "minutes");
// _now_ we can check if the off time is in the past
if (now.isAfter(selectedOffTime)) {
selectedOffTime = selectedOffTime.add(1, "day");
}
}
// handler function for the node payload, called below
let sendPayload = (payload, nextTime) => {
// var o = nextTime.goto(selectedTimeZone.name).offset()/60;
// if (o > 0) { o = "+" + o; }
// else {o = "-" + o; }
if (nextTime) {
if (payload == 1) {
node.status({
fill: "yellow",
shape: "dot",
text: `on until ${nextTime.goto(selectedTimeZone.name).format("time-24")}`
});
} else {
node.status({
fill: "blue",
shape: "dot",
text: `off until ${nextTime.goto(selectedTimeZone.name).format("time-24")}`
});
}
} else {
if (payload == 1) {
node.status({
fill: "yellow",
shape: "dot",
text: `on`
});
} else {
node.status({
fill: "blue",
shape: "dot",
text: `off`
});
}
}
var msg = {};
if (node.mytopic) {
msg.topic = node.mytopic;
}
msg.payload = payload;
node.send(msg);
};
var proceed = true;
// if today is not among the selected days of the week, stop here
switch (nowNative.getDay()) {
case 0 : { if (!node.sun) { proceed &= false; } break; }
case 1 : { if (!node.mon) { proceed &= false; } break; }
case 2 : { if (!node.tue) { proceed &= false; } break; }
case 3 : { if (!node.wed) { proceed &= false; } break; }
case 4 : { if (!node.thu) { proceed &= false; } break; }
case 5 : { if (!node.fri) { proceed &= false; } break; }
case 6 : { if (!node.sat) { proceed &= false; } break; }
}
if (!proceed) {
sendPayload(0, selectedOnTime);
return;
}
// if this month is not among the selected months, stop here
switch (nowNative.getMonth()) {
case 0 : { if (!node.jan) { proceed &= false; } break; }
case 1 : { if (!node.feb) { proceed &= false; } break; }
case 2 : { if (!node.mar) { proceed &= false; } break; }
case 3 : { if (!node.apr) { proceed &= false; } break; }
case 4 : { if (!node.may) { proceed &= false; } break; }
case 5 : { if (!node.jun) { proceed &= false; } break; }
case 6 : { if (!node.jul) { proceed &= false; } break; }
case 7 : { if (!node.aug) { proceed &= false; } break; }
case 8 : { if (!node.sep) { proceed &= false; } break; }
case 9 : { if (!node.oct) { proceed &= false; } break; }
case 10: { if (!node.nov) { proceed &= false; } break; }
case 11: { if (!node.dec) { proceed &= false; } break; }
}
if (!proceed) {
sendPayload(0, selectedOnTime);
return;
}
// if the chronological order is NOW --> ON --> OFF, then now should be OFF
if (proceed && selectedOffTime.isAfter(selectedOnTime)) {
sendPayload(0, selectedOnTime);
return;
}
// if the chronological order is NOW --> OFF --> ON, then now should be ON
if (proceed && selectedOffTime.isBefore(selectedOnTime)) {
sendPayload(1, selectedOffTime);
return;
}
// Note: we already ensured that all ON or OFF times would be in the future,
// so there is no midnight wrapping issue.
});
var tock = setTimeout(function () {
node.emit("input", {});
}, 2000); // wait 2 secs before starting to let things settle down – e.g. UI connect
var tick = setInterval(function () {
node.emit("input", {});
}, 60000); // trigger every 60 secs
this.on("close", function () {
if (tock) { clearTimeout(tock); }
if (tick) { clearInterval(tick); }
});
}
RED.httpAdmin.post("/timeswitch/:id", RED.auth.needsPermission("timeswitch.write"), function (req, res) {
var node = RED.nodes.getNode(req.params.id);
if (node != null) {
try {
node.emit("input", { payload: "reset" });
res.sendStatus(200);
} catch (err) {
res.sendStatus(500);
node.error("Inject failed:" + err);
}
} else {
res.sendStatus(404);
}
});
RED.nodes.registerType("timeswitch", TimeswitchNode);
};