-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-LothianBuses.js
178 lines (157 loc) · 6.45 KB
/
MMM-LothianBuses.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
Module.register('MMM-LothianBuses', {
// Default module config.
defaults: {
interval: 30,
animationSpeed: 1000,
apiKey: null,
busStopIds: []
},
apiHost: 'https://tfe-opendata.com/api/v1',
buses: [],
isError: false,
hasWarnings: false,
start: function() {
const self = this;
self.update();
setInterval(function() {
self.update();
}, this.config.interval * 1000);
},
getScripts: function () {
return ['moment.js'];
},
getStyles: function () {
return [
'font-awesome.css',
'MMM-LothianBuses.css'
];
},
update: function() {
if (!this.config.apiKey) {
return;
}
Promise.all(this.config.busStopIds.map(busStopItem => {
let busStopId = busStopItem;
let includeLines = null;
if (busStopItem instanceof Array) {
busStopId = busStopItem[0];
includeLines = busStopItem.slice(1);
}
return fetch(`${this.apiHost}/live_bus_times/${busStopId}`, {
method: 'POST',
headers: {
'Authorization': `Token ${this.config.apiKey}`
}
}).then(response => {
return includeLines instanceof Array
? response.json().then(json => json.filter(bus => includeLines.includes(bus.routeName)))
: response.json();
});
})).then(jsons => {
const newBuses = [];
let hasWarnings = false;
jsons.forEach(json => {
if (json === null) {
hasWarnings = hasWarnings || true;
return;
}
json.forEach(bus => {
newBuses.push(bus);
});
});
this.hasWarnings = hasWarnings;
newBuses.sort((a, b) => {
const d = a.departures[0].departureTimeUnix - b.departures[0].departureTimeUnix;
if (d !== 0) {
return d;
}
const n = a.routeName - b.routeName;
if (n !== 0) {
return n;
}
return a.departures[0].destination - b.departures[0].destination;
});
const animate = this.buses.length !== newBuses.length;
this.buses = newBuses;
this.isError = false;
this.updateDom(animate ? this.config.animationSpeed : undefined);
}).catch(error => {
Log.error("Failed to update data: ", error);
this.buses = [];
this.isError = true;
this.hasWarnings = true;
this.updateDom(this.config.animationSpeed);
});
},
getDom: function() {
const wrapper = document.createElement('div');
wrapper.className = 'lothian-buses';
if (!this.config.apiKey) {
wrapper.className = 'thin small dimmed';
wrapper.innerHTML = this.translate('Module not configured. Please specify an API key');
return wrapper;
}
if (!this.config.busStopIds.length === 0) {
wrapper.className = 'thin small dimmed';
wrapper.innerHTML = this.translate('Module not configured. Please specify at least one bus stop');
return wrapper;
}
if (this.isError) {
wrapper.className = 'light small dimmed';
wrapper.innerHTML = this.translate('Data unavailable. Please check logs.');
return wrapper;
}
if (this.buses.length === 0) {
wrapper.className = 'light small dimmed';
wrapper.innerHTML = this.translate('No buses');
return wrapper;
}
if (this.hasWarnings) {
wrapper.innerHTML += `<div class="light xsmall dimmed"><i class="fas fa-exclamation-triangle"></i> <em>${this.translate('Some of the routes are not available.')}</em></div>`;
}
this.buses.forEach(bus => {
const number = bus.routeName;
const departures = Array.from(bus.departures);
const row = document.createElement('div');
row.className = 'bus';
// Bus number
const busNumber = document.createElement('div');
busNumber.className = 'bus-number';
busNumber.innerHTML = `<span class="number light medium">${number}</span>`;
row.appendChild(busNumber);
if (departures && departures.length > 0) {
// Destination
const destination = document.createElement('span');
destination.className = 'light xsmall light';
destination.innerText = departures[0].destination;
busNumber.appendChild(destination);
// Bus times
const nextTime = moment(departures[0].departureTimeUnix * 1000).diff(new Date(), 'minutes');
const busTimes = document.createElement('div');
busTimes.className = 'bus-times';
const busTimeMain = document.createElement('div');
busTimeMain.className = `bus-time-main medium thin ${departures[0].isLive ? 'bright' : 'dimmed'}`;
const innerHTML = [
`<span><strong>${nextTime > 1 ? nextTime : this.translate('Due')}${!departures[0].isLive ? '<sup>*</sup>' : ''}</strong></span>`,
];
if (nextTime > 1) {
innerHTML.push(`<span>${this.translate('mins')}</span>`);
}
busTimeMain.innerHTML = innerHTML.join(' ');
busTimes.append(busTimeMain);
departures.shift();
const busTimeNext = document.createElement('div');
busTimeNext.className = 'bus-time-next xsmall light';
if (departures.length > 0) {
busTimeNext.innerHTML = `${this.translate('then in')} ${departures.map(departure => `<strong>${moment(departure.departureTimeUnix * 1000).diff(new Date(), 'minutes')}</strong>`).join(', ')} ${this.translate('mins')}`;
} else {
busTimeNext.innerHTML = this.translate('not available');
}
busTimes.appendChild(busTimeNext);
row.appendChild(busTimes);
}
wrapper.appendChild(row);
});
return wrapper;
}
});