-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-RNV.js
321 lines (280 loc) · 12.3 KB
/
MMM-RNV.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
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
/* Magic Mirror
* Module: MMM-RNV
*
* By jupadin
* MIT Licensed.
*/
Module.register("MMM-RNV",{
// Default module config.
defaults: {
header: "RNV Abfahrtsmonitor",
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: [],
apiKey: "",
clientID: "",
resourceID: "",
clientSecret: "",
oAuthURL: "",
tenantID: "",
clientAPIURL: "https://graphql-sandbox-dds.rnv-online.de",
icon: {
"STRASSENBAHN" : "fas fa-train",
"STADTBUS" : "fas fa-bus"
},
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.loaded = false;
this.error = null;
this.credentials = false;
this.stationName = "";
this.fetchedData = null;
this.fetchedColor = null;
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);
}
},
// 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;
}
},
// 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 = "MMM-RNV light small";
table.id = "RNVTable";
if (this.config.showTableHeader) {
const tableHead = document.createElement("tr");
const tableHeadTime = document.createElement("th");
tableHeadTime.className = "MMM-RNV header 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 = "MMM-RNV header line";
tableHeadLine.colSpan = "2";
if (this.config.showTableHeaderAsSymbols) {
const tableHeadLineIcon = document.createElement("i");
tableHeadLineIcon.className ="fas fa-tag";
tableHeadLine.appendChild(tableHeadLineIcon);
} else {
tableHeadLine.innerHTML = this.translate("LINE");
}
const tableHeadDestination = document.createElement("th");
tableHeadDestination.className = "MMM-RNV header direction";
if (this.config.showTableHeaderAsSymbols) {
const tableHeadDestinationIcon = document.createElement("i");
tableHeadDestinationIcon.className = "fas fa-arrows-alt-h";
tableHeadDestination.appendChild(tableHeadDestinationIcon);
} else {
tableHeadDestination.innerHTML = this.translate("DIRECTION");
}
const tableHeadPlatform = document.createElement("th");
tableHeadPlatform.className = "MMM-RNV header platform";
if (this.config.showTableHeaderAsSymbols) {
const tableHeadPlatformIcon = document.createElement("i");
tableHeadPlatformIcon.className = "fas 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 = "MMM-RNV data departure";
dataCellTime.innerHTML = plannedDeparture;
// - Delay
const dataCellTimeDelay = document.createElement("span");
dataCellTimeDelay.className = "MMM-RNV data 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 = "MMM-RNV data line";
const dataCellLineContent = document.createElement("div");
dataCellLineContent.className = "MMM-RNV data line 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 = "MMM-RNV data 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 = "MMM-RNV data direction";
dataCellDirection.innerHTML = destination;
dataRow.appendChild(dataCellDirection);
// Platform
const dataCellPlatform = document.createElement("td");
dataCellPlatform.className = "MMM-RNV data 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);
}
wrapper.appendChild(table);
// Return the wrapper to the dom.
return wrapper;
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === "DATA") {
let animationSpeed = this.config.animationSpeed;
if (this.loaded) {
animationSpeed = 0;
}
this.fetchedData = payload;
// Set station name of current fetch
this.stationName = payload.data.station.longName;
this.loaded = true;
// Update dom with given animation speed.
this.updateDom(animationSpeed);
} else if (notification === "COLOR") {
this.fetchedColor = payload;
// 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(this.config.animationSpeed);
}
} else if (notification === "ERROR") {
// TODO: Update front-end to display specific error.
this.error = true;
this.fetchedData = payload;
} else {
Log.warn(this.name + ": Unhandled message received.");
}
}
});