-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-stib2.js
executable file
·312 lines (253 loc) · 9.09 KB
/
MMM-stib2.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
Module.register("MMM-stib2", {
defaults: {
apiToken: "",
stops: []
},
requiresVersion: "2.1.0", // Required version of MagicMirror
/* Maps STIB messages to a font awesome icon id. This icon is shown next to
to the relevant waiting time. */
symbols: {
"TEMPS THÉORIQUE": "clock",
"TEMPS INDISP.": "exclamation-circle",
"DERNIER PASSAGE": "bus",
"VÉHICULE BLOQUÉ": "car-crash",
},
start: function() {
this.stibData = {};
this.messages = {};
this.colors = {};
this.fetchColors()
.then(() => this.update())
.then(() => {
// Schedule updates
setInterval(() => {
this.update();
}, 20000);
});
},
fetchColors: function() {
return fetch("modules/MMM-stib2/colors.json")
.then(r => r.json())
.then(colors => this.colors = colors);
},
update: function() {
let urlTimes = `https://data.stib-mivb.be/api/explore/v2.1/catalog/datasets/waiting-time-rt-production/records?apikey=${this.config.apiToken}&where=`;
const ids = this.getIds();
const promises = [];
urlTimes += encodeURIComponent( "pointid IN (" + ids.map(a => '"' + a + '"' ).join(",") + ")" )
this.fetch(urlTimes).then(response => this.processData(response)).then(() => this.updateDom(this.config.animationSpeed));
// TODO fetch traveller info
},
fetch: function(url) {
return fetch(url).then(this.handleErrors).then(r => r.json()).catch(console.error);
//Not sure the error handling is ideal here
},
handleErrors(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
},
processData: function(response) {
const idToName = {};
for (let stop of this.config.stops) {
for (let id of stop.id) {
idToName[id] = stop.name;
}
}
this.stibData = {};
for (var i = 0; i < response.results.length; i++) {
var point = response.results[i];
point.passingtimes = JSON.parse(point.passingtimes);
for (var j = 0; j < point.passingtimes.length; j++) {
var passage = point.passingtimes[j];
var pointData = this.stibData[idToName[point.pointid]] || {};
var lineData = pointData[passage.lineId] || {};
if (!passage.destination) {
continue;
}
var routeData = lineData[passage.destination.fr] || [];
routeData.push({
time: passage.expectedArrivalTime,
message: passage.message
});
lineData[passage.destination.fr] = routeData;
pointData[passage.lineId] = lineData;
this.stibData[idToName[point.pointid]] = pointData;
}
}
},
processMessages: function(response) {
var messages = response.messages;
var stopIds = this.getIds();
// Keep messages which relate to a point in the config.
var filtered = messages.filter(message => message.points.some(point => stopIds.includes(point.id)));
for (var i = 0; i < filtered.length; i++) {
var current = filtered[i];
current.lines.forEach(line => this.messages[line.id] = current.content);
}
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.classList.add("stib-table", "small");
if (Object.keys(this.stibData).length > 1) {
return this.getTable();
} else {
// TODO improve
wrapper = document.createElement("div");
wrapper.innerHTML = "no data";
}
return wrapper;
},
getTable: function() {
const currentTime = new Date();
const table = document.createElement("div");
table.classList.add("stib-table", "small");
let rowIndex = 1;
const seen = new Set();
const stopsInConfigOrder = this.config.stops.map(s => s.name);
for (let stopName of stopsInConfigOrder) {
// const stopName = stopsInConfigOrder[i];
let stop = this.stibData[stopName];
let stopSpan = document.createElement("span");
stopSpan.innerHTML = stopName;
stopSpan.classList.add("stib-stopname", "dimmed");
stopSpan.style.gridRowStart = rowIndex;
table.appendChild(stopSpan);
for (let line in stop) {
// Don't show the same line on multiple stops
if (seen.has(line)) {
continue;
}
const messageFrom = rowIndex;
const lineClass = "stib-" + stopName.replace(/[^a-zA-Z0-9]/g, "") + "-" + line;
const lineDiv = document.createElement("div");
const lineSpan = document.createElement("span");
lineSpan.innerHTML = line;
lineSpan.classList.add("stib-linenumber");
console.log(this.colors[line].COLOR_HEX);
lineSpan.style.backgroundColor = this.colors[line].COLOR_HEX || "#bbb";
lineDiv.classList.add("stib-linenumber-container");
const icon = document.createElement("span");
icon.classList.add("stib-linenumber-icon");
lineDiv.appendChild(lineSpan);
lineDiv.appendChild(icon);
const rowsForLine = Object.keys(stop[line]).length;
lineDiv.style.gridRow = rowIndex + " / span " + rowsForLine;
table.appendChild(lineDiv);
for (let route in stop[line]) {
const routeSpan = document.createElement("span");
routeSpan.innerHTML = route.toLowerCase();
routeSpan.classList.add("stib-routename", lineClass);
routeSpan.style.gridRow = rowIndex + " / span 1";
table.appendChild(routeSpan);
let div = this.getTimeDiv(stop[line][route][0], currentTime);
div.style.gridRow = rowIndex + " / span 1";
div.classList.add(lineClass);
table.appendChild(div);
div = this.getTimeDiv(stop[line][route][1], currentTime);
div.style.gridRow = rowIndex + " / span 1";
div.classList.add(lineClass, "dimmed");
table.appendChild(div);
rowIndex++;
}
if (this.messages[line]) {
icon.classList.add("fas", "fa-bullhorn");
table.appendChild(this.getMessage(this.messages[line][0].text[0], messageFrom, rowIndex));
table.querySelectorAll("." + lineClass).forEach(e => e.classList.add("stib-content-fade"));
}
const gap = document.createElement("span");
gap.classList.add("stib-routeseparator");
gap.style.gridRow = rowIndex + " / span 1";
table.appendChild(gap);
rowIndex++;
seen.add(line);
}
table.removeChild(table.lastChild);
rowIndex--;
stopSpan.style.gridRowEnd = rowIndex;
const gap = document.createElement("span");
gap.classList.add("stib-stopseparator");
gap.style.gridRow = rowIndex + " / span 1";
table.appendChild(gap);
rowIndex++;
}
// FIXME Find a better way to do this :( We don't want the separators after the last row
table.removeChild(table.lastChild);
return table;
},
getTimeDiv: function(passage, currentTime) {
const passageDiv = document.createElement("div");
const passageTime = document.createElement("span");
passageTime.innerHTML = this.getTime(currentTime, passage);
passageDiv.classList.add("stib-times");
passageDiv.appendChild(passageTime);
const icon = document.createElement("span");
icon.classList.add("fas", "stib-time-icon");
passageDiv.appendChild(icon);
if (passage && passage.message) {
const text = passage.message.fr;
const symbol = this.symbols[text];
if (!symbol) {
console.log(text);
} else {
// Special case where we need to stack two icons
if (symbol === "bus") {
icon.classList.add("fa-stack");
const bus = document.createElement("span");
bus.classList.add("fas", "fa-bus", "fa-stack-1x", "stib-time-icon");
const slash = document.createElement("span");
slash.classList.add("fas", "fa-slash", "fa-stack-1x", "stib-time-icon");
icon.appendChild(bus);
icon.appendChild(slash);
} else {
icon.classList.add("fa-" + symbol);
}
}
}
return passageDiv;
},
getMessage: function(message, from, upTo) {
const showMessage = false;
const messageSpan = document.createElement("span");
messageSpan.classList.add("stib-message", "stib-message-fade");
messageSpan.style.gridRow = from + " / " + upTo;
messageSpan.innerHTML = message.fr.toLowerCase();
return messageSpan;
},
getTime: function(currentTime, passage) {
if (passage === undefined || passage.time === undefined) {
return " ";
}
let diff = Date.parse(passage.time) - currentTime;
diff = Math.max(diff, 0);
return Math.floor(diff / (1000 * 60));
},
getIds: function() {
let ids = [];
for (let stop of this.config.stops) {
ids = ids.concat(stop.id);
}
return ids;
},
getLineIds: function() {
const ids = new Set();
for (stopName in this.stibData) {
Object.keys(this.stibData[stopName]).forEach(line => ids.add(line));
}
return Array.from(ids);
},
getScripts: function() {
return [];
},
getStyles: function() {
return [
"MMM-stib2.css", "font-awesome.css"
];
},
getTranslations: function() {
return {
en: "translations/en.json",
};
}
});