36#include <ESP8266WiFi.h>
113 _setTime(currentEpoch);
128 if (!this->
store()) {
141 auto epoch = mSNTP.getNtpTime();
156 settimeofday_cb([
this](
bool from_sntp) {
158 UNIOT_LOG_INFO(
"Time is set from %s", from_sntp ?
"SNTP" :
"RTC");
161 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
162 sntp_set_time_sync_notification_cb([](
struct timeval *tv) {
167 mSNTP.setSyncTimeCallback([](time_t epoch) {
183 void _timeSyncCallback() {
197 bool _setTime(time_t epoch) {
199 tune_timeshift64(epoch * 1000000ULL);
201 timeval tv = {epoch, 0};
202 if (settimeofday(&tv,
nullptr) != 0) {
215 void _reconfigure() {
225 return time(
nullptr);
236 String _getFormattedTime() {
238 auto currentEpoc = this->_now();
239 localtime_r(¤tEpoc, &tm);
241 char formattedTime[72] = {0};
242 snprintf(formattedTime,
sizeof(formattedTime),
"%04d-%02d-%02d %02d:%02d:%02d",
243 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
244 tm.tm_hour, tm.tm_min, tm.tm_sec);
246 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:399
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:170
Definition CBORStorage.h:39
virtual bool restore() override
Restore the CBOR object from the filesystem.
Definition CBORStorage.h:92
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
virtual void execute(short _) override
Periodic execution callback.
Definition Date.h:127
bool restore() override
Restores time from persistent storage.
Definition Date.h:110
static String getFormattedTime()
Gets the current time in human-readable format.
Definition Date.h:83
bool store() override
Stores the current time to persistent storage.
Definition Date.h:96
void forceSync()
Forces immediate NTP time synchronization.
Definition Date.h:139
static time_t now()
Returns the current Unix timestamp.
Definition Date.h:74
void emitEvent(unsigned int topic, int msg)
Interface for executing tasks in the scheduler system.
Definition IExecutor.h:31
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:268
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:226
@ 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
Contains all classes and functions related to the Uniot Core.