This repository has been archived by the owner on May 18, 2022. It is now read-only.
forked from deg0nz/MMM-PublicTransportBerlin
-
Notifications
You must be signed in to change notification settings - Fork 7
/
MMM-PublicTransportDB.js
365 lines (284 loc) · 13 KB
/
MMM-PublicTransportDB.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
"use strict";
Module.register("MMM-PublicTransportDB", {
// default values
defaults: {
name: "MMM-PublicTransportDB",
hidden: false,
stationId: 9160003,
ignoredStations: [], // Which stations should be ignored? (comma-separated list of station IDs)
excludedTransportationTypes: '', // Which transportation types should not be shown on the mirror? (comma-separated list of types) possible values: bus,tram,suburban,subway,ferry
direction: null, // Which direction should be displayed? (destination station ID)
marqueeLongDirections: true,
delay: 10, // How long do you need to walk to the next Station?
interval: 120000, // How often should the table be updated in ms?
departureMinutes: 10, // For how many minutes should departures be shown?
showColoredLineSymbols: true, // Want colored line symbols?
useColorForRealtimeInfo: true, // Want colored real time information (delay, early)?
showTableHeaders: true,
showTableHeadersAsSymbols: true, // Table Headers as symbols or written?
maxUnreachableDepartures: 3, // How many unreachable departures should be shown?
maxReachableDepartures: 7, // How many reachable departures should be shown?
fadeUnreachableDepartures: true,
fadeReachableDepartures: true,
fadePointForReachableDepartures: 0.25
},
start: function () {
Log.info("Starting module: " + this.name);
this.departuresArray = [];
this.stationName = "";
this.loaded = false;
this.sendSocketNotification('CREATE_FETCHER', this.config);
if(this.config.delay < 0) {
this.config.delay = 0;
}
if (this.config.interval < 30000) {
this.config.interval = 30000;
}
setInterval(() => {
this.sendSocketNotification('GET_DEPARTURES', this.config.stationId);
}, this.config.interval)
},
getDom: function () {
let wrapper = document.createElement("div");
wrapper.className = "ptbWrapper";
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.config.name;
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");
// Cell for departure time
let headerTime = document.createElement("td");
if (this.config.showTableHeadersAsSymbols) {
headerTime.className = "centeredTd";
let timeIcon = document.createElement("span");
timeIcon.className = "fa fa-clock-o";
headerTime.appendChild(timeIcon);
} else {
headerTime.innerHTML = "Abfahrt";
}
headerRow.appendChild(headerTime);
// Cell for delay time
let delayTime = document.createElement("td");
delayTime.innerHTML = " ";
headerRow.appendChild(delayTime);
// 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 = "Linie";
}
headerRow.appendChild(headerLine);
// 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 = "Nach";
}
headerRow.appendChild(headerDirection);
headerRow.className = "bold dimmed";
tHead.appendChild(headerRow);
table.appendChild(tHead);
}
// Create table body from data
let tBody = document.createElement("tbody");
// Handle empty departures array
if (this.departuresArray.length === 0) {
let row = this.getNoDeparturesRow("There are currently no departures.");
tBody.appendChild(row);
table.appendChild(tBody);
wrapper.appendChild(table);
return wrapper;
}
// handle delay === 0
if (this.config.delay === 0) {
this.departuresArray.forEach((current, i) => {
if (i < this.config.maxReachableDepartures) {
let row = this.getRow(current);
tBody.appendChild(row);
}
});
} else {
this.getFirstReachableDeparturePositionInArray().then((reachableDeparturePos) => {
this.departuresArray.forEach((current, i) => {
if (i >= reachableDeparturePos - this.config.maxUnreachableDepartures
&& i < reachableDeparturePos + this.config.maxReachableDepartures) {
// insert rule to separate reachable departures
if (i === reachableDeparturePos
&& reachableDeparturePos !== 0
&& this.config.maxUnreachableDepartures !== 0) {
let ruleRow = document.createElement("tr");
let ruleTimeCell = document.createElement("td");
ruleRow.appendChild(ruleTimeCell);
let ruleCell = document.createElement("td");
ruleCell.colSpan = 3;
ruleCell.className = "ruleCell";
ruleRow.appendChild(ruleCell);
tBody.appendChild(ruleRow);
}
// create standard row
let row = this.getRow(current);
// fading for entries before "delay rule"
if (this.config.fadeUnreachableDepartures && this.config.delay > 0) {
let steps = this.config.maxUnreachableDepartures;
if (i >= reachableDeparturePos - steps && i < reachableDeparturePos) {
let currentStep = reachableDeparturePos - i;
row.style.opacity = 1 - ((1 / steps * currentStep) - 0.2);
}
}
// fading for entries after "delay rule"
if (this.config.fadeReachableDepartures && this.config.fadePointForReachableDepartures < 1) {
if (this.config.fadePointForReachableDepartures < 0) {
this.config.fadePointForReachableDepartures = 0;
}
let startingPoint = this.config.maxReachableDepartures * this.config.fadePointForReachableDepartures;
let steps = this.config.maxReachableDepartures - startingPoint;
if (i >= reachableDeparturePos + startingPoint) {
let currentStep = (i - reachableDeparturePos) - startingPoint;
row.style.opacity = 1 - (1 / steps * currentStep);
}
}
tBody.appendChild(row);
}
});
}, (message) => {
let row = this.getNoDeparturesRow(message);
tBody.appendChild(row);
});
}
table.appendChild(tBody);
wrapper.appendChild(table);
return wrapper;
},
getNoDeparturesRow: function (message) {
let row = document.createElement("tr");
let cell = document.createElement("td");
cell.colSpan = 4;
cell.innerHTML = message;
row.appendChild(cell);
return row;
},
getRow: function (current) {
let currentWhen = moment(current.when);
let row = document.createElement("tr");
let timeCell = document.createElement("td");
timeCell.className = "centeredTd timeCell bright";
timeCell.innerHTML = currentWhen.format("HH:mm");
row.appendChild(timeCell);
let delayCell = document.createElement("td");
delayCell.className = "delayTimeCell";
let delay = Math.floor((((current.delay % 31536000) % 86400) % 3600) / 60);
if (delay > 0) {
delayCell.innerHTML = "+" + delay + " ";
if (this.config.useColorForRealtimeInfo) {
delayCell.style.color = "red";
}
} else if (delay < 0) {
delayCell.innerHTML = delay + " ";
if (this.config.useColorForRealtimeInfo) {
delayCell.style.color = "green";
}
} else if (delay === 0) {
delayCell.innerHTML = "";
}
row.appendChild(delayCell);
let lineCell = document.createElement("td");
let lineSymbol = this.getLineSymbol(current);
lineCell.className = "centeredTd noPadding lineCell";
lineCell.appendChild(lineSymbol);
row.appendChild(lineCell);
let directionCell = document.createElement("td");
directionCell.className = "directionCell bright";
if (this.config.marqueeLongDirections && current.direction.length >= 26) {
directionCell.className = "directionCell marquee";
let directionSpan = document.createElement("span");
directionSpan.innerHTML = current.direction;
directionCell.appendChild(directionSpan);
} else {
directionCell.innerHTML = this.trimDirectionString(current.direction);
}
row.appendChild(directionCell);
return row;
},
getFirstReachableDeparturePositionInArray: function () {
let now = moment();
let nowWithDelay = now.add(this.config.delay, 'minutes');
return new Promise((resolve, reject) => {
Log.log("------------------------------------------------------");
this.departuresArray.forEach((current, i, depArray) => {
let currentWhen = moment(current.when);
if (i <= depArray.length) {
if (currentWhen.isSameOrAfter(nowWithDelay)) {
Log.log("--> Reachable departure for " + this.stationId + ": " + i);
resolve(i);
}
} else if (i === depArray.length && currentWhen.isBefore(nowWithDelay)) {
Log.log("--> No reachable departure for " + this.stationId + " found.");
reject("No reachable departures found.");
}
});
});
},
trimDirectionString: function (string) {
let dirString = string;
if (dirString.indexOf(',') > -1) {
dirString = dirString.split(',')[0]
}
let viaIndex = dirString.search(/( via )/g);
if (viaIndex > -1) {
dirString = dirString.split(/( via )/g)[0]
}
return dirString
},
getLineSymbol: function (product) {
let symbol = document.createElement('div');
symbol.innerHTML = product.line;
symbol.className = product.cssClass + " xsmall";
if (this.config.showColoredLineSymbols) {
symbol.style.backgroundColor = product.color;
} else {
symbol.style.backgroundColor = "#333333";
}
return symbol;
},
getStyles: function () {
return ['style.css'];
},
getScripts: function () {
return [
"moment.js",
this.file('./vendor/bluebird-3.4.5.min.js')
];
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'FETCHER_INIT') {
if (payload.stationId === this.config.stationId) {
this.stationName = payload.stationName;
this.loaded = true;
}
}
if (notification === 'DEPARTURES') {
this.config.loaded = true;
if (payload.stationId === this.config.stationId) {
this.departuresArray = payload.departuresArray;
this.updateDom(3000);
}
}
}
});