2020-09-14 12:11:37 +01:00
|
|
|
const http = require('http')
|
|
|
|
const globalVars = require('../libs/globalVars')
|
|
|
|
const led = require('../libs/led')
|
2020-09-14 13:47:23 +01:00
|
|
|
const common = require('../libs/common')
|
2020-09-14 12:11:37 +01:00
|
|
|
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-21 21:38:46 +01:00
|
|
|
console.log('water request')
|
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.
|
2020-09-14 18:42:09 +01:00
|
|
|
response.on('end', async function () {
|
2020-09-14 12:27:18 +01:00
|
|
|
var data = JSON.parse(content);
|
|
|
|
let waterOn = data.waterOn
|
2020-09-14 18:42:09 +01:00
|
|
|
|
2020-09-14 13:47:23 +01:00
|
|
|
var time = await common.nextEvent(data.nextScheduleEventUtcTime)
|
|
|
|
|
|
|
|
globalVars.waterNextEvent = time
|
|
|
|
|
2020-09-21 21:38:46 +01:00
|
|
|
let m = await common.time(data.nextScheduleEventUtcTime)
|
|
|
|
|
2020-09-14 12:27:18 +01:00
|
|
|
if (globalVars.waterOn === "" || globalVars.waterOn === "error") {
|
2020-09-14 12:11:37 +01:00
|
|
|
if (waterOn === 'true') {
|
2020-09-14 18:42:09 +01:00
|
|
|
globalVars.waterOn = waterOn
|
2020-09-14 13:47:23 +01:00
|
|
|
lcd.waterStatus('Heating')
|
2020-09-21 21:38:46 +01:00
|
|
|
lcd.NextEvent(globalVars.waterNextEvent)
|
2020-09-14 12:11:37 +01:00
|
|
|
led.set('red')
|
|
|
|
} else {
|
2020-09-14 18:42:09 +01:00
|
|
|
globalVars.waterOn = waterOn
|
2020-09-14 13:47:23 +01:00
|
|
|
lcd.waterStatus('Not Heating')
|
2020-09-14 12:11:37 +01:00
|
|
|
led.set('blue')
|
2020-09-21 21:38:46 +01:00
|
|
|
lcd.NextEvent(globalVars.waterNextEvent)
|
2020-09-14 12:11:37 +01:00
|
|
|
}
|
2020-09-14 12:27:18 +01:00
|
|
|
} else {
|
2020-09-21 21:38:46 +01:00
|
|
|
// if (waterOn != globalVars.waterOn) {
|
|
|
|
if (waterOn === 'true') {
|
|
|
|
globalVars.waterOn = waterOn
|
|
|
|
lcd.waterStatus('Heating')
|
|
|
|
led.set('red')
|
|
|
|
lcd.NextEvent(globalVars.waterNextEvent)
|
|
|
|
} else {
|
|
|
|
globalVars.waterOn = waterOn
|
|
|
|
lcd.waterStatus('Not Heating')
|
|
|
|
led.set('blue')
|
|
|
|
lcd.NextEvent(globalVars.waterNextEvent)
|
2020-09-14 12:27:18 +01:00
|
|
|
}
|
2020-09-21 21:38:46 +01:00
|
|
|
// }
|
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) {
|
2020-09-14 13:47:23 +01:00
|
|
|
lcd.waterStatus('Error')
|
2020-09-14 12:27:18 +01:00
|
|
|
led.set('green')
|
2020-09-14 13:47:23 +01:00
|
|
|
lcd.waterStatus('Error No Data')
|
2020-09-14 12:27:18 +01:00
|
|
|
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
|
|
|
}
|