-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
306 lines (269 loc) · 13.4 KB
/
index.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
"use strict";
let Service, Characteristic, ContactState;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
ContactState = homebridge.hap.Characteristic.ContactSensorState;
homebridge.registerAccessory("homebridge-jewish-calendar","JewishCalendar", JewishCalendar);
};
class JewishCalendar {
constructor (log, config, api) {
this.log = log;
this.lat = parseFloat(config.latitude);
this.long = parseFloat(config.longitude);
this.name = config.name;
this.il = config.israel;
this.sheminiatzeret_in_sukkot = config.sheminiatzeret_in_sukkot;
this.candlelighting = config.candlelighting;
this.havdalah = config.havdalah;
this.sefiratOmerCustom = config.sefiratOmerCustom;
this.threeWeeksCustom = config.threeWeeksCustom;
this.HeDate = require('he-date');
this.SunCalc = require('suncalc');
this.offset = config.offset;
this.services = {};
this.services.Shabbat = new Service.ContactSensor(config.Shabbat, "Shabbat");
this.services.YomTov = new Service.ContactSensor(config.YomTov, "YomTov");
this.services.Kodesh = new Service.ContactSensor(config.Kodesh, "Kodesh");
this.services.RoshHashana = new Service.ContactSensor(config.RoshHashana, "RoshHashana");
this.services.YomKippur = new Service.ContactSensor(config.YomKippur, "YomKippur");
this.services.Sukkot = new Service.ContactSensor(config.Sukkot, "Sukkot");
this.services.SheminiAtzeret = new Service.ContactSensor(config.SheminiAtzeret, "SheminiAtzeret");
this.services.Pesach = new Service.ContactSensor(config.Pesach, "Pesach");
this.services.Shavuot = new Service.ContactSensor(config.Shavuot, "Shavuot");
this.services.Chanukah = new Service.ContactSensor(config.Chanukah, "Chanukah");
this.services.ThreeWeeks = new Service.ContactSensor(config.ThreeWeeks, "ThreeWeeks");
this.services.Omer = new Service.ContactSensor(config.Omer, "Omer");
this.services.SefiratOmer = new Service.ContactSensor(config.SefiratOmer, "SefiratOmer");
this.services.Mourning = new Service.ContactSensor(config.Mourning, "Mourning");
this.updateJewishDay();
this.updateSensors();
setTimeout(this.updateLoop.bind(this), 30000);
}
updateSensors() {
this.services.Shabbat.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isShabbat());
this.services.YomTov.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isYomTov());
this.services.Kodesh.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isKodesh());
this.services.RoshHashana.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isRoshHashana());
this.services.YomKippur.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isYomKippur());
this.services.Sukkot.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isSukkot());
this.services.SheminiAtzeret.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isSheminiAtzeret());
this.services.Pesach.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isPesach());
this.services.Shavuot.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isShavuot());
this.services.Chanukah.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isChanukah());
this.services.ThreeWeeks.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isThreeWeeks());
this.services.Omer.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isOmer());
this.services.SefiratOmer.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isSefiratOmer());
this.services.Mourning.getCharacteristic(Characteristic.ContactSensorState).setValue(this.isMourning());
}
getName(obj, callback) {
callback(null, obj.name);
}
getServices() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, "Alex Hochberger")
.setCharacteristic(Characteristic.Model, "Standard Jewish Calendar")
.setCharacteristic(Characteristic.SerialNumber, "613")
.setCharacteristic(Characteristic.FirmwareRevision, 613);
var services = [
informationService,
this.services.Shabbat,
this.services.YomTov,
this.services.Kodesh,
this.services.RoshHashana,
this.services.YomKippur,
this.services.Sukkot,
this.services.SheminiAtzeret,
this.services.Pesach,
this.services.Shavuot,
this.services.Chanukah,
this.services.ThreeWeeks,
this.services.Omer,
this.services.SefiratOmer,
this.services.Mourning
];
return services;
}
updateJewishDay() {
this.gDate = new Date();
if ((typeof this.offset !== 'undefined') && (this.offset != 0)) {
this.log.debug("Shifting the time by " + this.offset + " minutes.");
this.gDate = new Date(this.gDate.getTime() + this.offset * 60000);
}
this.log.debug("Test date is " + this.gDate.toISOString());
this.hDate = new this.HeDate(this.gDate);
// Extremely weird bug in Suncalc has it calculate the wrong times at edges of the day. Workaround is to always check at noon
var midday = new Date(this.gDate.getFullYear(), this.gDate.getMonth(), this.gDate.getDate(), 12, 0, 0, 0, 0);
// For debugging, track them both
this.log.debug("updateJewishDay(): today=" + this.gDate.toISOString());
this.log.debug("updateJewishDay(): midday=" + midday.toISOString());
var suntimes = this.SunCalc.getTimes(midday, this.lat, this.long);
this.sunset = suntimes.sunsetStart;
this.log.debug("Sunset Tonight: " + this.sunset.toLocaleString());
// Note, this is for programming. In non leap years, Adar1 and Adar2 are BOTH 5. Month is zero indexed.
this.hebrewMonths = {'Tishri': 0, 'Heshvan': 1, 'Kislev': 2, 'Tevet': 3, 'Shevat': 4, 'Adar1': 5};
var thisYear = this.hDate.getFullYear();
this.hebrewMonths.Adar2 = new this.HeDate(thisYear + 1, -7).getMonth();
this.hebrewMonths.Nisan = new this.HeDate(thisYear + 1, -6).getMonth();
this.hebrewMonths.Iyar = new this.HeDate(thisYear + 1, -5).getMonth();
this.hebrewMonths.Sivan = new this.HeDate(thisYear + 1, -4).getMonth();
this.hebrewMonths.Tamuz = new this.HeDate(thisYear + 1, -3).getMonth();
this.hebrewMonths.Av = new this.HeDate(thisYear + 1, -2).getMonth();
this.hebrewMonths.Elul = new this.HeDate(thisYear + 1, -1).getMonth();
this.log.debug("This Year's Hebrew Months: ");
this.log.debug(this.hebrewMonths);
}
updateLoop() {
var today = new Date();
/* if (
(this.gDate.getFullYear() != today.getFullYear()) ||
(this.gDate.getMonth() != today.getMonth()) ||
(this.gDate.getDate() != today.getDate())
) {
*/
this.updateJewishDay();
/*
}
*/
this.updateSensors();
setTimeout(this.updateLoop.bind(this), 30000);
}
isShabbat() {
var day = this.gDate.getDay();
var candletime = new Date(this.sunset);
candletime.setMinutes(this.sunset.getMinutes() - this.candlelighting);
var havdalahtime = new Date(this.sunset);
havdalahtime.setMinutes(this.sunset.getMinutes() + this.havdalah);
return (((5 == day) && (this.gDate > candletime)) || ((6 == day) && (this.gDate < havdalahtime)));
}
isRoshHashana() {
// Because of year wraps, if it's Elul 29, we check candle lighting, otherwise, use normal DateRange
if ((this.hDate.getMonth() == this.hebrewMonths.Elul)&& this.hDate.getDate () == 29) {
var candletime = new Date(this.sunset);
candletime.setMinutes(this.sunset.getMinutes() - this.candlelighting);
return this.gDate > candletime;
}
return this._inHebrewHolidayDateRange({month: this.hebrewMonths.Tishri, date: 0}, {month: this.hebrewMonths.Tishri, date: 2});
}
isYomKippur() {
return this._inHebrewHolidayDateRange({month: this.hebrewMonths.Tishri, date: 9}, {month: this.hebrewMonths.Tishri, date: 10});
}
isSukkot() {
var begin = {month: this.hebrewMonths.Tishri, date: 14};
var end = (!this.il && this.sheminiatzeret_in_sukkot) ? {month: this.hebrewMonths.Tishri, date: 22} : {month: this.hebrewMonths.Tishri, date: 21};
return this._inHebrewHolidayDateRange(begin, end);
}
_isSukkotYomTov() {
var begin = {month: this.hebrewMonths.Tishri, date: 14};
var end = (this.il) ? {month: this.hebrewMonths.Tishri, date: 15} : {month: this.hebrewMonths.Tishri, date: 16};
return this._inHebrewHolidayDateRange(begin, end);
}
isSheminiAtzeret() {
var begin = {month: this.hebrewMonths.Tishri, date: 21};
var end = (this.il) ? {month: this.hebrewMonths.Tishri, date: 22} : {month: this.hebrewMonths.Tishri, date: 23};
return this._inHebrewHolidayDateRange(begin, end);
}
isPesach() {
var begin = {month: this.hebrewMonths.Nisan, date: 14};
var end = (this.il) ? {month: this.hebrewMonths.Nisan, date: 21} : {month: this.hebrewMonths.Nisan, date: 22};
return this._inHebrewHolidayDateRange(begin, end);
}
isThreeWeeks() {
var begin; // night before Erev 17th of Tamuz
if (this.threeWeeksCustom == "Ashkenazi") {
begin = {month: this.hebrewMonths.Tamuz, date: 16};
} else if (this.threeWeeksCustom == "Sephardic") {
begin = {month: this.hebrewMonths.Tamuz, date: 29};
}
var Av9 = new this.HeDate(this.hDate.getFullYear(), this.hebrewMonths.Av, 9);
var endDate = (Av9.getDay() == 6) ? 11 : 10; // Includes day after Fast.
var end = {month: this.hebrewMonths.Av, date: endDate };
return this._inHebrewHolidayDateRange(begin, end);
}
_isPesachYomTov() {
// Leap years can make Nisan's month number "bounce" so we check for it
var begin = {month: this.hebrewMonths.Nisan, date: 14};
var end = (this.il) ? {month: this.hebrewMonths.Nisan, date: 15} : {month: this.hebrewMonths.Nisan, date: 16};
var firstDays = this._inHebrewHolidayDateRange(begin, end);
begin = {month: this.hebrewMonths.Nisan, date: 20};
end = (this.il) ? {month: this.hebrewMonths.Nisan, date: 21} : {month: this.hebrewMonths.Nisan, date: 22};
var secondDays = this._inHebrewHolidayDateRange(begin, end);
return firstDays || secondDays;
}
isOmer() {
var begin = {month: this.hebrewMonths.Nisan, date: 15};
var end = {month: this.hebrewMonths.Sivan, date: 6};
return this._inHebrewHolidayDateRange(begin, end);
}
isSefiratOmer() {
var begin = false;
var end = false;
if (this.sefiratOmerCustom == "Ashkenazi") {
begin = {month: this.hebrewMonths.Nisan, date: 15};
end = {month: this.hebrewMonths.Iyar, date: 18};
} else if (this.sefiratOmerCustom == "Sephardic") {
begin = {month: this.hebrewMonths.Nisan, date: 15};
end = {month: this.hebrewMonths.Iyar, date: 19};
} else if (this.sefiratOmerCustom == "Iyar") {
begin = {month: this.hebrewMonths.Nisan, date: 29};
end = {month: this.hebrewMonths.Sivan, date: 3};
} else if (this.sefiratOmerCustom == "Iyar2") {
begin = {month: this.hebrewMonths.Iyar, date: 2};
end = {month: this.hebrewMonths.Sivan, date: 5};
}
if (begin && end) {
return this._inHebrewHolidayDateRange(begin, end);
}
return false;
}
isMourning() { return this.isSefiratOmer() || this.isThreeWeeks();}
isShavuot() {
// Leap years can make Sivan's month number "bounce" so we check for it
var begin = {month: this.hebrewMonths.Sivan, date: 5};
var end = (this.il) ? {month: this.hebrewMonths.Sivan, date: 7} : {month: this.hebrewMonths.Sivan, date: 7};
return this._inHebrewHolidayDateRange(begin, end);
}
isYomTov() {
var holidays = this.isRoshHashana() || this.isYomKippur() || this._isSukkotYomTov() ||
this.isSheminiAtzeret() || this._isPesachYomTov() || this.isShavuot();
return holidays;
}
isKodesh() {
return (this.isShabbat() || this.isYomTov());
}
isChanukah() {
var ChanukahEnd = new this.HeDate(this.hDate.getFullYear(), 2, 32);
var begin = {month: this.hebrewMonths.Kislev, date: 24 };
var end = {month: ChanukahEnd.getMonth(), date: ChanukahEnd.getDate() };
return this._inHebrewHolidayDateRange(begin, end);
}
_inHebrewHolidayDateRange(erev, end) {
// Assumes that all ranges are within the same Hebraic year.
// We COULD support wrap arounds, but it is only needed for Rosh Hashana
// Handled there as a special case rule
var candletime = new Date(this.sunset);
candletime.setMinutes(this.sunset.getMinutes() - this.candlelighting);
var havdalahtime = new Date(this.sunset);
havdalahtime.setMinutes(this.sunset.getMinutes() + this.havdalah);
var todayHebrewMonth = this.hDate.getMonth();
var todayHebrewDate = this.hDate.getDate();
// Date should be in the format {month, date}
if ((todayHebrewMonth == erev.month) && (todayHebrewDate == erev.date)) {
// First Day -- true after sunset
return (this.gDate > candletime);
} else if ((todayHebrewMonth == end.month) && (todayHebrewDate == end.date)) {
// Last Day -- true until sunset
return (this.gDate < havdalahtime);
} else if (
((todayHebrewMonth > erev.month) || (todayHebrewMonth == erev.month && todayHebrewDate > erev.date))
&&
((todayHebrewMonth < end.month) || (todayHebrewMonth == end.month && todayHebrewDate < end.date))) {
return true;
} else {
// Not in the middle
return false;
}
}
}