From 3871bf76db1c4cf007da6788b3b283fb9411d54d Mon Sep 17 00:00:00 2001
From: Karl Hudgell <karl@k-world.me.uk>
Date: Tue, 29 Sep 2020 10:23:41 +0100
Subject: [PATCH] update weather every 30mins

---
 app.js              | 14 ++---------
 libs/common.js      | 13 ++++++++++
 libs/globalVars.js  |  5 +++-
 modules/mainMenu.js | 25 +++++++++----------
 modules/water.js    | 59 +++++++++++++++++++++++++++------------------
 modules/weather.js  | 29 ++++++++++++++++------
 6 files changed, 87 insertions(+), 58 deletions(-)

diff --git a/app.js b/app.js
index ea9e597..f55d9fa 100644
--- a/app.js
+++ b/app.js
@@ -23,10 +23,12 @@ async function main() {
         case 'water':
           console.log('Water Switch')
           if (globalVars.waterOn === 'true') {
+            globalVars.lastWaterRequest = ""
             await common.request(0)
             await common.pause(2000)
             await menu.water()
           } else {
+            globalVars.lastWaterRequest = ""
             await common.request(1)
             await common.pause(2000)
             await menu.water()
@@ -59,32 +61,20 @@ async function main() {
       switch (arr[i]) {
         case 'water':
           globalVars.currentPage = arr[i]
-          clearInterval(globalVars.waterPolling)
-          clearInterval(globalVars.heatingPageSwitch)
-          clearInterval(globalVars.heatingPolling)
           await menu.water()
           break;
         case 'mainMenu':
           globalVars.currentPage = arr[i]
-          clearInterval(globalVars.waterPolling)
-          clearInterval(globalVars.heatingPageSwitch)
-          clearInterval(globalVars.heatingPolling)
           led.set('off')
           await menu.home()
           break;
         case 'heating':
           globalVars.currentPage = arr[i]
-          clearInterval(globalVars.waterPolling)
-          clearInterval(globalVars.heatingPageSwitch)
-          clearInterval(globalVars.heatingPolling)
           led.set('off')
           await menu.heating()
           break;
         case 'pihole':
           globalVars.currentPage = arr[i]
-          clearInterval(globalVars.waterPolling)
-          clearInterval(globalVars.heatingPageSwitch)
-          clearInterval(globalVars.heatingPolling)
           led.set('off')
           await menu.piHole()
           break;
diff --git a/libs/common.js b/libs/common.js
index 6e12892..4433487 100644
--- a/libs/common.js
+++ b/libs/common.js
@@ -1,5 +1,7 @@
 const got = require('got');
 const moment = require('moment');
+const globalVars = require('./globalVars')
+
 
 module.exports = {
     request: async (input) => {
@@ -41,5 +43,16 @@ module.exports = {
         let currentDate = moment(new Date(new Date().toUTCString()))
         nextEvent = moment(nextEvent, "dddd, MMMM Do, YYYY LTS")
         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 = ""
     }
 }
\ No newline at end of file
diff --git a/libs/globalVars.js b/libs/globalVars.js
index 7cabf0f..107aaca 100644
--- a/libs/globalVars.js
+++ b/libs/globalVars.js
@@ -6,9 +6,12 @@ class globalVariables {
     heatingPolling = "";
     currentPage = "";
     heatingPageSwitch = "";
-    heatingPageSwitchActive = "";
     heatingPageS = 1;
     piHolePolling = "";
+    lastWeatherRequest = ""
+    lastWaterRequest = ""
+    lastHeatingRequest = ""
+    weatherPolling = ""
  }
 
 module.exports = new globalVariables();
\ No newline at end of file
diff --git a/modules/mainMenu.js b/modules/mainMenu.js
index 33b43a7..cdef3eb 100644
--- a/modules/mainMenu.js
+++ b/modules/mainMenu.js
@@ -9,21 +9,22 @@ var modules = requireDir('../modules');
 
 module.exports = {
     home: async () => {
-        clearInterval(globalVars.waterPolling);
-        clearInterval(globalVars.heatingPolling)
-        clearInterval(globalVars.piHolePolling)
+        common.clearPolling()
+        common.clearGlobalVars()
         await screen.clearScreen()
-        await screen.heading('Main Menu')
+        await screen.heading('Home')
         await screen.clearLine(2)
         await screen.clearLine(3)
         await screen.clearLine(4)
         await screen.time()
         await modules.weather.getStatus()
+        globalVars.weatherPolling = setInterval(() => {
+            modules.weather.getStatus()
+        }, 1800000);
     },
     water: async () => {
-        clearInterval(globalVars.waterPolling);
-        clearInterval(globalVars.heatingPolling)
-        clearInterval(globalVars.piHolePolling)
+        common.clearPolling()
+        common.clearGlobalVars()
         await screen.clearScreen()
         await screen.heading('Hot Water')
         await screen.time()
@@ -33,9 +34,8 @@ module.exports = {
         }, 60000);
     },
     heating: async () => {
-        clearInterval(globalVars.waterPolling);
-        clearInterval(globalVars.heatingPolling)
-        clearInterval(globalVars.piHolePolling)
+        common.clearPolling()
+        common.clearGlobalVars()
         await screen.clearScreen()
         await screen.heading('Heating')
         await screen.time()
@@ -45,9 +45,8 @@ module.exports = {
         }, 60000);
     },
     piHole: async () => {
-        clearInterval(globalVars.waterPolling);
-        clearInterval(globalVars.heatingPolling)
-        clearInterval(globalVars.piHolePolling)
+        common.clearPolling()
+        common.clearGlobalVars()
         await screen.clearScreen()
         await screen.heading('PiHole')
         await screen.time()
diff --git a/modules/water.js b/modules/water.js
index 0f0ae63..f41dcda 100644
--- a/modules/water.js
+++ b/modules/water.js
@@ -26,34 +26,45 @@ module.exports = {
             // Once we're done streaming the response, parse it as json.
             response.on('end', async function () {
                 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)
-
-                globalVars.waterNextEvent = time
-
-                let m = await common.time(data.nextScheduleEventUtcTime)
-
-                if (globalVars.waterOn === "" || globalVars.waterOn === "error") {
-                    if (waterOn === 'true') {
-                        globalVars.waterOn = waterOn
-                        lcd.waterPageOne('Heating', globalVars.waterNextEvent)
-                        led.set('red')
+                // let m = await common.time(data.nextScheduleEventUtcTime)
+                async function updateWater() {
+                    if (globalVars.waterOn === "" || globalVars.waterOn === "error") {
+                        if (waterdata.waterOn === 'true') {
+                            globalVars.waterOn = waterdata.waterOn
+                            lcd.waterPageOne('Heating', globalVars.waterNextEvent)
+                            led.set('red')
+                        } else {
+                            globalVars.waterOn = waterdata.waterOn
+                            lcd.waterPageOne('Not Heating', globalVars.waterNextEvent)
+                            led.set('blue')
+                        }
                     } else {
-                        globalVars.waterOn = waterOn
-                        lcd.waterPageOne('Not Heating', globalVars.waterNextEvent)
-                        led.set('blue')
+                        if (waterdata.waterOn === 'true') {
+                            globalVars.waterOn = waterdata.waterOn
+                            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 {
-                    if (waterOn === 'true') {
-                        globalVars.waterOn = waterOn
-                        lcd.waterPageOne('Heating', globalVars.waterNextEvent)
-                        led.set('red')
-                    } else {
-                        globalVars.waterOn = waterOn
-                        lcd.waterPageOne('Not Heating', globalVars.waterNextEvent)
-                        led.set('blue')
-                    }
+                    updateWater()
                 }
             });
         });
diff --git a/modules/weather.js b/modules/weather.js
index 8b91013..ce9ded7 100644
--- a/modules/weather.js
+++ b/modules/weather.js
@@ -29,14 +29,27 @@ module.exports = {
             // Once we're done streaming the response, parse it as json.
             response.on('end', async function () {
                 var data = JSON.parse(content);
-                let city = data.name
-                let weatherDescription = data.weather[0].description
-                let currentTemp = Math.floor(data.main.temp)
-                let highTemp = Math.floor(data.main.temp_max)
-                let lowTemp = Math.floor(data.main.temp_min)
-
-                await lcd.weather(city, weatherDescription, currentTemp, highTemp, lowTemp)
-
+                let weatherData = {
+                    "city": data.name,
+                    "weatherDescription": data.weather[0].description,
+                    "currentTemp": Math.floor(data.main.temp),
+                    "highTemp": Math.floor(data.main.temp_max),
+                    "lowTemp": Math.floor(data.main.temp_min)
+                }
+                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