forked from CatoAntonsen/MMM-Ruter
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMMM-Skyss.js
370 lines (306 loc) · 13.3 KB
/
MMM-Skyss.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
/* Magic Mirror
* Module: Ruter
*
* By Cato Antonsen (https://github.com/CatoAntonsen)
* MIT Licensed.
*/
Module.register("MMM-Skyss",{
// Default module config.
defaults: {
timeFormat: null, // This is set automatically based on global config
showHeader: false, // Set this to true to show header above the journeys (default is false)
showPlatform: false, // Set this to true to get the names of the platforms (default is false)
showStopName: false, // Show the name of the stop (you have to configure 'name' for each stop)
maxItems: 5, // Number of journeys to display (default is 5)
humanizeTimeTreshold: 15, // If time to next journey is below this value, it will be displayed as "x minutes" instead of time (default is 15 minutes)
serviceReloadInterval: 30000, // Refresh rate in MS for how often we call Skyss' web service. NB! Don't set it too low! (default is 30 seconds)
animationSpeed: 0, // How fast the animation changes when updating mirror (default is 0 second)
fade: true, // Set this to true to fade list from light to dark. (default is true)
fadePoint: 0.25, // Start on 1/4th of the list.
useRealtime: true // Whether to use realtime data from Skyss
},
getStyles: function () {
return ["skyss.css"];
},
getScripts: function() {
return [];
},
getTranslations: function() {
return {
en: "translations/en.json",
nb: "translations/nb.json"
}
},
start: function() {
console.log(this.translate("STARTINGMODULE") + ": " + this.name);
this.journeys = [];
this.previousJourneys = [];
var self = this;
// Set locale and time format based on global config
if (config.timeFormat === 24) {
this.config.timeFormat = "HH:mm";
} else {
this.config.timeFormat = "h:mm A";
}
// Just do an initial poll. Otherwise we have to wait for the serviceReloadInterval
self.startPolling();
setInterval(function() {
self.startPolling();
}, this.config.serviceReloadInterval);
},
getDom: function() {
if (this.journeys.length > 0) {
var table = document.createElement("table");
table.className = "ruter small";
if (this.config.showHeader) {
table.appendChild(this.getTableHeaderRow());
}
for(var i = 0; i < this.journeys.length; i++) {
var journey = this.journeys[i];
var tr = this.getTableRow(journey);
// Create fade effect. <-- stolen from default "calendar" module
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = this.journeys.length * this.config.fadePoint;
var steps = this.journeys.length - startingPoint;
if (i >= startingPoint) {
var currentStep = i - startingPoint;
tr.style.opacity = 1 - (1 / steps * currentStep);
}
}
table.appendChild(tr);
}
return table;
} else {
var wrapper = document.createElement("div");
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "small dimmed";
return wrapper;
}
},
startPolling: function() {
var self = this;
var promise = new Promise((resolv) => {
this.getStopInfo(this.config.stops, function(err, result) {
resolv(result);
});
});
promise.then(function(promiseResults) {
if (promiseResults.length > 0) {
var allJourneys = [];
for(var i=0; i < promiseResults.length; i++) {
allJourneys = allJourneys.concat(promiseResults[i])
}
allJourneys.sort(function(a,b) {
var dateA = new Date(a.time.Timestamp);
var dateB = new Date(b.time.Timestamp);
return dateA - dateB;
});
self.journeys = allJourneys.slice(0, self.config.maxItems);
self.updateDom();
}
});
},
getStopInfo: function(stopItems, callback) {
var self = this;
var HttpClient = function() {
this.get = function(requestUrl, requestCallback) {
// var httpRequest = new XMLHttpRequest();
// httpRequest.onreadystatechange = function() {
// if (httpRequest.readyState == 4 && httpRequest.status == 200){
// requestCallback(httpRequest.responseText);
// }
// };
// httpRequest.open("GET", requestUrl, true);
// httpRequest.setRequestHeader("Authorization", "");
// httpRequest.send(null);
self.requests.push(requestCallback);
self.sendSocketNotification("getstop", requestUrl);
}
}
//DisplayTime contains realtime-information. Formatted as "x min"(remaining time), or "HH:mm"
var processSkyssDisplaytime = function(displayTime) {
var realTime;
var regexInMinutes = new RegExp('([0-9]+) min');
var regexLocalTimeStamp = new RegExp('[0-9]{2}\:[0-9]{2}');
//Time format is "x min"
if (regexInMinutes.test(displayTime)) {
inMinutes = parseInt(displayTime.match(regexInMinutes)[1]);
// Adding 1 gives same result as skyss app -.-
realTime = moment().add(inMinutes+1, 'minutes');
//Time format is "HH:mm".
} else if (regexLocalTimeStamp.test(displayTime)) {
realTime = moment(displayTime, "HH:mm");
//Time is next day
if (realTime.isBefore(moment())) {
realTime.add(1, 'day');
}
}
return realTime;
};
// var shouldAddPlatform = function(platform, platformFilter) {
// if (platformFilter == null || platformFilter.length == 0) { return true; } // If we don't add any interesting platformFilter, then we asume we'll show all
// for(var i=0; i < platformFilter.length; i++) {
// if (platformFilter[i] === platform) { return true; }
// }
// return false;
// };
// var departureUrl = function() {
// var dateParam = "";
// if (stopItem.timeToThere) {
// var min = stopItem.timeToThere;
// var timeAhead = moment(moment.now()).add(min, "minute").format().substring(0, 16);
// console.log("Looking for journeys " + min + " minutes ahead in time.");
// dateParam = "?datetime=" + timeAhead;
// } else {
// console.log("Looking for current journeys");
// }
// return "http://reisapi.ruter.no/StopVisit/GetDepartures/" + stopItem.stopId + dateParam;
// };
var stopUrl = function() {
return "/public/departures?Hours=12&StopIdentifiers=" + stopItems.map(stopItem => stopItem.stopId).join();
};
var client = new HttpClient();
client.get(stopUrl(), function(stopResponse) {
var departure = JSON.parse(stopResponse);
var times = departure.PassingTimes;
var allStopItems = [];
for(var j = 0; j < times.length; j++) {
var journey = times[j];
var stop = departure.Stops[journey.StopIdentifier];
var timestamp;
var realtimeStamp = processSkyssDisplaytime(journey.DisplayTime);
if ( self.config.useRealtime && moment.isMoment(realtimeStamp) ) {
timestamp = realtimeStamp.toISOString();
} else {
timestamp = journey.AimedTime;
}
allStopItems.push({
stopId: journey.StopIdentifier,
stopName: stop.PlaceDescription,
lineName: journey.RoutePublicIdentifier,
destinationName: journey.TripDestination,
service: stop.ServiceModes[0],
time: {
Timestamp: timestamp,
Status: journey.Status,
},
platform: journey.Platform
});
}
callback(null, allStopItems);
})
},
getTableHeaderRow: function() {
var thLine = document.createElement("th");
thLine.className = "";
thLine.appendChild(document.createTextNode(this.translate("LINEHEADER")));
var thDestination = document.createElement("th");
thDestination.className = "";
thDestination.appendChild(document.createTextNode(this.translate("DESTINATIONHEADER")));
var thPlatform = document.createElement("th");
thPlatform.className = "";
thPlatform.appendChild(document.createTextNode(this.translate("PLATFORMHEADER")));
var thStopName = document.createElement("th");
thStopName.className = "";
thStopName.appendChild(document.createTextNode(this.translate("STOPNAMEHEADER")));
var thTime = document.createElement("th");
thTime.className = "time";
thTime.appendChild(document.createTextNode(this.translate("TIMEHEADER")));
var thead = document.createElement("thead");
thead.addClass = "xsmall dimmed";
thead.appendChild(document.createElement("th"));
thead.appendChild(thLine);
thead.appendChild(thDestination);
if (this.config.showStopName) { thead.appendChild(thStopName); }
if (this.config.showPlatform) { thead.appendChild(thPlatform); }
thead.appendChild(thTime);
return thead;
},
getTableRow: function(journey) {
var tdIcon = document.createElement("td");
var imageFA;
switch (journey.service) {
case "Bus":
case "Express":
case "Airport bus":
imageFA = "bus";
break;
case "Light rail":
imageFA = "subway";
break;
case "Ferry":
case "Boat":
imageFA = "ship";
break;
case "Train":
imageFA = "train";
break;
default:
imageFA = "rocket";
break;
}
tdIcon.className = "fa fa-"+imageFA;
var tdLine = document.createElement("td");
tdLine.className = "line";
var txtLine = document.createTextNode(journey.lineName);
tdLine.appendChild(txtLine);
var tdDestination = document.createElement("td");
tdDestination.className = "destination bright";
tdDestination.appendChild(document.createTextNode(journey.destinationName));
if (this.config.showPlatform) {
var tdPlatform = document.createElement("td");
tdPlatform.className = "platform";
tdPlatform.appendChild(document.createTextNode(journey.platform));
}
if (this.config.showStopName) {
var tdStopName = document.createElement("td");
tdStopName.className = "light";
tdStopName.appendChild(document.createTextNode(journey.stopName));
}
var tdTime = document.createElement("td");
if (journey.time.Status != "Schedule") {
tdTime.className = "time light sanntid";
} else {
tdTime.className = "time light";
}
tdTime.appendChild(document.createTextNode(this.formatTime(journey.time.Timestamp)));
var tr = document.createElement("tr");
tr.appendChild(tdIcon);
tr.appendChild(tdLine);
tr.appendChild(tdDestination);
if (this.config.showStopName) { tr.appendChild(tdStopName); }
if (this.config.showPlatform) { tr.appendChild(tdPlatform); }
tr.appendChild(tdTime);
return tr;
},
formatTime: function(t) {
var now = new Date();
var tti = new Date(t);
var diff = tti - now;
var min = Math.floor(diff/60000);
if (min == 0) {
return this.translate("NOW");
} else if (min == 1) {
return this.translate("1MIN");
} else if (min < this.config.humanizeTimeTreshold) {
return min + " " + this.translate("MINUTES");
} else {
return tti.getHours() + ":" + ("0" + tti.getMinutes()).slice(-2);
}
},
socketNotificationReceived: function(notification, payload) {
var self = this;
Log.log(this.name + " recieved a socket notification: " + notification);
if (notification == "getstop") {
if (payload.err) {
throw payload.err;
} else {
self.requests.shift()(payload.response);
}
}
},
requests: []
});