chutney/weather_display.py

54 lines
1.8 KiB
Python
Raw Normal View History

2022-12-24 10:59:04 -05:00
import colors
import requests
2022-12-24 12:10:46 -05:00
2022-12-24 10:59:04 -05:00
class WeatherDisplay:
def __init__(self, characterDisplay, topRow):
2022-12-24 10:59:04 -05:00
self.characterDisplay = characterDisplay
self.topRow = topRow
self.currentTemperature = [9, 9]
self.color = colors.BLUE
self.weatherFile = 'weather.txt'
2022-12-24 10:59:04 -05:00
def showWeather(self):
2022-12-24 19:10:15 -05:00
try:
self.showTemperature(
self.getTemperatureCharacters(self.getTemperature()))
except requests.exceptions.ConnectionError:
print('connection error', flush=True)
2022-12-24 19:10:15 -05:00
self.characterDisplay.clearRow(self.topRow)
except requests.exceptions.Timeout:
print('timeout', flush=True)
2022-12-24 19:10:15 -05:00
self.characterDisplay.clearRow(self.topRow)
2022-12-24 10:59:04 -05:00
def showTemperature(self, temperature):
2022-12-24 19:10:15 -05:00
if temperature != self.currentTemperature:
self.characterDisplay.clearRow(self.topRow)
2022-12-24 10:59:04 -05:00
2022-12-24 19:10:15 -05:00
start = 13 if (
len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1 else 9
for c in temperature:
if c == '-':
self.characterDisplay.displayNegative(
start-3, self.topRow, self.color)
else:
self.characterDisplay.displayDigit(
start, self.topRow, c, self.color)
start = start + 4
self.currentTemperature = temperature
2022-12-24 10:59:04 -05:00
def getTemperature(self):
# TODO - don't use a file left around from a previous run
# TODO - clear temp on errors from updater
try:
with open(self.weatherFile) as file:
return int(file.readline().strip())
except Exception as e:
print(e)
return 0
2022-12-24 10:59:04 -05:00
def getTemperatureCharacters(self, temperature):
2022-12-24 12:10:46 -05:00
return list(str(round(temperature)))