-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathMMM-3Day-Forecast.js
386 lines (310 loc) · 14.2 KB
/
MMM-3Day-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
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
/* Magic Mirror Module: MMM-FAA-Delay
* Version: 1.0.0
*
* By Nigel Daniels https://github.com/nigel-daniels/
* MIT Licensed.
*/
Module.register('MMM-3Day-Forecast', {
defaults: {
api_key: '',
lat: 0.0,
lon: 0.0,
units: 'M',
lang: 'en',
horizontalView: false,
interval: 900000 // Every 15 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 = 'http://api.weatherapi.com/v1/forecast.json?q=' + this.config.lat + ',' + this.config.lon + '&days=3&lang=' + this.config.lang + '&hour=12&key=' + this.config.api_key;
this.forecast = [];
this.horizontalView = this.config.horizontalView;
// Trigger the first request
this.getWeatherData(this);
},
getStyles: function() {
return ['3day_forecast.css', 'font-awesome.css'];
},
getTranslations: function() {
return {
da: 'translations/da.json',
de: 'translations/de.json',
en: 'translations/en.json',
fr: 'translations/fr.json',
it: 'translations/it.json',
nb: 'translations/nb.json',
nl: 'translations/nl.json',
pt: 'translations/pt.json'
};
},
getWeatherData: function(_this) {
// Make the initial request to the helper then set up the timer to perform the updates
_this.sendSocketNotification('GET-3DAY-FORECAST', _this.url);
setTimeout(_this.getWeatherData, _this.config.interval, _this);
},
getDom: function() {
// Set up the local wrapper
var wrapper = null;
// If we have some data to display then build the results
if (this.loaded) {
if (this.horizontalView) {
wrapper = document.createElement('table');
wrapper.className = 'small';
// Set up the forecast for three three days
for (var i = 0; i < 3; i++) {
var title = '';
// Determine which day we are detailing
switch (i) {
case 0:
title = this.translate('TODAY');
break;
case 1:
title = this.translate('TOMORROW');
break;
case 2:
title = this.translate('DAYAFTER');
break;
}
row1 = document.createElement('tr');
forecastIconCell = document.createElement('td');
forecastIconCell.className = 'forecastIcon2';
forecastIconCell.setAttribute('rowspan', '2');
forecastIcon = document.createElement('img');
forecastIcon.setAttribute('height', '50');
forecastIcon.setAttribute('width', '50');
forecastIcon.src = './modules/MMM-3Day-Forecast/images/icon/' + this.forecast[i].icon + '.gif';
forecastTitleCell = document.createElement('td');
forecastTitleCell.className = 'forecastTitle2 bright';
forecastTitleCell.setAttribute('colspan', '4');
forecastTitleCell.innerHTML = title;
row2 = document.createElement('tr');
tempIconCell = document.createElement('td');
tempIconCell.className = 'detailIcon2';
tempIcon = document.createElement('img');
tempIcon.setAttribute('height', '15');
tempIcon.setAttribute('width', '15');
tempIcon.src = './modules/MMM-3Day-Forecast/images/high.png';
tempCell = document.createElement('td');
tempCell.className = 'detailText2';
if (this.units === 'M') {
if (this.forecast[i].high_c !== '--') {
tempCell.innerHTML = Math.round(this.forecast[i].high_c) + '° C';
} else {
tempCell.innerHTML = this.forecast[i].high_c + '° C';
}
} else {
if (this.forecast[i].high_f !== '--') {
tempCell.innerHTML = Math.round(this.forecast[i].high_f) + '° F';
} else {
tempCell.innerHTML = this.forecast[i].high_f + '° F';
}
}
rainIconCell = document.createElement('td');
rainIconCell.className = 'detailIcon2';
rainIcon = document.createElement('img');
rainIcon.setAttribute('height', '15');
rainIcon.setAttribute('width', '15');
rainIcon.src = './modules/MMM-3Day-Forecast/images/wet.png';
rainCell = document.createElement('td');
rainCell.className = 'detailText2';
rainCell.innerHTML = this.forecast[i].pop + '%';
row3 = document.createElement('tr');
forecastTextCell = document.createElement('td');
forecastTextCell.className = 'forecastText2';
forecastTextCell.innerHTML = this.forecast[i].conditions;
humidityIconCell = document.createElement('td');
humidityIconCell.className = 'detailIcon2';
humidityIcon = document.createElement('img');
humidityIcon.setAttribute('height', '15');
humidityIcon.setAttribute('width', '15');
humidityIcon.src = './modules/MMM-3Day-Forecast/images/humid.png';
humidityCell = document.createElement('td');
humidityCell.className = 'detailText2';
humidityCell.innerHTML = this.forecast[i].humid + '%';
windIconCell = document.createElement('td');
windIconCell.className = 'detailIcon2';
windIcon = document.createElement('img');
windIcon.setAttribute('height', '15');
windIcon.setAttribute('width', '15');
windIcon.src = './modules/MMM-3Day-Forecast/images/dir/' + this.forecast[i].wdir + '.png';
windCell = document.createElement('td');
windCell.className = 'detailText2';
if (this.units === 'M') {
if (this.forecast[i].wspd_k !== '--') {
windCell.innerHTML = Math.round(( this.forecast[i].wspd_k / 3.6 )) + ' ' + this.translate('MPS');
} else {
windCell.innerHTML = this.forecast[i].wspd_k + ' ' + this.translate('MPS');
}
} else {
if (this.forecast[i].wspd_m !== '--') {
windCell.innerHTML = Math.round( this.forecast[i].wspd_m ) + ' ' + this.translate('MPH');
} else {
windCell.innerHTML = this.forecast[i].wspd_m + ' ' + this.translate('MPH');
}
}
forecastIconCell.appendChild(forecastIcon);
tempIconCell.appendChild(tempIcon);
rainIconCell.appendChild(rainIcon);
humidityIconCell.appendChild(humidityIcon);
windIconCell.appendChild(windIcon);
row1.appendChild(forecastIconCell);
row1.appendChild(forecastTitleCell);
row2.appendChild(tempIconCell);
row2.appendChild(tempCell);
row2.appendChild(rainIconCell);
row2.appendChild(rainCell);
row3.appendChild(forecastTextCell);
row3.appendChild(humidityIconCell);
row3.appendChild(humidityCell);
row3.appendChild(windIconCell);
row3.appendChild(windCell);
wrapper.appendChild(row1);
wrapper.appendChild(row2);
wrapper.appendChild(row3);
}
} else {
wrapper = document.createElement('table');
wrapper.className = 'forecast small';
forecastRow = document.createElement('tr');
// Set up the forecast for three three days
for (var i = 0; i < 3; i++) {
var forecastClass = '';
var title = '';
// Determine which day we are detailing
switch (i) {
case 0:
forecastClass = 'today';
title = this.translate('TODAY');
break;
case 1:
forecastClass = 'tomorrow';
title = this.translate('TOMORROW');
break;
case 2:
forecastClass = 'dayAfter';
title = this.translate('DAYAFTER');
break;
}
// Create the details for this day
forcastDay = document.createElement('td');
forcastDay.className = 'forecastday ' + forecastClass;
forcastTitle = document.createElement('div');
forcastTitle.className = 'forecastTitle';
forcastTitle.innerHTML = title;
forecastIcon = document.createElement('img');
forecastIcon.className = 'forecastIcon';
forecastIcon.setAttribute('height', '50');
forecastIcon.setAttribute('width', '50');
forecastIcon.src = './modules/MMM-3Day-Forecast/images/icon/' + this.forecast[i].icon + '.gif';
forecastText = document.createElement('div');
forecastText.className = 'forecastText horizontalView bright';
forecastText.innerHTML = this.forecast[i].conditions;
forecastBr = document.createElement('br');
// Create div to hold all of the detail data
forecastDetail = document.createElement('div');
forecastDetail.className = 'forecastDetail';
// Build up the details regarding temprature
tempIcon = document.createElement('img');
tempIcon.className = 'detailIcon';
tempIcon.setAttribute('height', '15');
tempIcon.setAttribute('width', '15');
tempIcon.src = './modules/MMM-3Day-Forecast/images/high.png';
tempText = document.createElement('span');
tempText.className = 'normal';
if (this.units === 'M') {
if (this.forecast[i].high_c !== '--') {
tempText.innerHTML = Math.round(this.forecast[i].high_c) + '° C';
} else {
tempText.innerHTML = this.forecast[i].high_c + '° C';
}
} else {
if (this.forecast[i].high_f !== '--') {
tempText.innerHTML = Math.round(this.forecast[i].high_f) + '° F';
} else {
tempText.innerHTML = this.forecast[i].high_f + '° F';
}
}
tempBr = document.createElement('br');
// Build up the details regarding precipitation %
rainIcon = document.createElement('img');
rainIcon.className = 'detailIcon';
rainIcon.setAttribute('height', '15');
rainIcon.setAttribute('width', '15');
rainIcon.src = './modules/MMM-3Day-Forecast/images/wet.png';
rainText = document.createElement('span');
rainText.innerHTML = this.forecast[i].pop + '%';
rainBr = document.createElement('br');
// Build up the details regarding humidity %
humidIcon = document.createElement('img');
humidIcon.className = 'detailIcon';
humidIcon.setAttribute('height', '15');
humidIcon.setAttribute('width', '15');
humidIcon.src = './modules/MMM-3Day-Forecast/images/humid.png';
humidText = document.createElement('span');
humidText.className = 'normal';
humidText.innerHTML = this.forecast[i].humid + '%';
humidBr = document.createElement('br');
// Build up the details regarding wind
windIcon = document.createElement('img');
windIcon.className = 'detailIcon';
windIcon.setAttribute('height', '15');
windIcon.setAttribute('width', '15');
windIcon.src = './modules/MMM-3Day-Forecast/images/wind.png';
windText = document.createElement('span');
if (this.units === 'M') {
if (this.forecast[i].wspd_k !== '--') {
windText.innerHTML = Math.round(( this.forecast[i].wspd_k / 3.6 )) + ' ' + this.translate('MPS');
} else {
windText.innerHTML = this.forecast[i].wspd_k + ' ' + this.translate('MPS');
}
} else {
if (this.forecast[i].wspd_m !== '--') {
windText.innerHTML = Math.round( this.forecast[i].wspd_m ) + ' ' + this.translate('MPH');
} else {
windText.innerHTML = this.forecast[i].wspd_m + ' ' + this.translate('MPH');
}
}
//windBr = document.createElement('br');
// Now assemble the details
forecastDetail.appendChild(tempIcon);
forecastDetail.appendChild(tempText);
forecastDetail.appendChild(tempBr);
forecastDetail.appendChild(rainIcon);
forecastDetail.appendChild(rainText);
forecastDetail.appendChild(rainBr);
forecastDetail.appendChild(humidIcon);
forecastDetail.appendChild(humidText);
forecastDetail.appendChild(humidBr);
forecastDetail.appendChild(windIcon);
forecastDetail.appendChild(windText);
//forecastDetail.appendChild(windBr);
forcastDay.appendChild(forcastTitle);
forcastDay.appendChild(forecastIcon);
forcastDay.appendChild(forecastText);
forcastDay.appendChild(forecastBr);
forcastDay.appendChild(forecastDetail);
// Now assemble the final output
forecastRow.appendChild(forcastDay);
}
wrapper.appendChild(forecastRow);
}
} else {
// Otherwise lets just use a simple div
wrapper = document.createElement('div');
wrapper.innerHTML = this.translate('LOADING');
}
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
// check to see if the response was for us and used the same url
if (notification === 'GOT-3DAY-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;
this.updateDom(1000);
}
}
});