forked from htilburgs/MMM-MyPrayerTimes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-MyPrayerTimes.js
140 lines (118 loc) · 4.54 KB
/
MMM-MyPrayerTimes.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
/*
//-------------------------------------------
MMM-MyPrayerTime
Copyright (C) 2024 - H. Tilburgs
MIT License
//-------------------------------------------
*/
Module.register('MMM-MyPrayerTimes', {
// Default values
defaults: {
mptLat: null,
mptLon: null,
mptMethod: 3,
mptOffset: "0,0,0,0,0,0,0,0,0",
showSunrise: true,
showSunset: true,
showMidnight: true,
showImsak: true,
show24Clock: true,
maxWidth: "500px",
animationSpeed: 1000,
initialLoadDelay: 1000,
retryDelay: 2500,
updateInterval: 60 * 60 * 1000,
},
// Define required files
getStyles: function () {
return ["MMM-MyPrayerTimes.css"];
},
getScripts: function () {
return ["moment.js"];
},
getTranslations: function () {
return {
en: "translations/en.json",
nl: "translations/nl.json",
tr: "translations/tr.json",
};
},
start: function () {
Log.info("Starting module: " + this.name);
this.url = "https://api.aladhan.com/v1/timings/" + new Date() + "?latitude=" + this.config.mptLat + "&longitude=" + this.config.mptLon + "&method=" + this.config.mptMethod + "&tune=" + this.config.mptOffset;
this.MPT = {};
this.loaded = false;
this.scheduleUpdate();
},
getDom: function () {
const wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
if (!this.loaded) {
wrapper.innerHTML = "Loading....";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
const table = document.createElement("table");
table.className = "small";
const prayerTimes = [
{ key: "Imsak", label: "IMSAK", arabic: "الإمساك", show: this.config.showImsak },
{ key: "Fajr", label: "FAJR", arabic: "الفجر" },
{ key: "Sunrise", label: "SUNRISE", arabic: "شروق الشمس", show: this.config.showSunrise },
{ key: "Dhuhr", label: "DHUHR", arabic: "الظهر" },
{ key: "Asr", label: "ASR", arabic: "العصر" },
{ key: "Sunset", label: "SUNSET", arabic: "غروب الشمس", show: this.config.showSunset },
{ key: "Maghrib", label: "MAGHRIB", arabic: "المغرب" },
{ key: "Isha", label: "ISHA", arabic: "العشاء" },
{ key: "Midnight", label: "MIDNIGHT", arabic: "منتصف الليل", show: this.config.showMidnight },
];
prayerTimes.forEach(prayer => {
if (prayer.show === false) return;
const row = this.createPrayerRow(prayer.label, this.MPT[prayer.key], prayer.arabic);
table.appendChild(row);
});
wrapper.appendChild(table);
return wrapper;
},
createPrayerRow: function (label, time, arabicText) {
const row = document.createElement("tr");
const textCell = document.createElement("td");
textCell.className = `${label.toLowerCase()}-text`;
textCell.innerHTML = this.translate(label);
row.appendChild(textCell);
const timeCell = document.createElement("td");
timeCell.className = `${label.toLowerCase()}-time bright`;
timeCell.innerHTML = this.config.show24Clock ? time : this.convert24Time(time);
row.appendChild(timeCell);
const arabicCell = document.createElement("td");
arabicCell.className = `${label.toLowerCase()}-arab`;
arabicCell.innerHTML = arabicText;
row.appendChild(arabicCell);
return row;
},
convert24Time: function (time) {
const match = time.toString().match(/^([01]\d|2[0-3]):([0-5]\d)(:[0-5]\d)?$/);
if (!match) return time;
let [hours, minutes] = match.slice(1, 3);
const suffix = hours < 12 ? " AM" : " PM";
hours = (hours % 12) || 12;
return `${hours}:${minutes}${suffix}`;
},
processMPT: function (data) {
this.MPT = data;
this.loaded = true;
},
scheduleUpdate: function () {
setInterval(() => this.getMPT(), this.config.updateInterval);
this.getMPT();
},
getMPT: function () {
this.sendSocketNotification("GET_MPT", this.url);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "MPT_RESULT") {
this.processMPT(payload);
this.updateDom(this.config.animationSpeed);
}
},
});