v.0.6.0 redesign weather checker wlan
This commit is contained in:
@@ -1,39 +1,69 @@
|
||||
from app.classes.weather import Weather
|
||||
import urequests # type: ignore
|
||||
import json
|
||||
|
||||
from app.classes import Weather, Location, CurrentCondition
|
||||
from app.display.neopixel_64x64 import NeoPixel_64x64
|
||||
from app.web.wlan import Wlan
|
||||
import app.utils.colors as colors
|
||||
import app.display.fonts as fonts
|
||||
from app.utils import URLEncoder
|
||||
|
||||
|
||||
API_KEY = '3545ce42d0ba436e8dc164532250410'
|
||||
ACTUAL_WEATHER_URL = 'http://api.weatherapi.com/v1/current.json?key={API_KEY}&q={city}&aqi=yes&lang={lang}'
|
||||
|
||||
class Weather_Checker():
|
||||
wlan: Wlan
|
||||
|
||||
def __init__(self, display: NeoPixel_64x64):
|
||||
def __init__(self, city: str, display: NeoPixel_64x64):
|
||||
self.display = display
|
||||
self.city = URLEncoder.encode(city) # url_encode
|
||||
self.display.set_font( font=fonts.font_5x7)
|
||||
|
||||
def setup_wlan(self):
|
||||
self.wlan = Wlan('WOKWI-Guest', '12345678')
|
||||
|
||||
def mock_weather_data(self):
|
||||
filepath = 'restapi/mock-weather.json'
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as file:
|
||||
return json.load(file)
|
||||
except OSError: # Use OSError instead of FileNotFoundError
|
||||
print(f"Error: File '{filepath}' not found.")
|
||||
return None
|
||||
except FileNotFoundError:
|
||||
print(f"Error: File '{filepath}' not found.")
|
||||
return None
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Error: Invalid JSON in '{filepath}': {e}")
|
||||
return None
|
||||
|
||||
|
||||
def actual_weather(self, city='grosshansdorf', lang='de', test_mode=False) -> Weather:
|
||||
weather_url = ACTUAL_WEATHER_URL.format(API_KEY=API_KEY, city=city, lang=lang)
|
||||
|
||||
if not test_mode:
|
||||
|
||||
print(f'query: {weather_url}')
|
||||
r = urequests.get(weather_url)
|
||||
|
||||
print('Status-Code:', r.status_code)
|
||||
json_resp = r.json()
|
||||
|
||||
r.close()
|
||||
else:
|
||||
print('Status-Code: test_mode')
|
||||
# json_resp = WEATHER_QUERY_MOCK
|
||||
json_resp = self.mock_weather_data()
|
||||
|
||||
loc = Location(**json_resp['location'])
|
||||
cc = CurrentCondition(**json_resp['current'])
|
||||
|
||||
weather = Weather(location=loc, current=cc)
|
||||
|
||||
return weather
|
||||
|
||||
|
||||
def check(self, test_mode: bool = False):
|
||||
try:
|
||||
self.display.clear()
|
||||
self.display.write_text('search wlan', 0, 0, color=colors.WHITE)
|
||||
|
||||
self.setup_wlan()
|
||||
|
||||
self.display.clear()
|
||||
# self.display.set_font(fonts.font_3x5)
|
||||
ypos = 0
|
||||
self.display.write_text('wlan connected', 0, ypos, color=colors.RAINBOW[0])
|
||||
ypos += self.display.font_height + 1
|
||||
self.display.write_text('querying', 0, ypos, color=colors.RAINBOW[1])
|
||||
ypos += self.display.font_height + 1
|
||||
self.display.write_text('weather data', 0, ypos, color=colors.RAINBOW[2])
|
||||
|
||||
w_resp: Weather = self.wlan.actual_weather(lang='de', test_mode=test_mode)
|
||||
|
||||
self.display.clear()
|
||||
w_resp: Weather = self.actual_weather(city=self.city, lang='de', test_mode=test_mode)
|
||||
|
||||
ypos = 0
|
||||
self.display.write_text(f'{str(w_resp.location.name)}', 0, ypos, color=colors.RAINBOW[0])
|
||||
|
||||
Reference in New Issue
Block a user