v.0.8.2 sync_ntp_time_task
This commit is contained in:
@@ -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",
|
||||||
|
|||||||
@@ -1,95 +1,129 @@
|
|||||||
import time
|
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
|
_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'):
|
|
||||||
"""
|
|
||||||
Return date/time as string with different formats
|
|
||||||
|
|
||||||
Args:
|
|
||||||
format: "full", "date", "time", "short", "ticks"
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset()
|
|
||||||
|
|
||||||
if format == 'full':
|
def get_datetime_string(format="full"):
|
||||||
return f'{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}'
|
"""
|
||||||
elif format == 'date':
|
Return date/time as string with different formats
|
||||||
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:
|
Args:
|
||||||
return f'Ticks: {time.ticks_ms()} ms'
|
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():
|
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():
|
||||||
"""Get German timestamp with short months (for Wokwi)"""
|
"""Get German timestamp with short months (for Wokwi)"""
|
||||||
ticks = time.ticks_ms()
|
ticks = time.ticks_ms()
|
||||||
day = (ticks // 86400000) % 31 + 1
|
day = (ticks // 86400000) % 31 + 1
|
||||||
month = (ticks // 2592000000) % 12 + 1
|
month = (ticks // 2592000000) % 12 + 1
|
||||||
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 = [
|
||||||
month_name = months[month - 1]
|
"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():
|
def get_german_time_ticks():
|
||||||
"""Get German time using ticks (works in Wokwi)"""
|
"""Get German time using ticks (works in Wokwi)"""
|
||||||
ticks = time.ticks_ms()
|
ticks = time.ticks_ms()
|
||||||
|
|
||||||
# Simulate time progression
|
# Simulate time progression
|
||||||
total_seconds = ticks // 1000
|
total_seconds = ticks // 1000
|
||||||
hours = (total_seconds // 3600) % 24
|
hours = (total_seconds // 3600) % 24
|
||||||
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():
|
||||||
"""Get German date using ticks"""
|
"""Get German date using ticks"""
|
||||||
ticks = time.ticks_ms()
|
ticks = time.ticks_ms()
|
||||||
|
|
||||||
# Simulate date progression (starting from Jan 15, 2024)
|
# Simulate date progression (starting from Jan 15, 2024)
|
||||||
days_passed = ticks // (24 * 3600 * 1000)
|
days_passed = ticks // (24 * 3600 * 1000)
|
||||||
day = 15 + (days_passed % 28) # Simple month simulation
|
day = 15 + (days_passed % 28) # Simple month simulation
|
||||||
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 = [
|
||||||
month_name = months[month - 1]
|
"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():
|
def sync_ntp_time():
|
||||||
try:
|
try:
|
||||||
@@ -99,4 +133,4 @@ def sync_ntp_time():
|
|||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("NTP sync failed:", e)
|
print("NTP sync failed:", e)
|
||||||
return False
|
return False
|
||||||
|
|||||||
83
main.py
83
main.py
@@ -2,59 +2,72 @@ 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.fonts_check(pretty=False)
|
||||||
|
|
||||||
# font_checker : Font_Checker = Font_Checker( display)
|
# emoji_checker : Emoji_Checker = Emoji_Checker(display)
|
||||||
# font_checker.fonts_check(pretty=False)
|
# emoji_checker.check()
|
||||||
|
|
||||||
# emoji_checker : Emoji_Checker = Emoji_Checker(display)
|
# tryout.weather_check(display, test_mode=False)
|
||||||
# emoji_checker.check()
|
display.set_font(font_5x7)
|
||||||
|
weather_checker: Weather_Checker = Weather_Checker(display=display)
|
||||||
|
|
||||||
# tryout.weather_check(display, test_mode=False)
|
# while True:
|
||||||
display.set_font(font_5x7)
|
# for city in sorted(CITY_LIST):
|
||||||
weather_checker: Weather_Checker = Weather_Checker( display=display)
|
# 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()
|
# show_system_load()
|
||||||
asyncio.run(main(weather_checker))
|
asyncio.run(main(weather_checker))
|
||||||
|
|||||||
Reference in New Issue
Block a user