72 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2020-09-21 21:38:46 +01:00
const http = require('http')
const globalVars = require('../libs/globalVars')
const led = require('../libs/led')
const common = require('../libs/common')
const lcd = require('../libs/lcd')
2020-10-10 13:56:55 +01:00
const logger = require('perfect-logger');
2020-09-21 21:38:46 +01:00
module.exports = {
2020-09-28 10:50:23 +01:00
getStatus: (page) => {
2020-09-21 21:38:46 +01:00
var rest_options = {
host: '192.168.4.5',
port: 2020,
path: '/heating/status',
method: 'GET'
};
2020-10-10 13:56:55 +01:00
logger.info('heating request')
2020-09-28 10:50:23 +01:00
request = http.request(rest_options, function (response) {
2020-09-21 21:38:46 +01:00
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)
2020-09-28 10:50:23 +01:00
globalVars.heatingNextEvent = time
2020-09-21 21:38:46 +01:00
let m = await common.time(data.nextScheduleEventUtcTime)
if (globalVars.heatingOn === "" || globalVars.heatingOn === "error") {
if (heatingOn === 'true') {
globalVars.heatingOn = heatingOn
2020-09-28 10:50:23 +01:00
lcd.heatingPageOne('On', data.measuredRoomTemp, data.lastTimerSetPoint, globalVars.heatingNextEvent)
2020-09-21 21:38:46 +01:00
led.set('red')
} else {
2020-09-21 21:53:55 +01:00
globalVars.heatingOn = heatingOn
2020-09-28 10:50:23 +01:00
lcd.heatingPageOne('Off', data.measuredRoomTemp, data.lastTimerSetPoint, globalVars.heatingNextEvent)
2020-09-21 21:53:55 +01:00
led.set('blue')
2020-09-21 21:38:46 +01:00
}
} else {
if (heatingOn === 'true') {
globalVars.heatingOn = heatingOn
2020-09-28 10:50:23 +01:00
lcd.heatingPageOne('On', data.measuredRoomTemp, data.lastTimerSetPoint, globalVars.heatingNextEvent)
2020-09-21 21:53:55 +01:00
led.set('red')
2020-09-21 21:38:46 +01:00
} else {
2020-09-28 10:50:23 +01:00
if (page === 1) {
await lcd.heatingPageOne('Off', globalVars.heatingNextEvent)
} else {
await lcd.heatingPageTwo(data.measuredRoomTemp, data.lastTimerSetPoint)
}
2020-09-21 21:38:46 +01:00
globalVars.heatingOn = heatingOn
2020-09-21 21:53:55 +01:00
led.set('blue')
2020-09-21 21:38:46 +01:00
}
}
});
});
// Report errors
request.on('error', function (error) {
led.set('green')
lcd.heatingStatus('Error No Data')
globalVars.heatingOn = 'error'
});
request.end();
}
}