diff options
author | Marcin Zelent <zelent.marcin@gmail.com> | 2018-03-16 17:48:44 +0100 |
---|---|---|
committer | Marcin Zelent <zelent.marcin@gmail.com> | 2018-03-16 17:48:44 +0100 |
commit | ed01011dfb563e7d8ab13b6a0718eed7bf857880 (patch) | |
tree | 104a22a75d287d0607668eea3177eec626ab3f80 /pollutometer-php/web/js | |
parent | 1d24e941dc7d9da10c9045203ae18f5234c774f5 (diff) |
Moved PHP website to separate directory.
Diffstat (limited to 'pollutometer-php/web/js')
-rw-r--r-- | pollutometer-php/web/js/AllDataChart.js | 63 | ||||
-rw-r--r-- | pollutometer-php/web/js/LastWeekDataChart.js | 63 | ||||
-rw-r--r-- | pollutometer-php/web/js/realtime.js | 110 |
3 files changed, 236 insertions, 0 deletions
diff --git a/pollutometer-php/web/js/AllDataChart.js b/pollutometer-php/web/js/AllDataChart.js new file mode 100644 index 0000000..6525382 --- /dev/null +++ b/pollutometer-php/web/js/AllDataChart.js @@ -0,0 +1,63 @@ +var gasReading = { + Co: [], + No: [], + So: [] +}; + +var canvas = document.querySelector('#ctx'); +var ctx = canvas.getContext('2d'); + +var datasets = [{ + label: "Co", + borderColor: "rgb(5, 0, 0)", + fill: false, + data: [] +}, { + label: "No", + borderColor: "rgb(69, 169, 230)", + fill: false, + data: [] +}, { + label: "So", + borderColor: "rgb(246, 250, 15)", + fill: false, + data: [] +}]; + +fetch('/AllDataAverage') + .then(function(response) { + return response.json(); + }) + .then(function(data) { + drawChart(data, datasets); + }); + +function drawChart(gasData, datasets) { + + var finishedData = { + labels: [] + }; + + Object.keys(gasData).forEach(function(key) { + finishedData.labels.push(key); + datasets[0].data.push(gasData[key].Co); + datasets[1].data.push(gasData[key].No); + datasets[2].data.push(gasData[key].So); + }); + + finishedData.datasets = datasets; + + var myLineChart = new Chart(ctx, { + type: 'line', + data: finishedData, + options: { + scales: { + xAxes: [{ + time: { + unit: 'day' + } + }] + } + } + }); +} diff --git a/pollutometer-php/web/js/LastWeekDataChart.js b/pollutometer-php/web/js/LastWeekDataChart.js new file mode 100644 index 0000000..b5e3976 --- /dev/null +++ b/pollutometer-php/web/js/LastWeekDataChart.js @@ -0,0 +1,63 @@ +var gasReading = { + Co: [], + No: [], + So: [] +}; + +var canvas = document.querySelector('#ctx'); +var ctx = canvas.getContext('2d'); + +var datasets = [{ + label: "Co", + borderColor: "rgb(5, 0, 0)", + fill: false, + data: [] +}, { + label: "No", + borderColor: "rgb(69, 169, 230)", + fill: false, + data: [] +}, { + label: "So", + borderColor: "rgb(246, 250, 15)", + fill: false, + data: [] +}]; + +fetch('/LastWeekDataAverage') + .then(function(response) { + return response.json(); + }) + .then(function(data) { + drawChart(data, datasets); + }); + +function drawChart(gasData, datasets) { + + var finishedData = { + labels: [] + }; + + Object.keys(gasData).forEach(function(key) { + finishedData.labels.push(key); + datasets[0].data.push(gasData[key].Co); + datasets[1].data.push(gasData[key].No); + datasets[2].data.push(gasData[key].So); + }); + + finishedData.datasets = datasets; + + var myLineChart = new Chart(ctx, { + type: 'line', + data: finishedData, + options: { + scales: { + xAxes: [{ + time: { + unit: 'day' + } + }] + } + } + }); +} diff --git a/pollutometer-php/web/js/realtime.js b/pollutometer-php/web/js/realtime.js new file mode 100644 index 0000000..1f5ef22 --- /dev/null +++ b/pollutometer-php/web/js/realtime.js @@ -0,0 +1,110 @@ +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; + + fetch('/latest') + .then(function(resp) { + return resp.json(); + }) + .then(function(response) { + data = response; + var table = document.querySelector('#latest').children; + table[0].textContent = data.TimeStamp; + table[1].textContent = data.Co; + 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); + }); + + + + +} + +function drawChart() { + +} + +setInterval(update, 10000); + + + + + + + + + + + + + |