2020-10-10 13:56:55 +01:00

72 lines
2.7 KiB
JavaScript

const http = require('http')
const globalVars = require('../libs/globalVars')
const led = require('../libs/led')
const common = require('../libs/common')
const lcd = require('../libs/lcd')
const logger = require('perfect-logger');
module.exports = {
getStatus: (page) => {
var rest_options = {
host: '192.168.4.5',
port: 2020,
path: '/heating/status',
method: 'GET'
};
logger.info('heating request')
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', async function () {
var data = JSON.parse(content);
let heatingOn = data.heatingOn
var time = await common.nextEvent(data.nextScheduleEventUtcTime)
globalVars.heatingNextEvent = time
let m = await common.time(data.nextScheduleEventUtcTime)
if (globalVars.heatingOn === "" || globalVars.heatingOn === "error") {
if (heatingOn === 'true') {
globalVars.heatingOn = heatingOn
lcd.heatingPageOne('On', data.measuredRoomTemp, data.lastTimerSetPoint, globalVars.heatingNextEvent)
led.set('red')
} else {
globalVars.heatingOn = heatingOn
lcd.heatingPageOne('Off', data.measuredRoomTemp, data.lastTimerSetPoint, globalVars.heatingNextEvent)
led.set('blue')
}
} else {
if (heatingOn === 'true') {
globalVars.heatingOn = heatingOn
lcd.heatingPageOne('On', data.measuredRoomTemp, data.lastTimerSetPoint, globalVars.heatingNextEvent)
led.set('red')
} else {
if (page === 1) {
await lcd.heatingPageOne('Off', globalVars.heatingNextEvent)
} else {
await lcd.heatingPageTwo(data.measuredRoomTemp, data.lastTimerSetPoint)
}
globalVars.heatingOn = heatingOn
led.set('blue')
}
}
});
});
// Report errors
request.on('error', function (error) {
led.set('green')
lcd.heatingStatus('Error No Data')
globalVars.heatingOn = 'error'
});
request.end();
}
}