-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-Metar.js
98 lines (86 loc) · 2.93 KB
/
MMM-Metar.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
// Module to display METARs with no formatting, fetching from `https://avwx.rest/` API for consistency and international use.
//https://avwx.rest/api/metar/{airport}?token={token}&filter=sanitized
Module.register("MMM-Metar", {
// Default module config.
defaults: {
apiKey: "",
airports: [ "KSFO","PAO","HAF","JFK" ],
updateInterval: 10 * 60 * 1000, //every 10 minutes
initialLoadDelay: 0, // 0 seconds delay
listClass: "metarsList",
alternateBackgrounds: true,
borderBottom: true,
warnLength: 10,
},
metars: {},
getHeader: function() {
return this.data.header ? this.data.header : "METARs";
},
start () {
//minor 'hack' to keep airports in order listed
this.config.airports.forEach((airport) => {
Log.info(airport);
this.metars[airport] = '';
});
if(this.config.length > this.config.warnLength) {
Log.warn(this.data.name + ": More than " + this.config.warnLength + " airports configured.");
}
Log.info(this.data.name + ": Fetching initial METARs");
this.scheduleUpdate(this.config.initialLoadDelay);
},
getStyles() {
return ["MMM-Metar.css"]
},
getTemplate () {
return "listView.njk";
},
getTemplateData() {
if(['top_left', 'bottom_left', 'top_right', 'bottom_right'].includes(this.data.position)) {
positionClass = 'metarHalf';
}
else {
positionClass = 'metarFull';
}
return {
config: this.config,
metars: this.metars,
positionClass: positionClass,
}
},
scheduleUpdate (delay = null) {
let nextLoad = this.config.updateInterval;
if (delay !== null && delay >= 0) {
nextLoad = delay;
}
setTimeout(() => {
this.update()
}, nextLoad);
},
update() {
Log.info(this.data.name + ": Fetching new METARs (" + this.config.airports.join(", ") + ")")
this.config.airports.forEach((airport)=> {
var updated = false;
Log.info(this.data.name + ": Fetching " + airport)
this.fetchData("https://avwx.rest/api/metar/" + airport + "?token=" + this.config.apiKey +" &filter=sanitized")
.then((data) => {
if(data.sanitized !== this.metars[airport]) {
this.metars[airport] = data.sanitized;
updated = true
}
//this.updateDom(300);
})
.catch((request) => {
Log.error(this.data.name + ": unable to load:", request);
})
.finally(() => {
if(updated) {
this.updateDom(300);
}
});
});
this.scheduleUpdate();
},
async fetchData(url) {
return performWebRequest(url);
}
});