Restore ntfy connection after an outage
This commit is contained in:
parent
0a212e5752
commit
26c1b22f43
|
@ -0,0 +1,2 @@
|
|||
garageTempFile = 'data/garage.tmp'
|
||||
garageFile = 'data/garage.json'
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
weatherTempFile = 'data/weather.tmp'
|
||||
weatherFile = 'data/weather.txt'
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue