-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMMM-aviationwx.js
244 lines (214 loc) · 8.54 KB
/
MMM-aviationwx.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Aviation Weather Module for MagicMirror2 (MMM-aviationwx)
* - Displays weather from METAR reports and FAA delay information where available
* - U.S. airports only
*
* METAR Code Quick Reference:
* http://chesapeakesportpilot.com/wp-content/uploads/2015/03/military_wx_codes.pdf
*
* Copyright 2017 Stuart Loh (www.hearye.org)
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
Module.register("MMM-aviationwx", {
// Initialize var for storing data
wxdata: [],
// Default module configuration variables
defaults: {
airports: "KSFO,PAO,HAF,JFK", // continental U.S. airports only
updateInterval: 10, // in minutes
},
getScripts: function() {
return ["moment.js"];
},
getStyles: function() {
return ["MMM-aviationwx.css"];
},
// Entry point for module
start: function() {
this.getWX();
this.scheduleUpdate();
},
// Override dom generator
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "medium";
// Create table element
var table = document.createElement("table");
table.classList.add("xsmall", "table");
// Create header row
var headerRow = document.createElement("tr");
var statusH = document.createElement("th");
statusH.innerHTML = " ";
headerRow.appendChild(statusH);
var airportH = document.createElement("th");
airportH.innerHTML = "Airport";
headerRow.appendChild(airportH);
var wxH = document.createElement("th");
wxH.className = "left-align";
wxH.innerHTML = "Weather";
headerRow.appendChild(wxH);
table.appendChild(headerRow);
// Abort if no wx data received
if (this.wxdata.length < 1) return wrapper;
// Format data for each airport
var airportList = this.config.airports.split(",");
airportList = airportList.map(function (ap) { return ap.trim(); });
var notFound = "";
for (var i = 0; i < airportList.length; i++) {
var airportKey = (airportList[i].length === 3) ? "K" + airportList[i] : airportList[i];
if (!(airportKey in this.wxdata)) {
if (airportKey) notFound += airportKey + " ";
console.log("Error: " + airportKey + " data not found. " +
"Check correct code, or airport has stopped reporting METAR for the day.");
continue;
}
// Pull out variables for display
var airport = this.wxdata[airportKey];
var icao = airport.id;
var iata = airport.id.substr(1);
var name = airport.site;
var fltcat = airport.fltcat;
var temp = parseInt(airport.temp);
var dewpoint = parseInt(airport.dewp);
var winddir = this.padZeroes(airport.wdir, 3);
var windspeed = this.padZeroes(airport.wspd, 2);
var wind = (windspeed > 0) ? winddir + "@" + windspeed + "kt" : "CALM";
var visibility = airport.visib;
var wx = airport.wx || "";
var cover = airport.cover;
var ceiling = (airport.ceil) ? cover + " " + airport.ceil : cover;
var obsTime = airport.obsTime; // yyyy-mm-ddThh:mm:ssZ
obsTime = obsTime.replace("Z", " +0000").replace("T", " ");
var obsTimeMoment = moment(obsTime, "YYYY-MM-DD HH:mm:ss Z").local();
var minsSinceObs = moment().diff(obsTimeMoment, "minutes");
var rawMETAR = airport.rawOb;
var delay = 0; // 0 = no data, 1 = delay, 2 = no delay
// Check if FAA delay data for airport exists
if ("FAA" in airport) {
if (airport.FAA.delay === "true") {
// See http://www.fly.faa.gov/Products/Glossary_of_Terms/glossary_of_terms.html
// for common FAA delay abbreviations
var delay = 1;
var delayReason = airport.FAA.status.reason;
var delayType = airport.FAA.status.type;
var minDelay = airport.FAA.status.minDelay;
var delayAvg = airport.FAA.status.avgDelay;
var maxDelay = airport.FAA.status.maxDelay;
var delayTime;
if (minDelay) delayTime = "Min delays of " + minDelay;
if (delayAvg) delayTime = "Avg delays of " + delayAvg;
if (maxDelay) delayTime = "Max delays of " + maxDelay;
} else {
var delay = 2;
}
}
// Create Table Row
var row = document.createElement("tr");
row.classList.add("small", "top-align");
// Show Flight Category (VFR, MVFR, IFR, LIFR)
var statusCell = document.createElement("td");
statusCell.className = "bright";
var statusSpan = document.createElement("span");
statusSpan.className = fltcat.toLowerCase();
statusSpan.setAttribute("title", fltcat);
statusSpan.innerHTML = "◉"
statusCell.appendChild(statusSpan);
row.appendChild(statusCell);
// Show Airport Name and any delays
var nameCell = document.createElement("td");
nameCell.className = "bright nodec left-align";
var tafUrl = "https://aviationweather.gov/taf/data?ids=" + icao + "&format=decoded&metars=on&layout=on";
nameCell.innerHTML = this.wrapInLink(iata, tafUrl) + " ";
nameCell.setAttribute("title", name + " Airport");
if (delay === 1) {
var delaySpan = document.createElement("span");
var faaUrl = "http://www.fly.faa.gov/flyfaa/usmap.jsp";
if (delayTime.includes("hour")) {
delaySpan.className = "major-delay nodec";
delaySpan.innerHTML = this.wrapInLink("▲", faaUrl);
} else {
delaySpan.className = "minor-delay nodec";
delaySpan.innerHTML = this.wrapInLink("△", faaUrl);
}
delaySpan.innerHTML += " ";
delaySpan.setAttribute("title", "Maximum delays of " + delayTime);
nameCell.appendChild(delaySpan);
this.sendNotification("SHOW_ALERT", {
type: "notification",
title: "Alert",
message: "Airport delays exist at " + icao + " (" + name + " Airport)",
});
}
row.appendChild(nameCell);
// Show WX
var wxCell = document.createElement("td");
wxCell.className = "xsmall bottom-align left-align";
wxCell.setAttribute("title", rawMETAR);
wxCell.innerHTML = "<b>" + wind + " " +
visibility + "SM " + ceiling + " " +
temp + "/" + dewpoint + "</b> " + wx + " " +
obsTimeMoment.format("[(]HH:mm[)]");
if (delay === 1) {
wxCell.innerHTML += "<br>Delays due to " + delayReason + " (" + delayType + ")";
wxCell.innerHTML += "<br>" + delayTime;
}
row.appendChild(wxCell);
// Append row
table.appendChild(row);
}
// Identify any airports for which data was not retrieved
if (notFound) {
var errorRow = document.createElement("tr");
errorRow.className = "xsmall dimmer";
var errorCell = document.createElement("td");
errorCell.setAttribute("colSpan", "3");
errorCell.innerHTML = "<i>No data for " + notFound + " (may not be reporting)</i>";
errorRow.appendChild(errorCell);
table.appendChild(errorRow);
notFound = "";
}
wrapper.appendChild(table);
return wrapper;
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval * 60000;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function() {
self.getWX();
}, nextLoad);
},
// Helper Functions
padZeroes: function(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
},
wrapInLink: function(text, url) {
return "<a href=\"" + url + "\" target=\"_blank\">" + text + "</a>";
},
// Data Handling Functions
getWX: function () {
var metarUrl = "https://aviationweather.gov/gis/scripts/MetarJSON.php?density=all";
var FAAUrl = "http://services.faa.gov/airport/status/<IATA_CODE>?format=application/json"
var payload = [this.config.airports, metarUrl, FAAUrl];
this.sendSocketNotification("GET_WX", payload);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "WX_RESULT") {
this.wxdata = payload;
this.updateDom(self.config.fadeSpeed);
}
},
});