diff options
author | marcinzelent <zelent.marcin@gmail.com> | 2017-11-30 11:34:01 +0100 |
---|---|---|
committer | marcinzelent <zelent.marcin@gmail.com> | 2017-11-30 11:34:01 +0100 |
commit | 2210e09dd3b70106a0a4ce2d963d28171a5ee579 (patch) | |
tree | 719a3681c95837a760ffb9438d0eb9e5c92886c7 /web | |
parent | 8a67529d0358af8b1c72f53c2ca6441c75be836d (diff) | |
parent | f7e9434a00b9001d16a28f3aaab4d4b0696dc12c (diff) |
Merge remote-tracking branch 'origin/master'
# Conflicts:
# .idea/workspace.xml
Diffstat (limited to 'web')
-rw-r--r-- | web/js/realtime.js | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/web/js/realtime.js b/web/js/realtime.js index 4526e5e..27cb25c 100644 --- a/web/js/realtime.js +++ b/web/js/realtime.js @@ -1,3 +1,56 @@ +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], + 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] + } +}; + + +function calculateAQI(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 +67,18 @@ function update() { table[2].textContent = data.No; table[3].textContent = data.So; + var indexes = []; + 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); + + document.querySelector("#aq").innerHTML = `The current air quality index is <strong>${max}</strong>`; + }) .catch(function(error) { console.log(error); @@ -24,4 +89,17 @@ function update() { } -setInterval(update, 60000); +setInterval(update, 10000); + + + + + + + + + + + + + |