-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMMM-PublicTransportVVO.js
386 lines (319 loc) · 11.9 KB
/
MMM-PublicTransportVVO.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
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
Module.register("MMM-PublicTransportVVO", {
// default values
defaults: {
name: "MMM-PublicTransportVVO",
hidden: false,
stationId: 33000037,
baseurl: "http://widgets.vvo-online.de/abfahrtsmonitor/",
stationuri: "Haltestelle.do?hst=",
departureuri: "Abfahrten.do?hst=",
colored: false, // show not reachable departures colored
animationSpeed: 1 * 1000, // 1 sec
updateInterval: 30 * 1000, // 30 sec
timeorminuteBorder: 15, // border to show departures in various formats
departuretimeDecoration: true, // show only plain departure times
fade: true, // fading out the bottom of the list
fadePoint: 0.25, // start on 1/4th of the list.
initialLoadDelay: 0, // how long should we wait to load data after starting
retryDelay: 2500, // if request fails, do a retry after 2.5 sec
marqueeLongDirections: true, // we want a marquee for long direction strings
delay: 10, // how long do you need to walk to the next station?
showTableHeaders: true, // show location and station in table header
showTableHeadersAsSymbols: false, // table headers as symbols or written?
maxReachableDepartures: 7
},
// create some variables to hold the station name and city based on the API result.
fetchedStationCity: "",
fetchedStationName: "",
start: function () {
Log.info("Starting module: " + this.name);
this.departuresArray = [];
this.loaded = false;
this.scheduleStationUpdate(this.config.initialLoadDelay);
this.scheduleDepartureUpdate(this.config.initialLoadDelay);
this.updateTimer = null;
// check some variables
if(this.config.delay < 0) {
this.config.delay = 0;
}
if (this.config.updateInterval < 30000) {
this.config.updateInterval = 30000;
}
if (this.config.timeorminuteBorder < 1) {
this.config.timeorminuteBorder = 1
}
},
getDom: function () {
let wrapper = document.createElement("div");
wrapper.className = "ptbWrapper";
if (this.config.stationId === "") {
wrapper.innerHTML = this.name + ": " + this.translate("NO-STATIONID");
wrapper.className = "small light dimmed";
return wrapper;
}
if (this.departuresArray.length === 0 && !this.loaded) {
wrapper.innerHTML = (this.loaded) ? this.translate("EMPTY") : this.translate("LOADING");
wrapper.className = "small light dimmed";
return wrapper;
}
let heading = document.createElement("header");
heading.innerHTML = this.fetchedStationCity + ", "+ this.fetchedStationName
wrapper.appendChild(heading);
// table header
let table = document.createElement("table");
table.className = "ptbTable small light";
if (this.config.showTableHeaders) {
let tHead = document.createElement("thead");
let headerRow = document.createElement("tr");
// Header Cell for line symbol
let headerLine = document.createElement("td");
if (this.config.showTableHeadersAsSymbols) {
headerLine.className = "centeredTd";
let lineIcon = document.createElement("span");
lineIcon.className = "fa fa-tag";
headerLine.appendChild(lineIcon);
} else {
headerLine.innerHTML = this.translate("LINE");
}
headerRow.appendChild(headerLine);
// Header Cell for direction
let headerDirection = document.createElement("td");
if (this.config.showTableHeadersAsSymbols) {
headerDirection.className = "centeredTd";
let directionIcon = document.createElement("span");
directionIcon.className = "fa fa-exchange";
headerDirection.appendChild(directionIcon);
} else {
headerDirection.innerHTML = this.translate("DESTINATION");
}
headerRow.appendChild(headerDirection);
// Header Cell for departure time
let headerTime = document.createElement("td");
headerTime.className = "centeredTd timeCell";
if (this.config.showTableHeadersAsSymbols) {
let timeIcon = document.createElement("span");
timeIcon.className = "fa fa-clock-o";
headerTime.appendChild(timeIcon);
} else {
headerTime.innerHTML = this.translate("DEPARTURE");
}
headerRow.appendChild(headerTime);
headerRow.className = "bold dimmed";
tHead.appendChild(headerRow);
table.appendChild(tHead);
}
// create table body
let tBody = document.createElement("tbody");
// handle empty departures array
if (this.departuresArray.length === 0) {
let row = this.getNoDeparturesRow(this.translate("NO-DEPS"));
tBody.appendChild(row);
table.appendChild(tBody);
wrapper.appendChild(table);
return wrapper;
}
this.departuresArray.forEach((current, i) => {
if (i < this.config.maxReachableDepartures) {
let row = this.getRow(current);
// fading out
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = this.departuresArray.length * this.config.fadePoint;
var steps = this.departuresArray.length - startingPoint;
if (i >= startingPoint) {
var currentStep = i - startingPoint;
row.style.opacity = 1 - (1 / steps * currentStep);
}
}
tBody.appendChild(row);
}
});
table.appendChild(tBody);
wrapper.appendChild(table);
return wrapper;
},
/* getNoDeparturesRow()
* Print row with message, if we have no departures.
*
* argument message - text to display.
*/
getNoDeparturesRow: function (message) {
let row = document.createElement("tr");
let cell = document.createElement("td");
cell.colSpan = 3;
cell.innerHTML = message;
row.appendChild(cell);
return row;
},
getRow: function (current) {
let currenttimestamp = new Date();
let departuretimestamp = new Date(currenttimestamp.getTime() + (current.departuretime*60*1000));
let row = document.createElement("tr");
// cell for line
let lineCell = document.createElement("td");
lineCell.className = "centeredTd noPadding lineCell";
lineCell.innerHTML = current.departurenumber;
row.appendChild(lineCell);
// cell for direction
let directionCell = document.createElement("td");
directionCell.className = "directionCell bright";
if (this.config.marqueeLongDirections && current.departuredirection.length >= 26) {
directionCell.className = "directionCell bright marquee";
let directionSpan = document.createElement("span");
directionSpan.innerHTML = current.departuredirection;
directionCell.appendChild(directionSpan);
} else {
directionCell.innerHTML = current.departuredirection;
}
row.appendChild(directionCell);
// cell for time
let timeCell = document.createElement("td");
timeCell.className = "centeredTd timeCell bright";
if (this.config.delay > 0 && current.departuretime <= this.config.delay && this.config.colored) {
timeCell.style.color = "red";
}
if (current.departuretime === "") {
timeCell.innerHTML = this.translate("NOW");
} else if (current.departuretime < this.config.timeorminuteBorder && this.config.departuretimeDecoration) {
if (current.departuretime === "1") {
timeCell.innerHTML = 'In ' + current.departuretime + ' ' + this.translate("MINUTE");
} else {
timeCell.innerHTML = 'In ' + current.departuretime + ' ' + this.translate("MINUTES");
}
} else if (current.departuretime < this.config.timeorminuteBorder) {
timeCell.innerHTML = current.departuretime;
} else {
var departuretimeHours = ('0' + departuretimestamp.getHours()).slice(-2);
var departuretimeMinutes = ('0' + departuretimestamp.getMinutes()).slice(-2);
if (this.config.departuretimeDecoration) {
timeCell.innerHTML = departuretimeHours + ':' + departuretimeMinutes + ' ' + this.translate("TIME");
} else {
timeCell.innerHTML = departuretimeHours + ':' + departuretimeMinutes;
}
}
row.appendChild(timeCell);
return row;
},
/* scheduleStationUpdate()
* Schedule next station update.
*
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleStationUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.getStationDetails();
}, nextLoad);
},
/* getStationDetails(compliments)
* Requests new data.
* Calls processStationDetails on succesfull response.
*/
getStationDetails: function () {
var url = this.config.baseurl + this.config.stationuri + this.config.stationId;
var self = this;
var retry = true;
var StationDetailsRequest = new XMLHttpRequest();
StationDetailsRequest.open("GET", url, true);
StationDetailsRequest.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
self.processStationDetails(JSON.parse(this.response));
} else if (this.status === 401) {
self.config.stationId = "";
self.updateDom(self.config.animationSpeed);
Log.error(self.name + ": Something was wrong.");
retry = false;
} else {
Log.error(self.name + ": Could not load details.");
}
if (retry) {
self.scheduleStationUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
};
StationDetailsRequest.send();
},
/* processStationDetails(data)
* Uses the received data to set the various values.
*
* argument data object - information received from the api.
*/
processStationDetails: function(data) {
this.fetchedStationCity = data[0][0][0];
this.fetchedStationName = data[1][0][0];
this.updateDom(this.config.animationSpeed);
},
/* scheduleDepartureUpdate()
* Schedule next departure update.
*
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleDepartureUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.getDepartureDetails();
}, nextLoad);
},
/* getDepartureDetails(compliments)
* Requests new data.
* Calls processDepartureDetails on succesfull response.
*/
getDepartureDetails: function () {
var url = this.config.baseurl + this.config.departureuri + this.config.stationId;
var self = this;
var retry = true;
var DepartureDetailsRequest = new XMLHttpRequest();
DepartureDetailsRequest.open("GET", url, true);
DepartureDetailsRequest.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
self.processDepartureDetails(JSON.parse(this.response));
} else {
Log.error(self.name + ": Could not load details.");
}
if (retry) {
self.scheduleDepartureUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
};
DepartureDetailsRequest.send();
},
/* processDepartureDetails(data)
* Uses the received data to set the various values.
*
* argument data object - information received from the api.
*/
processDepartureDetails: function(data) {
this.departuresArray = [];
for (var i = 0, count = data.length; i < count; i++) {
var departuresArray = data[i];
this.departuresArray.push({
departurenumber: departuresArray[0],
departuredirection: departuresArray[1],
departuretime: departuresArray[2]
});
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
getStyles: function () {
return ['style.css'];
},
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},
});