From f7e9434a00b9001d16a28f3aaab4d4b0696dc12c Mon Sep 17 00:00:00 2001 From: marwolaethblack Date: Mon, 27 Nov 2017 14:14:23 +0100 Subject: air quality index done --- web/js/realtime.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'web/js/realtime.js') diff --git a/web/js/realtime.js b/web/js/realtime.js index f0158a2..27cb25c 100644 --- a/web/js/realtime.js +++ b/web/js/realtime.js @@ -1,3 +1,14 @@ +function arrayMax(arr) { + var len = arr.length, max = -Infinity; + while (len--) { + if (arr[len] > max) { + max = arr[len]; + } + } + return max; +}; + + const table = { Co: { breakpoints: [0, 4.4, 4.5, 9.4, 9.5, 12.4, 12.5, 15.4, 15.5, 30.4, 30.5, 40.4, 40.5, 50.4], @@ -14,7 +25,7 @@ const table = { }; -var calculateAQI = function(gasName, concentration) { +function calculateAQI(gasName, concentration) { var bpLow,bpHi; var bpLowIndex, bpHiIndex; @@ -57,12 +68,16 @@ function update() { table[3].textContent = data.So; var indexes = []; - indexes.push(calculateAQI("Co", data.Co)); - indexes.push(calcaulteAQI("No", data.No)); - indexes.push(calculateAQI("So", data.So)); + var CO = isNaN(calculateAQI("Co", data.Co)) ? 0 : calculateAQI("Co", data.Co); + var SO = isNaN(calculateAQI("So", data.So)) ? 0 : calculateAQI("So", data.So); + var NO = isNaN(calculateAQI("No", data.No)) ? 0 : calculateAQI("No", data.No); + indexes.push(CO); + indexes.push(NO); + indexes.push(SO); + + var max = arrayMax(indexes); - var max = Math.max(...indexes); - console.log(max); + document.querySelector("#aq").innerHTML = `The current air quality index is ${max}`; }) .catch(function(error) { @@ -74,7 +89,7 @@ function update() { } -setInterval(update, 60000); +setInterval(update, 10000); -- cgit v1.2.3