-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-DublinRTPI.js
322 lines (278 loc) · 10.7 KB
/
MMM-DublinRTPI.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
/* A MagicMirror module to show bus, luas and rail arrival times.
* Copyright (C) 2017 Raphael Estrada
* http://raph.es/project/rtpi
* License: GNU General Public License */
Module.register("MMM-DublinRTPI", {
defaults: {
animationSpeed: 1000,
broadcastEvents: true,
colored: false,
destinations: [],
directions: [],
displayDestination: true,
displayRoute: true,
displayStopName: true,
displaySymbol: true,
fade: true,
fadePoint: 0.25,
fetchInterval: 60000,
maximumEntries: 10,
maximumNumberOfMinutes: 60,
routes: [],
stops: []
},
getStyles: function () {
return ["dublinrtpi.css", "font-awesome.css"];
},
getScripts: function () {
return [];
},
getTranslations: function () {
return false;
},
/* initialize */
start: function () {
Log.log("Starting module: " + this.name);
for (var s in this.config.stops) {
var stop = this.config.stops[s];
/* RTPI uses lowercase letters, so we need to
* ensure the IDs in our config are lowercase */
if (stop.id) {
stop.id = stop.id.toLowerCase();
}
/* if no individual symbols were defined, set
* the default ones here based on the stop id */
if (!stop.symbol) {
stop.symbol = this.defaultSymbolForStop(stop.id);
}
this.addStop(stop);
}
this.stopData = {};
this.loaded = false;
},
/* handle notifications */
socketNotificationReceived: function (notification, payload) {
if (notification === "RTPI_EVENTS") {
if (this.hasStopId(payload.stopId)) {
this.stopData[payload.stopId] = payload.events;
this.loaded = true;
if (this.config.broadcastEvents) {
this.broadcastEvents();
}
}
} else if (notification === "FETCH_ERROR") {
Log.error("DublinRTPI Error. Could not fetch stop: " + payload.stopId);
} else {
Log.log("DublinRTPI received an unknown socket notification: " + notification);
}
this.updateDom(this.config.animationSpeed);
},
/* build the HTML to render */
getDom: function () {
var events = this.createEventList();
//console.log(events);
var wrapper = document.createElement("table");
wrapper.className = "small";
if (events.length === 0) {
wrapper.innerHTML = (this.loaded) ? this.translate("EMPTY") : this.translate("LOADING");
wrapper.className = "small dimmed";
return wrapper;
}
for (var e in events) {
var event = events[e];
//console.log(event);
var eventWrapper = document.createElement("tr");
if (this.config.colored) {
eventWrapper.style.cssText = "color:" + this.colorForStop(event.stopId);
}
eventWrapper.className = "normal";
/* symbol */
if (this.config.displaySymbol) {
var symbolWrapper = document.createElement("td");
symbolWrapper.className = "symbol align-right";
var symbol = document.createElement("span");
symbol.className = "fa fa-fw fa-" + this.symbolForStop(event.stopId);
//console.log(symbol.className);
symbol.style.paddingLeft = "5px";
symbolWrapper.appendChild(symbol);
eventWrapper.appendChild(symbolWrapper);
}
/* stop name */
if (this.config.displayStopName) {
var stopNameWrapper = document.createElement("td");
stopNameWrapper.className = this.config.colored ? "stopname" : "stopname bright";
stopNameWrapper.innerHTML = this.nameForStop(event.stopId);
eventWrapper.appendChild(stopNameWrapper);
}
/* route */
if (this.config.displayRoute) {
var routeWrapper = document.createElement("td");
routeWrapper.className = this.config.colored ? "route" : "route bright";
routeWrapper.innerHTML = event.route;
eventWrapper.appendChild(routeWrapper);
}
/* destination */
if (this.config.displayDestination) {
var lineWrapper = document.createElement("td");
lineWrapper.className = this.config.colored ? "destination" : "destination bright";
lineWrapper.innerHTML = event.destination;
eventWrapper.appendChild(lineWrapper);
}
var timeWrapper = document.createElement("td");
timeWrapper.innerHTML = event.isDue ? "Due" : event.duetime + " min";
//console.log(event.duetime);
//console.log(event);
timeWrapper.className = "time light";
eventWrapper.appendChild(timeWrapper);
wrapper.appendChild(eventWrapper);
/* fade effect */
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = events.length * this.config.fadePoint;
var steps = events.length - startingPoint;
if (e >= startingPoint) {
var currentStep = e - startingPoint;
eventWrapper.style.opacity = 1 - (1 / steps * currentStep);
}
}
}
return wrapper;
},
/* Check if this config contains the stop ID.
*
* argument stopId string - stop ID to look for.
*
* return bool - The config has this stop ID
*/
hasStopId: function (stopId) {
for (var s in this.config.stops) {
var stop = this.config.stops[s];
if (stop.id === stopId) {
return true;
}
}
return false;
},
/* Creates the sorted list of all events.
*
* return array - Array with events.
*/
createEventList: function () {
var events = [];
for (var s in this.stopData) {
var stop = this.stopData[s];
for (var e in stop) {
events.push(stop[e]);
}
}
events.sort(function (a, b) {
return a.duetime - b.duetime;
});
return events.slice(0, this.config.maximumEntries);
},
/* Requests node helper to add a stop.
*
* argument stopConfig object - Configuration for the stop to add.
*/
addStop: function (stopConfig) {
Log.log("DublinRTPI adding stop id: " + stopConfig.id);
//console.log("addStop() " + stopConfig.id);
this.sendSocketNotification("ADD_RTPI_STOP", {
stopId: stopConfig.id,
directions: stopConfig.directions || this.config.directions,
routes: stopConfig.routes || this.config.routes,
destinations: stopConfig.destinations || this.config.destinations,
maximumEntries: stopConfig.maximumEntries || this.config.maximumEntries,
maximumNumberOfMinutes: stopConfig.maximumNumberOfMinutes || this.config.maximumNumberOfMinutes,
fetchInterval: stopConfig.fetchInterval || this.config.fetchInterval
});
},
/* Detects an appropriate default symbol based on the stop ID.
* - Luas stop IDs always start with 'luas' followed by digits
* - Rail stop IDs are always alphabetical
* - Bus stop IDs are always numerical
* source: https://data.dublinked.ie/cgi-bin/rtpi/busstopinformation
*
* argument stopId - The stop ID to match a symbol to
*
* return string - The symbol for the stop.
*/
defaultSymbolForStop: function(stopId) {
if(stopId.match(/^luas\d+$/)) {
return "subway"; // https://fontawesome.com/icons/subway
} else if (stopId.match(/^[A-z]+$/)) {
return "train"; // https://fontawesome.com/icons/train
} else if (stopId.match(/^\d+$/)) {
return "bus"; // https://fontawesome.com/icons/bus
} else {
return "question-circle"; // https://fontawesome.com/icons/question-circle
}
},
/* Retrieves the symbol for a specific stop ID.
*
* argument stopId string - The stop ID to look for.
*
* return string - The symbol for the stop.
*/
symbolForStop: function (stopId) {
return this.getStopProperty(stopId, "symbol", this.defaultSymbolForStop(stopId));
},
/* Retrieves the name for a specific stop ID.
*
* argument stopId string - The stop ID to look for.
*
* return string - The custom label if present, or the stop ID.
*/
nameForStop: function (stopId) {
return this.getStopProperty(stopId, "label", stopId);
},
/* Retrieves the color for a specific stop ID.
*
* argument stopId string - The stop ID to look for.
*
* return string - The color for the stop.
*/
colorForStop: function (stopId) {
return this.getStopProperty(stopId, "color", "#fff");
},
/* Helper method to retrieve the property for a specific stop.
*
* argument stopId string - The stop ID to look for.
* argument property string - The property to look for.
* argument defaultValue string - Value if property is not found.
*
* return string - The value of the property on the stop.
*/
getStopProperty: function (stopId, property, defaultValue) {
//console.log("getStopProperty()");
for (var s in this.config.stops) {
var stop = this.config.stops[s];
if (stop.id === stopId && stop.hasOwnProperty(property)) {
//console.log("getStopProperty(" + stopId + ", ", + property + ", " + defaultValue + "): " + stop[property]);
return stop[property];
}
}
return defaultValue;
},
/* Broadcasts the events to all other modules for reuse.
* The all events available in one array, sorted on duetime.
*/
broadcastEvents: function () {
var eventList = [];
for (var stopId in this.stopData) {
var stop = this.stopData[stopId];
for (var e in stop) {
var event = cloneObject(stop[e]);
event.symbol = this.symbolForStop(stopId);
event.color = this.colorForStop(stopId);
eventList.push(event);
}
}
eventList.sort(function(a, b) {
return a.duetime - b.duetime;
});
this.sendNotification("RTPI_EVENTS", eventList);
}
});