v.0.7.3 time_utils ntp access
This commit is contained in:
@@ -4,7 +4,7 @@ import json
|
||||
from app.classes import Weather, Location, CurrentCondition, WeatherResponse, ResponseStatus
|
||||
from app.display import NeoPixel_64x64
|
||||
import app.utils.colors as colors
|
||||
from app.utils import URLEncoder, http_message
|
||||
from app.utils import URLEncoder, http_message, get_datetime_string
|
||||
|
||||
API_KEY = '3545ce42d0ba436e8dc164532250410'
|
||||
ACTUAL_WEATHER_URL = 'http://api.weatherapi.com/v1/current.json?key={API_KEY}&q={city}&aqi=yes&lang={lang}'
|
||||
@@ -72,8 +72,12 @@ class Weather_Checker():
|
||||
|
||||
|
||||
def check(self, city:str, lang:str, test_mode: bool = False):
|
||||
bottom_ypos = self.display.MATRIX_HEIGHT - self.display.font_height
|
||||
|
||||
time_str: str = get_datetime_string('time')
|
||||
try:
|
||||
self.display.clear()
|
||||
self.display.write_text(time_str[:5], 0, bottom_ypos, color=colors.NEON_YELLOW)
|
||||
|
||||
city_encoded = URLEncoder.encode_utf8(city) # url_encode
|
||||
w_resp: WeatherResponse = self.actual_weather(city=city_encoded, lang=lang, test_mode=test_mode)
|
||||
@@ -103,9 +107,8 @@ class Weather_Checker():
|
||||
self.display.write_text(f'{w_resp.response_status.error_text}', 0, ypos, color=colors.RAINBOW[2])
|
||||
|
||||
# unten rechts in die Ecke
|
||||
ypos = self.display.MATRIX_HEIGHT - self.display.font_height - 1
|
||||
|
||||
self.display.write_text('done.', 38, ypos, color=colors.NEON_GREEN)
|
||||
self.display.write_text('done.', 39, bottom_ypos, color=colors.NEON_GREEN)
|
||||
except OSError as e:
|
||||
print(f'Error: connection closed - {e}')
|
||||
finally:
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
import time
|
||||
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
|
||||
@@ -8,7 +17,7 @@ def get_datetime_string(format='full'):
|
||||
format: "full", "date", "time", "short", "ticks"
|
||||
"""
|
||||
try:
|
||||
year, month, day, hour, minute, second, weekday, yearday = time.localtime()
|
||||
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}'
|
||||
@@ -28,7 +37,7 @@ def get_datetime_string(format='full'):
|
||||
def get_german_datetime():
|
||||
"""Return German date and time"""
|
||||
try:
|
||||
year, month, day, hour, minute, second, weekday, yearday = time.localtime()
|
||||
year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset()
|
||||
|
||||
weekdays = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']
|
||||
weekday_name = weekdays[weekday]
|
||||
@@ -81,3 +90,13 @@ def get_german_date_ticks():
|
||||
month_name = months[month - 1]
|
||||
|
||||
return f'{day:02d}.{month_name}.{str(year)[-2:]}'
|
||||
|
||||
def sync_ntp_time():
|
||||
try:
|
||||
print("Syncing time via NTP...")
|
||||
ntptime.settime() # Default uses pool.ntp.org
|
||||
print("Time synchronized successfully!")
|
||||
return True
|
||||
except Exception as e:
|
||||
print("NTP sync failed:", e)
|
||||
return False
|
||||
7
main.py
7
main.py
@@ -1,7 +1,7 @@
|
||||
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 show_system_load, sync_ntp_time, get_datetime_string, get_german_datetime
|
||||
from app.web import Wlan
|
||||
import time
|
||||
|
||||
@@ -13,6 +13,11 @@ if __name__ == '__main__':
|
||||
wlan: Wlan = Wlan( )
|
||||
wlan.connect( ssid="Wokwi-Wlan", password="12345678")
|
||||
|
||||
if sync_ntp_time():
|
||||
time_str: str = get_datetime_string('time')
|
||||
ger_time_str: str = get_german_datetime()
|
||||
print( f"time is: {time_str[:5]} & german: {ger_time_str}" )
|
||||
|
||||
# font_checker : Font_Checker = Font_Checker( display)
|
||||
# font_checker.fonts_check(pretty=False)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user