diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 3185d0d..491a572 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -17,7 +17,6 @@ "vscode": { "settings": {}, "extensions": [ - "streetsidesoftware.code-spell-checker", "ms-python.python", "frhtylcn.pythonsnippets", "kevinrose.vsc-python-indent", diff --git a/app/utils/time_utils.py b/app/utils/time_utils.py index 565a4f1..8b064f0 100644 --- a/app/utils/time_utils.py +++ b/app/utils/time_utils.py @@ -1,95 +1,129 @@ import time -import ntptime # type: ignore +import ntptime # type: ignore _UTC_OFFSET: int = 1 * 3600 # +1 or +2 hours for CEST, adjust for your timezone + def local_time_with_offset(): - # Set timezone offset (in seconds) - current_time = time.time() + _UTC_OFFSET - return time.localtime(current_time) - - -def get_datetime_string(format='full'): - """ - Return date/time as string with different formats + # Set timezone offset (in seconds) + current_time = time.time() + _UTC_OFFSET + return time.localtime(current_time) - Args: - format: "full", "date", "time", "short", "ticks" - """ - try: - year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset() - if format == 'full': - return f'{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}' - elif format == 'date': - return f'{year:04d}-{month:02d}-{day:02d}' - elif format == 'time': - return f'{hour:02d}:{minute:02d}:{second:02d}' - elif format == 'short': - return f'{month:02d}/{day:02d} {hour:02d}:{minute:02d}' - else: - return f'Ticks: {time.ticks_ms()} ms' +def get_datetime_string(format="full"): + """ + Return date/time as string with different formats - except: - return f'Ticks: {time.ticks_ms()} ms' + Args: + format: "full", "date", "time", "short", "ticks" + """ + try: + year, month, day, hour, minute, second, weekday, yearday = ( + local_time_with_offset() + ) + + if format == "full": + return ( + f"{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}" + ) + elif format == "date": + return f"{year:04d}-{month:02d}-{day:02d}" + elif format == "time": + return f"{hour:02d}:{minute:02d}:{second:02d}" + elif format == "short": + return f"{month:02d}/{day:02d} {hour:02d}:{minute:02d}" + else: + return f"Ticks: {time.ticks_ms()} ms" + + except: + return f"Ticks: {time.ticks_ms()} ms" def get_german_datetime(): - """Return German date and time""" - try: - year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset() + """Return German date and time""" + try: + year, month, day, hour, minute, second, weekday, yearday = ( + local_time_with_offset() + ) - weekdays = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'] - weekday_name = weekdays[weekday] + weekdays = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"] + weekday_name = weekdays[weekday] - return f'{weekday_name}.{day:02d}.{month:02d}.{year} {hour:02d}:{minute:02d}' + return f"{weekday_name}.{day:02d}.{month:02d}.{year} {hour:02d}:{minute:02d}" - except: - ticks = time.ticks_ms() // 1000 - return f'Zeit: {ticks} sek' + except: + ticks = time.ticks_ms() // 1000 + return f"Zeit: {ticks} sek" def get_german_timestamp_short(): - """Get German timestamp with short months (for Wokwi)""" - ticks = time.ticks_ms() - day = (ticks // 86400000) % 31 + 1 - month = (ticks // 2592000000) % 12 + 1 - hour = (ticks // 3600000) % 24 - minute = (ticks // 60000) % 60 + """Get German timestamp with short months (for Wokwi)""" + ticks = time.ticks_ms() + day = (ticks // 86400000) % 31 + 1 + month = (ticks // 2592000000) % 12 + 1 + hour = (ticks // 3600000) % 24 + minute = (ticks // 60000) % 60 - months = ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'] - month_name = months[month - 1] + months = [ + "Jan", + "Feb", + "Mär", + "Apr", + "Mai", + "Jun", + "Jul", + "Aug", + "Sep", + "Okt", + "Nov", + "Dez", + ] + month_name = months[month - 1] - return f'{day}.{month_name} {hour:02d}:{minute:02d}' + return f"{day}.{month_name} {hour:02d}:{minute:02d}" def get_german_time_ticks(): - """Get German time using ticks (works in Wokwi)""" - ticks = time.ticks_ms() + """Get German time using ticks (works in Wokwi)""" + ticks = time.ticks_ms() - # Simulate time progression - total_seconds = ticks // 1000 - hours = (total_seconds // 3600) % 24 - minutes = (total_seconds // 60) % 60 - seconds = total_seconds % 60 + # Simulate time progression + total_seconds = ticks // 1000 + hours = (total_seconds // 3600) % 24 + minutes = (total_seconds // 60) % 60 + seconds = total_seconds % 60 - return f'{hours:02d}:{minutes:02d}:{seconds:02d}' + return f"{hours:02d}:{minutes:02d}:{seconds:02d}" def get_german_date_ticks(): - """Get German date using ticks""" - ticks = time.ticks_ms() + """Get German date using ticks""" + ticks = time.ticks_ms() - # Simulate date progression (starting from Jan 15, 2024) - days_passed = ticks // (24 * 3600 * 1000) - day = 15 + (days_passed % 28) # Simple month simulation - month = 1 + (days_passed // 28) % 12 - year = 2024 + (days_passed // (28 * 12)) + # Simulate date progression (starting from Jan 15, 2024) + days_passed = ticks // (24 * 3600 * 1000) + day = 15 + (days_passed % 28) # Simple month simulation + month = 1 + (days_passed // 28) % 12 + year = 2024 + (days_passed // (28 * 12)) - months = ['JAN', 'FEB', 'MAR', 'APR', 'MAI', 'JUN', 'JUL', 'AUG', 'SEP', 'OKT', 'NOV', 'DEZ'] - month_name = months[month - 1] + months = [ + "JAN", + "FEB", + "MAR", + "APR", + "MAI", + "JUN", + "JUL", + "AUG", + "SEP", + "OKT", + "NOV", + "DEZ", + ] + month_name = months[month - 1] + + return f"{day:02d}.{month_name}.{str(year)[-2:]}" - return f'{day:02d}.{month_name}.{str(year)[-2:]}' def sync_ntp_time(): try: @@ -99,4 +133,4 @@ def sync_ntp_time(): return True except Exception as e: print("NTP sync failed:", e) - return False \ No newline at end of file + return False diff --git a/main.py b/main.py index ed50dcf..d74f8e4 100644 --- a/main.py +++ b/main.py @@ -2,59 +2,72 @@ from app.display import NeoPixel_64x64 from app.display.fonts import font_5x7 from app.tryout import Font_Checker, Weather_Checker, Emoji_Checker from app.utils import show_system_load -from app.utils import sync_ntp_time, get_datetime_string, get_german_datetime # Time-related functions +from app.utils import ( + sync_ntp_time, + get_datetime_string, + get_german_datetime, +) # Time-related functions from app.utils import SimpleCounter -from app.web import Wlan import time -import uasyncio as asyncio # type: ignore +import uasyncio as asyncio # type: ignore +from app.web import Wlan -CITY_LIST: list[str]= sorted(["Großhansdorf","Columbus", "London", "Ebeltoft", "Tokio"]) +CITY_LIST: list[str] = sorted( + ["Großhansdorf", "Columbus", "London", "Ebeltoft", "Tokio"] +) -async def weather_check_task(weather_checker:Weather_Checker): + +async def weather_check_task(weather_checker: Weather_Checker): while True: for city in CITY_LIST: weather_checker.check(city=city, lang="de", test_mode=False) print(f"Checked {city}") await asyncio.sleep(3 * 60) # Non-blocking sleep + async def print_time_task() -> None: simpleCnt: SimpleCounter = SimpleCounter() while True: - dt1:int = get_datetime_string() - dt2:int = get_german_datetime() - simpleCnt+=1 - print(f"print_time_task running... {simpleCnt.value%16} {dt1} {dt2}") + dt1: int = get_datetime_string() + dt2: int = get_german_datetime() + simpleCnt += 1 + print(f"print_time_task running... {simpleCnt.value % 16} {dt1} {dt2}") await asyncio.sleep(10) - - -async def main(weather_checker:Weather_Checker)->None: + + +async def sync_ntp_time_task() -> None: + while True: + sync_ntp_time() + await asyncio.sleep(60) + + +async def main(weather_checker: Weather_Checker) -> None: # Run both tasks concurrently - await asyncio.gather(weather_check_task(weather_checker), print_time_task()) - + await asyncio.gather( + sync_ntp_time_task(), weather_check_task(weather_checker), print_time_task() + ) + + # Programm Startpunkt -if __name__ == '__main__': - display = NeoPixel_64x64() - wlan: Wlan = Wlan( ) - wlan.connect( ssid="Wokwi-Wlan", password="12345678") +if __name__ == "__main__": + display = NeoPixel_64x64() + wlan: Wlan = Wlan() + wlan.connect(ssid="Wokwi-Wlan", password="12345678") - sync_ntp_time() + # font_checker : Font_Checker = Font_Checker( display) + # font_checker.fonts_check(pretty=False) - # font_checker : Font_Checker = Font_Checker( display) - # font_checker.fonts_check(pretty=False) + # emoji_checker : Emoji_Checker = Emoji_Checker(display) + # emoji_checker.check() - # emoji_checker : Emoji_Checker = Emoji_Checker(display) - # emoji_checker.check() + # tryout.weather_check(display, test_mode=False) + display.set_font(font_5x7) + weather_checker: Weather_Checker = Weather_Checker(display=display) - # tryout.weather_check(display, test_mode=False) - display.set_font(font_5x7) - weather_checker: Weather_Checker = Weather_Checker( display=display) - - - - # while True: - # for city in sorted(CITY_LIST): - # weather_checker.check(city=city, lang="de", test_mode=False) - # time.sleep(5*60) + # while True: + # for city in sorted(CITY_LIST): + # weather_checker.check(city=city, lang="de", test_mode=False) + # time.sleep(5*60) - # show_system_load() - asyncio.run(main(weather_checker)) + # show_system_load() + asyncio.run(main(weather_checker))