Handle weather connection errors
This commit is contained in:
parent
132005d938
commit
5d1caff78c
|
@ -25,16 +25,6 @@ class CharacterDisplay:
|
|||
self.maxX = maxX
|
||||
|
||||
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,
|
||||
'1': self.displayOne,
|
||||
'2': self.displayTwo,
|
||||
|
@ -66,7 +56,7 @@ class CharacterDisplay:
|
|||
|
||||
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-1, color)
|
||||
self.setPixel(x, y-2, colors.BLACK)
|
||||
|
@ -85,6 +75,7 @@ class CharacterDisplay:
|
|||
self.setPixel(x, y-4, colors.BLACK)
|
||||
self.setPixel(x+1, y-3, colors.BLACK)
|
||||
self.setPixel(x+1, y-4, colors.BLACK)
|
||||
self.unicorn.show()
|
||||
|
||||
def displayZero(self, x, y, color):
|
||||
self.fullLine(x, y, color)
|
||||
|
|
|
@ -30,7 +30,7 @@ class TimeDisplay:
|
|||
if hour[0] != self.currentHour[0]:
|
||||
self.currentHour[0] = hour[0]
|
||||
|
||||
if hour[0] == 0:
|
||||
if hour[0] == '0':
|
||||
self.hideDigit(self.hourStartColumn)
|
||||
else:
|
||||
self.showDigit(self.hourStartColumn, hour[0])
|
||||
|
@ -71,4 +71,4 @@ class TimeDisplay:
|
|||
def getTimeDigits(self, format):
|
||||
digits = datetime.now().strftime(format).strip().rjust(2, '0')
|
||||
|
||||
return [int(x) for x in digits]
|
||||
return list(digits)
|
||||
|
|
|
@ -9,27 +9,37 @@ class WeatherDisplay:
|
|||
self.characterDisplay = characterDisplay
|
||||
self.topRow = topRow
|
||||
self.currentTemperature = [9, 9]
|
||||
self.currentColor = colors.BLACK
|
||||
self.color = colors.BLUE
|
||||
|
||||
config = ConfigParser()
|
||||
config.read(configFile)
|
||||
|
||||
self.host = config['weather'].get('host')
|
||||
self.user = config['weather'].get('user')
|
||||
self.password = config['weather'].get('pass')
|
||||
self.lat = config['weather'].get('lat')
|
||||
self.lon = config['weather'].get('lon')
|
||||
host = config['weather'].get('host')
|
||||
user = config['weather'].get('user')
|
||||
password = config['weather'].get('pass')
|
||||
lat = config['weather'].get('lat')
|
||||
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):
|
||||
self.characterDisplay.clearRow(self.topRow)
|
||||
try:
|
||||
self.showTemperature(
|
||||
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):
|
||||
start = 13 if ( len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1 else 9
|
||||
if temperature != self.currentTemperature:
|
||||
self.characterDisplay.clearRow(self.topRow)
|
||||
|
||||
start = 13 if (
|
||||
len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1 else 9
|
||||
|
||||
for c in temperature:
|
||||
if c == '-':
|
||||
|
@ -40,8 +50,10 @@ class WeatherDisplay:
|
|||
start, self.topRow, c, self.color)
|
||||
start = start + 4
|
||||
|
||||
self.currentTemperature = temperature
|
||||
|
||||
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):
|
||||
return list(str(round(temperature)))
|
||||
|
|
Loading…
Reference in New Issue