-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
95 lines (77 loc) · 2.69 KB
/
script.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
function formatData(data, time) {
return ("'" + data.getFullYear() + "-" + complementa(data.getMonth() + 1) + "-" + complementa(data.getDate()) + "T" + time + "'");
}
function complementa(valoreData) {
if (valoreData >= 1 && valoreData <= 9) {
return "0" + valoreData;
}
else if ((valoreData >= 10 && valoreData <= 31)) {
return "" + valoreData;
}
else {
return "valore data non ammissibile"
}
}
function ajaxCall(url, callback) {
xhr.onload = function () {
if (this.status >= 200 && this.status < 400) {
callback(this.responseText);
}
}
xhr.open("GET", url);
xhr.send();
}
function loadData(date) {
$("#data-table-body").html("");
$("#media").html("");
$("#minima").html("");
$("#massima").html("");
$("#ex-term").html("");
temperature.length = 0;
if (selezione.value != "") {
ajaxCall("https://www.dati.lombardia.it/resource/647i-nhxk.json?idsensore=" + selezione.value + "&$where=data between" + formatData(date, "00:00:00.000") + "and" + formatData(date, "23:59:59.000") + "&$order=data ASC", function (res) {
var table = document.getElementById("data-table-body");
var response = JSON.parse(res);
for (var i = 0; i < response.length; i++) {
temperature.push(response[i].valore);
var fTemplate = compiled(response[i]);
var row = table.insertRow(0);
row.innerHTML = fTemplate;
}
w.postMessage(temperature);
})
}
}
var rowView = "<td><%= data %></td><td><%= valore %>°</td>";
var compiled = _.template(rowView);
var xhr = new XMLHttpRequest();
var w = new Worker('worker.js');
var selezione = document.getElementById("selezione");
var radios = document.getElementsByName("giorno");
var data = new Date();
var temperature = [];
var dateYest = new Date();
var dateToda = new Date();
dateYest.setDate(dateYest.getDate() - 1);
selezione.addEventListener("change", function () {
loadData(dateToda);
});
$("#show-list").click(function(){
$("#table-container").toggle("slow", function(){});
})
for (var i = 0; i < radios.length; i++) {
radios[i].addEventListener("change", function () {
if (this.value == "0") {
loadData(dateToda);
}
else if (this.value == "-1") {
loadData(dateYest);
}
})
}
w.onmessage = function (e) {
$("#media").html("Temperatura media: " + e.data.media);
$("#minima").html("Temperatura minima: " + e.data.minTemp);
$("#massima").html("Temperatura massima: " + e.data.maxTemp);
$("#ex-term").html("Escursione termica: " + e.data.exTerm);
}