-
Notifications
You must be signed in to change notification settings - Fork 8
/
MMM-PilotWX.js
465 lines (401 loc) · 15.6 KB
/
MMM-PilotWX.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* Magic Mirror
*
* Module: MMM-PilotWX
*
* By Mykle1
* Mod 12/9/17 by Area_49: revised to loop through all Metars returned in the Static mode (variable length).
*
*/
Module.register('MMM-PilotWX', {
// Module config defaults.
defaults: {
ICAO: 'KJFK,EGLL,UUDD,EDDT,RJAA,ZBAA,LFPG,LIRF', // separated by comma only
colorCode: 'Standard', // Standard or Alternative color coding
mode: 'Static', // Static or Rotating display
sym: '@', // @ or / (Separator for Wind speed and direction)
measure: 'KM', // SM or KM (KM converted from SM data)
tempUnits: 'C', // C or F (F converted from C)
pressureUnits: 'Hg', // mb or hg (mb converted from hg)
time: 'Zulu', // Zulu or Local (observation time)
maxWidth: '100%', // 100% for mode: Rotating, approx 300px for mode: Static
useHeader: false,
header: '',
mostRecentPerStation: true,
useAltHeader: true,
rotateInterval: 15 * 1000, // seconds
updateInterval: 10 * 60 * 1000, // every 10 minutes
animationSpeed: 3000,
initialLoadDelay: 1875, // of module
retryDelay: 1500
},
getStyles: function () {
return ['MMM-PilotWX.css']
},
// Define start sequence.
start: function () {
Log.info('Starting module: ' + this.name)
// Set locale.
;(this.url =
'https://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=' +
this.config.ICAO +
'&hoursBeforeNow=1'),
(this.url =
this.url +
'&mostRecentForEachStation=' +
this.config.mostRecentPerStation)
console.log('URL: ' + this.url)
this.WISP = []
this.activeItem = 0
this.rotateInterval = null
this.scheduleUpdate()
},
getDom: function () {
function to_fahrenheit (t) {
return (t * 9) / 5 + 32 // convert celcius to fahrenheit
}
function to_km (d) {
return d * 1.609344 // convert SM to Kilometer
}
function to_millibar (p) {
return p * 33.864 // convert to Mb
}
function winds (dir, speed) {
var winddir = 'unavailable'
var windspeed = 'unavailable'
if (dir !== undefined) {
dirs = dir + ''
while (dirs.length < 3) dirs = '0' + dirs
winddir = dirs
}
if (speed !== undefined) {
windspeed = speed + 'KT'
}
var winds = 'unavailable'
if (winddir !== 'unavailable' || windspeed !== 'unavailable') {
winds = winddir + sym + windspeed
}
return winds
}
var wrapper = document.createElement('div')
wrapper.className = 'wrapper'
wrapper.style.maxWidth = this.config.maxWidth
if (!this.loaded) {
wrapper.classList.add('wrapper')
wrapper.innerHTML = 'Loading PilotWX . .'
wrapper.className = 'bright light small'
return wrapper
}
if (this.config.useHeader != false) {
var header = document.createElement('header')
header.classList.add('xsmall', 'bright', 'header')
header.innerHTML = this.config.header
wrapper.appendChild(header)
}
/// Begin config option for rotating or static //
///////////// First - The rotating data ///////////////////
if (this.config.mode != 'Static') {
// Rotating my data
var WISP = this.WISP
var WISPKeys = Object.keys(this.WISP)
if (WISPKeys.length > 0) {
if (this.activeItem >= WISPKeys.length) {
this.activeItem = 0
}
var WISP = this.WISP[WISPKeys[this.activeItem]]
// start config opton for color coding flight category/rules bullet
if (this.config.colorCode != 'Standard') {
// Alternative color coding flight category/rules bullet
if (WISP.flight_category == 'VFR') {
var bullet = '<font color = green> ⦿ </font>'
} else if (WISP.flight_category == 'MVFR') {
var bullet = '<font color = blue> ⦿ </font >'
} else if (WISP.flight_category == 'IFR') {
var bullet = '<font color = red> ⦿ </font>'
} else if (WISP.flight_category == 'LIFR') {
var bullet = '<font color = magenta> ⦿ </font>'
} else {
var bullet = '<font color = grey> ⦿ </font>'
}
} else {
// Standard color coding flight category/rules bullet
if (WISP.flight_category == 'VFR') {
var bullet = '<font color = blue> ⦿ </font>'
} else if (WISP.flight_category == 'MVFR') {
var bullet = '<font color = green> ⦿ </font >'
} else if (WISP.flight_category == 'IFR') {
var bullet = '<font color = yellow> ⦿ </font>'
} else if (WISP.flight_category == 'LIFR') {
var bullet = '<font color = red> ⦿ </font>'
} else {
var bullet = '<font color = grey> ⦿ </font>'
}
} // <-- end config option for color coding flight category/rules bullet
// if cloud_base_ft_agl is missing, display nothing
if (WISP.sky_condition[0]['$'].cloud_base_ft_agl == undefined) {
WISP.sky_condition[0]['$'].cloud_base_ft_agl = ''
}
var top = document.createElement('div')
top.classList.add('list-row')
var sym = this.config.sym
var measure = this.config.measure
if (this.config.measure != 'KM') {
var convert =
Math.round(WISP.visibility_statute_mi) + measure + '     '
} else {
var convert =
Math.round(to_km(WISP.visibility_statute_mi)) +
measure +
'     '
}
if (this.config.tempUnits != 'C') {
var tempCurr = Math.round(to_fahrenheit(WISP.temp_c))
var dewCurr = Math.round(to_fahrenheit(WISP.dewpoint_c))
} else {
var tempCurr = Math.round(WISP.temp_c)
var dewCurr = Math.round(WISP.dewpoint_c)
}
if (this.config.pressureUnits !== 'Hg') {
var pressureCurr = Math.round(to_millibar(WISP.altim_in_hg))
} else {
var pressureCurr = Math.round(WISP.altim_in_hg + 'e+2') + 'e-2'
}
if (this.config.time == 'Zulu') {
var time = moment
.utc(WISP.observation_time, 'YYYY-MM-DD HH:mm:ss Z')
.format('[(]HH:mm[Z)]')
} else {
var time = moment(WISP.observation_time, 'YYYY-MM-DD HH:mm:ss Z')
.local()
.format('[(]HH:mm[)]')
}
var synopsis = document.createElement('div')
synopsis.classList.add('small', 'bright', 'bottom_bar')
synopsis.innerHTML =
bullet +
'   ' +
WISP.station_id +
'     ' +
winds(WISP.wind_dir_degrees, WISP.wind_speed_kt) +
'     ' +
convert + // var for KM or SM //
WISP.sky_condition[0]['$'].sky_cover +
WISP.sky_condition[0]['$'].cloud_base_ft_agl +
'     ' +
tempCurr +
'/' +
dewCurr +
'     ' +
pressureCurr +
this.config.pressureUnits +
'     ' +
time
// + moment(WISP.observation_time, "YYYY-MM-DD HH:mm:ss Z").local().format("[(]HH:mm[)]")
top.appendChild(synopsis)
wrapper.appendChild(top)
} // <-- closes rotation loop
////////////////// ELSE - the Static data (Below) //////////////
} else {
var Plength = Object.keys(this.WISP).length
var Pindex = 0
var Fcolor_even = '<font color = white>'
var Fcolor_odd = '<font color = #33FFEE>'
function isEven (n) {
return n == parseFloat(n) ? !(n % 2) : void 0 // true if even number or zero
}
var top = document.createElement('div')
top.classList.add('list-row')
var WISP = this.WISP
//console.log (WISP)
//Station and conditions column headers if true in config useAltHeader
if (this.config.useAltHeader != false) {
var station = document.createElement('div')
station.classList.add('small', 'bright', 'station')
station.innerHTML =
'<u>Station</u>                 <u>Conditions</u>'
top.appendChild(station)
}
var table = document.createElement('table')
table.className = 'small'
///loop through all METAR items here
while (Pindex < Plength) {
// vars for color coding flight_category bullets
var a = WISP[Pindex].flight_category
// start config opton for color coding
if (this.config.colorCode != 'Standard') {
// Alternative color coding
if (a == 'VFR') {
var aBullet = '<font color = green> ⦿ </font>'
} else if (a == 'MVFR') {
var aBullet = '<font color = blue> ⦿ </font >'
} else if (a == 'IFR') {
var aBullet = '<font color = red> ⦿ </font>'
} else if (a == 'LIFR') {
var aBullet = '<font color = magenta> ⦿ </font>'
} else {
var aBullet = '<font color = grey> ⦿ </font>'
}
} else {
// continue config option for color coding
// Stardard color coding
if (a == 'VFR') {
var aBullet = '<font color = blue> ⦿ </font>'
} else if (a == 'MVFR') {
var aBullet = '<font color = green> ⦿ </font >'
} else if (a == 'IFR') {
var aBullet = '<font color = yellow> ⦿ </font>'
} else if (a == 'LIFR') {
var aBullet = '<font color = red> ⦿ </font>'
} else {
var aBullet = '<font color = grey> ⦿ </font>'
}
} // <-- end config option for color coding SHEEESH!
// if cloud_base_ft_agl == undefined then show nothing
if (WISP[Pindex].sky_condition[0]['$'].cloud_base_ft_agl == undefined) {
WISP[Pindex].sky_condition[0]['$'].cloud_base_ft_agl = ''
}
var sym = this.config.sym
var measure = this.config.measure
// conversion from Statute Miles(SM)(data) to Kilometers(KM)
// 1 statute mile = 1.609344km
// Rounded
if (this.config.measure !== 'KM') {
var convert0 =
Math.round(WISP[Pindex].visibility_statute_mi) + measure
} else {
var convert0 =
Math.round(to_km(WISP[Pindex].visibility_statute_mi)) + measure
}
if (this.config.tempUnits !== 'C') {
var tempCurr = Math.round(to_fahrenheit(WISP[Pindex].temp_c))
var dewCurr = Math.round(to_fahrenheit(WISP[Pindex].dewpoint_c))
} else {
var tempCurr = Math.round(WISP[Pindex].temp_c)
var dewCurr = Math.round(WISP[Pindex].dewpoint_c)
}
if (this.config.pressureUnits !== 'Hg') {
var pressureCurr = Math.round(to_millibar(WISP[Pindex].altim_in_hg))
} else {
var pressureCurr =
Math.round(WISP[Pindex].altim_in_hg + 'e+2') + 'e-2'
}
if (this.config.time == 'Zulu') {
var time0 = moment
.utc(WISP[Pindex].observation_time, 'YYYY-MM-DD HH:mm:ss Z')
.format('[(]HH:mm[Z)]')
} else {
var time0 = moment(
WISP[Pindex].observation_time,
'YYYY-MM-DD HH:mm:ss Z'
)
.local()
.format('[(]HH:mm[)]')
}
// flight_category
// station_id
// wind_dir_degrees @ wind_speed_kt
// visibility in SM
// sky condition
// temp and dew point in C
// observation time
if (isEven(Pindex)) {
var Fcolor = 'dataeven'
} else {
var Fcolor = 'dataodd'
}
var row = document.createElement('tr')
if (this.config.colored) {
row.className = 'colored'
}
table.appendChild(row)
var bulletCell = document.createElement('td')
bulletCell.className = 'xsmall bright bullet'
bulletCell.innerHTML = aBullet
row.appendChild(bulletCell)
var staCell = document.createElement('td')
staCell.className = 'xsmall bright ' + Fcolor
staCell.innerHTML = WISP[Pindex].station_id
console.log('Creating row for ' + WISP[Pindex].station_id)
row.appendChild(staCell)
var windCell = document.createElement('td')
windCell.className = 'xsmall bright ' + Fcolor
windCell.innerHTML = winds(
WISP[Pindex].wind_dir_degrees,
WISP[Pindex].wind_speed_kt
)
row.appendChild(windCell)
var visCell = document.createElement('td')
visCell.className = 'xsmall bright ' + Fcolor
visCell.innerHTML = convert0
row.appendChild(visCell)
var skycovCell = document.createElement('td')
skycovCell.className = 'xsmall bright ' + Fcolor
skycovCell.innerHTML =
WISP[Pindex].sky_condition[0]['$'].sky_cover +
WISP[Pindex].sky_condition[0]['$'].cloud_base_ft_agl
row.appendChild(skycovCell)
var temperatureCell = document.createElement('td')
temperatureCell.className = 'xsmall bright ' + Fcolor
temperatureCell.innerHTML = tempCurr + '/' + dewCurr
row.appendChild(temperatureCell)
var altimCell = document.createElement('td')
altimCell.className = 'xsmall bright ' + Fcolor
altimCell.innerHTML = +pressureCurr + this.config.pressureUnits
row.appendChild(altimCell)
var timeCell = document.createElement('td')
timeCell.className = 'xsmall bright ' + Fcolor
timeCell.innerHTML = time0
row.appendChild(timeCell)
top.appendChild(table)
Pindex++
}
//end loop
wrapper.appendChild(top)
} // closes else
return wrapper
}, // <-- closes the getDom function
// roundToTwo: function(num) {
// return +(Math.round(num + "e+2") + "e-2");
// },
///// Add this function to the modules you want to control with voice //////
notificationReceived: function (notification, payload) {
if (notification === 'HIDE_PILOTS') {
this.hide(1000)
this.updateDom(300)
} else if (notification === 'SHOW_PILOTS') {
this.show(1000)
this.updateDom(300)
}
},
processWISP: function (data) {
this.WISP = data[0].METAR // take this down to just before what I really need
console.log(this.WISP) // for checking
this.loaded = true
},
scheduleCarousel: function () {
// console.log("WISP Carousel"); // for checking
this.rotateInterval = setInterval(() => {
this.activeItem++
this.updateDom(this.config.animationSpeed)
}, this.config.rotateInterval)
},
scheduleUpdate: function () {
setInterval(() => {
this.getWISP()
}, this.config.updateInterval)
this.getWISP(this.config.initialLoadDelay)
var self = this
},
getWISP: function () {
this.sendSocketNotification('GET_WISP', this.url)
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'WISP_RESULT') {
this.processWISP(payload)
if (this.rotateInterval == null) {
this.scheduleCarousel()
}
this.updateDom(this.config.animationSpeed)
}
this.updateDom(this.config.initialLoadDelay)
}
})