From 664b85ca0b3ee6bd2ee2e1a28864540ac4bdcbaf Mon Sep 17 00:00:00 2001 From: marwolaethblack Date: Mon, 27 Nov 2017 13:53:08 +0100 Subject: air quality index calculations --- web/js/realtime.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'web/js/realtime.js') diff --git a/web/js/realtime.js b/web/js/realtime.js index 4526e5e..f0158a2 100644 --- a/web/js/realtime.js +++ b/web/js/realtime.js @@ -1,3 +1,45 @@ +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], + aq: [0, 50, 51, 100, 101, 150, 151, 200, 201, 300, 301, 400, 401, 500] + }, + So: { + breakpoints: [0.000, 0.034, 0.035, 0.144, 0.145, 0.224, 0.225, 0.304, 0.305, 0.604, 0.605, 0.804, 0.805, 1.004], + aq:[0, 50, 51, 100, 101, 150, 151, 200, 201, 300, 301, 400, 401, 500] + }, + No: { + breakpoints: [0.65, 1.24, 1.25, 1.64, 1.65, 2.04], + aq:[201, 300, 301, 400, 401, 500] + } +}; + + +var calculateAQI = function(gasName, concentration) { + var bpLow,bpHi; + var bpLowIndex, bpHiIndex; + + table[gasName].breakpoints.forEach(function(value, index) { + if(value < concentration && table[gasName].breakpoints[index + 1] > concentration) { + bpLow = value; + bpLowIndex = index; + } + + if(value > concentration && table[gasName].breakpoints[index - 1] < concentration) { + bpHi = value; + bpHiIndex = index; + } + + }); + + + + var airQualityIndex = ((table[gasName].aq[bpHiIndex] - table[gasName].aq[bpLowIndex]) / (bpHi - bpLow)) * (concentration - bpLow) + table[gasName].aq[bpLowIndex]; + + return airQualityIndex; + +} + + function update() { var data; @@ -14,6 +56,14 @@ function update() { table[2].textContent = data.No; 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 max = Math.max(...indexes); + console.log(max); + }) .catch(function(error) { console.log(error); @@ -25,3 +75,16 @@ function update() { } setInterval(update, 60000); + + + + + + + + + + + + + -- cgit v1.2.3 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