forked from VickyKoblinski/radio_thermostat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadioThermostat.js
64 lines (55 loc) · 1.24 KB
/
RadioThermostat.js
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
const vars = require('./private/vars');
const axios = require('axios');
let data = {};
let programHeat = {};
let programCool = {};
//* * THIS WORKS TOO */
// async function fetchData() {
// const res = await axios.get(`${vars.address}/tstat/`);
// return res.data;
// }
function fetchData() {
return axios.get(`${vars.address}/tstat/`).then(res => res.data);
}
async function fetchProgram(url) {
const program = await axios.get(url).then(res => res.data);
const formattedResult = {
mon: program['0'],
tue: program['1'],
wed: program['2'],
thur: program['3'],
fri: program['4'],
sat: program['5'],
sun: program['6']
};
return formattedResult;
}
function fetchProgramHeat() {
return fetchProgram(`${vars.address}/tstat/program/heat`);
}
function fetchProgramCool() {
return fetchProgram(`${vars.address}/tstat/program/cool`);
}
(async () => {
programCool = await fetchProgramCool();
programHeat = await fetchProgramHeat();
})();
(() => {
setInterval(async () => {
data = await fetchData();
}, 5000);
})();
module.exports = {
getData() {
return data;
},
setData(newData) {
data = newData;
},
getProgramHeat() {
return programHeat;
},
getProgramCool() {
return programCool;
}
};