From 5d59840036631411b4bd20c4cc964e348dd35cda Mon Sep 17 00:00:00 2001 From: CodeGoat Date: Mon, 27 Jan 2025 19:24:42 +0000 Subject: [PATCH] Feat(Time Sync): Use new internal API Uses a new internally hosted time API for date and time synchronisation. --- changes.md | 8 ++++++++ readme.md | 2 +- src/NetworkManager.py | 17 ++++++++--------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/changes.md b/changes.md index 43cf82e..aabd06f 100644 --- a/changes.md +++ b/changes.md @@ -2,6 +2,14 @@ This document outlines the changes made between versions of the **Goat - Pico Network Manager** library. +## V1.0.4 + +### Changes + +#### Date And Time Synchronisation + +Date and time synchronisation now uses a new internal API to improve reliability. + ## V1.0.3 ### New Features diff --git a/readme.md b/readme.md index d6e23e9..6d688b4 100644 --- a/readme.md +++ b/readme.md @@ -59,7 +59,7 @@ Pico Network Manager comes packed with the following features: Configure a web interface for your firmware to be used when connected to a network. - **Time Synchronisation** - Enable automatic time synchronisation from World Time API when connected to wi-fi. + Enable automatic time synchronisation from our own internally hosted time API when connected to wi-fi. --- diff --git a/src/NetworkManager.py b/src/NetworkManager.py index 7f8ef41..e7a16a3 100644 --- a/src/NetworkManager.py +++ b/src/NetworkManager.py @@ -442,7 +442,7 @@ def serve_index(self): async def get_ntp_time(self): """Fetches the current date and time from an NTP server or time API and sets the system time.""" - url = "http://worldtimeapi.org/api/ip" + url = "https://goatbot.org/api/time" try: print("Fetching time from API...") @@ -452,16 +452,15 @@ async def get_ntp_time(self): data = response.json() # Extract datetime string from response - datetime_str = data['datetime'] + datetime_str = data['currentTime'] - # Parse datetime string: "2025-01-26T12:34:56.789123+00:00" - date_time = datetime_str.split('T') - date = date_time[0].split('-') - time_ = date_time[1].split(':') + if not datetime_str: + raise ValueError("Missing 'currentTime' in API response.") - # Extract year, month, day, hour, minute, second - year, month, day = map(int, date) - hour, minute, second = map(int, time_[:2]) + # Parse datetime string: "2025-01-26T12:34:56" + date_part, time_part = datetime_str.split('T') + year, month, day = map(int, date_part.split('-')) + hour, minute, second = map(int, time_part.split(':')) # Set the system time time_tuple = (year, month, day, hour, minute, second, 0, 0, 0)