-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-FAA-Delay.js
118 lines (89 loc) · 4.07 KB
/
MMM-FAA-Delay.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
/* Magic Mirror Module: MMM-FAA-Delay
* Version: 1.0.0
*
* By Nigel Daniels https://github.com/nigel-daniels/
* MIT Licensed.
*/
Module.register('MMM-FAA-Delay', {
defaults: {
interval: 900000, // 15 minutes
showWeather: true // Display the weather for the airports
},
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.urls = [];
for (i in this.config.airports) {
//this.urls.push({code: this.config.airports[i], url: 'http://services.faa.gov/airport/status/' + this.config.airports[i] + '?format=application/json'});
this.urls.push({code: this.config.airports[i], url: 'https://soa.smext.faa.gov/asws/api/airport/status/' + this.config.airports[i]});
}
this.results = [];
// Trigger the first request
this.getAirportData(this);
},
getStyles: function() {
return ['airport.css', 'font-awesome.css'];
},
getAirportData: function(_this) {
// Make the initial request to the helper then set up the timer to perform the updates
_this.sendSocketNotification('GET-FAA-DATA', _this.urls);
setTimeout(_this.getAirportData, _this.config.interval, _this);
},
getDom: function() {
// Set up the local wrapper
var wrapper = null;
// If we have some data to display then build the results table
if (this.loaded) {
wrapper = document.createElement("table");
wrapper.className = "airport bright small";
for (i in this.results)
{
// Set up the first row with the aiport data
airportRow = document.createElement("tr");
airportCode = document.createElement("td");
airportCode.className = "code bright";
airportCode.innerHTML = this.results[i].code;
airportInfo = document.createElement("td");
airportInfo.className = "type bright";
airportInfo.innerHTML = this.results[i].type;
airportRow.appendChild(airportCode);
airportRow.appendChild(airportInfo);
// Set up the next row with detailed information
messageRow = document.createElement("tr");
blank1 = document.createElement("td");
airportMessage = document.createElement("td");
airportMessage.className = "message normal";
airportMessage.innerHTML = this.results[i].message;
messageRow.appendChild(blank1);
messageRow.appendChild(airportMessage);
// Set up the last row with weather data
weatherRow = document.createElement("tr");
blank2 = document.createElement("td");
airportWeather = document.createElement("td");
airportWeather.className = "weather normal";
airportWeather.innerHTML = this.results[i].weather;
weatherRow.appendChild(blank2);
weatherRow.appendChild(airportWeather);
// Add the rows to the table
wrapper.appendChild(airportRow);
wrapper.appendChild(messageRow);
if (this.config.showWeather) {wrapper.appendChild(weatherRow);}
}
} else {
// Otherwise lets just use a simple div
wrapper = document.createElement('div');
wrapper.innerHTML = 'Loading airport data...';
}
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
// check to see if the response was for us and used the same url
if (notification === 'GOT-FAA-DATA') {
// we got some data so set the flag, stash the data to display then request the dom update
this.loaded = true;
this.results = payload;
this.updateDom(1000);
}
}
});