-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
148 lines (126 loc) · 6.17 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<title>Purple Air Device</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body style="height: 100vh;">
<nav id="navbar" class="navbar navbar-fixed-top navbar-light bg-light" style="z-index: 1;">
<div class="container-fluid">
<form class="d-flex" id="navbar-form">
<input id="apiKey" name="apiKey" class="form-control me-2" type="text" placeholder="PA API Key" aria-label="PA API Key" required>
<input id="deviceId" name="deviceId" class="form-control me-2" type="number" placeholder="PA Device ID" aria-label="PA Device ID" required>
<input id="readKey" name="readKey" class="form-control me-2" type="text" placeholder="PA Read Key (for private devices)" aria-label="PA Read Key (for private devices)">
<button id="submit-button" class="btn btn-outline-success ms-2" type="submit">Submit</button>
</form>
</div>
</nav>
<div class="container-xl">
<div id="sensorName"></div>
<div id="updateTime"></div>
<div id="dataDiv" class="row justify-content-center"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
<script type="module">
import { g, h, x, r } from 'https://unpkg.com/@xeserv/xeact@0.69.0/xeact.js';
// Load previous values or query parameters
let storedApiKey = q('apiKey');
if (!storedApiKey) storedApiKey = localStorage.getItem('paApiKey');
if (storedApiKey) g('apiKey').value = storedApiKey;
let storedDeviceId = q('deviceId');
if (!storedDeviceId) storedDeviceId = localStorage.getItem('paDeviceId');
if (storedDeviceId) g('deviceId').value = storedDeviceId;
let storedReadKey = q('readKey');
if (!storedReadKey) storedReadKey = localStorage.getItem('paReadKey');
if (storedReadKey) g('readKey').value = storedReadKey;
// Handle form submit
const form = g('navbar-form');
if (q('apiKey') && q('deviceId') && q('readKey')) {
g('navbar').classList.add('visually-hidden');
populateData();
}
form.onsubmit = (event) => {
event.preventDefault();
populateData();
// Return false to the form to prevent it from posting
return false;
};
function populateData() {
const paApiKey = g('apiKey').value;
localStorage.setItem('paApiKey', paApiKey);
const paDeviceId = g('deviceId').value;
localStorage.setItem('paDeviceId', paDeviceId);
const paReadKey = g('readKey').value;
if (paReadKey) localStorage.setItem('paReadKey', paReadKey);
// Get Purple Air group's data
let url = `https://api.purpleair.com/v1/sensors/${paDeviceId}`;
if (paReadKey) url += `?read_key=${paReadKey}`;
console.log(`URL: ${url}`);
fetch(url, {
headers: {
'X-API-Key': paApiKey
}
})
.then(response => response.json())
.then(data => {
// Clear old data
const root = g('dataDiv');
const sensorName = g('sensorName');
const updateTime = g('updateTime');
x(root);
x(sensorName);
x(updateTime);
console.dir(data);
if (data.sensor) {
sensorName.append(h('h1', { className: 'display-6 text-center', innerText: `${data.sensor.name} - ${data.sensor['pm2.5']} μg/m³` }));
const lastSeen = data.sensor['last_seen'];
const updated = new Date(lastSeen * 1000);
updateTime.append(h('p', { className: 'text-center', innerText: `Last update: ${updated.toLocaleString()}`}));
const columnClass = 'col-6 col-md-2 pt-2';
root.append(h('div', { className: columnClass}, [ generateCard('10 min', data.sensor.stats['pm2.5_10minute'], lastSeen) ]));
root.append(h('div', { className: columnClass}, [ generateCard('30 min', data.sensor.stats['pm2.5_30minute'], lastSeen) ]));
root.append(h('div', { className: columnClass}, [ generateCard('1 hour', data.sensor.stats['pm2.5_60minute'], lastSeen) ]));
root.append(h('div', { className: columnClass}, [ generateCard('6 hour', data.sensor.stats['pm2.5_6hour'], lastSeen) ]));
root.append(h('div', { className: columnClass}, [ generateCard('24 hour', data.sensor.stats['pm2.5_24hour'], lastSeen) ]));
root.append(h('div', { className: columnClass}, [ generateCard('1 week', data.sensor.stats['pm2.5_1week'], lastSeen) ]));
} else {
root.append(h('p', { innerText: `Error retrieving device data: ${data.description}` }));
}
});
}
function generateCard(title, value, lastSeen) {
return h('div', {
className: `card text-${value >= 140 ? 'white' : 'black'}`,
style: `background-color: ${getPmColor(value, lastSeen)}`
}, [
h('div', { className: 'card-header', innerText: title }),
h('div', { className: 'card-body' }, [
h('p', { className: 'card-text', innerText: value })
])
]);
}
// Translate pm2.5 value to a color
function getPmColor(pm, lastSeen) {
const now = Math.floor(Date.now()/1000);
if (now - lastSeen > 24*60*60) return '#d3d3d3';
if (pm < 12) {
return '#47af22';
} else if (pm < 35) {
return '#ee2';
} else if (pm < 55) {
return '#ff8b14';
} else if (pm < 140) {
return '#f30';
} else if (pm < 270) {
return '#800080';
}
return '#581d00';
}
function q(paramName) {
const params = new URLSearchParams(window.location.search);
if (params.has(paramName)) return params.get(paramName);
return "";
}
</script>
</body>
</html>