Handle weather connection errors

This commit is contained in:
Mike Cifelli 2022-12-24 19:10:15 -05:00
parent 132005d938
commit 5d1caff78c
3 changed files with 36 additions and 33 deletions

View File

@ -25,16 +25,6 @@ class CharacterDisplay:
self.maxX = maxX self.maxX = maxX
self.digits = { self.digits = {
0: self.displayZero,
1: self.displayOne,
2: self.displayTwo,
3: self.displayThree,
4: self.displayFour,
5: self.displayFive,
6: self.displaySix,
7: self.displaySeven,
8: self.displayEight,
9: self.displayNine,
'0': self.displayZero, '0': self.displayZero,
'1': self.displayOne, '1': self.displayOne,
'2': self.displayTwo, '2': self.displayTwo,
@ -66,7 +56,7 @@ class CharacterDisplay:
self.unicorn.show() self.unicorn.show()
def displayTimeDots(self, x, y, color): def displayTimeDots(self, x, y, color,):
self.setPixel(x, y, colors.BLACK) self.setPixel(x, y, colors.BLACK)
self.setPixel(x, y-1, color) self.setPixel(x, y-1, color)
self.setPixel(x, y-2, colors.BLACK) self.setPixel(x, y-2, colors.BLACK)
@ -85,6 +75,7 @@ class CharacterDisplay:
self.setPixel(x, y-4, colors.BLACK) self.setPixel(x, y-4, colors.BLACK)
self.setPixel(x+1, y-3, colors.BLACK) self.setPixel(x+1, y-3, colors.BLACK)
self.setPixel(x+1, y-4, colors.BLACK) self.setPixel(x+1, y-4, colors.BLACK)
self.unicorn.show()
def displayZero(self, x, y, color): def displayZero(self, x, y, color):
self.fullLine(x, y, color) self.fullLine(x, y, color)

View File

@ -30,7 +30,7 @@ class TimeDisplay:
if hour[0] != self.currentHour[0]: if hour[0] != self.currentHour[0]:
self.currentHour[0] = hour[0] self.currentHour[0] = hour[0]
if hour[0] == 0: if hour[0] == '0':
self.hideDigit(self.hourStartColumn) self.hideDigit(self.hourStartColumn)
else: else:
self.showDigit(self.hourStartColumn, hour[0]) self.showDigit(self.hourStartColumn, hour[0])
@ -71,4 +71,4 @@ class TimeDisplay:
def getTimeDigits(self, format): def getTimeDigits(self, format):
digits = datetime.now().strftime(format).strip().rjust(2, '0') digits = datetime.now().strftime(format).strip().rjust(2, '0')
return [int(x) for x in digits] return list(digits)

View File

@ -9,39 +9,51 @@ class WeatherDisplay:
self.characterDisplay = characterDisplay self.characterDisplay = characterDisplay
self.topRow = topRow self.topRow = topRow
self.currentTemperature = [9, 9] self.currentTemperature = [9, 9]
self.currentColor = colors.BLACK
self.color = colors.BLUE self.color = colors.BLUE
config = ConfigParser() config = ConfigParser()
config.read(configFile) config.read(configFile)
self.host = config['weather'].get('host') host = config['weather'].get('host')
self.user = config['weather'].get('user') user = config['weather'].get('user')
self.password = config['weather'].get('pass') password = config['weather'].get('pass')
self.lat = config['weather'].get('lat') lat = config['weather'].get('lat')
self.lon = config['weather'].get('lon') lon = config['weather'].get('lon')
print(self.host) self.uri = f'https://{host}/api/weather?lat={lat}&lon={lon}&units=metric'
self.auth = (user, password)
def showWeather(self): def showWeather(self):
self.characterDisplay.clearRow(self.topRow) try:
self.showTemperature( self.showTemperature(
self.getTemperatureCharacters(self.getTemperature())) self.getTemperatureCharacters(self.getTemperature()))
except requests.exceptions.ConnectionError:
print("connection error", flush=True)
self.characterDisplay.clearRow(self.topRow)
except requests.exceptions.Timeout:
print("timeout", flush=True)
self.characterDisplay.clearRow(self.topRow)
def showTemperature(self, temperature): def showTemperature(self, temperature):
start = 13 if ( len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1 else 9 if temperature != self.currentTemperature:
self.characterDisplay.clearRow(self.topRow)
for c in temperature: start = 13 if (
if c == '-': len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1 else 9
self.characterDisplay.displayNegative(
start-3, self.topRow, self.color) for c in temperature:
else: if c == '-':
self.characterDisplay.displayDigit( self.characterDisplay.displayNegative(
start, self.topRow, c, self.color) start-3, self.topRow, self.color)
start = start + 4 else:
self.characterDisplay.displayDigit(
start, self.topRow, c, self.color)
start = start + 4
self.currentTemperature = temperature
def getTemperature(self): def getTemperature(self):
return list(filter(lambda t: t["timestep"] == 'current', requests.get(f'https://{self.host}/api/weather?lat={self.lat}&lon={self.lon}&units=metric', auth=(self.user, self.password)).json()["tomorrow"]["data"]['timelines']))[0]["intervals"][0]["values"]["temperature"] return list(filter(lambda t: t["timestep"] == 'current', requests.get(self.uri, auth=self.auth, timeout=2).json()["tomorrow"]["data"]['timelines']))[0]["intervals"][0]["values"]["temperature"]
def getTemperatureCharacters(self, temperature): def getTemperatureCharacters(self, temperature):
return list(str(round(temperature))) return list(str(round(temperature)))