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 chutney.colors as colors
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from .garage import garageFile
|
||||||
|
|
||||||
|
|
||||||
class GarageDisplayer:
|
class GarageDisplayer:
|
||||||
def __init__(self, symbolDisplayer, topRow):
|
def __init__(self, symbolDisplayer, topRow):
|
||||||
|
@ -13,7 +15,6 @@ class GarageDisplayer:
|
||||||
self.closedFillColor = colors.RED
|
self.closedFillColor = colors.RED
|
||||||
self.openOutlineColor = colors.WHITE
|
self.openOutlineColor = colors.WHITE
|
||||||
self.openFillColor = colors.WHITE
|
self.openFillColor = colors.WHITE
|
||||||
self.garageFile = 'data/garage.json'
|
|
||||||
|
|
||||||
self.symbolDisplayer.clearRow(self.topRow, rowHeight=4)
|
self.symbolDisplayer.clearRow(self.topRow, rowHeight=4)
|
||||||
|
|
||||||
|
@ -30,7 +31,7 @@ class GarageDisplayer:
|
||||||
|
|
||||||
def getGarageState(self):
|
def getGarageState(self):
|
||||||
try:
|
try:
|
||||||
with open(self.garageFile) as file:
|
with open(garageFile) as file:
|
||||||
return json.load(file)
|
return json.load(file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e, flush=True)
|
print(e, flush=True)
|
||||||
|
|
|
@ -15,14 +15,10 @@ class GarageListener:
|
||||||
self.ntfyUri = f'https://{ntfy}/{topic}/json'
|
self.ntfyUri = f'https://{ntfy}/{topic}/json'
|
||||||
|
|
||||||
def listenForNotifications(self, queue):
|
def listenForNotifications(self, queue):
|
||||||
resp = requests.get(self.ntfyUri, stream=True)
|
with requests.get(self.ntfyUri, stream=True, timeout=60) as response:
|
||||||
|
for line in response.iter_lines():
|
||||||
try:
|
|
||||||
for line in resp.iter_lines():
|
|
||||||
if line:
|
if line:
|
||||||
data = json.loads(line.decode('utf-8'))
|
data = json.loads(line.decode('utf-8'))
|
||||||
|
|
||||||
if (data['event'] == 'message'):
|
if (data['event'] == 'message'):
|
||||||
queue.put('update')
|
queue.put('update')
|
||||||
finally:
|
|
||||||
resp.close()
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
from .garage import garageFile
|
||||||
|
from .garage import garageTempFile
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,8 +12,6 @@ class GarageUpdater:
|
||||||
config.read(configFile)
|
config.read(configFile)
|
||||||
|
|
||||||
self.uri = config['garage'].get('uri')
|
self.uri = config['garage'].get('uri')
|
||||||
self.garageTempFile = 'data/garage.tmp'
|
|
||||||
self.garageFile = 'data/garage.json'
|
|
||||||
|
|
||||||
self.persistGarageState('{"error": "init"}')
|
self.persistGarageState('{"error": "init"}')
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ class GarageUpdater:
|
||||||
return requests.get(self.uri, timeout=6).text
|
return requests.get(self.uri, timeout=6).text
|
||||||
|
|
||||||
def persistGarageState(self, state):
|
def persistGarageState(self, state):
|
||||||
with open(self.garageTempFile, 'w') as file:
|
with open(garageTempFile, 'w') as file:
|
||||||
file.write(state)
|
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)
|
garageListener = GarageListener(configFile=CONFIG_FILE)
|
||||||
isRunning = True
|
isRunning = True
|
||||||
|
isFirstRun = True
|
||||||
|
|
||||||
while isRunning:
|
while isRunning:
|
||||||
try:
|
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)
|
garageListener.listenForNotifications(queue)
|
||||||
except HaltException:
|
except HaltException:
|
||||||
isRunning = False
|
isRunning = False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e, flush=True)
|
print(e, flush=True)
|
||||||
sleep(GARAGE_LISTENER_RETRY_INTERVAL_IN_SECONDS)
|
|
||||||
|
|
||||||
|
|
||||||
def cleanExit(unicorn, haltEvent, queue):
|
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
|
import chutney.colors as colors
|
||||||
|
|
||||||
|
from .weather import weatherFile
|
||||||
|
|
||||||
|
|
||||||
class WeatherDisplayer:
|
class WeatherDisplayer:
|
||||||
def __init__(self, symbolDisplayer, topRow):
|
def __init__(self, symbolDisplayer, topRow):
|
||||||
|
@ -10,7 +12,6 @@ class WeatherDisplayer:
|
||||||
self.digitWidth = 4
|
self.digitWidth = 4
|
||||||
self.currentTemperature = ''
|
self.currentTemperature = ''
|
||||||
self.color = colors.ORANGE
|
self.color = colors.ORANGE
|
||||||
self.weatherFile = 'data/weather.txt'
|
|
||||||
|
|
||||||
def showWeather(self):
|
def showWeather(self):
|
||||||
temperature = self.getTemperature()
|
temperature = self.getTemperature()
|
||||||
|
@ -25,7 +26,7 @@ class WeatherDisplayer:
|
||||||
|
|
||||||
def getTemperature(self):
|
def getTemperature(self):
|
||||||
try:
|
try:
|
||||||
with open(self.weatherFile) as file:
|
with open(weatherFile) as file:
|
||||||
return file.readline().strip()
|
return file.readline().strip()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e, flush=True)
|
print(e, flush=True)
|
||||||
|
@ -55,7 +56,8 @@ class WeatherDisplayer:
|
||||||
|
|
||||||
def getStartColumn(self, temperature):
|
def getStartColumn(self, temperature):
|
||||||
offset = 1 if self.startsWithOne(temperature) else 0
|
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
|
return startColumn - offset
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
from .weather import weatherFile
|
||||||
|
from .weather import weatherTempFile
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,8 +19,6 @@ class WeatherUpdater:
|
||||||
|
|
||||||
self.uri = f'https://{host}/api/temperature?lat={lat}&lon={lon}&units=metric'
|
self.uri = f'https://{host}/api/temperature?lat={lat}&lon={lon}&units=metric'
|
||||||
self.auth = (user, password)
|
self.auth = (user, password)
|
||||||
self.weatherTempFile = 'data/weather.tmp'
|
|
||||||
self.weatherFile = 'data/weather.txt'
|
|
||||||
self.persistTemperature('init')
|
self.persistTemperature('init')
|
||||||
|
|
||||||
def updateWeather(self):
|
def updateWeather(self):
|
||||||
|
@ -36,7 +36,7 @@ class WeatherUpdater:
|
||||||
return requests.get(self.uri, auth=self.auth, timeout=3).json()['temperature']
|
return requests.get(self.uri, auth=self.auth, timeout=3).json()['temperature']
|
||||||
|
|
||||||
def persistTemperature(self, temperature):
|
def persistTemperature(self, temperature):
|
||||||
with open(self.weatherTempFile, 'w') as file:
|
with open(weatherTempFile, 'w') as file:
|
||||||
file.write(str(temperature))
|
file.write(str(temperature))
|
||||||
|
|
||||||
os.replace(self.weatherTempFile, self.weatherFile)
|
os.replace(weatherTempFile, weatherFile)
|
||||||
|
|
Loading…
Reference in New Issue