-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMMM-RNV.js
More file actions
423 lines (352 loc) · 15.7 KB
/
MMM-RNV.js
File metadata and controls
423 lines (352 loc) · 15.7 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* Magic Mirror
* Module: MMM-RNV
*
* By jupadin
* MIT Licensed.
*/
Module.register("MMM-RNV",{
// Default module config.
defaults: {
header: "MMM-RNV",
animationSpeed: 2 * 1000, // 2 seconds
updateInterval: 1 * 60 * 1000, // every 1 minute
stationID: 2417,
numJourneys: 10,
coloredLines: true,
showLineIcons: true,
useColorForRealTimeInfo: true,
showTableHeader: true,
showTableHeaderAsSymbols: false,
focus_on: [],
showFooter: true,
apiKey: "",
clientID: "",
resourceID: "",
clientSecret: "",
oAuthURL: "",
tenantID: "",
clientAPIURL: "",
icon: {
"STRASSENBAHN" : "fas fa-train",
"STADTBUS" : "fas fa-bus"
},
timeFormat: "HH:mm",
},
// Define start sequence.
start: function() {
Log.info(`Starting module: ${this.name}`);
moment.updateLocale(this.config.language, this.config.timeFormat);
// Indicate no data available yet
this.loaded = false;
this.error = null;
this.credentials = false;
this.stationName = "";
// Clear data before start
this.fetchedData = null;
this.fetchedColor = null;
this.config.identifier = this.identifier;
if ( (this.config.apiKey) || (this.config.clientID && this.config.clientSecret && this.config.tenantID && this.config.resourceID) ) {
this.credentials = true;
// Build oAuthURL based on given tenantID.
this.config.oAuthURL = "https://login.microsoftonline.com/" + this.config.tenantID + "/oauth2/token";
}
this.sendSocketNotification("SET_CONFIG", this.config);
const now = new Date();
const msUntilNextMinute = (60 - now.getSeconds()) * 1000 - now.getMilliseconds();
// Live-Minuten-Update
setTimeout(() => {
// Update DOM-Daten
this.updateDom(0);
// console.log("update UI1");
this.minuteTimer = setInterval(() => {
this.updateDom(0);
// console.log("update UI2");
}, 60 * 1000); // alle 60 Sekunden+
}, msUntilNextMinute);
// console.log("Time until next minute:", msUntilNextMinute);
},
// Define required styles.
getStyles: function() {
return['MMM-RNV.css', "font-awesome.css"];
},
// Define required scripts.
getScripts: function() {
return [];
},
// Define required translations.
getTranslations: function() {
return {
de: "translations/de.json",
en: "translations/en.json",
fr: "translations/fr.json"
}
},
// Define header.
getHeader: function() {
if (!this.loaded) {
return this.config.header;
} else {
return this.config.header + " - " + this.stationName;
}
},
getTemplate() {
return "MMM-RNV.njk";
},
getTemplateData() {
if (!this.loaded) {
return {loading: this.translate("LOADING")};
} else if (this.error) {
return {error: this.translate("ERROR")}
}
const journeys = this.fetchedData;
if (!this.lastFetch) {
this.config.showFooter = false;
}
const lastUpdateMinutes = new Date(this.lastFetch).getMinutes();
const nowMinutes = new Date().getMinutes();
const diffMinutes = nowMinutes - lastUpdateMinutes;
// console.log(new Date().toString("HH:mm"), lastUpdateMinutes, nowMinutes, diffMinutes)
let updateClass;
if (diffMinutes >= 2) updateClass = "aging";
else if (diffMinutes >= 5) updateClass = "stale";
else updateClass = "fresh";
this.last_update = moment(this.lastFetch).format(this.config.timeFormat)
return {
loading: null,
error: null,
journeys: journeys,
icons: this.config.icon,
showHeaderAsIcon: this.config.showHeaderAsIcon,
showFooter: this.config.showFooter,
last_updated: this.last_update,
minutesAgo: diffMinutes,
updateClass: updateClass
}
},
// Override dom generator.
// getDom: function() {
// const wrapper = document.createElement("div");
// if (!this.credentials) {
// wrapper.innerHTML = "There are no <i>RNV Credentials</i> in config file set.";
// wrapper.className = "light small dimmed";
// return wrapper;
// }
// if (this.config.stationID == "") {
// wrapper.innerHTML = "No <i>stationID</i> in config file set.";
// wrapper.className = "light small dimmed";
// return wrapper;
// }
// if (!this.loaded) {
// wrapper.innerHTML = this.translate("LOADING");
// wrapper.className = "light small dimmed";
// return wrapper;
// }
// if (this.loaded && this.fetchedData.data.station.journeys.elements.length == 0) {
// wrapper.innerHTML = "No data available";
// wrapper.className = "light small dimmed";
// return wrapper;
// }
// // Create dom table
// const table = document.createElement("table");
// table.className = "table";
// if (this.config.showTableHeader) {
// const tableHead = document.createElement("tr");
// const tableHeadTime = document.createElement("th");
// tableHeadTime.className = "departure";
// if (this.config.showTableHeaderAsSymbols) {
// const tableHeadTimeIcon = document.createElement("i");
// tableHeadTimeIcon.classList.add("far");
// tableHeadTimeIcon.classList.add("fa-clock");
// tableHeadTime.appendChild(tableHeadTimeIcon);
// tableHeadTime.style.textAlign = "center";
// } else {
// tableHeadTime.style.textAlign = "left";
// tableHeadTime.innerHTML = this.translate("DEPARTURE");
// }
// const tableHeadLine = document.createElement("th");
// tableHeadLine.className = "line";
// tableHeadLine.colSpan = "2";
// if (this.config.showTableHeaderAsSymbols) {
// const tableHeadLineIcon = document.createElement("i");
// tableHeadLineIcon.className ="fa fa-tag";
// tableHeadLine.appendChild(tableHeadLineIcon);
// } else {
// tableHeadLine.innerHTML = this.translate("LINE");
// }
// const tableHeadDestination = document.createElement("th");
// tableHeadDestination.className = "direction";
// if (this.config.showTableHeaderAsSymbols) {
// const tableHeadDestinationIcon = document.createElement("i");
// tableHeadDestinationIcon.className = "fa fa-arrows-alt-h";
// tableHeadDestination.appendChild(tableHeadDestinationIcon);
// } else {
// tableHeadDestination.innerHTML = this.translate("DIRECTION");
// }
// const tableHeadPlatform = document.createElement("th");
// tableHeadPlatform.className = "platform";
// if (this.config.showTableHeaderAsSymbols) {
// const tableHeadPlatformIcon = document.createElement("i");
// tableHeadPlatformIcon.className = "fa fa-question";
// tableHeadPlatform.appendChild(tableHeadPlatformIcon);
// } else {
// tableHeadPlatform.innerHTML = this.translate("PLATFORM");
// }
// tableHead.appendChild(tableHeadTime);
// tableHead.appendChild(tableHeadLine);
// tableHead.appendChild(tableHeadDestination);
// tableHead.appendChild(tableHeadPlatform);
// table.appendChild(tableHead);
// // Horizontal rule after table header
// const hruleRow = document.createElement("tr");
// const hruleData = document.createElement("td");
// hruleData.colSpan = 5;
// hruleData.innerHTML = "<hr>";
// hruleRow.appendChild(hruleData);
// table.appendChild(hruleRow);
// }
// const numDepartures = this.fetchedData.data.station.journeys.elements.length;
// // Iterating over received data
// for (let i = 0; i < numDepartures; i++) {
// const dataRow = document.createElement("tr");
// const currentDeparture = this.fetchedData.data.station.journeys.elements[i];
// const line = currentDeparture.line.id.split("-")[1];
// const type = currentDeparture.type;
// const destination = currentDeparture.stops[0].destinationLabel;
// const platform = currentDeparture.stops[0].pole.platform.label;
// const delay = currentDeparture.stops[0].delay;
// const departureTimes = currentDeparture.stops[0];
// const plannedDepartureIsoString = departureTimes.plannedDeparture.isoString;
// const plannedDepartureDate = new Date(plannedDepartureIsoString);
// const plannedDeparture = plannedDepartureDate.toLocaleTimeString('de-DE', {hour: '2-digit', minute: '2-digit', hour12: false});
// // Time
// const dataCellTime = document.createElement("td");
// dataCellTime.className = "departure";
// dataCellTime.innerHTML = plannedDeparture;
// // - Delay
// const dataCellTimeDelay = document.createElement("span");
// dataCellTimeDelay.className = "delay";
// if (delay > 0) {
// dataCellTimeDelay.innerHTML = "+ " + delay;
// dataCellTimeDelay.classList.add("late");
// if (this.config.useColorForRealTimeInfo) {
// dataCellTimeDelay.style.color = "#FF0000";
// }
// } else if (delay < 0) {
// dataCellTimeDelay.innerHTML = "- " + delay;
// dataCellTimeDelay.classList.add("early");
// if (this.config.useColorForRealTimeInfo) {
// dataCellTimeDelay.style.color = "#00FF00";
// }
// } else {
// dataCellTimeDelay.innerHTML = "+ " + delay;
// dataCellTimeDelay.style.visibility = "hidden";
// }
// // Add delay to time cell
// dataCellTime.appendChild(dataCellTimeDelay);
// dataRow.appendChild(dataCellTime);
// // Line
// const dataCellLine = document.createElement("td");
// dataCellLine.className = "line";
// const dataCellLineContent = document.createElement("div");
// dataCellLineContent.className = "content";
// dataCellLineContent.innerHTML = line;
// if (this.config.coloredLines) {
// if (this.fetchedColor !== null && this.fetchedColor !== undefined) {
// const colorData = this.fetchedColor.find(x => x.id == line);
// if (colorData !== undefined) {
// dataCellLineContent.style.backgroundColor = colorData.primary.hex;;
// } else {
// dataCellLineContent.style.backgroundColor = "grey";
// }
// }
// }
// dataCellLine.appendChild(dataCellLineContent);
// if (type === "STRASSENBAHN") {
// dataCellLineContent.classList.add("train");
// } else if (type ==="STADTBUS") {
// dataCellLineContent.classList.add("bus");
// dataCellLineContent.style.borderRadius = "50%";
// }
// dataRow.appendChild(dataCellLine);
// // Icon
// const dataCellLineSpan = document.createElement("td");
// dataCellLineSpan.className = "icon";
// if (this.config.showLineIcons) {
// const dataCellLineIcon = document.createElement("i");
// dataCellLineIcon.className = this.config.icon[type];
// dataCellLineSpan.appendChild(dataCellLineIcon);
// }
// dataRow.appendChild(dataCellLineSpan);
// // Direction
// const dataCellDirection = document.createElement("td");
// dataCellDirection.className = "direction";
// dataCellDirection.innerHTML = destination;
// dataRow.appendChild(dataCellDirection);
// // Platform
// const dataCellPlatform = document.createElement("td");
// dataCellPlatform.className = "platform";
// dataCellPlatform.innerHTML = platform;
// dataRow.appendChild(dataCellPlatform);
// if (Array.isArray(this.config.focus_on)) {
// this.config.focus_on.forEach(element => {
// if (element == line) {
// dataRow.classList.add("bright");
// }
// });
// }
// // Append data row to table.
// table.appendChild(dataRow);
// }
// // Create footer row with last update time.
// const footerRow = document.createElement("tr");
// footerRow.className = "footerRow";
// const footer = document.createElement("td");
// footer.className = "footer";
// footer.setAttribute("colspan", 8);
// footer.innerHTML = this.translate("UPDATED") + ": " + moment().format("dd, DD.MM.YYYY, HH:mm[h]");
// footerRow.appendChild(footer);
// table.appendChild(footerRow);
// wrapper.appendChild(table);
// // Return the wrapper to the dom.
// return wrapper;
// },
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
// console.log(payload.id, notification, payload.data, payload);
// If this message isn't for me...
// Since we could have the module deployed multiple times, I want to process only the message which is for me.
if (this.identifier !== payload.id) {
return;
}
let animationSpeed = this.config.animationSpeed;
if (notification === "DATA") {
if (this.loaded) {
animationSpeed = 0;
}
this.fetchedData = payload.data;
// Set station name of current fetch
this.stationName = payload.stationName;
this.lastFetch = payload.lastFetch;
this.loaded = true;
// Update dom with given animation speed.
this.updateDom(animationSpeed);
// } else if (notification === "COLOR") {
// this.fetchedColor = payload.data;
// // // If there is already data available, update dom with fetched color data.
// // if (this.config.fetchedData != null) {
// // // Use default animation speed to update dom with color data
// this.updateDom(animationSpeed);
// // }
} else if (notification === "ERROR") {
// TODO: Update front-end to display specific error.
this.error = true;
this.fetchedData = payload;
this.updateDom(animationSpeed);
} else {
// TODO: Update front-end to display specific error.
this.error = true;
this.fetchedData = payload;
this.updateDom(animationSpeed);
}
}
});