-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-LAMetro.js
116 lines (97 loc) · 2.75 KB
/
MMM-LAMetro.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
// Magic Mirror
// Module: MMM-PasadenaTransit
//
// By Pai Buabthong https://github.com/pbuabthong
// MIT Licensed.
//
Module.register('MMM-LAMetro', {
defaults: { // Start with the information needed for a single stop
updateInterval : 30000, // 30 seconds
maxNumETA: 5,
maxNameLength: 20
},
start: function() {
Log.info('Starting module: ' + this.name);
var self = this;
this.getDepartureInfo();
// Schedule update timer.
setInterval(() => {
self.getDepartureInfo();
}, this.config.updateInterval);
},
getStyles: function () {
return ['MMM-LAMetro.css'];
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement('div');
if (!this.info) {
wrapper.innerHTML = 'LOADING...';
wrapper.className = 'dimmed light small';
return wrapper;
}
if (this.config.maxNumETA) {
if (this.info.departures.length > this.config.maxNumETA) {
this.info.departures = this.info.departures.slice(0, this.config.maxNumETA);
}
}
if (this.info.departures.length > 0) {
console.log('no arrivals');
var table = document.createElement('table');
table.className = 'small';
var departures = this.info.departures;
departures.forEach((departure) => {
var row = document.createElement('tr');
table.appendChild(row);
var routeNo = document.createElement('td');
routeNo.className = 'route_no';
routeNo.innerHTML = departure.routeNo;
row.appendChild(routeNo);
var routeName = document.createElement('td');
routeName.className = 'route_name';
if (this.config.maxNameLength && this.config.maxNameLength != 0) {
if (departure.routeName.length > this.config.maxNameLength + 3) {
departure.routeName = departure.routeName.substr(0, this.config.maxNameLength) + '...';
}
}
routeName.innerHTML = departure.routeName;
row.appendChild(routeName);
var ETA= document.createElement('td');
ETA.className = 'eta';
ETA.innerHTML = departure.ETA;
row.appendChild(ETA);
});
return table;
} else {
wrapper.innerHTML = 'No departures';
wrapper.className = 'dimmed light small';
return wrapper;
}
},
// Override get header function
getHeader: function() {
if (this.config.header) {
return this.config.header;
}
if (this.info) {
console.log(this.info.stop_name);
return this.info.stop_name;
}
return 'Departure Times';
},
// Override notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === 'DEPARTURES') {
if (payload.stop_code == this.config.stopCode) {
this.info = payload;
this.updateDom();
}
}
},
getDepartureInfo: function() {
Log.info('Requesting departure info');
this.sendSocketNotification('GET_DEPARTURE', {
config: this.config
});
}
});