forked from CanberraAir/CanberraAir.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purple_api.html
53 lines (44 loc) · 1.82 KB
/
purple_api.html
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
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
function update_purple_p_2_5(element_id, station_id) {
let p_2_5 = null;
let purpleJSONURL = "https://www.purpleair.com/json?callback=?&show=" + station_id;
$.getJSON(purpleJSONURL, function (data) {
let numer = 0;
let denom = 2;
// Purple devices return two values from two sensors. Sometimes one sensor returns a value, sometimes no sensors. So - ajust the average calculation to use whatever sensors we have, and throw exception if none are working.
if (typeof data.results[0].PM2_5Value === 'undefined') {
denom--;
} else {
numer = numer + Number(data.results[0].PM2_5Value);
}
if (typeof data.results[1].PM2_5Value === 'undefined') {
denom--;
} else {
numer = numer + Number(data.results[1].PM2_5Value);
}
if (denom > 0) {
p_2_5 = numer / denom;
p_2_5 = Math.round(p_2_5 * 10) / 10
} else {
throw 'PurpleAir API error';
}
})
.done(function () {
$(element_id).html("PM 2.5 is " + p_2_5);
setInterval(update_purple_p_2_5(element_id, station_id), (900000 + (Math.random() * 60000)));
});
}
$(function () {
update_purple_p_2_5("#purp42085", 42085); // Downer
update_purple_p_2_5("#purp41449", 41449); // Chisholm
});
</script>
</head>
<body>
<div id="purp42085">Loading...</div>
<div id="purp41449">Loading...</div>
</body>
</html>