From 26c1b22f43dd36e05ab994367261b904e99d87a0 Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Sat, 14 Jan 2023 15:43:19 -0500 Subject: [PATCH] Restore ntfy connection after an outage --- chutney/garage/garage.py | 2 ++ chutney/garage/garagedisplayer.py | 5 +++-- chutney/garage/garagelistener.py | 8 ++------ chutney/garage/garageupdater.py | 8 ++++---- chutney/runner.py | 9 ++++++++- chutney/weather/weather.py | 2 ++ chutney/weather/weatherdisplayer.py | 8 +++++--- chutney/weather/weatherupdater.py | 8 ++++---- 8 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 chutney/garage/garage.py create mode 100644 chutney/weather/weather.py diff --git a/chutney/garage/garage.py b/chutney/garage/garage.py new file mode 100644 index 0000000..68a16cf --- /dev/null +++ b/chutney/garage/garage.py @@ -0,0 +1,2 @@ +garageTempFile = 'data/garage.tmp' +garageFile = 'data/garage.json' diff --git a/chutney/garage/garagedisplayer.py b/chutney/garage/garagedisplayer.py index d9e5d72..0520e5c 100644 --- a/chutney/garage/garagedisplayer.py +++ b/chutney/garage/garagedisplayer.py @@ -1,6 +1,8 @@ import chutney.colors as colors import json +from .garage import garageFile + class GarageDisplayer: def __init__(self, symbolDisplayer, topRow): @@ -13,7 +15,6 @@ class GarageDisplayer: self.closedFillColor = colors.RED self.openOutlineColor = colors.WHITE self.openFillColor = colors.WHITE - self.garageFile = 'data/garage.json' self.symbolDisplayer.clearRow(self.topRow, rowHeight=4) @@ -30,7 +31,7 @@ class GarageDisplayer: def getGarageState(self): try: - with open(self.garageFile) as file: + with open(garageFile) as file: return json.load(file) except Exception as e: print(e, flush=True) diff --git a/chutney/garage/garagelistener.py b/chutney/garage/garagelistener.py index 939e1f7..707c35a 100644 --- a/chutney/garage/garagelistener.py +++ b/chutney/garage/garagelistener.py @@ -15,14 +15,10 @@ class GarageListener: self.ntfyUri = f'https://{ntfy}/{topic}/json' def listenForNotifications(self, queue): - resp = requests.get(self.ntfyUri, stream=True) - - try: - for line in resp.iter_lines(): + with requests.get(self.ntfyUri, stream=True, timeout=60) as response: + for line in response.iter_lines(): if line: data = json.loads(line.decode('utf-8')) if (data['event'] == 'message'): queue.put('update') - finally: - resp.close() diff --git a/chutney/garage/garageupdater.py b/chutney/garage/garageupdater.py index 84651b4..7b4a33a 100644 --- a/chutney/garage/garageupdater.py +++ b/chutney/garage/garageupdater.py @@ -1,6 +1,8 @@ import os import requests +from .garage import garageFile +from .garage import garageTempFile from configparser import ConfigParser @@ -10,8 +12,6 @@ class GarageUpdater: config.read(configFile) self.uri = config['garage'].get('uri') - self.garageTempFile = 'data/garage.tmp' - self.garageFile = 'data/garage.json' self.persistGarageState('{"error": "init"}') @@ -30,7 +30,7 @@ class GarageUpdater: return requests.get(self.uri, timeout=6).text def persistGarageState(self, state): - with open(self.garageTempFile, 'w') as file: + with open(garageTempFile, 'w') as file: file.write(state) - os.replace(self.garageTempFile, self.garageFile) + os.replace(garageTempFile, garageFile) diff --git a/chutney/runner.py b/chutney/runner.py index 0f486ef..0a02d58 100644 --- a/chutney/runner.py +++ b/chutney/runner.py @@ -87,15 +87,22 @@ def garageNotifyLoop(queue): garageListener = GarageListener(configFile=CONFIG_FILE) isRunning = True + isFirstRun = True while isRunning: try: + if isFirstRun: + isFirstRun = False + else: + print('waiting to restart garage notify loop') + sleep(GARAGE_LISTENER_RETRY_INTERVAL_IN_SECONDS) + + print('listening for garage updates') garageListener.listenForNotifications(queue) except HaltException: isRunning = False except Exception as e: print(e, flush=True) - sleep(GARAGE_LISTENER_RETRY_INTERVAL_IN_SECONDS) def cleanExit(unicorn, haltEvent, queue): diff --git a/chutney/weather/weather.py b/chutney/weather/weather.py new file mode 100644 index 0000000..0c91db0 --- /dev/null +++ b/chutney/weather/weather.py @@ -0,0 +1,2 @@ +weatherTempFile = 'data/weather.tmp' +weatherFile = 'data/weather.txt' diff --git a/chutney/weather/weatherdisplayer.py b/chutney/weather/weatherdisplayer.py index 24276e8..4ad70aa 100644 --- a/chutney/weather/weatherdisplayer.py +++ b/chutney/weather/weatherdisplayer.py @@ -1,5 +1,7 @@ import chutney.colors as colors +from .weather import weatherFile + class WeatherDisplayer: def __init__(self, symbolDisplayer, topRow): @@ -10,7 +12,6 @@ class WeatherDisplayer: self.digitWidth = 4 self.currentTemperature = '' self.color = colors.ORANGE - self.weatherFile = 'data/weather.txt' def showWeather(self): temperature = self.getTemperature() @@ -25,7 +26,7 @@ class WeatherDisplayer: def getTemperature(self): try: - with open(self.weatherFile) as file: + with open(weatherFile) as file: return file.readline().strip() except Exception as e: print(e, flush=True) @@ -55,7 +56,8 @@ class WeatherDisplayer: def getStartColumn(self, temperature): offset = 1 if self.startsWithOne(temperature) else 0 - startColumn = self.oneDigitStartColumn if self.isOneDigit(temperature) else self.twoDigitStartColumn + startColumn = self.oneDigitStartColumn if self.isOneDigit( + temperature) else self.twoDigitStartColumn return startColumn - offset diff --git a/chutney/weather/weatherupdater.py b/chutney/weather/weatherupdater.py index bf92f9a..03bea63 100644 --- a/chutney/weather/weatherupdater.py +++ b/chutney/weather/weatherupdater.py @@ -1,6 +1,8 @@ import os import requests +from .weather import weatherFile +from .weather import weatherTempFile from configparser import ConfigParser @@ -17,8 +19,6 @@ class WeatherUpdater: self.uri = f'https://{host}/api/temperature?lat={lat}&lon={lon}&units=metric' self.auth = (user, password) - self.weatherTempFile = 'data/weather.tmp' - self.weatherFile = 'data/weather.txt' self.persistTemperature('init') def updateWeather(self): @@ -36,7 +36,7 @@ class WeatherUpdater: return requests.get(self.uri, auth=self.auth, timeout=3).json()['temperature'] def persistTemperature(self, temperature): - with open(self.weatherTempFile, 'w') as file: + with open(weatherTempFile, 'w') as file: file.write(str(temperature)) - os.replace(self.weatherTempFile, self.weatherFile) + os.replace(weatherTempFile, weatherFile)