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": {
"settings": {},
"extensions": [
"streetsidesoftware.code-spell-checker",
"ms-python.python",
"frhtylcn.pythonsnippets",
"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
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'):
def get_datetime_string(format="full"):
"""
Return date/time as string with different formats
@@ -17,36 +18,42 @@ def get_datetime_string(format='full'):
format: "full", "date", "time", "short", "ticks"
"""
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':
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}'
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'
return f"Ticks: {time.ticks_ms()} ms"
except:
return f'Ticks: {time.ticks_ms()} ms'
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()
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]
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'
return f"Zeit: {ticks} sek"
def get_german_timestamp_short():
@@ -57,10 +64,23 @@ def get_german_timestamp_short():
hour = (ticks // 3600000) % 24
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]
return f'{day}.{month_name} {hour:02d}:{minute:02d}'
return f"{day}.{month_name} {hour:02d}:{minute:02d}"
def get_german_time_ticks():
@@ -73,7 +93,7 @@ def get_german_time_ticks():
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():
@@ -86,10 +106,24 @@ def get_german_date_ticks():
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']
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:

31
main.py
View File

@@ -2,13 +2,20 @@ 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
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):
while True:
@@ -17,6 +24,7 @@ async def weather_check_task(weather_checker:Weather_Checker):
print(f"Checked {city}")
await asyncio.sleep(3 * 60) # Non-blocking sleep
async def print_time_task() -> None:
simpleCnt: SimpleCounter = SimpleCounter()
while True:
@@ -27,18 +35,25 @@ async def print_time_task() -> None:
await asyncio.sleep(10)
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__':
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)
@@ -49,8 +64,6 @@ if __name__ == '__main__':
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)