-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMMM-UKNationalRail.js
458 lines (377 loc) · 15.9 KB
/
MMM-UKNationalRail.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* Timetable for Trains Module */
/* Magic Mirror
* Module: UK National Rail
*
* By Nick Wootton
* based on SwissTransport module by Benjamin Angst http://www.beny.ch
* MIT Licensed.
*/
Module.register("MMM-UKNationalRail", {
// Define module defaults
defaults: {
updateInterval: 5 * 60 * 1000, // Update every 5 minutes.
animationSpeed: 2000,
fade: true,
fadePoint: 0.25, // Start on 1/4th of the list.
initialLoadDelay: 0, // start delay seconds.
apiBase: 'https://transportapi.com/v3/uk/train/station/',
stationCode: '', // CRS code for station
app_key: '', // TransportAPI App Key
app_id: '', // TransportAPI App ID
called_at: '',
calling_at: '',
darwin: false,
destination: '',
from_offset: '',
operator: '',
origin: '',
service: '',
to_offset: '',
train_status: '',
type: '',
maxResults: 5, //Maximum number of results to display
showOrigin: false, //Show origin of train
showPlatform: true, //Show departure platform of train
showActualDeparture: true, //Show real-time departure time
debug: false
},
// Define required scripts.
getStyles: function() {
return ["trains.css", "font-awesome.css"];
},
// Define required scripts.
getScripts: function() {
return ["moment.js", this.file('titleCase.js')];
},
//Define header for module.
getHeader: function() {
return this.data.header;
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
// Set locale.
moment.locale(config.language);
this.trains = {};
this.loaded = false;
this.url = encodeURI(this.config.apiBase + this.config.stationCode + '/live.json' + this.getParams());
if (this.config.debug) {
Log.warn('URL Request is: ' + this.url);
}
// Initial start up delay via a timeout
this.updateTimer = setTimeout(() => {
this.fetchTrainInfo();
// Now we've had our initial delay, re-fetch our train information at the interval given in the config
this.updateTimer = setInterval(() => {
this.fetchTrainInfo();
}, this.config.updateInterval);
}, this.config.initialLoadDelay);
},
// Trigger an update of our train data
fetchTrainInfo: function() {
if (!this.hidden) {
this.sendSocketNotification("GET_TRAININFO", { 'url': this.url } );
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
if (this.config.stationCode === "") {
wrapper.innerHTML = "Please set the Station Code: " + this.stationCode + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.config.app_id === "") {
wrapper.innerHTML = "Please set the application ID: " + this.app_id + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.config.app_key === "") {
wrapper.innerHTML = "Please set the application key: " + this.app_key + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = "Loading trains ...";
wrapper.className = "dimmed light small";
return wrapper;
}
//Dump train data
if (this.config.debug) {
Log.info(this.trains);
}
// *** Start Building Table
var table = document.createElement("table");
table.className = "small";
//With data returned
if (this.trains.data.length > 0) {
for (var t in this.trains.data) {
var myTrain = this.trains.data[t];
//Create row for data item
var row = document.createElement("tr");
table.appendChild(row);
//If platform is required, create first table cell
if (this.config.showPlatform) {
if (myTrain.platform) {
platform = myTrain.platform;
} else {
platform = '-';
}
var trainPlatformCell = document.createElement("td");
trainPlatformCell.innerHTML = " " + platform + " ";
trainPlatformCell.className = "platform";
row.appendChild(trainPlatformCell);
}
//Train destination cell
var trainDestCell = document.createElement("td");
trainDestCell.innerHTML = myTrain.destination;
trainDestCell.className = "bright dest";
row.appendChild(trainDestCell);
//If required train origin cell
if (this.config.showOrigin) {
var trainOriginCell = document.createElement("td");
trainOriginCell.innerHTML = myTrain.origin;
trainOriginCell.className = "trainOrigin";
row.appendChild(trainOriginCell);
}
//Timetabled departure time
var plannedDepCell = document.createElement("td");
plannedDepCell.innerHTML = myTrain.plannedDeparture;
plannedDepCell.className = "timeTabled";
row.appendChild(plannedDepCell);
//If required, live departure time
if (this.config.showActualDeparture) {
var actualDepCell = document.createElement("td");
if(myTrain.actualDeparture != null) { // Only display actual time if it exists
actualDepCell.innerHTML = "(" + myTrain.actualDeparture + ")";
} else {
actualDepCell.innerHTML = " ";
}
actualDepCell.className = "actualTime";
row.appendChild(actualDepCell);
}
//Train status cell
var statusCell = document.createElement("td");
statusCell.innerHTML = " " + titleCase(myTrain.status) + " ";
if (myTrain.status == "ON TIME") {
statusCell.className = "bright nonews status";
} else if (myTrain.status == "LATE") {
statusCell.className = "bright late status";
} else if (myTrain.status == "EARLY") {
statusCell.className = "bright early status";
} else if (myTrain.status == "CANCELLED") {
statusCell.className = "late status";
} else if (myTrain.status == "ARRIVED") {
statusCell.className = "early status";
} else if (myTrain.status == "REINSTATEMENT" || myTrain.status == "STARTS HERE") {
statusCell.className = "goodnews status";
} else if (myTrain.status == "NO REPORT" || myTrain.status == "OFF ROUTE") {
statusCell.className = "nonews status";
} else {
statusCell.className = "nonews status";
}
row.appendChild(statusCell);
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = this.trains.length * this.config.fadePoint;
var steps = this.trains.length - startingPoint;
if (t >= startingPoint) {
var currentStep = t - startingPoint;
row.style.opacity = 1 - (1 / steps * currentStep);
}
}
}
} else {
var row1 = document.createElement("tr");
table.appendChild(row1);
var messageCell = document.createElement("td");
messageCell.innerHTML = " " + this.trains.message + " ";
messageCell.className = "bright";
row1.appendChild(messageCell);
var row2 = document.createElement("tr");
table.appendChild(row2);
var timeCell = document.createElement("td");
timeCell.innerHTML = " " + this.trains.timestamp + " ";
timeCell.className = "bright";
row2.appendChild(timeCell);
}
wrapper.appendChild(table);
// *** End building results table
return wrapper;
},
/* processTrains(data)
* Uses the received data to set the various values.
*
* argument data object - Weather information received form openweather.org.
*/
processTrains: function(data) {
//Check we have data back from API
if (typeof data !== 'undefined' && data !== null) {
//define object to hold train info
this.trains = {};
//Define array of departure data
this.trains.data = [];
//Define timestamp of current data
this.trains.timestamp = new Date();
//Define message holder
this.trains.message = null;
//Figure out Station Name
//Define empty name
var stationName = "";
if (typeof data.station_name !== 'undefined' && data.station_name !== null) {
//Populate with stop name returned by TransportAPI info
stationName = data.station_name;
} else {
//Default
stationName = "Departures";
}
//Set value
this.trains.stationName = stationName;
//See if the data is Arrivals or Updates instead of departures
if (typeof data.arrivals !== 'undefined' && data.arrivals !== null) {
if (this.config.debug) {
Log.error("Arrival detected");
}
//Change label to departures
var deps = data.arrivals;
data.departures = deps;
delete data.arrivals;
}
else if (typeof data.updates !== 'undefined' && data.updates !== null) {
if (this.config.debug) {
Log.error("Update detected");
}
//Change label to departures
var deps = data.updates;
data.departures = deps;
delete data.updates;
}
else if (typeof data.passes !== 'undefined' && data.passes !== null) {
if (this.config.debug) {
Log.error("Pass detected");
}
//Change label to departures
var deps = data.passes;
data.departures = deps;
delete data.passes;
}
//Check we have route info
if (typeof data.departures !== 'undefined' && data.departures !== null) {
//... and some departures
if (typeof data.departures.all !== 'undefined' && data.departures.all !== null) {
//.. and actual departures
if (data.departures.all.length > 0) {
//Figure out how long the results are
var counter = 0;
if (this.config.maxResults > data.departures.all.length) {
counter = data.departures.all.length;
} else {
counter = this.config.maxResults;
}
for (var i = 0; i < counter; i++) {
var thisTrain = data.departures.all[i];
this.trains.data.push({
plannedDeparture: thisTrain.aimed_departure_time,
actualDeparture: thisTrain.expected_departure_time,
status: thisTrain.status,
origin: thisTrain.origin_name,
destination: thisTrain.destination_name,
leavesIn: thisTrain.best_arrival_estimate_mins,
platform: thisTrain.platform
});
}
} else {
//No departures info returned - set message
this.trains.message = "No departure info found";
if (this.config.debug) {
Log.error("=======LEVEL 4=========");
Log.error(this.trains);
Log.error("^^^^^^^^^^^^^^^^^^^^^^^");
}
}
} else {
//No departures info returned - set message
this.trains.message = "No departures scheduled";
if (this.config.debug) {
Log.error("=======LEVEL 3=========");
Log.error(this.trains);
Log.error("^^^^^^^^^^^^^^^^^^^^^^^");
}
}
} else {
//No info returned - set message
this.trains.message = "No info about the station returned";
if (this.config.debug) {
Log.error("=======LEVEL 2=========");
Log.error(this.trains);
Log.error("^^^^^^^^^^^^^^^^^^^^^^^");
}
}
} else {
//No data returned - set message
this.trains.message = "No data returned";
if (this.config.debug) {
Log.error("=======LEVEL 1=========");
Log.error(this.trains);
Log.error("^^^^^^^^^^^^^^^^^^^^^^^");
}
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
/* getParams(compliments)
* Generates an url with api parameters based on the config.
*
* return String - URL params.
*/
getParams: function() {
var params = "?";
params += "app_id=" + this.config.app_id;
params += "&app_key=" + this.config.app_key;
if (this.config.called_at.length > 0) {
params += "&called_at=" + this.config.called_at;
}
if (this.config.calling_at.length > 0) {
params += "&calling_at=" + this.config.calling_at;
}
if (this.config.darwin) {
params += "&darwin=" + this.config.darwin;
}
if (this.config.destination.length > 0) {
params += "&destination=" + this.config.destination;
}
if (this.config.from_offset.length > 0) {
params += "&from_offset=" + this.config.from_offset;
}
if (this.config.operator.length > 0) {
params += "&operator=" + this.config.operator;
}
if (this.config.origin.length > 0) {
params += "&origin=" + this.config.origin;
}
if (this.config.service.length > 0) {
params += "&service=" + this.config.service;
}
if (this.config.to_offset.length > 0) {
params += "&to_offset=" + this.config.to_offset;
}
if (this.config.train_status.length > 0) {
params += "&train_status=" + this.config.train_status;
}
if (this.config.type.length > 0) {
params += "&type=" + this.config.type;
}
if (this.config.debug) {
Log.warn(params);
}
return params;
},
// Process data returned
socketNotificationReceived: function(notification, payload) {
if (notification === 'TRAIN_DATA' && payload.url === this.url) {
this.processTrains(payload.data);
}
}
});