diff --git a/esp32/lib/ESP32-sveltekit/NTPSettingsService.cpp b/esp32/lib/ESP32-sveltekit/NTPSettingsService.cpp index 3beda25..1ff416a 100644 --- a/esp32/lib/ESP32-sveltekit/NTPSettingsService.cpp +++ b/esp32/lib/ESP32-sveltekit/NTPSettingsService.cpp @@ -14,20 +14,15 @@ #include -NTPSettingsService::NTPSettingsService(PsychicHttpServer *server, - FS *fs, - SecurityManager *securityManager) : _server(server), - _securityManager(securityManager), - _httpEndpoint(NTPSettings::read, NTPSettings::update, this, server, NTP_SETTINGS_SERVICE_PATH, securityManager), - _fsPersistence(NTPSettings::read, NTPSettings::update, this, fs, NTP_SETTINGS_FILE) -{ - addUpdateHandler([&](const String &originId) - { configureNTP(); }, - false); +NTPSettingsService::NTPSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager) + : _server(server), + _securityManager(securityManager), + _httpEndpoint(NTPSettings::read, NTPSettings::update, this, server, NTP_SETTINGS_SERVICE_PATH, securityManager), + _fsPersistence(NTPSettings::read, NTPSettings::update, this, fs, NTP_SETTINGS_FILE) { + addUpdateHandler([&](const String &originId) { configureNTP(); }, false); } -void NTPSettingsService::begin() -{ +void NTPSettingsService::begin() { WiFi.onEvent( std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED); @@ -35,8 +30,7 @@ void NTPSettingsService::begin() WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP); _httpEndpoint.begin(); - _server->on(TIME_PATH, - HTTP_POST, + _server->on(TIME_PATH, HTTP_POST, _securityManager->wrapCallback( std::bind(&NTPSettingsService::configureTime, this, std::placeholders::_1, std::placeholders::_2), AuthenticationPredicates::IS_ADMIN)); @@ -47,39 +41,26 @@ void NTPSettingsService::begin() configureNTP(); } -void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) -{ - configureNTP(); -} +void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { configureNTP(); } -void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) -{ - configureNTP(); -} +void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { configureNTP(); } -void NTPSettingsService::configureNTP() -{ - if (WiFi.isConnected() && _state.enabled) - { +void NTPSettingsService::configureNTP() { + if (WiFi.isConnected() && _state.enabled) { configTzTime(_state.tzFormat.c_str(), _state.server.c_str()); - } - else - { + } else { setenv("TZ", _state.tzFormat.c_str(), 1); tzset(); sntp_stop(); } } -esp_err_t NTPSettingsService::configureTime(PsychicRequest *request, JsonVariant &json) -{ - if (!sntp_enabled() && json.is()) - { +esp_err_t NTPSettingsService::configureTime(PsychicRequest *request, JsonVariant &json) { + if (!sntp_enabled() && json.is()) { struct tm tm = {0}; String timeLocal = json["local_time"]; char *s = strptime(timeLocal.c_str(), "%Y-%m-%dT%H:%M:%S", &tm); - if (s != nullptr) - { + if (s != nullptr) { time_t time = mktime(&tm); struct timeval now = {.tv_sec = time}; settimeofday(&now, nullptr); diff --git a/esp32/lib/ESP32-sveltekit/NTPSettingsService.h b/esp32/lib/ESP32-sveltekit/NTPSettingsService.h index 701ca47..5eeabd2 100644 --- a/esp32/lib/ESP32-sveltekit/NTPSettingsService.h +++ b/esp32/lib/ESP32-sveltekit/NTPSettingsService.h @@ -43,24 +43,21 @@ #define TIME_PATH "/api/time" -class NTPSettings -{ -public: +class NTPSettings { + public: bool enabled; String tzLabel; String tzFormat; String server; - static void read(NTPSettings &settings, JsonObject &root) - { + static void read(NTPSettings &settings, JsonObject &root) { root["enabled"] = settings.enabled; root["server"] = settings.server; root["tz_label"] = settings.tzLabel; root["tz_format"] = settings.tzFormat; } - static StateUpdateResult update(JsonObject &root, NTPSettings &settings) - { + static StateUpdateResult update(JsonObject &root, NTPSettings &settings) { settings.enabled = root["enabled"] | FACTORY_NTP_ENABLED; settings.server = root["server"] | FACTORY_NTP_SERVER; settings.tzLabel = root["tz_label"] | FACTORY_NTP_TIME_ZONE_LABEL; @@ -69,14 +66,13 @@ public: } }; -class NTPSettingsService : public StatefulService -{ -public: +class NTPSettingsService : public StatefulService { + public: NTPSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager); void begin(); -private: + private: PsychicHttpServer *_server; SecurityManager *_securityManager; HttpEndpoint _httpEndpoint; diff --git a/esp32/lib/ESP32-sveltekit/NTPStatus.cpp b/esp32/lib/ESP32-sveltekit/NTPStatus.cpp index f6cc932..6f8358a 100644 --- a/esp32/lib/ESP32-sveltekit/NTPStatus.cpp +++ b/esp32/lib/ESP32-sveltekit/NTPStatus.cpp @@ -14,15 +14,11 @@ #include -NTPStatus::NTPStatus(PsychicHttpServer *server, SecurityManager *securityManager) : _server(server), - _securityManager(securityManager) -{ -} +NTPStatus::NTPStatus(PsychicHttpServer *server, SecurityManager *securityManager) + : _server(server), _securityManager(securityManager) {} -void NTPStatus::begin() -{ - _server->on(NTP_STATUS_SERVICE_PATH, - HTTP_GET, +void NTPStatus::begin() { + _server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET, _securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1), AuthenticationPredicates::IS_AUTHENTICATED)); @@ -34,25 +30,17 @@ void NTPStatus::begin() * * Uses a 25 byte buffer, large enough to fit an ISO time string with offset. */ -String formatTime(tm *time, const char *format) -{ +String formatTime(tm *time, const char *format) { char time_string[25]; strftime(time_string, 25, format, time); return String(time_string); } -String toUTCTimeString(tm *time) -{ - return formatTime(time, "%FT%TZ"); -} +String toUTCTimeString(tm *time) { return formatTime(time, "%FT%TZ"); } -String toLocalTimeString(tm *time) -{ - return formatTime(time, "%FT%T"); -} +String toLocalTimeString(tm *time) { return formatTime(time, "%FT%T"); } -esp_err_t NTPStatus::ntpStatus(PsychicRequest *request) -{ +esp_err_t NTPStatus::ntpStatus(PsychicRequest *request) { PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject root = response.getRoot(); diff --git a/esp32/lib/ESP32-sveltekit/NTPStatus.h b/esp32/lib/ESP32-sveltekit/NTPStatus.h index 41691ae..5d69210 100644 --- a/esp32/lib/ESP32-sveltekit/NTPStatus.h +++ b/esp32/lib/ESP32-sveltekit/NTPStatus.h @@ -25,14 +25,13 @@ #define NTP_STATUS_SERVICE_PATH "/api/ntpStatus" -class NTPStatus -{ -public: +class NTPStatus { + public: NTPStatus(PsychicHttpServer *server, SecurityManager *securityManager); void begin(); -private: + private: PsychicHttpServer *_server; SecurityManager *_securityManager; esp_err_t ntpStatus(PsychicRequest *request);