update weather every 30mins

This commit is contained in:
Karl Hudgell 2020-09-29 10:23:41 +01:00
parent c94bacb71e
commit 3871bf76db
6 changed files with 87 additions and 58 deletions

14
app.js
View File

@ -23,10 +23,12 @@ async function main() {
case 'water': case 'water':
console.log('Water Switch') console.log('Water Switch')
if (globalVars.waterOn === 'true') { if (globalVars.waterOn === 'true') {
globalVars.lastWaterRequest = ""
await common.request(0) await common.request(0)
await common.pause(2000) await common.pause(2000)
await menu.water() await menu.water()
} else { } else {
globalVars.lastWaterRequest = ""
await common.request(1) await common.request(1)
await common.pause(2000) await common.pause(2000)
await menu.water() await menu.water()
@ -59,32 +61,20 @@ async function main() {
switch (arr[i]) { switch (arr[i]) {
case 'water': case 'water':
globalVars.currentPage = arr[i] globalVars.currentPage = arr[i]
clearInterval(globalVars.waterPolling)
clearInterval(globalVars.heatingPageSwitch)
clearInterval(globalVars.heatingPolling)
await menu.water() await menu.water()
break; break;
case 'mainMenu': case 'mainMenu':
globalVars.currentPage = arr[i] globalVars.currentPage = arr[i]
clearInterval(globalVars.waterPolling)
clearInterval(globalVars.heatingPageSwitch)
clearInterval(globalVars.heatingPolling)
led.set('off') led.set('off')
await menu.home() await menu.home()
break; break;
case 'heating': case 'heating':
globalVars.currentPage = arr[i] globalVars.currentPage = arr[i]
clearInterval(globalVars.waterPolling)
clearInterval(globalVars.heatingPageSwitch)
clearInterval(globalVars.heatingPolling)
led.set('off') led.set('off')
await menu.heating() await menu.heating()
break; break;
case 'pihole': case 'pihole':
globalVars.currentPage = arr[i] globalVars.currentPage = arr[i]
clearInterval(globalVars.waterPolling)
clearInterval(globalVars.heatingPageSwitch)
clearInterval(globalVars.heatingPolling)
led.set('off') led.set('off')
await menu.piHole() await menu.piHole()
break; break;

View File

@ -1,5 +1,7 @@
const got = require('got'); const got = require('got');
const moment = require('moment'); const moment = require('moment');
const globalVars = require('./globalVars')
module.exports = { module.exports = {
request: async (input) => { request: async (input) => {
@ -41,5 +43,16 @@ module.exports = {
let currentDate = moment(new Date(new Date().toUTCString())) let currentDate = moment(new Date(new Date().toUTCString()))
nextEvent = moment(nextEvent, "dddd, MMMM Do, YYYY LTS") nextEvent = moment(nextEvent, "dddd, MMMM Do, YYYY LTS")
return currentDate.isAfter(nextEvent); return currentDate.isAfter(nextEvent);
},
clearPolling: async () => {
await clearInterval(globalVars.waterPolling);
await clearInterval(globalVars.heatingPolling)
await clearInterval(globalVars.piHolePolling)
await clearInterval(globalVars.weatherPolling)
},
clearGlobalVars: async () => {
globalVars.lastHeatingRequest = ""
globalVars.lastWaterRequest = ""
globalVars.lastWeatherRequest = ""
} }
} }

View File

@ -6,9 +6,12 @@ class globalVariables {
heatingPolling = ""; heatingPolling = "";
currentPage = ""; currentPage = "";
heatingPageSwitch = ""; heatingPageSwitch = "";
heatingPageSwitchActive = "";
heatingPageS = 1; heatingPageS = 1;
piHolePolling = ""; piHolePolling = "";
lastWeatherRequest = ""
lastWaterRequest = ""
lastHeatingRequest = ""
weatherPolling = ""
} }
module.exports = new globalVariables(); module.exports = new globalVariables();

View File

@ -9,21 +9,22 @@ var modules = requireDir('../modules');
module.exports = { module.exports = {
home: async () => { home: async () => {
clearInterval(globalVars.waterPolling); common.clearPolling()
clearInterval(globalVars.heatingPolling) common.clearGlobalVars()
clearInterval(globalVars.piHolePolling)
await screen.clearScreen() await screen.clearScreen()
await screen.heading('Main Menu') await screen.heading('Home')
await screen.clearLine(2) await screen.clearLine(2)
await screen.clearLine(3) await screen.clearLine(3)
await screen.clearLine(4) await screen.clearLine(4)
await screen.time() await screen.time()
await modules.weather.getStatus() await modules.weather.getStatus()
globalVars.weatherPolling = setInterval(() => {
modules.weather.getStatus()
}, 1800000);
}, },
water: async () => { water: async () => {
clearInterval(globalVars.waterPolling); common.clearPolling()
clearInterval(globalVars.heatingPolling) common.clearGlobalVars()
clearInterval(globalVars.piHolePolling)
await screen.clearScreen() await screen.clearScreen()
await screen.heading('Hot Water') await screen.heading('Hot Water')
await screen.time() await screen.time()
@ -33,9 +34,8 @@ module.exports = {
}, 60000); }, 60000);
}, },
heating: async () => { heating: async () => {
clearInterval(globalVars.waterPolling); common.clearPolling()
clearInterval(globalVars.heatingPolling) common.clearGlobalVars()
clearInterval(globalVars.piHolePolling)
await screen.clearScreen() await screen.clearScreen()
await screen.heading('Heating') await screen.heading('Heating')
await screen.time() await screen.time()
@ -45,9 +45,8 @@ module.exports = {
}, 60000); }, 60000);
}, },
piHole: async () => { piHole: async () => {
clearInterval(globalVars.waterPolling); common.clearPolling()
clearInterval(globalVars.heatingPolling) common.clearGlobalVars()
clearInterval(globalVars.piHolePolling)
await screen.clearScreen() await screen.clearScreen()
await screen.heading('PiHole') await screen.heading('PiHole')
await screen.time() await screen.time()

View File

@ -26,34 +26,45 @@ module.exports = {
// Once we're done streaming the response, parse it as json. // Once we're done streaming the response, parse it as json.
response.on('end', async function () { response.on('end', async function () {
var data = JSON.parse(content); var data = JSON.parse(content);
let waterOn = data.waterOn let waterdata = {
"waterOn": data.waterOn,
"time": await common.nextEvent(data.nextScheduleEventUtcTime),
}
globalVars.waterNextEvent = waterdata.time
var time = await common.nextEvent(data.nextScheduleEventUtcTime) // let m = await common.time(data.nextScheduleEventUtcTime)
async function updateWater() {
globalVars.waterNextEvent = time if (globalVars.waterOn === "" || globalVars.waterOn === "error") {
if (waterdata.waterOn === 'true') {
let m = await common.time(data.nextScheduleEventUtcTime) globalVars.waterOn = waterdata.waterOn
lcd.waterPageOne('Heating', globalVars.waterNextEvent)
if (globalVars.waterOn === "" || globalVars.waterOn === "error") { led.set('red')
if (waterOn === 'true') { } else {
globalVars.waterOn = waterOn globalVars.waterOn = waterdata.waterOn
lcd.waterPageOne('Heating', globalVars.waterNextEvent) lcd.waterPageOne('Not Heating', globalVars.waterNextEvent)
led.set('red') led.set('blue')
}
} else { } else {
globalVars.waterOn = waterOn if (waterdata.waterOn === 'true') {
lcd.waterPageOne('Not Heating', globalVars.waterNextEvent) globalVars.waterOn = waterdata.waterOn
led.set('blue') lcd.waterPageOne('Heating', globalVars.waterNextEvent)
led.set('red')
} else {
globalVars.waterOn = waterdata.waterOn
lcd.waterPageOne('Not Heating', globalVars.waterNextEvent)
led.set('blue')
}
} }
}
if (globalVars.lastWaterRequest === "") {
console.log('initial load')
globalVars.lastWaterRequest = waterdata
updateWater()
} else if (globalVars.lastWaterRequest = waterdata) {
console.log('no update')
} else { } else {
if (waterOn === 'true') { updateWater()
globalVars.waterOn = waterOn
lcd.waterPageOne('Heating', globalVars.waterNextEvent)
led.set('red')
} else {
globalVars.waterOn = waterOn
lcd.waterPageOne('Not Heating', globalVars.waterNextEvent)
led.set('blue')
}
} }
}); });
}); });

View File

@ -29,14 +29,27 @@ module.exports = {
// Once we're done streaming the response, parse it as json. // Once we're done streaming the response, parse it as json.
response.on('end', async function () { response.on('end', async function () {
var data = JSON.parse(content); var data = JSON.parse(content);
let city = data.name let weatherData = {
let weatherDescription = data.weather[0].description "city": data.name,
let currentTemp = Math.floor(data.main.temp) "weatherDescription": data.weather[0].description,
let highTemp = Math.floor(data.main.temp_max) "currentTemp": Math.floor(data.main.temp),
let lowTemp = Math.floor(data.main.temp_min) "highTemp": Math.floor(data.main.temp_max),
"lowTemp": Math.floor(data.main.temp_min)
await lcd.weather(city, weatherDescription, currentTemp, highTemp, lowTemp) }
async function updateWeather() {
console.log('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')
globalVars.lastWeatherRequest = weatherData
updateWeather()
} else if (globalVars.lastWeatherRequest = weatherData) {
console.log('no update')
} else {
updateWeather()
}
}); });
}) })
// Report errors // Report errors