-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
120 lines (100 loc) · 4.61 KB
/
node_helper.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
var NodeHelper = require("node_helper");
var KosherZmanim = require("kosher-zmanim");
var moment = require("moment");
module.exports = NodeHelper.create({
start: function() {},
socketNotificationReceived: function (notification, payload) {
if (notification === 'FETCH_ZMANIM') {
this.getZmanim(payload)
}
console.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
},
getZmanim: function(config) {
const self = this;
const currentDate = new Date();
const options = {
date: currentDate,
timeZoneId: config.timeZoneId,
locationName: config.locationName,
latitude: config.latitude,
longitude: config.longitude,
elevation: config.elevation,
complexZmanim: true,
};
const zmanim = KosherZmanim.getZmanimJson(options)["Zmanim"];
let tomorrowZmanim;
if(config.showTomorrowsZmanim){
options.date.setDate(options.date.getDate() + 1 );
tomorrowZmanim = KosherZmanim.getZmanimJson(options)["Zmanim"];
}
var zmanimDateArray = [];
var calendarArray = [];
String.prototype.capitalizedFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
}
const calendar = new KosherZmanim.JewishCalendar(currentDate);
const hebrewDateFormatter = new KosherZmanim.HebrewDateFormatter();
const parsha = this.getUpcomingParsha(currentDate);
const parshaName = hebrewDateFormatter.getTransliteratedParshiosList()[parsha];
calendar.setInIsrael(config.inIsrael);
if (config.showsHebrewDate) {
calendarArray.push(hebrewDateFormatter.format(new KosherZmanim.JewishDate(currentDate)));
}
if (config.showsParsha) {
calendarArray.push("Parsha: " + parshaName);
}
if (config.showsDaf) {
calendarArray.push("Daf: " + hebrewDateFormatter.formatDafYomiBavli(KosherZmanim.YomiCalculator.getDafYomiBavli(calendar)));
}
if (config.showsYomTov && calendar.getYomTovIndex() !== -1) {
calendarArray.push(hebrewDateFormatter.formatYomTov(calendar));
}
if (config.showsDayOfOmer && calendar.getDayOfOmer() !== -1) {
calendarArray.push("Day of Omer: " + calendar.getDayOfOmer());
}
if (config.showsSpecialShabbos && self.getUpcomingSpecialShabbos(currentDate) !== KosherZmanim.Parsha.NONE) {
calendarArray.push("Shabbat " + hebrewDateFormatter.getTransliteratedParshiosList()[self.getUpcomingSpecialShabbos(currentDate)]);
}
for (var i in zmanim) {
if (i == "CandleLighting" && !calendar.hasCandleLighting()) {
continue;
}
if (i in config.displayedFields) {
zmanimDateArray.push([config.displayedFields[i], zmanim[i], moment(zmanim[i]).format('h:mm A')])
}
}
if(config.showTomorrowsZmanim){
for (var i in tomorrowZmanim) {
if (i in config.displayedFields) {
zmanimDateArray.push([config.displayedFields[i], tomorrowZmanim[i], moment(tomorrowZmanim[i]).format('h:mm A')])
}
}
}
self.sendSocketNotification(
'FETCHED_ZMANIM', {
'zmanim': zmanimDateArray,
'calendar': calendarArray,
'id': config.id,
}
);
},
getUpcomingParsha: function(current) {
const nearestSaturday = new Date(this.getNextDayOfWeek(current, 6));
const calendar = new KosherZmanim.JewishCalendar(nearestSaturday);
const yearType = calendar.getParshaYearType();
const roshHashanaDayOfWeek = KosherZmanim.JewishCalendar.getJewishCalendarElapsedDays(calendar.getJewishYear()) % 7;
const day = roshHashanaDayOfWeek + calendar.getDaysSinceStartOfJewishYear();
return KosherZmanim.JewishCalendar.parshalist[yearType][day / 7];
},
getUpcomingSpecialShabbos: function(current) {
const nearestSaturday = new Date(this.getNextDayOfWeek(current, 6));
const calendar = new KosherZmanim.JewishCalendar(nearestSaturday);
return calendar.getSpecialShabbos();
},
// Get the next day of the week given index 0-6
getNextDayOfWeek: function(date, dayOfWeek) {
var resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
},
})