diff --git a/.gitignore b/.gitignore index 3c3629e..ce339da 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ node_modules +logs/ +logger.lock +package-lock.json diff --git a/app.js b/app.js index f55d9fa..f28895b 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,15 @@ const globalVars = require('./libs/globalVars') const led = require('./libs/led') const lcd = require('./libs/lcd') const menu = require('./modules/mainMenu') +const logger = require('perfect-logger'); + +logger.initialize('houseStatus', { + logLevelFile: 0, // Log level for file + logLevelConsole: 0, // Log level for STDOUT/STDERR + logDirectory: 'logs/', // Log directory + // customBannerHeaders: 'This is a custom banner' // Custom Log Banner +}); + async function main() { lcd.clearScreen() @@ -16,12 +25,12 @@ async function main() { functionSwitch.watch(async (err, value) => { if (err) { - console.log('Error', err); + logger.info('Error', err); } if (value === 1) { switch (globalVars.currentPage) { case 'water': - console.log('Water Switch') + logger.info('Water Switch') if (globalVars.waterOn === 'true') { globalVars.lastWaterRequest = "" await common.request(0) @@ -50,12 +59,12 @@ async function main() { } }) - const arr = ['water', 'heating', 'pihole', 'mainMenu'] + const arr = ['hassOcto', 'water', 'heating', 'pihole', 'capitalXtra', 'mainMenu'] arrLength = arr.length let i = 0 appSwitch.watch(async (err, value) => { if (err) { - console.log('Error', err); + logger.info('Error', err); } if (value === 1) { switch (arr[i]) { @@ -78,10 +87,20 @@ async function main() { led.set('off') await menu.piHole() break; + case 'capitalXtra': + globalVars.currentPage = arr[i] + led.set('off') + await menu.capitalXtra() + break; + case 'hassOcto': + globalVars.currentPage = arr[i] + led.set('off') + await menu.hassOcto() + break; default: break; } - console.log(arr[i]) + logger.info(arr[i]) i = i + 1 if (i === arrLength) { i = 0 diff --git a/libs/clock.js b/libs/clock.js index 9384074..408286a 100644 --- a/libs/clock.js +++ b/libs/clock.js @@ -1,3 +1,5 @@ +const logger = require('perfect-logger'); + module.exports = { time: () => { try { @@ -6,7 +8,7 @@ module.exports = { ("0" + time.getMinutes()).slice(-2) return currentTime } catch (error) { - console.log(error) + logger.info(error) } } } diff --git a/libs/common.js b/libs/common.js index 4433487..e8b405e 100644 --- a/libs/common.js +++ b/libs/common.js @@ -1,7 +1,7 @@ const got = require('got'); const moment = require('moment'); const globalVars = require('./globalVars') - +const logger = require('perfect-logger'); module.exports = { request: async (input) => { @@ -49,6 +49,8 @@ module.exports = { await clearInterval(globalVars.heatingPolling) await clearInterval(globalVars.piHolePolling) await clearInterval(globalVars.weatherPolling) + await clearInterval(globalVars.capitalXtraPolling) + await clearInterval(globalVars.hassOctoPolling) }, clearGlobalVars: async () => { globalVars.lastHeatingRequest = "" diff --git a/libs/globalVars.js b/libs/globalVars.js index 107aaca..dc444d0 100644 --- a/libs/globalVars.js +++ b/libs/globalVars.js @@ -8,10 +8,12 @@ class globalVariables { heatingPageSwitch = ""; heatingPageS = 1; piHolePolling = ""; - lastWeatherRequest = "" - lastWaterRequest = "" - lastHeatingRequest = "" - weatherPolling = "" + lastWeatherRequest = ""; + lastWaterRequest = ""; + lastHeatingRequest = ""; + weatherPolling = ""; + capitalXtraPolling = ""; + hassOctoPolling = ""; } module.exports = new globalVariables(); \ No newline at end of file diff --git a/libs/lcd.js b/libs/lcd.js index 21228da..a3ae73b 100644 --- a/libs/lcd.js +++ b/libs/lcd.js @@ -3,6 +3,8 @@ const common = require('./common') const globalVars = require('./globalVars') const clock = require('./clock'); var commaNumber = require('comma-number') +const logger = require('perfect-logger'); + const lcd = new LCD(1, 0x27, 20, 4); @@ -31,7 +33,7 @@ module.exports = { }, time: async () => { let t = new Date().toUTCString() - console.log(t + ' get time') + logger.info('get time') lcd.setCursorSync(15, 0); lcd.printSync(await clock.time()) }, @@ -94,6 +96,8 @@ module.exports = { lcd.printLineSync(3, 'Blocked=' + commaNumber(queriesBlockedToday) + ' (' + percentageBlocked.toFixed() + '%)') }, weather: async (city, weatherDescription, currentTemp, highTemp, lowTemp) => { + module.exports.clearLine(3) + module.exports.clearLine(4) weatherDescription = weatherDescription.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase()); if (currentTemp.toString().length == 2) { lcd.printLineSync(1, ' ' + currentTemp + '\xDF') @@ -103,5 +107,14 @@ module.exports = { lcd.printLineSync(2, weatherDescription) lcd.printLineSync(3, 'High-' + highTemp + '\xDF' + ' / ' + 'Low-' + lowTemp + '\xDF') - } + }, + capitalXtra: async (capitalXtraData) => { + lcd.printLineSync(2, capitalXtraData.currentlyPlaying.artist) + lcd.printLineSync(3, capitalXtraData.currentlyPlaying.song) + }, + hassOcto: async (octoData) => { + lcd.printLineSync(1, 'State=' + octoData.currentState) + lcd.printLineSync(2, 'Progress=' + octoData.jobPercentage) + lcd.printLineSync(3, 'Remaining=' + octoData.timeRemaining) + }, } \ No newline at end of file diff --git a/modules/capitalXtra.js b/modules/capitalXtra.js new file mode 100644 index 0000000..6f93c38 --- /dev/null +++ b/modules/capitalXtra.js @@ -0,0 +1,51 @@ +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'); +const https = require('https') + +const wrapKey = 'UBAJvtJksjWnJDQh3KUySmPjDUaZOCLO' + +module.exports = { + + getStatus: () => { + var rest_options = { + host: 'wrapapi.com', + port: 443, + path: '/use/karl0ss/statusscreen/capitalXtraReloadedPlaying/0.0.1?wrapAPIKey=' + wrapKey, + method: 'GET' + }; + logger.info('capital request') + var request = https.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 capitalXtraData = { + "currentlyPlaying": { + "artist": data.data.nowPlaying[0].artist, + "song": data.data.nowPlaying[0].song + }, + "lastplayed": data.data.LastPlayed, + } + lcd.capitalXtra(capitalXtraData) + }); + }) + // Report errors + request.on('error', function (error) { + console.log(error) + led.set('green') + lcd.heatingStatus('Error No Data') + globalVars.heatingOn = 'error' + }); + request.end(); + } +} \ No newline at end of file diff --git a/modules/hassOcto.js b/modules/hassOcto.js new file mode 100644 index 0000000..7e7ef39 --- /dev/null +++ b/modules/hassOcto.js @@ -0,0 +1,28 @@ +const HomeAssistant = require('homeassistant'); + +const hass = new HomeAssistant({ + host: 'http://192.168.4.5', + port: 8123, + token: process.env.homeAssistantKey, + ignoreCert: false +}); + +const lcd = require('../libs/lcd') +const logger = require('perfect-logger'); + +module.exports = { + + getStatus: async () => { + logger.info('Printer Request request') + let jobPercentage = await hass.states.get('sensor', 'octoprint_job_percentage') + let timeRemaining = await hass.states.get('sensor', 'octoprint_time_remaining') + let currentState = await hass.states.get('sensor', 'octoprint_current_state') + + let octoData = { + "currentState": currentState.state, + "jobPercentage": jobPercentage.state + jobPercentage.attributes.unit_of_measurement, + "timeRemaining": new Date(timeRemaining.state * 1000).toISOString().substr(11, 8), + } + lcd.hassOcto(octoData) + } +} \ No newline at end of file diff --git a/modules/heating.js b/modules/heating.js index 6139003..5869da9 100644 --- a/modules/heating.js +++ b/modules/heating.js @@ -3,6 +3,7 @@ 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 = { @@ -13,8 +14,7 @@ module.exports = { path: '/heating/status', method: 'GET' }; - let t = new Date().toUTCString() - console.log(t + ' heating request') + logger.info('heating request') request = http.request(rest_options, function (response) { var content = ""; diff --git a/modules/mainMenu.js b/modules/mainMenu.js index cdef3eb..0447f70 100644 --- a/modules/mainMenu.js +++ b/modules/mainMenu.js @@ -21,6 +21,7 @@ module.exports = { globalVars.weatherPolling = setInterval(() => { modules.weather.getStatus() }, 1800000); + // 1800000 }, water: async () => { common.clearPolling() @@ -55,4 +56,26 @@ module.exports = { modules.pihole.getStatus() }, 30000); }, + capitalXtra: async () => { + common.clearPolling() + common.clearGlobalVars() + await screen.clearScreen() + await screen.heading('Capital Xtra') + await screen.time() + await modules.capitalXtra.getStatus() + globalVars.capitalXtraPolling = setInterval(() => { + modules.capitalXtra.getStatus() + }, 180000); + }, + hassOcto: async () => { + common.clearPolling() + common.clearGlobalVars() + await screen.clearScreen() + await screen.heading('3d Printer') + await screen.time() + await modules.hassOcto.getStatus() + globalVars.hassOctoPolling = setInterval(() => { + modules.hassOcto.getStatus() + }, 30000); + } } \ No newline at end of file diff --git a/modules/pihole.js b/modules/pihole.js index 5aaf17a..6955b86 100644 --- a/modules/pihole.js +++ b/modules/pihole.js @@ -3,6 +3,8 @@ 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 = { @@ -13,8 +15,7 @@ module.exports = { path: '/admin/api.php', method: 'GET' }; - let t = new Date().toUTCString() - console.log(t + ' piHole request') + logger.info('piHole request') var request = http.request(rest_options, function (response) { var content = ""; @@ -26,12 +27,14 @@ module.exports = { // Once we're done streaming the response, parse it as json. response.on('end', async function () { var data = JSON.parse(content); - let domainsBeingBlocked = data.domains_being_blocked - let queriesToday = data.dns_queries_today - let queriesBlockedToday = data.ads_blocked_today - let percentageBlocked = data.ads_percentage_today + let piHoleData = { + "domainsBeingBlocked": data.domains_being_blocked, + "queriesToday": data.dns_queries_today, + "queriesBlockedToday": data.ads_blocked_today, + "percentageBlocked": data.ads_percentage_today + } - lcd.piHolePage(domainsBeingBlocked, queriesToday, queriesBlockedToday, percentageBlocked) + lcd.piHolePage(piHoleData.domainsBeingBlocked, piHoleData.queriesToday, piHoleData.queriesBlockedToday, piHoleData.percentageBlocked) }); }) diff --git a/modules/water.js b/modules/water.js index f41dcda..4dd20b0 100644 --- a/modules/water.js +++ b/modules/water.js @@ -3,6 +3,8 @@ 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 = { @@ -13,8 +15,7 @@ module.exports = { path: '/water/status', method: 'GET' }; - let t = new Date().toUTCString() - console.log(t + ' water request') + logger.info('water request') var request = http.request(rest_options, function (response) { var content = ""; @@ -58,11 +59,11 @@ module.exports = { } if (globalVars.lastWaterRequest === "") { - console.log('initial load') + logger.info('initial load') globalVars.lastWaterRequest = waterdata updateWater() - } else if (globalVars.lastWaterRequest = waterdata) { - console.log('no update') + } else if (JSON.stringify(globalVars.lastWaterRequest) == JSON.stringify(waterdata)) { + logger.info('no update') } else { updateWater() } diff --git a/modules/weather.js b/modules/weather.js index ce9ded7..e397fc0 100644 --- a/modules/weather.js +++ b/modules/weather.js @@ -3,6 +3,8 @@ 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'); + const city = "Basingstoke" const appid = "ba24c6018ddd72041749018d0c1b1ef8" @@ -16,8 +18,7 @@ module.exports = { path: '/data/2.5/weather?q=' + city + '&appid=' + appid + '&units=metric', method: 'GET' }; - let t = new Date().toUTCString() - console.log(t + ' weather request') + logger.info('weather request') var request = http.request(rest_options, function (response) { var content = ""; @@ -37,16 +38,16 @@ module.exports = { "lowTemp": Math.floor(data.main.temp_min) } async function updateWeather() { - console.log('weather updated') + logger.info('weather updated') globalVars.lastWeatherRequest = weatherData await lcd.weather(weatherData.city, weatherData.weatherDescription, weatherData.currentTemp, weatherData.highTemp, weatherData.lowTemp) } if (globalVars.lastWeatherRequest === "") { - console.log('initial load') + logger.info('initial load') globalVars.lastWeatherRequest = weatherData updateWeather() - } else if (globalVars.lastWeatherRequest = weatherData) { - console.log('no update') + } else if (JSON.stringify(globalVars.lastWeatherRequest) == JSON.stringify(weatherData)) { + logger.info('no update') } else { updateWeather() } diff --git a/package-lock.json b/package-lock.json index 70a5b7a..51e4889 100644 --- a/package-lock.json +++ b/package-lock.json @@ -679,6 +679,11 @@ "better-assert": "~1.0.0" } }, + "perfect-logger": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/perfect-logger/-/perfect-logger-2.0.1.tgz", + "integrity": "sha512-MGjZ4KcKFJ0w2LOvO2kSILZMU2KUOESaIJnI4sJ6qAumEmrRHiJVPx088WgAYnZMnys6gFI2ZP2YbNTL308xkA==" + }, "performance-now": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", diff --git a/package.json b/package.json index 44433f7..44e4722 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "test_1", + "name": "house_status", "version": "1.0.0", "description": "", "main": "index.js", @@ -16,6 +16,7 @@ "http": "0.0.1-security", "moment": "^2.28.0", "onoff": "^6.0.0", + "perfect-logger": "^2.0.1", "pigpio": "^3.2.3", "raspberrypi-liquid-crystal": "^1.15.0", "readline": "^1.3.0", diff --git a/samples/lcd.js b/samples/lcd.js index b8f119d..d5b49a2 100644 --- a/samples/lcd.js +++ b/samples/lcd.js @@ -2,10 +2,43 @@ const LCD = require('raspberrypi-liquid-crystal'); const lcd = new LCD(1, 0x27, 20, 4); -lcd.beginSync(); +lcd.beginSync(); -lcd.clearSync(); -lcd.printLineSync(0, 'Welcome to KWorld'); -lcd.printLineSync(1, ' then'); -lcd.printLineSync(2, 'This is line 3'); -lcd.printLineSync(3, 'This is line 4'); \ No newline at end of file +async function main(input) { + function sleep(millis) { + return new Promise(resolve => setTimeout(resolve, millis)); + } + + lcd.clearSync(); + lcd.printLineSync(0, input) + await sleep(500); + lcd.scrollDisplayRightSync() + await sleep(500); + lcd.scrollDisplayRightSync() + await sleep(500); + lcd.scrollDisplayRightSync() + await sleep(500); + lcd.scrollDisplayRightSync() + await sleep(500); + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.scrollDisplayRightSync() + lcd.printLine(0, ' ') +} + +async function runner() { + await main('karl') + await main('karl2') +} + +runner() +// lcd.scrollDisplayLeftSync(3) \ No newline at end of file diff --git a/samples/rgb.html b/samples/rgb.html new file mode 100644 index 0000000..0342138 --- /dev/null +++ b/samples/rgb.html @@ -0,0 +1,99 @@ + + + + + +
+ +Or pick a color:
+