-
Notifications
You must be signed in to change notification settings - Fork 1
/
AwairDashboard.html
408 lines (374 loc) · 14.8 KB
/
AwairDashboard.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Awair Air Quality Charts</title>
<meta charset="UTF-8">
<!-- disable cache -->
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<!-- instruct browser to reload page every hour -->
<meta http-equiv="refresh" content="3600" />
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="manifest" href="site.webmanifest">
<link rel="stylesheet" type="text/css" href="https://code.highcharts.com/css/highcharts.css">
<style>
html {
height: 100%;
background-color: black;
}
body {
height: 98%;
}
.chart {
height: 20%;
}
.grid-container {
height: 80px;
/*background-color: gray;*/
border-bottom: 2px solid white;
display: grid;
grid-template-columns: auto auto auto auto auto;
}
.grid-item {
font-size: 40px;
text-align: center;
color: white;
vertical-align: middle;
}
#charts_container {
height: 100%;
}
.highcharts-background {
fill: black;
}
.highcharts-grid-line {
fill: none;
stroke: #707073;
}
.highcharts-xaxis-grid .highcharts-grid-line {
stroke-width: 1px;
}
</style>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
</head>
<body>
<div id="charts_container"></div>
<script type="text/javascript">
// Air Quality Charts:
var datasets = [{
title: "Temperature",
data: [],
type: "spline",
unit: "°C",
color: Highcharts.getOptions().colors[8],
tickInterval: 1,
margin: [0, 0, 0, 0],
}, {
title: "Relative Humidity",
data: [],
type: "spline",
unit: "%",
color: Highcharts.getOptions().colors[6],
tickInterval: 5,
margin: [0, 0, 0, 0],
}, {
title: "CO2",
data: [],
type: "spline",
unit: "ppm",
color: Highcharts.getOptions().colors[2],
tickInterval: 100,
margin: [0, 0, 0, 0],
}, {
title: "Chemicals/VOC",
data: [],
type: "spline",
unit: "ppb",
color: Highcharts.getOptions().colors[5],
margin: [0, 0, 0, 0],
}, {
title: "PM2.5",
data: [],
type: "spline",
unit: "µg/m³",
color: Highcharts.getOptions().colors[3],
margin: [0, 0, 0, 0],
}];
var charts = []; // chart objects
/**
* Highlight a point by showing tooltip, setting hover state and draw crosshair
*/
Highcharts.Point.prototype.highlight = function (event) {
this.onMouseOver(); // Show the hover marker
this.series.chart.tooltip.refresh(this); // Show the tooltip
this.series.chart.xAxis[0].drawCrosshair(event, this); // Show the crosshair
};
/**
* Highlight a point by showing tooltip, setting hover state and draw crosshair
*/
const charts_container = document.getElementById("charts_container");
datasets.forEach(dataset => {
subchart = document.createElement('div');
subchart.setAttribute("id", dataset.title);
subchart.className = "chart";
charts_container.appendChild(subchart);
charts.push(Highcharts.chart({
chart: {
renderTo: dataset.title,
zoomType: "x",
marginRight: 80, // Keep all charts left aligned
},
accessibility: {
enabled: false
},
title: {
text: dataset.title,
align: "left",
margin: 0,
floating: true,
x: 0,
style: {
fontWeight: "bold",
fontSize: "28px",
color: dataset.color,
}
},
xAxis: {
// visible: false,
// type: "linear",
type: "datetime",
// tickInterval: 1 * 3600 * 1000, // 1hr
ordinal: false,
crosshair: true,
title: {
text: null
},
labels: {
style: {
color: "white",
fontWeight: "bold"
}
},
dateTimeLabelFormats: {
day: "%A, %b %e, %Y",
hour: "%I:%M %p",
minute: "%I:%M %p",
second: "%I:%M:%S %p",
},
},
yAxis: {
ordinal: false,
opposite: true,
title: {
text: null
},
tickInterval: dataset.tickInterval,
startOnTick: false,
endOnTick: false,
labels: {
format: "{value} " + dataset.unit,
style: {
// fontWeight: "bold",
fontSize: "16px",
color: dataset.color,
}
}
},
series: [{
name: dataset.title,
data: dataset.data,
type: dataset.type,
color: dataset.color,
// fillOpacity: 0.3,
tooltip: {
valueSuffix: " " + dataset.unit
}
}],
tooltip: {
shared: true,
borderWidth: 0,
pointFormat: "{point.y}",
shadow: false,
backgroundColor: "none",
style: {
fontSize: "14px",
fontWeight: "bold"
},
dateTimeLabelFormats: {
day: "%A, %b %e, %Y",
hour: "%I:%M %p",
minute: "%I:%M %p",
second: "%I:%M:%S %p",
},
},
plotOptions: {
series: {
marker: {
enabled: false
}
}
},
// rangeSelector: {
// buttons: [{
// count: 1,
// type: "hour",
// text: "1H"
// }, {
// count: 3,
// type: "hour",
// text: "3H"
// }, {
// count: 12,
// type: "hour",
// text: "12H"
// }, {
// count: 23,
// type: "hour",
// text: "24H"
// }],
// enabled: true,
// floating: true,
// inputEnabled: false,
// selected: 2,
// },
loading: {
labelStyle: {
fontSize: "42px",
}
},
credits: {
enabled: false
},
legend: {
enabled: false
},
}));
});
// epoch time offset (from UTC to PST)
var UTCoffset = new Date().getTimezoneOffset();
Highcharts.setOptions({
global: {
timezoneOffset: UTCoffset
}
});
var xAxis_range = 24 * 60 * 60 * 1000; // 24h
function addPoint(chart, p) {
chart.series[0].addPoint(p, true, false);
chart.xAxis[0].setExtremes(Math.max(p[0] - xAxis_range, chart.xAxis[0].dataMin), p[0]);
chart.yAxis[0].setExtremes(chart.yAxis[0].dataMin, Math.max(chart.yAxis[0].dataMax, 10));
}
function setChartSeries(chart, data) {
chart.showLoading();
chart.series[0].update({
pointStart: data[0],
data: data,
}, true);
chart.xAxis[0].setExtremes(Math.max(chart.xAxis[0].dataMax - xAxis_range, chart.xAxis[0].dataMin), chart.xAxis[0].dataMax);
chart.yAxis[0].setExtremes(chart.yAxis[0].dataMin, Math.max(chart.yAxis[0].dataMax, 10));
chart.hideLoading();
}
function loadJSON(URL) {
fetch(URL, requestOptions)
.then(response => response.json())
.then(result => {
const dimensions = 5;
const data_points = Array(dimensions);
for (var i = 0; i < dimensions; i++) {
data_points[i] = [];
}
result.data.forEach(value => {
const x = Date.parse(value.timestamp);
const temp = value.sensors.filter(function (obj) {
return (obj.comp === "temp");
})[0].value;
const humid = value.sensors.filter(function (obj) {
return (obj.comp === "humid");
})[0].value;
const co2 = value.sensors.filter(function (obj) {
return (obj.comp === "co2");
})[0].value;
const voc = value.sensors.filter(function (obj) {
return (obj.comp === "voc");
})[0].value;
const pm25 = value.sensors.filter(function (obj) {
return (obj.comp === "pm25");
})[0].value;
data_points[0].push([x, parseFloat(parseFloat(temp).toFixed(1))]); // temp
data_points[1].push([x, parseFloat(parseFloat(humid).toFixed(0))]); // humid
data_points[2].push([x, parseFloat(parseFloat(co2).toFixed(0))]); // co2
data_points[3].push([x, parseFloat(parseFloat(voc).toFixed(0))]); // voc
data_points[4].push([x, parseFloat(parseFloat(pm25).toFixed(1))]); // pm2.5
});
for (var i = 0; i < dimensions; i++) {
setChartSeries(charts[i], data_points[i]);
}
})
.catch(error => console.error("error", error));
}
function updateSeries(URL) {
fetch(URL, requestOptions)
.then(response => response.json())
.then(result => {
result.data.forEach(value => {
console.log("received a new data:", result.data);
const x = Date.parse(value.timestamp);
const temp = value.sensors.filter(function (obj) {
return (obj.comp === "temp");
})[0].value;
const humid = value.sensors.filter(function (obj) {
return (obj.comp === "humid");
})[0].value;
const co2 = value.sensors.filter(function (obj) {
return (obj.comp === "co2");
})[0].value;
const voc = value.sensors.filter(function (obj) {
return (obj.comp === "voc");
})[0].value;
const pm25 = value.sensors.filter(function (obj) {
return (obj.comp === "pm25");
})[0].value;
addPoint(charts[0], [x, parseFloat(parseFloat(temp).toFixed(1))]); // temp
addPoint(charts[1], [x, parseFloat(parseFloat(humid).toFixed(0))]); // humid
addPoint(charts[2], [x, parseFloat(parseFloat(co2).toFixed(0))]); // co2
addPoint(charts[3], [x, parseFloat(parseFloat(voc).toFixed(0))]); // voc
addPoint(charts[4], [x, parseFloat(parseFloat(pm25).toFixed(1))]); // pm2.5
console.log("Added an new data point at", new Date(x).toLocaleString());
});
})
.catch(error => console.error("error", error));
}
let AWAIR_API_ENDPOINT = `https://developer-apis.awair.is/v1/users/self`
// Awair Access Token
// Reference: https://developer.getawair.com/console/access-token
const AWAIR_ACCESS_TOKEN = "PASTE_TOKEN_HERE"
var requestOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${AWAIR_ACCESS_TOKEN}`
}
};
const SELECTED_DEVICE_INDEX = 0;
// get devices and populate charts
fetch(`${AWAIR_API_ENDPOINT}/devices`, requestOptions)
.then(response => response.json())
.then(result => {
const SELECTED_DEVICE = result.devices[SELECTED_DEVICE_INDEX];
console.log("Selected device:", SELECTED_DEVICE);
const DEVICE_TYPE = SELECTED_DEVICE.deviceType;
const DEVICE_ID = SELECTED_DEVICE.deviceId;
// populate charts with last 24-hours data
loadJSON(`${AWAIR_API_ENDPOINT}/devices/${DEVICE_TYPE}/${DEVICE_ID}/air-data/5-min-avg`);
// set up a timer to update charts
const UPDATE_INTERVAL_SECONDS = 60 // 2.5 min
setInterval(function () {
updateSeries(`${AWAIR_API_ENDPOINT}/devices/${DEVICE_TYPE}/${DEVICE_ID}/air-data/latest`);
}, UPDATE_INTERVAL_SECONDS * 1000);
})
</script>
</body>
</html>