36#include <ESP8266WiFi.h>
114 _setTime(currentEpoch);
129 if (!this->
store()) {
156 auto epoch = mSNTP.getNtpTime();
184 settimeofday_cb([](
bool) {
188 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
189 sntp_set_time_sync_notification_cb([](
struct timeval *) {
193 mSNTP.setSyncTimeCallback([](time_t) {
208 void _timeSyncCallback() {
223 bool _setTime(time_t epoch) {
225 tune_timeshift64(epoch * 1000000ULL);
227 timeval tv = {epoch, 0};
228 if (settimeofday(&tv,
nullptr) != 0) {
241 void _reconfigure() {
251 return time(
nullptr);
262 String _getFormattedTime() {
264 auto currentEpoc = this->_now();
265 localtime_r(¤tEpoc, &tm);
267 char formattedTime[72] = {0};
268 snprintf(formattedTime,
sizeof(formattedTime),
"%04d-%02d-%02d %02d:%02d:%02d",
269 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
270 tm.tm_hour, tm.tm_min, tm.tm_sec);
272 return String(formattedTime);
Date and time event definitions for the Uniot event system.
long getInt(int key) const
Get an integer value at a specific integer key.
Definition CBORObject.h:409
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:180
Definition CBORStorage.h:39
virtual bool restore() override
Restore the CBOR object from the filesystem.
Definition CBORStorage.h:102
virtual bool store() override
Store the CBOR object to the filesystem.
Definition CBORStorage.h:74
CBORStorage(const String &path)
Constructs a new CBORStorage object.
Definition CBORStorage.h:46
CBORObject & object()
Get access to the underlying CBORObject.
Definition CBORStorage.h:60
TaskScheduler::TaskPtr getSyncTask()
Get the task that stores and reports time synchronizations.
Definition Date.h:143
virtual void execute(short _) override
Periodic execution callback.
Definition Date.h:128
bool restore() override
Restores time from persistent storage.
Definition Date.h:111
static String getFormattedTime()
Gets the current time in human-readable format.
Definition Date.h:84
bool store() override
Stores the current time to persistent storage.
Definition Date.h:97
void forceSync()
Forces immediate NTP time synchronization.
Definition Date.h:154
static time_t now()
Returns the current Unix timestamp.
Definition Date.h:75
void emitEvent(unsigned int topic, int msg)
Interface for executing tasks in the scheduler system.
Definition IExecutor.h:31
Definition TaskScheduler.h:67
static constexpr const char * servers[]
Array of NTP server hostnames to try connecting to.
Definition SimpleNTP.h:34
Singleton(const Singleton &)=delete
static Date & getInstance()
Definition Singleton.h:73
EventEmitter< unsigned int, int, Bytes > CoreEventEmitter
A specialized EventEmitter for core system events.
Definition EventEmitter.h:78
#define UNIOT_LOG_INFO(...)
Log an INFO level message Used for general information about system operation. Only compiled if UNIOT...
Definition Logger.h:315
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:273
SharedPointer< SchedulerTask > TaskPtr
Shared pointer type for scheduler tasks.
Definition TaskScheduler.h:190
static TaskPtr make(SchedulerTask::SchedulerTaskCallback callback)
Static factory method to create a task with a callback.
Definition TaskScheduler.h:212
@ TIME
Time synchronization and date-related operations.
Definition DateEvents.h:61
@ SYNCED
System time has been successfully synchronized with time source.
Definition DateEvents.h:72
@ SYNC_FAILED
Forced NTP synchronization attempt failed (no response from any server)
Definition DateEvents.h:73
Contains all classes and functions related to the Uniot Core.