-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
112 lines (93 loc) · 4.62 KB
/
node_helper.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
/* Magic Mirror Module: MMM-FAA-Delay helper
* Version: 1.0.0
*
* By Nigel Daniels https://github.com/nigel-daniels/
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-FAA-Delay helper, started...');
},
getAirportData: function(payload) {
// The payload should be the request url we want to use
var urls = payload;
var results = [];
this.count = 0;
var _this = this;
for (var i = 0; i < urls.length; i++)
{
request({
url: urls[i].url,
method: 'GET',
headers: {'accept': 'application/json'}},
function(error, response, body) {
// Lets convert the body into JSON
var result = JSON.parse(body);
//var path = response.request.uri.pathname;
//var code = path.substring(path.lastIndexOf('/') + 1, path.length);
var faaResult = {
code: '???',
type: '',
message: '',
weather: ''
};
// Check to see if we are error free and got an OK response
if (!error && response.statusCode == 200) {
faaResult.code = result.IATA;
// Is there a delay at the airport?
if (result.Delay) {
// Figure out the type of delay and craft the response accordingly
faaResult.type = result.Status[0].Type === undefined ? 'Delay' : result.Status[0].Type;
switch (faaResult.type) {
case 'Airport Closure':
faaResult.message = 'Airport closed due to ' + result.Status[0].Reason + ', expected reopening ' + result.Status[0].Reopen + '.';
break;
case 'Ground Stop':
faaResult.message = 'Ground stoppage due to ' + result.Status[0].Reason + ', expected end is ' + result.Status[0].EndTime + '.';
break;
case 'Ground Delay':
faaResult.message = 'Ground delay due to ' + result.Status[0].Reason + ', average delay is ' + result.Status[0].AvgDelay + '.';
break;
case 'Delay':
faaResult.message = 'Delay is due to ' + result.Status[0].Reason + '.';
break;
default:
faaResult.message = 'Delay due to ' + result.Status[0].Reason + ', delays are from ' + result.Status[0].MinDelay + ' to ' + result.Status[0].MaxDelay + ', and ' + result.Status[0].Trend + '.';
break;
}
} else {
faaResult.type = 'Ok';
faaResult.message = result.Status[0].Reason;
}
// Now let's get the weather at the airport
faaResult.weather = result.Weather.Weather[0].Temp[0] + ', temp ' + result.Weather.Temp + ', wind ' + result.Weather.Wind + ', visibility ' + result.Weather.Visibility + '.';
} else if (error && response.statusCode == 502) {
// If we get an error and a 502 it's what the FAA use to indicate thier system is down.
faaResult.type = 'No Data';
faaResult.message = 'FAA system down.';
faaResult.weather = 'No weather data.';
} else {
// In all other cases it's some other error
faaResult.type = 'Error';
faaResult.message = 'Error requesting data.';
faaResult.weather = 'No weather data.';
}
results.push(faaResult);
_this.count++;
if (_this.count === urls.length)
{
// We have the responses figured out so lets fire off the notifiction
_this.sendSocketNotification('GOT-FAA-DATA', results);
}
});
}
},
socketNotificationReceived: function(notification, payload) {
// Check this is for us and if it is let's get the airport info
if (notification === 'GET-FAA-DATA') {
this.getAirportData(payload);
}
}
});