From ba3e106a1bb78469de1070622c7bc0974df17c57 Mon Sep 17 00:00:00 2001 From: Karl Hudgell Date: Mon, 21 Sep 2020 21:38:46 +0100 Subject: [PATCH] reworked and working heating --- app.js | 24 ++++++---- libs/common.js | 36 +++++++++------ libs/globalVars.js | 4 +- libs/lcd.js | 21 ++++++--- modules/heating.js | 77 +++++++++++++++++++++++++++++++ modules/mainMenu.js | 26 ++++++++--- modules/{hotwater.js => water.js} | 33 ++++++------- package-lock.json | 15 ++++++ package.json | 6 ++- t.js | 5 ++ 10 files changed, 191 insertions(+), 56 deletions(-) create mode 100644 modules/heating.js rename modules/{hotwater.js => water.js} (68%) create mode 100644 t.js diff --git a/app.js b/app.js index bd68fcc..9b67bbb 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,5 @@ const { Gpio } = require('onoff'); -const hotwater = require('./modules/hotwater') +const hotwater = require('./modules/water') const common = require('./libs/common') const globalVars = require('./libs/globalVars') const led = require('./libs/led') @@ -9,8 +9,8 @@ const menu = require('./modules/mainMenu') async function main() { lcd.clearScreen() await lcd.intro() - menu.home() - + menu.water() + const heatingSwitch = new Gpio('17', 'in', 'both'); const appSwitch = new Gpio('23', 'in', 'both'); @@ -27,7 +27,7 @@ async function main() { } }) - const arr = ['hotwater', 'main-menu'] + const arr = ['water', 'heating', 'mainMenu'] arrLength = arr.length let i = 0 appSwitch.watch(async (err, value) => { @@ -36,14 +36,18 @@ async function main() { } if (value === 1) { switch (arr[i]) { - case 'hotwater': - menu.hotWater() + case 'water': + await menu.water() break; - case 'main-menu': - globalVars.waterPolling = 0 - setInterval(hotwater.getStatus, globalVars.waterPolling) + case 'mainMenu': + clearInterval(globalVars.heatingPolling) led.set('off') - menu.home() + await menu.home() + break; + case 'heating': + clearInterval(globalVars.waterPolling) + led.set('off') + await menu.heating() break; default: break; diff --git a/libs/common.js b/libs/common.js index 19d43dd..b86b26a 100644 --- a/libs/common.js +++ b/libs/common.js @@ -1,4 +1,5 @@ const got = require('got'); +const moment = require('moment'); module.exports = { request: async (input) => { @@ -19,21 +20,26 @@ module.exports = { await new Promise(resolve => setTimeout(resolve, time)); }, nextEvent: async (time) => { - - time = time.match(/.{11}$/gm) - time = time[0].toLowerCase() - - var hours = parseInt(time.substr(0, 2)); - if (time.indexOf('am') != -1 && hours == 12) { - time = time.replace('12', '0'); + if (time === 'Thursday, January 1st, 1970 12:13:00 AM') { + return 'No Timer Set' + } else { + time = time.match(/.{11}$/gm) + time = time[0].toLowerCase() + var hours = parseInt(time.substr(0, 2)); + if (time.indexOf('am') != -1 && hours == 12) { + time = time.replace('12', '0'); + } + if (time.indexOf('pm') != -1 && hours < 12) { + time = time.replace(hours, (hours + 12)); + } + time = time.replace(/(am|pm)/, ''); + time = time.replace(/ /g, '') + return time.substring(0, time.length - 3); } - if (time.indexOf('pm') != -1 && hours < 12) { - time = time.replace(hours, (hours + 12)); - } - - - time = time.replace(/(am|pm)/, ''); - time = time.replace(/ /g,'') - return time.substring(0, time.length - 3); + }, + time: async (nextEvent) => { + let currentDate = moment(new Date(new Date().toUTCString())) + nextEvent = moment(nextEvent, "dddd, MMMM Do, YYYY LTS") + return currentDate.isAfter(nextEvent); } } \ No newline at end of file diff --git a/libs/globalVars.js b/libs/globalVars.js index 30234a8..fb6339b 100644 --- a/libs/globalVars.js +++ b/libs/globalVars.js @@ -1,7 +1,9 @@ class globalVariables { waterOn = ""; waterNextEvent = ""; - waterPolling = 0 + waterPolling = ""; + heatingNextEvent = ""; + heatingPolling = ""; } module.exports = new globalVariables(); \ No newline at end of file diff --git a/libs/lcd.js b/libs/lcd.js index 520adb1..d47c755 100644 --- a/libs/lcd.js +++ b/libs/lcd.js @@ -19,20 +19,19 @@ module.exports = { intro: async () => { lcd.clearSync(); lcd.printLineSync(1, 'MiGenie Status') - lcd.printLineSync(2, ' By Karl') + lcd.setCursorSync(13, 2); + lcd.printSync('By Karl') await common.pause(3000) lcd.clearSync(); }, heading: async (title) => { lcd.setCursorSync(0, 0); lcd.printSync(title) - module.exports.time() }, time: async () => { + console.log('get time') lcd.setCursorSync(15, 0); - let f - f = await clock.time() - lcd.printSync(f) + lcd.printSync(await clock.time()) }, waterStatus: async (input) => { lcd.printLineSync(2, 'Water=') @@ -41,7 +40,14 @@ module.exports = { lcd.setCursorSync(6, 2); lcd.printSync(input) }, - waterNextEvent: async (input) => { + waterStatus: async (input) => { + lcd.printLineSync(2, 'Heating=') + lcd.setCursorSync(8, 2); + lcd.printSync(' ') + lcd.setCursorSync(8, 2); + lcd.printSync(input) + }, + NextEvent: async (input) => { if (globalVars.waterOn === "true") { lcd.printLineSync(3, 'Switch Off=') lcd.setCursorSync(11, 3); @@ -56,4 +62,7 @@ module.exports = { lcd.printSync(input) } }, + degrees: async () => { + let icon = '°' + } } \ No newline at end of file diff --git a/modules/heating.js b/modules/heating.js new file mode 100644 index 0000000..63c27f5 --- /dev/null +++ b/modules/heating.js @@ -0,0 +1,77 @@ +const http = require('http') +const globalVars = require('../libs/globalVars') +const led = require('../libs/led') +const common = require('../libs/common') +const lcd = require('../libs/lcd') + +module.exports = { + + getStatus: () => { + var rest_options = { + host: '192.168.4.5', + port: 2020, + path: '/heating/status', + method: 'GET' + }; + console.log('heating request') + 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', async function () { + var data = JSON.parse(content); + let heatingOn = data.heatingOn + let currentRoomTemp = data.measuredRoomTemp + let currentSetPoint = data.currentSetpoint + + var time = await common.nextEvent(data.nextScheduleEventUtcTime) + + globalVars.waterNextEvent = time + + let m = await common.time(data.nextScheduleEventUtcTime) + + if (globalVars.heatingOn === "" || globalVars.heatingOn === "error") { + if (heatingOn === 'true') { + globalVars.heatingOn = heatingOn + lcd.heatingStatus('On') + lcd.NextEvent(globalVars.heatingNextEvent) + led.set('red') + } else { + globalVars.waterOn = waterOn + lcd.heatingStatus('Off') + led.set('blue') + lcd.NextEvent(globalVars.heatingNextEvent) + } + } else { + // if (waterOn != globalVars.waterOn) { + if (heatingOn === 'true') { + globalVars.heatingOn = heatingOn + lcd.heatingStatus('On') + led.set('red') + lcd.NextEvent(globalVars.heatingNextEvent) + } else { + globalVars.heatingOn = heatingOn + lcd.heatingStatus('Off') + led.set('blue') + lcd.NextEvent(globalVars.waterNextEvent) + } + // } + } + }); + }); + + // Report errors + request.on('error', function (error) { + led.set('green') + lcd.heatingStatus('Error No Data') + globalVars.heatingOn = 'error' + }); + + request.end(); + } +} \ No newline at end of file diff --git a/modules/mainMenu.js b/modules/mainMenu.js index 1546ac5..d561afd 100644 --- a/modules/mainMenu.js +++ b/modules/mainMenu.js @@ -2,25 +2,37 @@ const http = require('http') const globalVars = require('../libs/globalVars') const led = require('../libs/led') const common = require('../libs/common') -const modules = require('../modules/hotwater') - const screen = require('../libs/lcd') +var requireDir = require('require-dir'); +var modules = requireDir('../modules'); module.exports = { home: () => { clearInterval(globalVars.waterPolling); screen.clearScreen() screen.heading('Main Menu') + screen.time() + setInterval(screen.time, 55000) }, - hotWater: () => { - globalVars.waterPolling = 30000 + water: () => { screen.clearScreen() screen.heading('Hot Water') + screen.time() setInterval(screen.time, 55000) - modules.getStatus() + modules.water.getStatus() globalVars.waterPolling = setInterval(() => { - modules.getStatus() - }, 30000); + modules.water.getStatus() + }, 60000); + }, + heating: () => { + screen.clearScreen() + screen.heading('Heating') + screen.time() + setInterval(screen.time, 55000) + modules.heating.getStatus() + globalVars.heatingPolling = setInterval(() => { + modules.heating.getStatus() + }, 60000); } } \ No newline at end of file diff --git a/modules/hotwater.js b/modules/water.js similarity index 68% rename from modules/hotwater.js rename to modules/water.js index 619758b..b28a651 100644 --- a/modules/hotwater.js +++ b/modules/water.js @@ -2,7 +2,6 @@ const http = require('http') const globalVars = require('../libs/globalVars') const led = require('../libs/led') const common = require('../libs/common') - const lcd = require('../libs/lcd') module.exports = { @@ -14,7 +13,7 @@ module.exports = { path: '/water/status', method: 'GET' }; - + console.log('water request') var request = http.request(rest_options, function (response) { var content = ""; @@ -32,32 +31,34 @@ module.exports = { globalVars.waterNextEvent = time + let m = await common.time(data.nextScheduleEventUtcTime) + if (globalVars.waterOn === "" || globalVars.waterOn === "error") { if (waterOn === 'true') { globalVars.waterOn = waterOn lcd.waterStatus('Heating') - lcd.waterNextEvent(globalVars.waterNextEvent) + lcd.NextEvent(globalVars.waterNextEvent) led.set('red') } else { globalVars.waterOn = waterOn lcd.waterStatus('Not Heating') led.set('blue') - lcd.waterNextEvent(globalVars.waterNextEvent) + lcd.NextEvent(globalVars.waterNextEvent) } } else { - if (waterOn != globalVars.waterOn) { - if (waterOn === 'true') { - globalVars.waterOn = waterOn - lcd.waterStatus('Heating') - led.set('red') - lcd.waterNextEvent(globalVars.waterNextEvent) - } else { - globalVars.waterOn = waterOn - lcd.waterStatus('Not Heating') - led.set('blue') - lcd.waterNextEvent(globalVars.waterNextEvent) - } + // 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) } + // } } }); }); diff --git a/package-lock.json b/package-lock.json index 85039d0..dfa2bfb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -726,6 +726,11 @@ "sleep": "^6.1.0" } }, + "readline": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz", + "integrity": "sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw=" + }, "request": { "version": "2.88.2", "resolved": "https://registry.npmjs.org/request/-/request-2.88.2.tgz", @@ -753,6 +758,11 @@ "uuid": "^3.3.2" } }, + "require-dir": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/require-dir/-/require-dir-1.2.0.tgz", + "integrity": "sha512-LY85DTSu+heYgDqq/mK+7zFHWkttVNRXC9NKcKGyuGLdlsfbjEPrIEYdCVrx6hqnJb+xSu3Lzaoo8VnmOhhjNA==" + }, "require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -881,6 +891,11 @@ "tweetnacl": "~0.14.0" } }, + "time-loop": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/time-loop/-/time-loop-2.0.3.tgz", + "integrity": "sha1-AI0jivfcj/tyt9iO3C3fOK0cJpI=" + }, "to-array": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/to-array/-/to-array-0.1.4.tgz", diff --git a/package.json b/package.json index 182fb9b..5562529 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,14 @@ "got": "^11.6.2", "homeassistant": "^0.2.0", "http": "0.0.1-security", + "moment": "^2.28.0", "onoff": "^6.0.0", "pigpio": "^3.2.3", "raspberrypi-liquid-crystal": "^1.15.0", + "readline": "^1.3.0", + "require-dir": "^1.2.0", "require-directory": "^2.1.1", - "socket.io": "^2.3.0" + "socket.io": "^2.3.0", + "time-loop": "^2.0.3" } } diff --git a/t.js b/t.js new file mode 100644 index 0000000..fbf7cf2 --- /dev/null +++ b/t.js @@ -0,0 +1,5 @@ +// repeat with the interval of 2 seconds +let timerId = setInterval(() => console.log('tick'), 2000); + +// after 5 seconds stop +setTimeout(() => { clearInterval(timerId); console.log('stop'); }, 5000); \ No newline at end of file