#include <TimeManager.h>
Public Member Functions | |
TimeManager ()=default | |
bool | syncTimeWithNtp (int timeoutSeconds=20) |
Initialize the time manager. | |
void | setTimeFromEpoch (uint32_t epochSeconds) |
Set the system time from an epoch timestamp (e.g. from SNTP) | |
void | fetchAndApplyTimezoneFromOpenMeteo (float lat, float lon, const std::string &tzName) |
Fetch and apply the timezone from OpenMeteo API using latitude and longitude. | |
void | detectAndApplyTimezone () |
Detect and apply the timezone based on the current location or IP address. | |
void | applyFixedTimezoneOffset (int offsetSeconds, const char *stdName="UTC", const char *dstName="UTC") |
Apply a fixed timezone offset. | |
int | getTimezoneOffset () const |
Get the current timezone offset in seconds. | |
const char * | getTimezoneName () const |
Get the name of the current timezone. | |
std::string | formatTimeWithZone (time_t rawTime=0) const |
std::string | currentTimeForTrace () const |
Get the current time formatted as a string. | |
void | setTime (timespec *ts) |
Set the system time using a timespec structure. | |
bool | isTimeSynced () const |
Returns true if system time has been synced with NTP servers. | |
bool | isTimeValid () |
void | start () |
Start the time manager. | |
void | onNetworkReady () |
hanles network ready event. | |
void | onHttpServerStarted () |
Private Member Functions | |
void | initNtpClient () |
bool | getLocationFromIp (std::string &tzName, float &lat, float &lon) |
Private Attributes | |
bool | timeSynced = false |
int | timezoneOffsetSeconds = 0 |
std::string | timezoneName = "UTC" |
Definition at line 10 of file TimeManager.h.
|
default |
void TimeManager::applyFixedTimezoneOffset | ( | int | offsetSeconds, |
const char * | stdName = "UTC" , |
||
const char * | dstName = "UTC" |
||
) |
offsetSeconds | Offset in seconds from UTC. |
stdName | Standard timezone name (default "UTC"). |
dstName | Daylight Saving Time name (default "UTC"). |
Definition at line 148 of file TimeManager.cpp.
References eventMask(), AppContext::getTypeKey(), LocalTimeValid, timezoneName, and timezoneOffsetSeconds.
Referenced by detectAndApplyTimezone(), fetchAndApplyTimezoneFromOpenMeteo(), and onHttpServerStarted().
std::string TimeManager::currentTimeForTrace | ( | ) | const |
This will return the current time timezone information.
Definition at line 276 of file TimeManager.cpp.
References formatTimeWithZone(), and PicoTime::now().
Referenced by getFormattedTimestamp().
void TimeManager::detectAndApplyTimezone | ( | ) |
This will attempt to determine the timezone automatically.
Definition at line 239 of file TimeManager.cpp.
References applyFixedTimezoneOffset(), fetchAndApplyTimezoneFromOpenMeteo(), and getLocationFromIp().
Referenced by onHttpServerStarted().
void TimeManager::fetchAndApplyTimezoneFromOpenMeteo | ( | float | lat, |
float | lon, | ||
const std::string & | tzName | ||
) |
lat | Latitude of the location. |
lon | Longitude of the location. |
tzName | Optional timezone name to apply if available. |
Definition at line 205 of file TimeManager.cpp.
References applyFixedTimezoneOffset(), HttpRequest::get(), HttpResponse::getBody(), HttpResponse::getStatusCode(), and TRACE.
Referenced by detectAndApplyTimezone().
std::string TimeManager::formatTimeWithZone | ( | time_t | rawTime = 0 | ) | const |
Definition at line 256 of file TimeManager.cpp.
References gmtime_r(), PicoTime::now(), timezoneName, and timezoneOffsetSeconds.
Referenced by currentTimeForTrace().
|
private |
Definition at line 161 of file TimeManager.cpp.
References HttpRequest::get(), and HttpResponse::getBody().
Referenced by detectAndApplyTimezone().
|
inline |
Definition at line 58 of file TimeManager.h.
References timezoneName.
|
inline |
Definition at line 52 of file TimeManager.h.
References timezoneOffsetSeconds.
|
private |
Definition at line 39 of file TimeManager.cpp.
Referenced by onNetworkReady().
|
inline |
Note that sntp syncing is setup for every hour by default - this can be changed in framework_config.h This retrieves the current time in a timespec format.
Definition at line 82 of file TimeManager.h.
References timeSynced.
|
inline |
Definition at line 84 of file TimeManager.h.
Referenced by onNetworkReady(), start(), and syncTimeWithNtp().
void TimeManager::onHttpServerStarted | ( | ) |
Definition at line 297 of file TimeManager.cpp.
References applyFixedTimezoneOffset(), DETECT_LOCAL_TIMEZONE, and detectAndApplyTimezone().
void TimeManager::onNetworkReady | ( | ) |
This is called when the network is ready and the time manager can start syncing time.
Definition at line 287 of file TimeManager.cpp.
References initNtpClient(), isTimeValid(), NTP_TIMEOUT_SECONDS, and syncTimeWithNtp().
Referenced by FrameworkManager::onStart().
void TimeManager::setTime | ( | timespec * | ts | ) |
This is useful for setting the time directly from a timespec object.
ts | Pointer to a timespec structure containing the time to set. |
This function sets the system time using the provided timespec structure. It initializes the RTC if available and sets the always-on timer to the specified time. If the always-on timer is already running, it does nothing.
ts | Pointer to a timespec structure containing the time to set. |
Definition at line 119 of file TimeManager.cpp.
References AppContext::getTypeKey(), and TimeValid.
Referenced by setTimeFromEpoch().
void TimeManager::setTimeFromEpoch | ( | uint32_t | epochSeconds | ) |
Definition at line 96 of file TimeManager.cpp.
References AppContext::getTypeKey(), Event::notification, setTime(), TimeSync, and timeSynced.
void TimeManager::start | ( | ) |
It will check if time is valid and post event if it is
Definition at line 281 of file TimeManager.cpp.
References AppContext::getTypeKey(), isTimeValid(), and TimeValid.
Referenced by FrameworkManager::onStart().
bool TimeManager::syncTimeWithNtp | ( | int | timeoutSeconds = 20 | ) |
This sets up the SNTP client and attempts to sync time with NTP servers.
Definition at line 50 of file TimeManager.cpp.
References AppContext::getTypeKey(), isTimeValid(), Event::notification, TimeInvalid, and timeSynced.
Referenced by onNetworkReady().
|
private |
Definition at line 102 of file TimeManager.h.
Referenced by isTimeSynced(), setTimeFromEpoch(), and syncTimeWithNtp().
|
private |
Definition at line 106 of file TimeManager.h.
Referenced by applyFixedTimezoneOffset(), formatTimeWithZone(), and getTimezoneName().
|
private |
Definition at line 105 of file TimeManager.h.
Referenced by applyFixedTimezoneOffset(), formatTimeWithZone(), and getTimezoneOffset().