65 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-09-14 12:11:37 +01:00
const http = require('http')
const globalVars = require('../libs/globalVars')
const led = require('../libs/led')
const lcd = require('../libs/lcd')
2020-09-14 12:27:18 +01:00
module.exports = {
2020-09-14 12:11:37 +01:00
2020-09-14 12:27:18 +01:00
getStatus: () => {
var rest_options = {
host: '192.168.4.5',
port: 2020,
path: '/water/status',
method: 'GET'
};
2020-09-14 12:11:37 +01:00
2020-09-14 12:27:18 +01:00
var request = http.request(rest_options, function (response) {
var content = "";
2020-09-14 12:11:37 +01:00
2020-09-14 12:27:18 +01:00
// Handle data chunks
response.on('data', function (chunk) {
content += chunk;
});
2020-09-14 12:11:37 +01:00
2020-09-14 12:27:18 +01:00
// Once we're done streaming the response, parse it as json.
response.on('end', function () {
var data = JSON.parse(content);
let waterOn = data.waterOn
if (globalVars.waterOn === "" || globalVars.waterOn === "error") {
2020-09-14 12:11:37 +01:00
if (waterOn === 'true') {
lcd.heatingStatus('Heating')
led.set('red')
globalVars.waterOn = waterOn
} else {
lcd.heatingStatus('Not Heating')
led.set('blue')
globalVars.waterOn = waterOn
}
2020-09-14 12:27:18 +01:00
} else {
if (waterOn != globalVars.waterOn) {
if (waterOn === 'true') {
lcd.heatingStatus('Heating')
led.set('red')
globalVars.waterOn = waterOn
} else {
lcd.heatingStatus('Not Heating')
led.set('blue')
globalVars.waterOn = waterOn
}
}
2020-09-14 12:11:37 +01:00
}
2020-09-14 12:27:18 +01:00
});
2020-09-14 12:11:37 +01:00
});
2020-09-14 12:27:18 +01:00
// Report errors
request.on('error', function (error) {
lcd.heatingStatus('Error')
led.set('green')
lcd.heatingStatus('Error No Data')
globalVars.waterOn = 'error'
});
2020-09-14 12:11:37 +01:00
2020-09-14 12:27:18 +01:00
request.end();
}
2020-09-14 12:11:37 +01:00
}