This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
MMM-Ruter.js
executable file
·321 lines (260 loc) · 9.8 KB
/
MMM-Ruter.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
/* Magic Mirror
* Module: Ruter
*
* By Cato Antonsen (https://github.com/CatoAntonsen)
* MIT Licensed.
*/
Module.register("MMM-Ruter",{
// 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 Ruter's web service. NB! Don't set it too low! (default is 30 seconds)
timeReloadInterval: 1000, // Refresh rate how often we check if we need to update the time shown on the mirror (default is every second)
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.
},
getStyles: function () {
return ["ruter.css"];
},
getScripts: function() {
return ["moment.js"];
},
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
moment.locale(config.language);
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);
setInterval(function() {
self.updateDomIfNeeded();
}, this.config.timeReloadInterval);
},
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 promises = [];
for(var i=0; i < this.config.stops.length; i++) {
promises.push(new Promise((resolv) => {
this.getStopInfo(this.config.stops[i], function(err, result) {
resolv(result);
});
}));
}
Promise.all(promises).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);
var dateB = new Date(b.time);
return dateA - dateB;
});
self.journeys = allJourneys.slice(0, self.config.maxItems);
}
});
},
updateDomIfNeeded: function() {
var needUpdate = false;
for(var i=0; i < this.journeys.length; i++) {
var time = this.formatTime(this.journeys[i].time);
if (this.previousJourneys[i] == undefined || this.previousJourneys[i].lineName != this.journeys[i].lineName || this.previousJourneys[i].time != time) {
needUpdate = true;
this.previousJourneys[i] = {};
this.previousJourneys[i].lineName = this.journeys[i].lineName;
this.previousJourneys[i].time = time;
}
}
if (needUpdate) {
this.updateDom(this.config.animationSpeed);
}
},
getStopInfo: function(stopItem, 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.send( null );
}
}
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 "http://reisapi.ruter.no/Place/GetStop/" + stopItem.stopId;
};
var client = new HttpClient();
client.get(stopUrl(), function(stopResponse) {
var stop = JSON.parse(stopResponse);
client.get(departureUrl(), function(response) {
var stops = JSON.parse(response);
var allStopItems = new Array();
for(var j = 0; j < stops.length; j++) {
var journey = stops[j].MonitoredVehicleJourney;
if (shouldAddPlatform(journey.MonitoredCall.DeparturePlatformName, stopItem.platforms)) {
var numBlockParts = null;
if (journey.TrainBlockPart != null) {
numBlockParts = journey.TrainBlockPart.NumberOfBlockParts;
}
var stopName = stopItem.stopName ? stopItem.stopName : stop.Name;
if (self.config.maxNameLength) {
stopName = stopName.substring(0, self.config.maxNameLength);
}
allStopItems.push({
stopId: stopItem.stopId,
stopName: stopName,
lineName: journey.PublishedLineName,
destinationName: journey.DestinationName,
time: journey.MonitoredCall.ExpectedDepartureTime,
platform: journey.MonitoredCall.DeparturePlatformName
});
}
};
callback(null, allStopItems)
});
})
},
getTableHeaderRow: function() {
var thLine = document.createElement("th");
thLine.className = "light";
thLine.appendChild(document.createTextNode(this.translate("LINEHEADER")));
var thDestination = document.createElement("th");
thDestination.className = "light";
thDestination.appendChild(document.createTextNode(this.translate("DESTINATIONHEADER")));
var thPlatform = document.createElement("th");
thPlatform.className = "light";
thPlatform.appendChild(document.createTextNode(this.translate("PLATFORMHEADER")));
var thStopName = document.createElement("th");
thStopName.className = "light"
thStopName.appendChild(document.createTextNode(this.translate("STOPNAMEHEADER")));
var thTime = document.createElement("th");
thTime.className = "light time"
thTime.appendChild(document.createTextNode(this.translate("TIMEHEADER")));
var thead = document.createElement("thead");
thead.addClass = "xsmall dimmed";
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 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");
tdTime.className = "time light";
tdTime.appendChild(document.createTextNode(this.formatTime(journey.time)));
var tr = document.createElement("tr");
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 diff = moment.duration(moment(t) - moment.now());
var min = diff.minutes() + diff.hours() * 60;
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 moment(t).format(this.config.timeFormat);
}
}
});