aboutsummaryrefslogtreecommitdiff
blob: f0158a20f42200079998517c2dec9c231143497a (plain)
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
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;

    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 = [];
            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);
        });




}

setInterval(update, 60000);