-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetatmo.js
486 lines (430 loc) · 13.6 KB
/
netatmo.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
(function ($) {
'use strict';
var weekDays = ["Sunnuntai", "Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai"];
//var weekDays = ["Su", "Ma", "Ti", "Ke", "To", "Pe", "La"];
var updateInterval = netatmo.update_interval;
var updateInProgress = false;
function updateClock() {
var currentTime = new Date();
var currentDay = currentTime.getDate();
//var currentMonthName = months[ currentTime.getMonth() ];
var currentMonth = currentTime.getMonth() + 1;
var currentYear = currentTime.getFullYear();
var currentWeekDay = weekDays[currentTime.getDay()];
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;
var currentDateString = currentWeekDay + ' ' + currentDay + '.' + currentMonth + '.';
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds;
document.getElementById('date').innerText = currentDateString;
document.getElementById('time').innerText = currentTimeString;
}
function updateTemperatures() {
updateInProgress = true;
var placeholder = document.getElementById('temperatures-and-forecast');
var updateButton = document.getElementById('refresh');
updateButton.classList.add('updating');
var request = new XMLHttpRequest();
// Make an AJAX request to handler PHP file
request.open('GET', 'temperatures.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.onload = function () {
if (request.status === 200) {
placeholder.innerHTML = request.response;
drawCharts();
//console.log('Temperatures updated.');
}
else {
placeholder.innerHTML = 'Request status != 200: ' + request.status + request.response;
drawCharts();
//console.log('server error');
}
updateInProgress = false;
//clearInterval( tickUpdate );
updateButton.classList.remove('updating');
};
request.onerror = function () {
placeholder.innerHTML = 'Request error: ' + request.status + request.response;
//console.log('something went wrong');
};
request.send();
}
function updateTimeDifferences() {
var time_difference_placeholders = document.getElementsByClassName('module__details--time');
for (var i = 0; i < time_difference_placeholders.length; i++) {
var time_measured = time_difference_placeholders[i].getAttribute('data-time-measured') * 1000;
if( i === 1 && updateInProgress === false && Math.floor( new Date() / 1000 ) - time_difference_placeholders[i].getAttribute('data-time-measured') > updateInterval ) {
//console.log( 'Interval update triggered.');
updateTemperatures();
}
time_difference_placeholders[i].innerHTML = timeSince( time_measured );
}
}
function timeSince(timestamp) {
var seconds = Math.floor( ( new Date() - timestamp ) / 1000 );
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + " vuotta";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " kuukautta";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " päivää";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " tuntia";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minuuttia";
}
if (interval >= 1) {
return "Minuutti";
}
return Math.floor(seconds) + " sekuntia";
}
function removeNulls(value) {
return value !== null;
}
/**
* Find the smallest value in array. If value is greater than "floor"
* parameter, return floor instead.
* Optional multiplier can be used to scale the value.
*/
function array_min(values, floor, multiplier) {
// Remove null values from temperatures first
//var noNullsArray = array.filter(removeNulls);
//var min = Math.min.apply(Math, noNullsArray);
multiplier = (typeof multiplier !== 'undefined') ? multiplier : 1;
var min = Math.min.apply(Math, values);
if (min > floor) {
return floor;
} else {
return min * multiplier;
}
}
/**
* Find the largest value in array. If value is smaller than "ceil"
* parameter, return ceil instead.
* Optional multiplier can be used to scale up if greater than "ceil".
*/
function array_max(values, ceil, multiplier) {
multiplier = (typeof multiplier !== 'undefined') ? multiplier : 1;
var max = Math.max.apply(Math, values);
if (max < ceil) {
return ceil;
} else {
return max * multiplier;
}
}
function drawCharts() {
var progress_bar = $('.update-timer__bar');
var now = new Date();
var secondsPassedToday = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds();
var secondsInADay = 24 * 60 * 60;
var todayPercentage = secondsPassedToday / secondsInADay * 100;
progress_bar.width(todayPercentage + '%');
var rain_data_points, n;
// Put all rain measures to individual array
var rain_measures = [];
var outdoor_modules = document.querySelectorAll('div[data-module-type="outdoor"]');
if (outdoor_modules.length >= 1) {
for (var m = 0; m < outdoor_modules.length; m++) {
rain_data_points = JSON.parse(outdoor_modules[m].getAttribute('data-rain-points'));
if (rain_data_points) {
for (n = 0; n < rain_data_points.length; n++) {
rain_measures.push(rain_data_points[n][1]);
}
}
}
}
var outdoor_options = {};
var indoor_modules = document.querySelectorAll('div[data-module-type="indoor"]');
var indoor_min_temp = null;
var indoor_max_temp = null;
var indoor_min_hmdy = null;
var indoor_max_hmdy = null;
if (indoor_modules.length >= 1) {
indoor_min_temp = Math.floor( indoor_modules[0].getAttribute('data-min-temp') );
indoor_max_temp = Math.ceil(indoor_modules[0].getAttribute('data-max-temp'));
indoor_min_hmdy = Math.floor( indoor_modules[0].getAttribute('data-min-hmdy') );
indoor_max_hmdy = Math.ceil( indoor_modules[0].getAttribute('data-max-hmdy') );
for (var o = 0; o < indoor_modules.length; o++) {
if( Math.floor( indoor_modules[o].getAttribute('data-min-temp') ) < indoor_min_temp ) {
indoor_min_temp = Math.floor( indoor_modules[o].getAttribute('data-min-temp') );
}
if( Math.ceil( indoor_modules[o].getAttribute('data-max-temp') ) > indoor_max_temp ) {
indoor_max_temp = Math.ceil( indoor_modules[o].getAttribute('data-max-temp') );
}
if( Math.floor( indoor_modules[o].getAttribute('data-min-hmdy') ) < indoor_min_hmdy ) {
indoor_min_hmdy = Math.floor( indoor_modules[o].getAttribute('data-min-hmdy') );
}
if( Math.ceil( indoor_modules[o].getAttribute('data-max-hmdy') ) > indoor_max_hmdy ) {
indoor_max_hmdy = Math.ceil( indoor_modules[o].getAttribute('data-max-hmdy') );
}
}
}
if ( indoor_max_temp - indoor_min_temp < 3 ) {
indoor_min_temp = indoor_min_temp - 1.5;
indoor_max_temp = indoor_max_temp + 1.5;
} else {
indoor_min_temp = indoor_min_temp - 0.5;
indoor_max_temp = indoor_max_temp + 0.5;
}
var line_color, xaxis_options, yaxis_options, font_spec, data_series;
var d = new Date();
var startOfDay = d.setUTCHours(0,0,0,0);
startOfDay = startOfDay + d.getTimezoneOffset() * 60 * 1000;
$('.flot-chart').each(function (index, element) {
var element_id = '#' + $(element).attr('id');
var temperature_data_recent = $(element).data('points');
var temperature_data_further = $(element).data('further-points');
var rain_data = $(element).data('rain-points');
var humidity_data = $(element).data('humidity');
var element_type = $(element).data('module-type');
var min_temp = $(element).data('min-temp');
var max_temp = $(element).data('max-temp');
if (element_type === 'outdoor') {
var outdoor_temp_diff = max_temp - min_temp;
var outdoor_normalization = 15; // Normalize to this diff, i.e. the difference between chart min/max
if ( outdoor_temp_diff > ( outdoor_normalization - 2) ) {
//console.log( 'Outdoor temperature graph normalized, option A.');
outdoor_options = {
low: min_temp - 1,
high: max_temp + 1,
};
} else {
//console.log( 'Outdoor temperature graph normalized, option B.');
outdoor_options = {
low: Math.round( min_temp - ( outdoor_normalization - outdoor_temp_diff )/2 ),
high: Math.round( max_temp + ( outdoor_normalization - outdoor_temp_diff )/2 ),
};
}
if ( min_temp >= 0) {
outdoor_options.low = 0;
}
if ( max_temp <= 0 ) {
outdoor_options.high = 0;
}
}
font_spec = {
size: 11,
lineHeight: 13,
family: "HelveticaNeue, sans-serif",
color: "#888888"
};
xaxis_options = {
mode: 'time',
timeformat: '%H',
timezone: 'browser',
twelveHourClock: false,
timebase: 'seconds',
font: font_spec,
};
yaxis_options = {
font: font_spec,
tickDecimals: 0,
showTicks: true,
showMinorTicks: true
};
if (element_type === 'outdoor') {
yaxis_options.min = outdoor_options.low;
yaxis_options.max = outdoor_options.high;
line_color = 'rgb(200, 20, 30)';
//yaxis_options.tickSize = 1;
yaxis_options.minTickSize = 2;
xaxis_options.ticks = 12;
data_series = [ temperature_data_recent, temperature_data_further ];
}
if (element_type === 'indoor') {
yaxis_options.min = indoor_min_temp;
yaxis_options.max = indoor_max_temp;
line_color = 'rgb(200, 20, 30)';
yaxis_options.tickSize = null;
yaxis_options.minTickSize = 2;
xaxis_options.ticks = 6;
}
if( element_type === 'outdoor' ) {
$.plot(element_id, [
{
data: humidity_data,
color: 'rgba(41,171,226,0.2)',
yaxis: 3,
shadowSize: 0,
lines: {
show: true,
fill: false,
lineWidth: 2,
borderWidth: 1,
}
},
{
data: temperature_data_further,
color: "rgba(200, 20, 30, 0.3)",
threshold: {
below: 0,
color: 'rgba(41,171,226,0.2)',
},
shadowSize: 0,
lines: {
show: true,
fill: false,
}
},
{
data: temperature_data_recent,
color: line_color,
threshold: {
below: 0,
color: 'rgb(41,171,226)',
},
shadowSize: 0,
lines: {
show: true,
fill: true,
fillColor: { colors: [{ opacity: 0.2 }, { opacity: 0.5 }] }
//fillColor: { colors: [ 'rgba(0,0,0,0)', 'red'] }
}
},
{
data: rain_data,
color: 'rgba(24,24,24,0.9)',
yaxis: 2,
bars: {
show: true,
fill: true,
barWidth: 20 * 60 * 1000,
lineWidth: 1,
shadowSize: 0,
borderWidth: 1,
align: 'center',
fillColor: 'rgba(41,171,226,0.7)',
}
}
],
{
xaxis: xaxis_options,
yaxes: [
yaxis_options, // options for the first y-axis
{ // options for the second y-axis
min: 0,
max: array_max( rain_measures, 3, 4 ),
tickDecimals: 1,
font: font_spec,
position: 'right',
tickColor: 'rgba(240,240,240,0.2)',
alignTicksWithAxis: 1
},
{ // options for the third y-axis
min: 30,
max: 100,
tickDecimals: 0,
font: font_spec,
position: 'right',
tickColor: 'rgba(240,240,240,0.2)',
alignTicksWithAxis: 1,
}
],
grid: {
borderWidth: 0,
color: '#666666',
backgroundColor: 'rgba(240,240,240,0.1)',
//markings: [ { xaxis: { from: startOfDay, to: startOfDay }, color: "rgba(41,171,226,.2" } ]
},
});
}
if( element_type === 'indoor' ) {
$.plot(element_id, [
{
data: humidity_data,
color: 'rgba(41,171,226,0.2)',
yaxis: 2,
shadowSize: 0,
lines: {
show: true,
fill: false,
lineWidth: 2,
borderWidth: 1,
}
},
{
data: temperature_data_further,
color: 'rgba(200, 20, 30,0.2)',
shadowSize: 0,
lines: {
show: true,
fill: false,
}
},
{
data: temperature_data_recent,
color: line_color,
threshold: {
below: 0,
color: "rgb(41,171,226)"
},
shadowSize: 0,
lines: {
show: true,
fill: true,
fillColor: { colors: [{ opacity: 0.2 }, { opacity: 0.5 }] }
}
},
],
{
xaxis: xaxis_options,
yaxes: [
yaxis_options, // options for the first y-axis
{ // options for the second y-axis
min: indoor_min_hmdy - 1,
max: indoor_max_hmdy * 1.75,
tickDecimals: 0,
font: font_spec,
position: 'right',
tickColor: 'rgba(240,240,240,0.2)',
alignTicksWithAxis: 1,
minTickSize: 5,
}
],
grid: {
borderWidth: 0,
color: '#666666',
backgroundColor: 'rgba(240,240,240,0.1)',
//markings: [ { xaxis: { from: startOfDay, to: startOfDay }, color: "rgba(41,171,226,.2" } ]
},
});
}
});
}
$(function () {
updateClock();
drawCharts();
$('#refresh').click( function() {
//console.log('Refresh called.');
updateTemperatures();
});
$('#dark-mode').click(function () {
$('body').toggleClass('dark-mode');
if ($('body').hasClass('dark-mode')) {
$('[data-class="dark-mode"]').show();
$('[data-class="light-mode"]').hide();
} else {
$('[data-class="dark-mode"]').hide();
$('[data-class="light-mode"]').show();
}
});
setInterval(updateClock, 1000);
//setInterval(updateTemperatures, netatmo.update_interval * 1000);
setInterval(updateTimeDifferences, 30000);
// Reload the whole page at interval
setTimeout( function() {
location.reload();
}, 3 * 60 * 60 * 1000 );
});
})(jQuery);