-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-VTA-Live.js
137 lines (105 loc) · 4.19 KB
/
MMM-VTA-Live.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
/* Magic Mirror Module: MMM-VTA-Live
* Version: 1.0.0
*
* By Nigel Daniels https://github.com/nigel-daniels/
* MIT Licensed.
*/
Module.register('MMM-VTA-Live', {
defaults: {
api_key: '',
agency: 'SC',
stop_code: '',
stop_name: '???',
interval: 60000 // Every min
},
start: function() {
Log.log('Starting module: ' + this.name);
// Set up the local values, here we construct the request url to use
this.loaded = false;
this.dataReq = {
api_key: this.config.api_key,
url: 'http://api.511.org/transit/StopMonitoring?api_key=' + this.config.api_key + '&agency=' + this.config.agency + '&stopcode=' + this.config.stop_code + '&format=json'
};
this.location = '';
this.result = null;
// Trigger the first request
this.getVTAData(this);
},
getStyles: function() {
return ['vta-live.css', 'font-awesome.css'];
},
getVTAData: function(_this) {
// Make the initial request to the helper then set up the timer to perform the updates
_this.sendSocketNotification('GET-VTA-DATA', _this.dataReq);
setTimeout(_this.getVTAData, _this.config.interval, _this);
},
getDom: function() {
// Set up the local wrapper
var wrapper = document.createElement('div');
wrapper.class = 'bright medium';
// If we have some data to display then build the results table
if (this.loaded) {
stopName = document.createElement('div');
stopName.className = 'vta_stop';
stopName.innerHTML = this.config.stop_name;
routeResults = document.createElement('table');
routeResults.className = 'vta_result bright';
if (this.result !== null) {
for (var key in this.result) {
var name = this.result[key][0];
var entry = this.result[key].slice(1);
routeRow = document.createElement('tr');
routeRow.className = 'vta_data vta_route';
routeSName = document.createElement('td');
routeSName.className = 'vta_sname';
//routeSName.setAttribute('style', ('color:#' + entry.route.color));
routeSName.innerHTML = key;
routeLName = document.createElement('td');
routeLName.className = 'vta_lname normal';
routeLName.innerHTML = name;
arrivalRow = document.createElement('tr');
arrivalRow.className = 'vta_data vta_arrivals';
routeBlank = document.createElement('td');
routeTimes = document.createElement('td');
routeTimes.className = 'vta_times normal';
var filteredTimes = entry.filter(this.arrivalFilter);
filteredTimes.sort(this.sortAscending);
var timeText = filteredTimes.join(', ');
if (filteredTimes.length !== entry.length)
{timeText = filteredTimes.length > 0 ? 'Arriving, ' + timeText : 'Arriving';}
routeTimes.innerHTML = timeText === 'Arriving' ? timeText : timeText + ' mins';
routeRow.appendChild(routeSName);
routeRow.appendChild(routeLName);
arrivalRow.appendChild(routeBlank)
arrivalRow.appendChild(routeTimes);
routeResults.appendChild(routeRow);
routeResults.appendChild(arrivalRow);
}
wrapper.appendChild(stopName)
wrapper.appendChild(routeResults);
} else {
// Otherwise lets just use a simple div
wrapper.innerHTML = 'Error getting stops arrival data.';
}
} else {
// Otherwise lets just use a simple div
wrapper.innerHTML = 'Loading stops arrival data...';
}
return wrapper;
},
arrivalFilter: function(element) {
return element > 1;
},
sortAscending: function(a, b) {
return a - b;
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'GOT-VTA-DATA' && payload.url === this.dataReq.url) {
// we got some data so set the flag, stash the data to display then request the dom update
this.loaded = true;
this.result = payload.result;
console.log('result: ' + payload.result);
this.updateDom(1000);
}
}
});