v.0.8.2 sync_ntp_time_task

This commit is contained in:
tiijay
2025-11-18 16:45:13 +00:00
parent a748117ac5
commit c817f0330a
3 changed files with 145 additions and 99 deletions

View File

@@ -17,7 +17,6 @@
"vscode": { "vscode": {
"settings": {}, "settings": {},
"extensions": [ "extensions": [
"streetsidesoftware.code-spell-checker",
"ms-python.python", "ms-python.python",
"frhtylcn.pythonsnippets", "frhtylcn.pythonsnippets",
"kevinrose.vsc-python-indent", "kevinrose.vsc-python-indent",

View File

@@ -3,13 +3,14 @@ import ntptime # type: ignore
_UTC_OFFSET: int = 1 * 3600 # +1 or +2 hours for CEST, adjust for your timezone _UTC_OFFSET: int = 1 * 3600 # +1 or +2 hours for CEST, adjust for your timezone
def local_time_with_offset(): def local_time_with_offset():
# Set timezone offset (in seconds) # Set timezone offset (in seconds)
current_time = time.time() + _UTC_OFFSET current_time = time.time() + _UTC_OFFSET
return time.localtime(current_time) return time.localtime(current_time)
def get_datetime_string(format='full'): def get_datetime_string(format="full"):
""" """
Return date/time as string with different formats Return date/time as string with different formats
@@ -17,36 +18,42 @@ def get_datetime_string(format='full'):
format: "full", "date", "time", "short", "ticks" format: "full", "date", "time", "short", "ticks"
""" """
try: try:
year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset() year, month, day, hour, minute, second, weekday, yearday = (
local_time_with_offset()
)
if format == 'full': if format == "full":
return f'{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}' return (
elif format == 'date': f"{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}"
return f'{year:04d}-{month:02d}-{day:02d}' )
elif format == 'time': elif format == "date":
return f'{hour:02d}:{minute:02d}:{second:02d}' return f"{year:04d}-{month:02d}-{day:02d}"
elif format == 'short': elif format == "time":
return f'{month:02d}/{day:02d} {hour:02d}:{minute:02d}' return f"{hour:02d}:{minute:02d}:{second:02d}"
elif format == "short":
return f"{month:02d}/{day:02d} {hour:02d}:{minute:02d}"
else: else:
return f'Ticks: {time.ticks_ms()} ms' return f"Ticks: {time.ticks_ms()} ms"
except: except:
return f'Ticks: {time.ticks_ms()} ms' return f"Ticks: {time.ticks_ms()} ms"
def get_german_datetime(): def get_german_datetime():
"""Return German date and time""" """Return German date and time"""
try: try:
year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset() year, month, day, hour, minute, second, weekday, yearday = (
local_time_with_offset()
)
weekdays = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'] weekdays = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"]
weekday_name = weekdays[weekday] 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: except:
ticks = time.ticks_ms() // 1000 ticks = time.ticks_ms() // 1000
return f'Zeit: {ticks} sek' return f"Zeit: {ticks} sek"
def get_german_timestamp_short(): def get_german_timestamp_short():
@@ -57,10 +64,23 @@ def get_german_timestamp_short():
hour = (ticks // 3600000) % 24 hour = (ticks // 3600000) % 24
minute = (ticks // 60000) % 60 minute = (ticks // 60000) % 60
months = ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'] months = [
"Jan",
"Feb",
"Mär",
"Apr",
"Mai",
"Jun",
"Jul",
"Aug",
"Sep",
"Okt",
"Nov",
"Dez",
]
month_name = months[month - 1] 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(): def get_german_time_ticks():
@@ -73,7 +93,7 @@ def get_german_time_ticks():
minutes = (total_seconds // 60) % 60 minutes = (total_seconds // 60) % 60
seconds = total_seconds % 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(): def get_german_date_ticks():
@@ -86,10 +106,24 @@ def get_german_date_ticks():
month = 1 + (days_passed // 28) % 12 month = 1 + (days_passed // 28) % 12
year = 2024 + (days_passed // (28 * 12)) year = 2024 + (days_passed // (28 * 12))
months = ['JAN', 'FEB', 'MAR', 'APR', 'MAI', 'JUN', 'JUL', 'AUG', 'SEP', 'OKT', 'NOV', 'DEZ'] months = [
"JAN",
"FEB",
"MAR",
"APR",
"MAI",
"JUN",
"JUL",
"AUG",
"SEP",
"OKT",
"NOV",
"DEZ",
]
month_name = months[month - 1] 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(): def sync_ntp_time():
try: try:

49
main.py
View File

@@ -2,42 +2,57 @@ from app.display import NeoPixel_64x64
from app.display.fonts import font_5x7 from app.display.fonts import font_5x7
from app.tryout import Font_Checker, Weather_Checker, Emoji_Checker from app.tryout import Font_Checker, Weather_Checker, Emoji_Checker
from app.utils import show_system_load 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.utils import SimpleCounter
from app.web import Wlan
import time 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: while True:
for city in CITY_LIST: for city in CITY_LIST:
weather_checker.check(city=city, lang="de", test_mode=False) weather_checker.check(city=city, lang="de", test_mode=False)
print(f"Checked {city}") print(f"Checked {city}")
await asyncio.sleep(3 * 60) # Non-blocking sleep await asyncio.sleep(3 * 60) # Non-blocking sleep
async def print_time_task() -> None: async def print_time_task() -> None:
simpleCnt: SimpleCounter = SimpleCounter() simpleCnt: SimpleCounter = SimpleCounter()
while True: while True:
dt1:int = get_datetime_string() dt1: int = get_datetime_string()
dt2:int = get_german_datetime() dt2: int = get_german_datetime()
simpleCnt+=1 simpleCnt += 1
print(f"print_time_task running... {simpleCnt.value%16} {dt1} {dt2}") print(f"print_time_task running... {simpleCnt.value % 16} {dt1} {dt2}")
await asyncio.sleep(10) 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 # 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 # Programm Startpunkt
if __name__ == '__main__': if __name__ == "__main__":
display = NeoPixel_64x64() display = NeoPixel_64x64()
wlan: Wlan = Wlan( ) wlan: Wlan = Wlan()
wlan.connect( ssid="Wokwi-Wlan", password="12345678") wlan.connect(ssid="Wokwi-Wlan", password="12345678")
sync_ntp_time()
# font_checker : Font_Checker = Font_Checker( display) # font_checker : Font_Checker = Font_Checker( display)
# font_checker.fonts_check(pretty=False) # font_checker.fonts_check(pretty=False)
@@ -47,9 +62,7 @@ if __name__ == '__main__':
# tryout.weather_check(display, test_mode=False) # tryout.weather_check(display, test_mode=False)
display.set_font(font_5x7) display.set_font(font_5x7)
weather_checker: Weather_Checker = Weather_Checker( display=display) weather_checker: Weather_Checker = Weather_Checker(display=display)
# while True: # while True:
# for city in sorted(CITY_LIST): # for city in sorted(CITY_LIST):