72 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-09-14 12:11:37 +01:00
const http = require('http')
const { Gpio } = require('onoff');
const globalVars = require('../libs/globalVars')
const led = require('../libs/led')
const redLED = new Gpio('4', 'out');
const blueLED = new Gpio('27', 'out')
const greenLED = new Gpio('22', 'out')
const lcd = require('../libs/lcd')
setInterval(function () {
var rest_options = {
host: '192.168.4.5',
port: 2020,
path: '/water/status',
method: 'GET'
};
var request = http.request(rest_options, function (response) {
var content = "";
// Handle data chunks
response.on('data', function (chunk) {
content += chunk;
});
// 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") {
if (waterOn === 'true') {
lcd.heatingStatus('Heating')
led.set('red')
globalVars.waterOn = waterOn
} else {
lcd.heatingStatus('Not Heating')
led.set('blue')
globalVars.waterOn = waterOn
}
} 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
}
}
}
});
});
// Report errors
request.on('error', function (error) {
lcd.heatingStatus('Error')
led.set('green')
lcd.heatingStatus('Error No Data')
globalVars.waterOn = 'error'
});
request.end();
}, 5000);
module.exports = {
setInterval
}