-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMMM-2Day-NOAA-Forecast.js
271 lines (228 loc) · 9.4 KB
/
MMM-2Day-NOAA-Forecast.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
/* MagicMirror² Module: MMM-2Day-NOAA-Forecast
* Version: 0.2.0
*
* By Jinserk Baik https://github.com/jinserk/
* MIT Licensed.
*/
Module.register("MMM-2Day-NOAA-Forecast", {
defaults: {
lat: 0.0,
lon: 0.0,
units: "metric",
interval: 600000 // Every 10 mins
},
start: function () {
Log.log(`Starting module: ${this.name}`);
// Set up the local values, here we construct the request url to use
this.units = this.config.units;
this.loaded = false;
this.url = `https://api.weather.gov/points/${this.config.lat},${this.config.lon}`;
this.forecast = [];
// Trigger the first request
this.getWeatherData(this);
},
getStyles: function () {
return ["font-awesome.css", "MMM-2Day-NOAA-Forecast.css"];
},
getDom: function () {
// Set up the local wrapper
let wrapper = null;
// If we have some data to display then build the results
if (this.loaded) {
wrapper = document.createElement("table");
wrapper.className = "forecast small";
wrapper.style.cssText = "border-spacing: 5px";
// title
var forecastRow1 = document.createElement("tr");
if (this.forecast[0].isDay) {
let forecastToday = document.createElement("th");
forecastToday.className = "forecast-title";
forecastToday.colSpan = "2";
forecastToday.innerHTML = "Today";
let forecastTomorrow = document.createElement("th");
forecastTomorrow.className = "forecast-title";
forecastTomorrow.colSpan = "2";
forecastTomorrow.innerHTML = "Tomorrow";
forecastRow1.appendChild(forecastToday);
forecastRow1.appendChild(forecastTomorrow);
} else {
let forecastToday = document.createElement("th");
forecastToday.className = "forecast-title";
forecastToday.innerHTML = "Today";
let forecastTomorrow = document.createElement("th");
forecastTomorrow.className = "forecast-title";
forecastTomorrow.colSpan = "2";
forecastTomorrow.innerHTML = "Tomorrow";
let forecastAfter = document.createElement("th");
forecastAfter.className = "forecast-title";
forecastAfter.innerHTML = "Day After";
forecastRow1.appendChild(forecastToday);
forecastRow1.appendChild(forecastTomorrow);
forecastRow1.appendChild(forecastAfter);
}
// icons
let forecastRow2 = document.createElement("tr");
for (var i = 0; i < 4; i++) {
let forecastDayNight = document.createElement("td");
forecastDayNight.className = "forecast-day-night";
let forecastIcon = document.createElement("i");
forecastIcon.className = `fa fa-${
this.iconMap[this.forecast[i].icon][this.forecast[i].isDay ? 0 : 1]
} fa-2x forecast-icon`;
forecastIcon.setAttribute("height", "50");
forecastIcon.setAttribute("width", "50");
forecastDayNight.appendChild(forecastIcon);
forecastRow2.appendChild(forecastDayNight);
}
// text
let forecastRow3 = document.createElement("tr");
for (let i = 0; i < 4; i++) {
let forecastText = document.createElement("td");
forecastText.className = "forecast-text horizontalView bright";
forecastText.innerHTML = this.forecast[i].conditions;
forecastRow3.appendChild(forecastText);
}
// details
let forecastRow4 = document.createElement("tr");
for (let i = 0; i < 4; i++) {
let forecastDetail = document.createElement("td");
forecastDetail.className = "forecast-detail";
// Build up the details regarding temprature
let tempIcon = document.createElement("i");
tempIcon.className = `fa ${
this.forecast[i].isDay
? "fa-temperature-three-quarters"
: "fa-temperature-quarter"
} fa-fw detail-icon`;
tempIcon.setAttribute("height", "15");
tempIcon.setAttribute("width", "15");
let tempText = document.createElement("span");
tempText.className = "detail-text";
tempText.innerHTML = this.convertTemp(this.forecast[i].temp);
let tempBr = document.createElement("br");
// Build up the details regarding precipitation %
let rainIcon = document.createElement("i");
rainIcon.className = "fa fa-umbrella fa-fw detail-icon";
rainIcon.setAttribute("height", "15");
rainIcon.setAttribute("width", "15");
let rainText = document.createElement("span");
rainText.className = "detail-text";
rainText.innerHTML = `${this.forecast[i].pop} %`;
let rainBr = document.createElement("br");
// Removed per https://www.weather.gov/media/notification/pdf_2023_24/scn24-55_api_v1.13.pdf
// Build up the details regarding humidity %
// let humidIcon = document.createElement("i");
// humidIcon.className = "fa fa-droplet fa-fw detail-icon";
// humidIcon.setAttribute("height", "15");
// humidIcon.setAttribute("width", "15");
// let humidText = document.createElement("span");
// humidText.className = "detail-text";
// humidText.innerHTML = `${this.forecast[i].humid} %`;
// let humidBr = document.createElement("br");
// Build up the details regarding wind
let windIcon = document.createElement("i");
windIcon.className = "fa fa-wind fa-fw detail-icon";
windIcon.setAttribute("height", "15");
windIcon.setAttribute("width", "15");
let windText = document.createElement("span");
windText.className = "detail-text";
windText.innerHTML = `${this.convertWindSpeed(this.forecast[i].wspd)} ${
this.forecast[i].wdir
}`;
// Now assemble the details
forecastDetail.appendChild(tempIcon);
forecastDetail.appendChild(tempText);
forecastDetail.appendChild(tempBr);
forecastDetail.appendChild(rainIcon);
forecastDetail.appendChild(rainText);
forecastDetail.appendChild(rainBr);
// Removed per https://www.weather.gov/media/notification/pdf_2023_24/scn24-55_api_v1.13.pdf
// forecastDetail.appendChild(humidIcon);
// forecastDetail.appendChild(humidText);
// forecastDetail.appendChild(humidBr);
forecastDetail.appendChild(windIcon);
forecastDetail.appendChild(windText);
forecastRow4.appendChild(forecastDetail);
}
wrapper.appendChild(forecastRow1);
wrapper.appendChild(forecastRow2);
wrapper.appendChild(forecastRow3);
wrapper.appendChild(forecastRow4);
} else {
// Otherwise lets just use a simple div
wrapper = document.createElement("div");
wrapper.innerHTML = "Loading ...";
}
return wrapper;
},
getWeatherData: function (_this) {
// Make the initial request to the helper then set up the timer to perform the updates
_this.sendSocketNotification("GET-2DAY-NOAA-FORECAST", _this.url);
setTimeout(_this.getWeatherData, _this.config.interval, _this);
},
socketNotificationReceived: function (notification, payload) {
// check to see if the response was for us and used the same url
if (notification === "GOT-2DAY-NOAA-FORECAST" && payload.url === this.url) {
// we got some data so set the flag, stash the data to display then request the dom update
this.loaded = true;
this.forecast = payload.forecast;
//console.log(this.forecast);
this.updateDom(1000);
}
},
iconMap: {
skc: ["sun", "moon"],
few: ["sun", "moon"],
sct: ["cloud-sun", "cloud-moon"],
bkn: ["cloud-sun", "cloud-moon"],
ovc: ["cloud", "cloud"],
wind_skc: ["sun", "moon"],
wind_few: ["sun", "moon"],
wind_sct: ["cloud-sun", "cloud-moon"],
wind_bkn: ["cloud-sun", "cloud-moon"],
wind_ovc: ["cloud", "cloud"],
snow: ["snowflake", "snowflake"],
rain_snow: ["snowflake", "snowflake"],
rain_sleet: ["snowflake", "snowflake"],
fzra: ["snowflake", "snowflake"],
rain_fzra: ["snowflake", "snowflake"],
snow_fzra: ["snowflake", "snowflake"],
sleet: ["snowflake", "snowflake"],
rain: ["cloud-sun-rain", "cloud-moon-rain"],
rain_showers: ["cloud-rain", "cloud-rain"],
rain_showers_hi: ["cloud-showers-heavy", "cloud-showers-heavy"],
tsra: ["cloud-bolt", "cloud-bolt"],
tsra_sct: ["bolt", "bolt"],
tsra_hi: ["bolt-lightning", "bolt-lightning"],
tornado: ["tornado", "tornado"],
hurricane: ["hurricane", "hurricane"],
tropical_storm: ["hurricane", "hurricane"],
dust: ["smog", "smog"],
smoke: ["smog", "smog"],
haze: ["cloud-meatball", "cloud-meatball"],
hot: ["temperature-arrow-up", "temperature-arrow-up"],
cold: ["temperature-arrow-down", "temperature-arrow-down"],
blizzard: ["wind", "wind"],
fog: ["smog", "smog"]
},
convertDate: function (name, isday) {
return (
name.split("T")[0].split("-").slice(1, 3).join("/") +
(isday ? " Day" : " Night")
);
},
convertTemp: function (temp) {
// convert F -> C
return this.units === "metric"
? `${Math.round(((temp - 32) * 5) / 9)} °C`
: `${Math.round(temp)} °F`;
},
convertWindSpeed: function (wspd) {
// convert mph -> m/s
let converted = this.units === "metric" ? wspd.map((x) => x * 0.447) : wspd;
let unit = this.units === "metric" ? "m/s" : "mph";
return converted.length === 1
? `${Math.round(converted[0])} ${unit}`
: `${Math.round(converted[0])}-${Math.round(converted[1])} ${unit}`;
}
});