50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
const http = require('http')
|
|
const { Gpio } = require('onoff');
|
|
const globalVars = require('./libs/globalVars')
|
|
|
|
const redLED = new Gpio('4', 'out');
|
|
const blueLED = new Gpio('27', 'out')
|
|
|
|
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
|
|
globalVars.waterOn = waterOn
|
|
if (waterOn === 'true') {
|
|
redLED.writeSync(1)
|
|
blueLED.writeSync(0)
|
|
} else {
|
|
blueLED.writeSync(1)
|
|
redLED.writeSync(0)
|
|
}
|
|
});
|
|
});
|
|
|
|
// Report errors
|
|
request.on('error', function (error) {
|
|
console.log("Error while calling endpoint.", error);
|
|
});
|
|
|
|
request.end();
|
|
}, 5000);
|
|
|
|
module.exports = {
|
|
setInterval
|
|
} |