-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-UVData.js
409 lines (335 loc) · 13.4 KB
/
MMM-UVData.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
/* UV Data Module */
/* Magic Mirror
* Module: UV Data
*
* By Nick Wootton
* based on SwissTransport module by Benjamin Angst http://www.beny.ch
* MIT Licensed.
*/
Module.register('MMM-UVData', {
// Define module defaults
defaults: {
updateInterval: 15 * 60 * 1000, // Update every 15 minutes.
animationSpeed: 2000,
fade: true,
fadePoint: 0.25, // Start on 1/4th of the list.
initialLoadDelay: 0, // start delay seconds.
apiBase: 'https://api.openuv.io/api/v1/uv',
lat: '', // Lat
lng: '', // Lng
api_key: '', // OpenUV API Key
alt: '', // Altitude
header: 'UV Index',
showOzone: true, //DIsplay Ozone measurements in Dobson Units (du)
debug: false
},
// Define required scripts.
getStyles: function() {
return ['uvdata.css', 'font-awesome.css'];
},
// Define required scripts.
getScripts: function() {
return ['moment.js', this.file('titleCase.js')];
},
//Define header for module.
getHeader: function() {
if (this.config.showOzone) {
this.config.header = this.translate('HEADER_PLUS');
}
else {
this.translate('HEADER');
}
return this.config.header;
},
//Get translations
getTranslations: function() {
return {
en: 'translations/en.json',
de: 'translations/de.json'
};
},
// Define start sequence.
start: function() {
Log.info('Starting module: ' + this.name);
// Set locale.
moment.locale(config.language);
this.UVData = {};
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
this.updateTimer = null;
this.url = encodeURI(this.config.apiBase + this.getParams());
this.api_key = this.config.api_key;
if (this.config.debug) {
Log.warn('URL Request is: ' + this.url);
}
this.updateUVInfo(this);
},
// updateUVInfo
updateUVInfo: function(self) {
if (this.hidden != true) {
self.sendSocketNotification('GET_UVINFO', { 'url': this.url, 'api_key': this.api_key });
}
},
//format date output
formatDate: function(formatDate, formatString) {
if(formatDate instanceof Date) {
var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var yyyy = formatDate.getFullYear();
var yy = yyyy.toString().substring(2);
var m = formatDate.getMonth();
var mm = m < 10 ? '0' + m : m;
var mmm = months[m];
var d = formatDate.getDate();
var dd = d < 10 ? '0' + d : d;
var h = formatDate.getHours();
var hh = h < 10 ? '0' + h : h;
var n = formatDate.getMinutes();
var nn = n < 10 ? '0' + n : n;
var s = formatDate.getSeconds();
var ss = s < 10 ? '0' + s : s;
formatString = formatString.replace(/yyyy/i, yyyy);
formatString = formatString.replace(/yy/i, yy);
formatString = formatString.replace(/mmm/i, mmm);
formatString = formatString.replace(/mm/i, mm);
formatString = formatString.replace(/m/i, m);
formatString = formatString.replace(/dd/i, dd);
formatString = formatString.replace(/d/i, d);
formatString = formatString.replace(/hh/i, hh);
formatString = formatString.replace(/h/i, h);
formatString = formatString.replace(/nn/i, nn);
formatString = formatString.replace(/n/i, n);
formatString = formatString.replace(/ss/i, ss);
formatString = formatString.replace(/s/i, s);
return formatString;
} else {
return '';
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement('div');
if (this.config.lat === '') {
wrapper.innerHTML = this.translate('SET_LAT') + ': ' + this.lat + '.';
wrapper.className = 'dimmed light small';
return wrapper;
}
if (this.config.lng === '') {
wrapper.innerHTML = this.translate('SET_LON') + ': ' + this.lng + '.';
wrapper.className = 'dimmed light small';
return wrapper;
}
if (this.config.api_key === '') {
wrapper.innerHTML = this.translate('SET_KEY') + ': ' + this.api_key + '.';
wrapper.className = 'dimmed light small';
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOAD_MSG');
wrapper.className = 'dimmed light small';
return wrapper;
}
// *** Start Building Table
var table = document.createElement('table');
table.className = 'small';
//With data returned
if (typeof this.UVData.result !== 'undefined' && this.UVData.result !== null) {
var myUV = this.UVData.result;
//Create row for Current UV
var uvrow = document.createElement('tr');
table.appendChild(uvrow);
//UV cell
var UVCurrentCell = document.createElement('td');
UVCurrentCell.innerHTML = myUV.uv_current;
if ((myUV.uv_current >=0) && (myUV.uv_current <=3)) {
UVCurrentCell.className = 'bright status low';
}
else if ((myUV.uv_current >=3) && (myUV.uv_current <=6)) {
UVCurrentCell.className = 'bright status moderate';
}
else if ((myUV.uv_current >=6) && (myUV.uv_current <=8)) {
UVCurrentCell.className = 'bright status high';
}
else if ((myUV.uv_current >=8) && (myUV.uv_current <=11)) {
UVCurrentCell.className = 'bright status veryhigh';
}
else if (myUV.uv_current >=11) {
UVCurrentCell.className = 'bright status extreme';
}
else {
UVCurrentCell.className = 'bright ';
}
uvrow.appendChild(UVCurrentCell);
//Time reported
var UVTimeCell = document.createElement('td');
var oUVDate = new Date (Date.parse(myUV.uv_time));
UVTimeCell.innerHTML = this.translate('CURRENT_UV') + ' ' + this.formatDate(oUVDate, 'hh:nn');
UVTimeCell.className = 'time';
uvrow.appendChild(UVTimeCell);
//Create row for Max UV
var maxrow = document.createElement('tr');
table.appendChild(maxrow);
//UV Max Values cell
var UVMaxCell = document.createElement('td');
UVMaxCell.innerHTML = myUV.uv_max;
if ((myUV.uv_max >=0) && (myUV.uv_max <=3)) {
UVMaxCell.className = 'bright status low';
}
else if ((myUV.uv_max >=3) && (myUV.uv_max <=6)) {
UVMaxCell.className = 'bright status moderate';
}
else if ((myUV.uv_max >=6) && (myUV.uv_max <=8)) {
UVMaxCell.className = 'bright status high';
}
else if ((myUV.uv_max >=8) && (myUV.uv_max <=11)) {
UVMaxCell.className = 'bright status veryhigh';
}
else if (myUV.uv_max >=11) {
UVMaxCell.className = 'bright status extreme';
}
else {
UVMaxCell.className = 'bright';
}
maxrow.appendChild(UVMaxCell);
//Time Max UV reported
var UVMaxTimeCell = document.createElement('td');
var oUVMaxDate = new Date (Date.parse(myUV.uv_max_time));
UVMaxTimeCell.innerHTML = this.translate('MAX_UV') + ' ' + this.formatDate(oUVMaxDate, 'hh:nn');
UVMaxTimeCell.className = 'time';
maxrow.appendChild(UVMaxTimeCell);
//If required, show ozone levels
if (this.config.showOzone) {
//Create row for Ozone
var ozonerow = document.createElement('tr');
table.appendChild(ozonerow);
//Ozone Values cell
var OzoneCell = document.createElement('td');
OzoneCell.innerHTML = myUV.ozone + ' du';
OzoneCell.className = 'bright status';
ozonerow.appendChild(OzoneCell);
//Time Ozone reported
var OzoneTimeCell = document.createElement('td');
var oOzoneDate = new Date (Date.parse(myUV.ozone_time));
OzoneTimeCell.innerHTML = this.translate('OZONE') + ' ' + this.formatDate(oOzoneDate, 'hh:nn');
OzoneTimeCell.className = 'time';
ozonerow.appendChild(OzoneTimeCell);
}
} else {
var row1 = document.createElement('tr');
table.appendChild(row1);
var messageCell = document.createElement('td');
messageCell.innerHTML = ' ' + this.UVData.message + ' ';
messageCell.className = 'bright';
row1.appendChild(messageCell);
var row2 = document.createElement('tr');
table.appendChild(row2);
var timeCell = document.createElement('td');
timeCell.innerHTML = ' ' + this.UVData.timestamp + ' ';
timeCell.className = 'bright';
row2.appendChild(timeCell);
}
wrapper.appendChild(table);
// *** End building results table
return wrapper;
},
/* processUVData(data)
* Uses the received data to set the various values.
*
* argument data object - Weather information received form openweather.org.
*/
processUVData: function(data) {
//Dump UV data
if (this.config.debug) {
Log.info(data);
}
//Check we have data back from API
if (typeof data !== 'undefined' && data !== null) {
//define object to hold UV info
this.UVData = {};
//Define object for UV data
this.UVData.result = {};
//Define message holder
this.UVData.message = null;
//Timestamp
this.UVData.timestamp = new Date();
//Check we have UV info
if (typeof data.result !== 'undefined' && data.result !== null) {
if (this.config.debug) {
Log.info(data.result);
}
//.. and actual value
if (typeof data.result.uv !== 'undefined' && data.result.uv !== null) {
this.UVData.result.uv_current = data.result.uv;
this.UVData.result.uv_time = data.result.uv_time;
this.UVData.result.uv_max = data.result.uv_max;
this.UVData.result.uv_max_time = data.result.uv_max_time;
this.UVData.result.ozone = data.result.ozone;
this.UVData.result.ozone_time = data.result.ozone_time;
} else {
//No uv info returned - set message
this.UVData.message = 'No UV info found';
if (this.config.debug) {
Log.error('=======LEVEL 3=========');
Log.error(this.UVData);
Log.error('^^^^^^^^^^^^^^^^^^^^^^^');
}
}
} else {
//No info returned - set message
this.UVData.message = 'No info about the UV levels returned';
if (this.config.debug) {
Log.error('=======LEVEL 2=========');
Log.error(this.UVData);
Log.error('^^^^^^^^^^^^^^^^^^^^^^^');
}
}
} else {
//No data returned - set message
this.UVData.message = 'No data returned';
if (this.config.debug) {
Log.error('=======LEVEL 1=========');
Log.error(this.UVData);
Log.error('^^^^^^^^^^^^^^^^^^^^^^^');
}
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
/* getParams(compliments)
* Generates an url with api parameters based on the config.
*
* return String - URL params.
*/
getParams: function() {
var params = '?';
params += 'lat=' + this.config.lat;
params += '&lng=' + this.config.lng;
params += '&altlat=' + this.config.alt;
if (this.config.debug) {
Log.warn(params);
}
return params;
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update. If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== 'undefined' && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function() {
self.updateUVInfo(self);
}, nextLoad);
},
// Process data returned
socketNotificationReceived: function(notification, payload) {
if (notification === 'UV_DATA' && payload.url === this.url) {
this.processUVData(payload.data);
this.scheduleUpdate(this.config.updateInterval);
}
}
});