-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-DBF.js
526 lines (481 loc) · 16.4 KB
/
MMM-DBF.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* global Module */
/* Magic Mirror
* Module: MMM-DBF
*
* By Marc Helpenstein <helpi9007@gmail.com>
* MIT Licensed.
*/
Module.register("MMM-DBF", {
defaults: {
updateInterval: 60000, // 1 minute
retryDelay: 30000, // 30 seconds
station: "Düsseldorf Hbf",
platform: '',
via: '',
showApp: false,
showArrivalTime: false,
showRealTime: false,
onlyArrivalTime: false,
numberOfResults: 10,
hideLowDelay: false,
withoutDestination: '',
onlyDestination: '',
train: '',
height: "600px",
width: "400px",
setTableWidth: "",
timeOption: "time", // time+countdown or countdown
showDelayMsg: false
},
requiresVersion: "2.1.0",
/**
* @description Helper function to generate API url
*
* @returns {String} url
*/
gennerateUrl: function () {
let base_url = "https://dbf.finalrewind.org/";
base_url += this.config.station + "?platforms=" + this.config.platform + "&via=" + this.config.via + "&hide_opts=1";
if (this.config.showArrivalTime) {
base_url += "&detailed=1";
}
if (this.config.showRealTime) {
base_url += "&show_realtime=1";
}
if (this.config.onlyArrivalTime) {
base_url += "&admode=dep";
} else {
base_url += "&admode=dep";
}
if (this.config.hideLowDelay) {
base_url += "&hidelowdelay=1"
}
return base_url;
},
/**
* @description Calls updateIterval
*/
start: function () {
let self = this;
let dataRequest = null;
let dataNotification = null;
//Flag for check if module is loaded
this.loaded = false;
// Schedule update timer.
this.getData();
},
/**
* @description Gets data from dbf.finalrewind.org
*/
getData: function () {
let self = this;
let urlApi = this.gennerateUrl() + "&mode=json&version=3";
let retry = true;
let dataRequest = new XMLHttpRequest();
dataRequest.open("GET", urlApi, true);
dataRequest.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
self.processData(JSON.parse(this.response));
} else if (this.status === 401) {
self.updateDom(self.config.animationSpeed);
Log.error(self.name, this.status);
retry = false;
} else {
Log.error(self.name, "Could not load data.");
}
if (retry) {
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
};
dataRequest.send();
},
/**
* @description Schedule next update.
* @param {int} delay - Milliseconds before next update.
*/
scheduleUpdate: function (delay) {
let self = this;
let nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
setTimeout(function () {
self.getData();
}, nextLoad);
if (!this.config.showApp) {
this.updateDom();
}
},
/**
* @description Create App Frame or HTML table
*
* @returns {HTMLIframeElement}
*/
getDom: function () {
if (this.config.showApp) {
let iframe = document.createElement("IFRAME");
iframe.style = "border:0";
iframe.width = this.config.width;
iframe.height = this.config.height;
iframe.src = this.gennerateUrl();
return iframe;
}
let tableWrapper = document.createElement("table");
tableWrapper.className = "small mmm-dbf-table";
if (this.dataRequest) {
if (!this.dataRequest.error) {
if (this.config.setTableWidth) {
tableWrapper.style.width = this.config.setTableWidth;
}
let departures = this.dataRequest["departures"]
let tableHead = this.createTableHeader(departures);
tableWrapper.appendChild(tableHead);
this.createTableContent(departures, tableWrapper);
} else {
Log.error(this.dataRequest.error);
}
}
return tableWrapper;
},
/**
* @description Get the size for showing entrys
* @param {Object[]} departures
*/
getSize: function (departures) {
if (departures.length < this.config.numberOfResults) {
return departures.length;
} else {
return this.config.numberOfResults;
}
},
/**
* @description Check delay exist
* @param {Object[]} departures
*/
checkDelayExist: function (departures) {
for (let index = 0; index < this.getSize(departures); index++) {
if (departures[index]["delayDeparture"]) {
if (this.config.hideLowDelay && departures[index]["delayDeparture"] >= 5) {
return true;
}
if (!this.config.hideLowDelay) {
return true;
}
}
}
return false;
},
/**
* @description Check cancelled exist
* @param {Object[]} departures
*/
checkCancelledExist: function (departures) {
for (let index = 0; index < this.getSize(departures); index++) {
if (departures[index]["isCancelled"]) {
if (this.config.hideLowDelay && departures[index]["isCancelled"] >= 1) {
return true;
}
if (!this.config.hideLowDelay) {
return true;
}
}
}
return false;
},
/**
* @description Get col number
*/
getColDelay: function () {
if (this.config.via !== "") {
return 5;
} else {
return 4;
}
},
/**
* @param {Object} train
*/
getViaFromRoute: function (train) {
let viaConfigList = this.config.via.split(",");
let route = train["via"];
for (let i = 0; i < route.length; i++) {
const city = route[i];
for (let j = 0; j < viaConfigList.length; j++) {
if (city.includes(viaConfigList[j])) {
return viaConfigList[j];
}
}
}
},
/**
* @description Check if destination is in list config.withoutDestination
* @param {Object} train
*/
checkDestination: function (train, destinationConfig) {
let destinations = destinationConfig.split(",")
for (let index = 0; index < destinations.length; index++) {
if (train['destination'] === destinations[index]) {
return true;
}
}
return false;
},
/**
* @description Check if train is in list config.train
* @param {Object} train
*/
checkTrain: function (train) {
let trains = this.config.train.split(",")
let trainName = train["train"].split(" ")[0] + train["train"].split(" ")[1];
for (let i = 0; i < trains.length; i++) {
if (trainName.includes(trains[i])) {
return true;
}
}
return false;
},
/**
* @description Checks time and return day/hour/mins
* @param {int} time - Remaining time
*/
convertTime: function (scheduledTime) {
let time = this.calculateTime(scheduledTime);
if (time >= 3600) {
let strTime = (Math.floor(time / 3600)).toString();
return "+" + strTime + " " + this.translate("HOUR");
}
if (time >= 60) {
let strTime = (Math.floor(time / 60)).toString();
return strTime + " " + this.translate("MINUTE");
} else {
return this.translate("NOW");
}
},
/**
* @description Calculate remaining time
* @param {int:int} scheduledTime
*/
calculateTime: function (scheduledTime) {
let d = new Date();
let time = scheduledTime.split(":");
let dateTrain = new Date(d.getFullYear(), d.getMonth(), d.getDate(), time[0], time[1])
let newStamp = new Date().getTime();
return Math.round((dateTrain.getTime() - newStamp) / 1000);;
},
/**
* @description Check msg exists
* @param {Object[]} departures
*/
checkMsgExist: function (departures) {
for (let index = 0; index < this.getSize(departures); index++) {
if (departures[index] !== undefined && departures[index]["messages"]["delay"].length > 0) {
return true;
}
}
return false;
},
/**
* @description Creates the header for the Table
*/
createTableHeader: function (departures) {
let tableHead = document.createElement("tr");
tableHead.className = 'border-bottom';
let tableHeadValues = [
this.translate("TRAIN"),
this.translate('TRACK'),
this.translate('DESTINATION'),
];
if (this.config.via !== "") {
tableHeadValues.push(this.translate('VIA'));
}
if (!this.config.onlyArrivalTime) {
tableHeadValues.push(this.translate('DEPARTURE'));
} else {
tableHeadValues.push(this.translate('ARRIVAL'));
}
if (this.checkDelayExist(departures) || this.checkCancelledExist(departures)) {
let delayClockIcon = '<i class="fa fa-clock-o"></i>';
tableHeadValues.push(delayClockIcon);
}
if (this.config.showDelayMsg && this.checkMsgExist(departures)) {
tableHeadValues.push(this.translate("DELAYMSG"));
}
for (let thCounter = 0; thCounter < tableHeadValues.length; thCounter++) {
let tableHeadSetup = document.createElement("th");
if (thCounter === 5) {
tableHeadSetup.style.textAlign = "Left";
}
tableHeadSetup.innerHTML = tableHeadValues[thCounter];
tableHead.appendChild(tableHeadSetup);
}
return tableHead;
},
/**
* @param usableResults
* @param tableWrapper
* @returns {HTMLTableRowElement}
*/
createTableContent: function (departures, tableWrapper) {
let self = this;
let size = this.getSize(departures);
let count = 0;
for (let index = 0; index < size; index++) {
let obj = departures[index];
let trWrapper = document.createElement("tr");
trWrapper.className = 'tr';
this.checkMsgExist(obj);
// Check train
if (this.config.train !== "" && !this.checkTrain(obj)) {
if (size + 1 <= departures.length) {
size += 1;
continue;
} else if (size === departures.length) {
continue;
}
}
if (this.config.withoutDestination !== "" && this.checkDestination(obj, this.config.withoutDestination)) {
if (size + 1 <= departures.length) {
size += 1;
continue;
} else if (size === departures.length) {
continue;
}
}
if (this.config.onlyDestination !== "" && !this.checkDestination(obj, this.config.onlyDestination)) {
if (size + 1 <= departures.length) {
size += 1;
continue;
} else if (size === departures.length) {
continue;
}
}
let tdValues = [
obj.train,
obj.platform,
obj.destination,
];
if (this.config.via !== "") {
let via = this.getViaFromRoute(obj);
if (via === undefined) {
continue;
} else {
tdValues.push(this.getViaFromRoute(obj));
}
}
let time;
if (this.config.onlyArrivalTime) {
time = obj.scheduledArrival;
} else {
time = obj.scheduledDeparture;
}
let remainingTime = this.convertTime(time);
switch (this.config.timeOption) {
case "time+countdown":
tdValues.push(time + " (" + remainingTime + ")");
break;
case "countdown":
tdValues.push(remainingTime);
break;
default:
tdValues.push(time);
break;
}
if (this.checkDelayExist(departures) || this.checkCancelledExist(departures)) {
if (obj.delayDeparture > 0 && !this.config.hideLowDelay) {
let delay = ' +' + obj.delayDeparture;
tdValues.push(delay);
}
else if (obj.delayDeparture >= 5) {
let delay = ' +' + obj.delayDeparture;
tdValues.push(delay);
} else if (obj.isCancelled > 0) {
tdValues.push(this.translate("CANCELMSG"));
}
}
if (this.config.showDelayMsg && this.checkMsgExist(departures) && obj.delayDeparture > 0) {
if (obj["messages"]["delay"].length > 0) {
tdValues.push(obj["messages"]["delay"][0].text);
}
}
count++;
for (let c = 0; c < tdValues.length; c++) {
let tdWrapper = document.createElement("td");
tdWrapper.innerHTML = tdValues[c];
if (c === this.getColDelay()) {
tdWrapper.className = 'delay';
}
if (c === this.getColDelay() + 1) {
tdWrapper.className = 'delay';
tdWrapper.style.width = "200px";
tdWrapper.style.textAlign = "Left";
//tdWrapper.innerHTML = '<marquee scrollamount="3" >' + tdValues[c] + '<marquee>';
}
trWrapper.appendChild(tdWrapper);
}
tableWrapper.appendChild(trWrapper);
}
if (count === 0) {
let trWrapper = document.createElement("tr");
trWrapper.className = 'tr';
let tdWrapper = document.createElement("td");
if (this.config.onlyDestination !== "" && this.config.train !== "") {
tdWrapper.innerHTML = "Destination or train not found";
Log.error("Destination or train not found");
}
else if (this.config.onlyDestination !== "") {
tdWrapper.innerHTML = "Destination not found";
Log.error("Destination not found");
} else if (this.config.train !== "") {
tdWrapper.innerHTML = "Train not found";
Log.error("Train not found");
}
trWrapper.appendChild(tdWrapper);
tableWrapper.appendChild(trWrapper);
}
},
/**
* @description Define required styles.
* @returns {[string,string]}
*/
getStyles: function () {
return ["MMM-DBF.css", "font-awesome.css"];
},
/**
* @description Load translations files
* @returns {{en: string, de: string}}
*/
getTranslations: function () {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},
/**
* @description Update data and send notification to node_helper
* @param {*} data
*/
processData: function (data) {
this.dataRequest = data;
if (this.loaded === false) {
this.updateDom(this.config.animationSpeed);
}
this.loaded = true;
// the data if load
// send notification to helper
this.sendSocketNotification("MMM-DBF-NOTIFICATION_TEST", "data");
},
/**
* @description Handle notification
* @param {*} notification
* @param {*} payload
*/
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-DBF-NOTIFICATION_TEST") {
// set dataNotification
this.dataNotification = payload;
//this.updateDom();
}
},
});