-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-TFL-Arrivals.js
293 lines (265 loc) · 8.62 KB
/
MMM-TFL-Arrivals.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
/* Magic Mirror Module: MMM-TFL-Arrivals
* By Ricardo Gonzalez https://github.com/ryck/MMM-TFL-Arrivals
* MIT Licensed.
*/
Module.register("MMM-TFL-Arrivals", {
defaults: {
app_id: "",
app_key: "",
naptanId: "", // StopPoint id
updateInterval: 60 * 1 * 1000, // Every minute
animationSpeed: 2000,
fade: true,
fadePoint: 0.25, // Start on 1/4th of the list.
limit: 5,
lateThreshold: 2,
initialLoadDelay: 0, // start delay in milliseconds.
color: true,
debug: false,
},
start: function () {
Log.log("Starting module: " + this.name);
if (this.data.classes === "MMM-TFL-Arrivals") {
this.data.classes = "bright medium";
}
// Set up the local values, here we construct the request url to use
this.apiBase = "https://api.tfl.gov.uk/StopPoint/";
this.loaded = false;
this.buses = {};
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
this.updateTimer = null;
this.url = encodeURI(
this.apiBase + this.config.naptanId + "/arrivals" + this.getParams()
);
if (this.config.debug) {
Log.info(this.url);
}
this.updateBusInfo(this);
},
// updateBusInfo
updateBusInfo: function (self) {
self.sendSocketNotification("GET_TFL_ARRIVALS_DATA", { url: self.url });
},
getStyles: function () {
return ["MMM-TFL-Arrivals.css", "font-awesome.css"];
},
// Define required scripts.
getScripts: function () {
return ["moment.js"];
},
//Define header for module.
getHeader: function () {
return this.config.header;
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
if (this.config.naptanId === "") {
wrapper.innerHTML =
"Please set the station naptan code: " + this.atcocode + ".";
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 bus arrival predictions...";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.buses.data !== null) {
this.config.header = this.buses.data[0].stopName;
}
//Dump bus data
if (this.config.debug) {
Log.info(this.buses);
}
// *** Start Building Table
var bustable = document.createElement("table");
bustable.className = "small";
//If we have departure info
if (this.buses.data !== null) {
//Figure out how long the results are
var counter = this.buses.data.length;
//See if there are more results than requested and limit if necessary
if (counter > this.config.limit) {
counter = this.config.limit;
}
for (var t = 0; t < counter; t++) {
var bus = this.buses.data[t];
var row = document.createElement("tr");
bustable.appendChild(row);
//Route name/Number
var routeCell = document.createElement("td");
routeCell.className = "route";
var icon = "";
switch (bus.modeName) {
case "bus":
routeCell.className += " bus";
icon = '<i class="fas fa-bus"></i>';
break;
case "tube":
routeCell.className += " tube";
icon = '<i class="fas fa-subway"></i>';
break;
}
routeCell.innerHTML = icon + bus.routeName + " ";
row.appendChild(routeCell);
//Direction Info
var directionCell = document.createElement("td");
directionCell.className = "dest";
directionCell.innerHTML = bus.direction;
row.appendChild(directionCell);
//Time Tabled Departure
var timeTabledCell = document.createElement("td");
var timeToStation = "";
timeTabledCell.className = "timeTabled";
var minutes = moment.duration(bus.timeToStation, "seconds").minutes();
if (minutes < 1) {
timeToStation = "Due";
if (this.config.color) {
timeTabledCell.className += " due";
}
} else if (minutes < this.config.lateThreshold) {
timeToStation = minutes + " " + "min";
if (this.config.color) {
timeTabledCell.className += " late";
}
} else {
timeToStation = minutes + " " + "mins";
}
timeTabledCell.innerHTML = timeToStation;
row.appendChild(timeTabledCell);
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = this.buses.data.length * this.config.fadePoint;
var steps = this.buses.data.length - startingPoint;
if (t >= startingPoint) {
var currentStep = t - startingPoint;
row.style.opacity = 1 - (1 / steps) * currentStep;
}
}
}
} else {
var row1 = document.createElement("tr");
bustable.appendChild(row1);
var messageCell = document.createElement("td");
messageCell.innerHTML = " " + this.buses.message + " ";
messageCell.className = "bright";
row1.appendChild(messageCell);
var row2 = document.createElement("tr");
bustable.appendChild(row2);
var timeCell = document.createElement("td");
timeCell.innerHTML = " " + this.buses.timestamp + " ";
timeCell.className = "bright";
row2.appendChild(timeCell);
}
wrapper.appendChild(bustable);
// *** End building results table
return wrapper;
},
/* processBuses(data)
* Uses the received data to set the various values into a new array.
*/
processBuses: function (data) {
//Check we have data back from API
if (typeof data !== "undefined" && data !== null && data.length !== 0) {
if (this.config.debug) {
Log.info(data);
}
//Define object to hold bus data
this.buses = {};
//Define array of departure info
this.buses.data = [];
//Define timestamp of current data
this.buses.timestamp = moment().format("LLL");
//Define message holder
this.buses.message = null;
//Figure out how long the results are
var counter = data.length;
for (var i = 0; i < counter; i++) {
var bus = data[i];
if (this.config.debug) {
Log.info(
bus.stationName +
", " +
bus.lineName +
", " +
bus.towards +
", " +
bus.expectedArrival +
", " +
bus.timeToStation +
", " +
bus.modeName
);
}
this.buses.data.push({
stopName: bus.stationName,
routeName: bus.lineName,
direction: bus.destinationName,
expectedDeparture: bus.expectedArrival,
timeToStation: bus.timeToStation,
modeName: bus.modeName,
});
}
this.buses.data.sort(function (a, b) {
return a.timeToStation - b.timeToStation;
});
} else {
//No data returned - set error message
this.buses.message = "No data returned";
this.buses.data = null;
this.buses.timestamp = moment().format("LLL");
if (this.config.debug) {
Log.error("No data returned");
Log.error(this.buses);
}
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
getParams: function () {
var params = "?";
params += "app_id=" + this.config.app_id;
params += "&app_key=" + this.config.app_key;
if (this.config.debug) {
Log.info(params);
}
return params;
},
/* scheduleUpdate()
* Schedule next update.
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function () {
self.updateBusInfo(self);
}, nextLoad);
},
// Process data returned
socketNotificationReceived: function (notification, payload) {
if (notification === "TFL_ARRIVALS_DATA" && payload.url === this.url) {
this.processBuses(payload.data);
this.scheduleUpdate(this.config.updateInterval);
}
},
});