-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMMM-IPT.js
179 lines (162 loc) · 5.09 KB
/
MMM-IPT.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
Module.register("MMM-IPT",{
/*
* This module uses http://api.aladhan.com/timings/ to fetch Prayer
* Timings. One caveat here is the the timings api assumes the
* unix timestamp in seconds and hence make sure to use this to
* get the timestamp:
* Math.floor(Date.now()/1000)
* It also uses islamcan.com for the Adhan audio file.
* This module uses the existing built-in module to copy the code
* structure and develop on top of this.
Methods:
1 - Muslim World League
2 - Islamic Society of North America
3 - Egyptian General Authority of Survey
4 - Umm Al-Qura University, Makkah
5 - University of Islamic Sciences, Karachi
6 - Institute of Geophysics, University of Tehran
7 - Shia Ithna-Ashari, Leva Institute, Qum
8 - Gulf Region
9 - Kuwait
10 - Qatar
11 - Majlis Ugama Islam Singapura, Singapore
12 - Union Organization islamic de France
13 - Diyanet İşleri Başkanlığı, Turkey
14 - Spiritual Administration of Muslims of Russia
Thanks for the [eulhaque](https://github.com/eulhaque).
*/
defaults: {
updateInterval: 4 * 60 * 1000, // every 24 hour
initLoadDelay: 0,
retryDelay: 2500,
latitude: 37.861676, //defaults to Norwalk, CT, US
longitude: 32.4641803,
timeZoneString: "Europe/Istanbul",
method: 13, //https://aladhan.com/prayer-times-api
timingsApi: 'http://api.aladhan.com/v1/timings/',
school: 1, //"school" - Optional. 0 for Shafii, 1 for Hanfi. If you leave this empty, it defaults to Shafii.
adhanSrc: '',
css_class: 'bright medium',
},
start: function() {
Log.info("starting module " + this.name);
this.loaded = false;
this.result = null;
this.adhan_src = this.config.adhanSrc;
this.scheduleUpdate(this.config.initLoadDelay);
},
getDateObj: function(time_str) {
now = new Date();
tmp = time_str.split(':');
hour = parseInt(tmp[0]);
min = parseInt(tmp[1]);
d = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hour, min, 0);
return d;
},
getFormattedTime: function(time_str) {
var tmp = time_str.split(':');
var minute = tmp[1];
var old_hour = tmp[0];
var am_pm = "AM";
var new_hour_str = old_hour;
var new_hour = parseInt(old_hour);
if (new_hour >= 12) {
am_pm = "PM";
}
if (new_hour > 12) {
new_hour = old_hour - 12;
if (new_hour < 10) {
new_hour_str = '0' + new_hour;
}
}
return new_hour_str + ':' + minute + ' ' + am_pm;
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
if (this.loaded) {
var timings = this.result['data']['timings'];
var keys = ['Fajr', 'Dhuhr', 'Asr', 'Maghrib', 'Isha'];
var dates = Array();
var cur_time = null;
var cur_key = 'Fajr';
var now = Date.now();
var self = this;
for(var i=0; i < keys.length; ++i) {
var key = keys[i];
date = this.getDateObj(timings[key]);
dates[key] = date;
Log.info(dates[key]);
if (dates[key] > now ) {
cur_key = key;
cur_time = dates[key];
Log.info("Scheduling Adhan for " + key + ' at ' + date + " Src: " + self.config.adhanSrc);
setTimeout(
function() {
Log.info("Scheduling Adhan for " + key);
},
date - now
);
}
}
for(var i=0; i < keys.length; ++i) {
var key = keys[i];
var div = document.createElement("div");
div.className = this.config.css_class;
div.innerHTML = key + " " + this.getFormattedTime(timings[key]);
wrapper.appendChild(div);
}
}else {
Log.info("Still not loaded yet");
}
return wrapper;
},
playAdhan: function() {
Log.info(this.config);
Log.info("Playinng adhan now. SRC: " + this.config.adhanSrc);
var audio = new Audio(this.config.adhanSrc);
audio.play();
},
processTimings: function(data) {
this.loaded = true;
this.result = data;
this.updateDom(this.config.animationSpeed);
},
updateTimings: function() {
var currentDate = new Date();
var url = this.config.timingsApi + currentDate.getDate() + "-" + (currentDate.getMonth() + 1) + "-" + currentDate.getFullYear() + "?";
url += "longitude=" + this.config.longitude;
url += "&latitude=" + this.config.latitude;
url += "&method=" + this.config.method;
url += "&school=" + this.config.school;
var self = this;
var request = new XMLHttpRequest();
Log.info(url)
request.open("GET", url, true);
request.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
self.processTimings(JSON.parse(this.response));
}else {
Log.error("Got error. Failed to fetch timings");
}
}
};
request.send();
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.updateTimings();
}, nextLoad);
},
});