-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-MVVWiesty.js
213 lines (180 loc) · 8.47 KB
/
MMM-MVVWiesty.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
Module.register("MMM-MVVWiesty", {
defaults: {
maxEntries: 5,
stopId: "de:09162:6",
filter: {},
displayNotifications: true,
scrollSpeed: 40,
minTimeUntilDeparture: 0
},
start () {
Log.info(`Starting module: ${this.name} with identifier: ${this.identifier}`);
this.departures = [];
this.filteredDepartures = [];
this.loadDepartures();
this.scheduleUpdate();
this.scheduleMinuteUpdate();
},
getStyles () {
return ["MMM-MVVWiesty.css"];
},
getHeader () {
return this.config.header && (this.config.header !== "") ? this.config.header : "MVV Abfahrtsmonitor";
},
getDom () {
const wrapper = document.createElement("div");
wrapper.classList.add("mvv-table-wrapper");
if (this.filteredDepartures.length > 0) {
const table = document.createElement("table");
table.classList.add("mvv-table");
for (let i = 0; i < this.filteredDepartures.length && i < this.config.maxEntries; i++) {
const departure = this.filteredDepartures[i];
const row = document.createElement("tr");
row.classList.add("departure-row");
const iconCell = document.createElement("td");
iconCell.classList.add("icon-cell");
const lineImage = document.createElement("img");
lineImage.classList.add("productsvg");
lineImage.src = this.getLineIcon(departure.line.name);
iconCell.appendChild(lineImage);
row.appendChild(iconCell);
const lineCell = document.createElement("td");
lineCell.classList.add("line-cell");
lineCell.innerHTML = departure.line.number;
row.appendChild(lineCell);
const directionCell = document.createElement("td");
directionCell.classList.add("direction-cell");
directionCell.innerHTML = departure.direction;
row.appendChild(directionCell);
const timeCell = document.createElement("td");
timeCell.classList.add("time-cell");
timeCell.innerHTML = departure.departureLive;
row.appendChild(timeCell);
const untilCell = document.createElement("td");
untilCell.classList.add("until-cell");
const minutesUntilDeparture = this.calculateTimeUntil(departure.departureLive);
untilCell.innerHTML = minutesUntilDeparture >= 1 ? `in ${minutesUntilDeparture} Min` : "";
row.appendChild(untilCell);
table.appendChild(row);
if (this.config.displayNotifications && departure.notifications && departure.notifications.length > 0) {
const notificationRow = document.createElement("tr");
const notificationCell = document.createElement("td");
notificationCell.colSpan = 5;
notificationCell.classList.add("notification-cell");
const notificationText = document.createElement("div");
notificationText.classList.add("scroll-container");
const scrollNotification = document.createElement("div");
scrollNotification.classList.add("scroll-text");
scrollNotification.innerHTML = departure.notifications[0].text;
this.setScrollAnimation(scrollNotification, this.config.scrollSpeed);
notificationText.appendChild(scrollNotification);
notificationCell.appendChild(notificationText);
notificationRow.appendChild(notificationCell);
table.appendChild(notificationRow);
row.classList.remove("departure-row");
}
}
wrapper.appendChild(table);
} else {
wrapper.innerHTML = "Keine Abfahrten gefunden.";
}
return wrapper;
},
setScrollAnimation (scrollTextElement, scrollSpeed) {
document.body.appendChild(scrollTextElement);
const scrollWidth = scrollTextElement.scrollWidth;
document.body.removeChild(scrollTextElement);
scrollTextElement.style.width = `${scrollWidth}px`;
const duration = scrollWidth / scrollSpeed;
scrollTextElement.style.animationDuration = `${duration}s`;
},
getLineIcon (lineName) {
switch (lineName) {
case "Bus": case "MetroBus": case "MVV-Regionalbus": case "RegionalBus": case "ExpressBus": case "NachtBus":
return this.file("assets/bus.svg");
case "S-Bahn":
return this.file("assets/sbahn.svg");
case "Tram": case "NachtTram":
return this.file("assets/tram.svg");
case "U-Bahn":
return this.file("assets/ubahn.svg");
default:
return this.file("assets/default.svg");
}
},
calculateTimeUntil (departureTime) {
const now = new Date();
const departure = new Date();
departure.setHours(departureTime.split(":")[0]);
departure.setMinutes(departureTime.split(":")[1]);
const diff = Math.floor((departure - now) / (1000 * 60));
return diff >= 0 ? diff : 0;
},
async loadDepartures () {
const self = this;
const stopId = this.config.stopId.replace(/:/g, "%3A");
const url = `https://www.mvv-muenchen.de/?eID=departuresFinder&action=get_departures&stop_id=${stopId}&requested_timestamp=${Math.floor(Date.now() / 1000)}&lines=`;
try {
const response = await fetch(url);
if (response.ok) {
const data = await response.json();
if (data && data.departures && data.departures.length > 0) {
self.departures = self.filterDepartures(data.departures);
self.departures.sort(function (a, b) {
return new Date(`1970-01-01T${a.departureLive}:00Z`) - new Date(`1970-01-01T${b.departureLive}:00Z`);
});
self.updateFilteredDepartures();
self.updateDom();
}
} else {
Log.error("[MMM-MVVWiesty]: Failed to load departures or no departures found.");
}
} catch (error) {
Log.error("[MMM-MVVWiesty]: Error fetching departures:", error);
}
},
filterDepartures (departures) {
const self = this;
const filterKeys = Object.keys(self.config.filter);
const minTime = this.config.minTimeUntilDeparture;
return departures.filter(function (departure) {
const minutesUntilDeparture = self.calculateTimeUntil(departure.departureLive);
if (minutesUntilDeparture < minTime) return false;
if (filterKeys.length === 0 || self.config.filter.hasOwnProperty("all")) return true;
const lineFilter = self.config.filter[departure.line.number];
if (!lineFilter) return false;
if (Array.isArray(lineFilter)) {
return lineFilter.includes(departure.direction);
}
return departure.direction === lineFilter || lineFilter === "";
});
},
updateFilteredDepartures () {
const now = new Date();
this.filteredDepartures = this.departures.filter((departure) => {
const departureDate = new Date();
departureDate.setHours(departure.departureLive.split(":")[0]);
departureDate.setMinutes(departure.departureLive.split(":")[1]);
return departureDate >= now;
});
},
scheduleUpdate () {
const self = this;
setInterval(function () {
self.loadDepartures();
}, 300000);
},
scheduleMinuteUpdate () {
const self = this;
const now = new Date();
const msUntilNextMinute = (60 - now.getSeconds()) * 1000;
setTimeout(function () {
self.updateFilteredDepartures();
self.updateDom();
setInterval(function () {
self.updateFilteredDepartures();
self.updateDom();
}, 60000);
}, msUntilNextMinute);
}
});