From aeb89a8d9641e1d26b2524167cb56c88a3b3e758 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Wed, 29 Dec 2021 15:49:52 +0100 Subject: [PATCH] moved time related settings into newsettings --- main/accessors/settingsaccessors.h | 26 ++++++++---- main/displays/menus/timesettingsmenu.cpp | 5 +-- main/newsettings.h | 19 +++++++++ main/presets.cpp | 3 -- main/presets.h | 11 ----- main/settings.h | 19 --------- main/stringsettings.h | 7 ---- main/time_bobbycar.cpp | 51 ++++++++++++++++-------- main/time_bobbycar.h | 2 + main/utils.cpp | 24 +++-------- main/utils.h | 1 - 11 files changed, 82 insertions(+), 86 deletions(-) diff --git a/main/accessors/settingsaccessors.h b/main/accessors/settingsaccessors.h index b5eb737..d393361 100644 --- a/main/accessors/settingsaccessors.h +++ b/main/accessors/settingsaccessors.h @@ -90,17 +90,29 @@ struct CloudTransmitTimeoutAccessor : public RefAccessorSaveSettings { // Time struct TimezoneOffsetAccessor : public virtual espgui::AccessorInterface { - int32_t getValue() const override { return settings.timeSettings.timezoneOffset.count(); } - void setValue(int32_t value) override { settings.timeSettings.timezoneOffset = espchrono::minutes32{value}; saveSettings(); } + int32_t getValue() const override { return configs.timezoneOffset.value.count(); } + void setValue(int32_t value) override { configs.write_config(configs.timezoneOffset, espchrono::minutes32{value}); } +}; +struct DaylightSavingModeAccessor : public virtual espgui::AccessorInterface +{ + espchrono::DayLightSavingMode getValue() const override { return configs.timeDst.value; } + void setValue(espchrono::DayLightSavingMode value) override { configs.write_config(configs.timeDst, value); } }; -struct DaylightSavingModeAccessor : public RefAccessorSaveSettings { espchrono::DayLightSavingMode &getRef() const override { return settings.timeSettings.daylightSavingMode; } }; #ifdef FEATURE_NTP -struct TimeServerEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.timeSettings.timeServerEnabled; } }; -struct TimeSyncModeAccessor : public RefAccessorSaveSettings { sntp_sync_mode_t &getRef() const override { return settings.timeSettings.timeSyncMode; } }; +struct TimeServerEnabledAccessor : public virtual espgui::AccessorInterface +{ + bool getValue() const override { return configs.timeServerEnabled.value; } + void setValue(bool value) override { configs.write_config(configs.timeServerEnabled, value); } +}; +struct TimeSyncModeAccessor : public virtual espgui::AccessorInterface +{ + sntp_sync_mode_t getValue() const override { return configs.timeSyncMode.value; } + void setValue(sntp_sync_mode_t value) override { configs.write_config(configs.timeSyncMode, value); } +}; struct TimeSyncIntervalAccessor : public virtual espgui::AccessorInterface { - int32_t getValue() const override { return settings.timeSettings.timeSyncInterval.count(); } - void setValue(int32_t value) override { settings.timeSettings.timeSyncInterval = espchrono::milliseconds32{value}; saveSettings(); } + int32_t getValue() const override { return configs.timeSyncInterval.value.count(); } + void setValue(int32_t value) override { configs.write_config(configs.timeSyncInterval, espchrono::milliseconds32{value}); } }; #endif diff --git a/main/displays/menus/timesettingsmenu.cpp b/main/displays/menus/timesettingsmenu.cpp index 1464893..92dac51 100644 --- a/main/displays/menus/timesettingsmenu.cpp +++ b/main/displays/menus/timesettingsmenu.cpp @@ -38,9 +38,8 @@ public: { #ifdef CONFIG_ESPCHRONO_SUPPORT_DEFAULT_TIMEZONE return fmt::format("local: {}", espchrono::toString(espchrono::toDateTime(espchrono::local_clock::now()))); -#else - // Crude local time implementation - return fmt::format("local: {}", espchrono::toString(espchrono::toDateTime(espchrono::utc_clock::now() + settings.timeSettings.timezoneOffset))); +#else // Mir egal ob die lokalzeit richtig is + return fmt::format("local: {}", espchrono::toString(espchrono::toDateTime(espchrono::utc_clock::now() + configs.timezoneOffset.value))); #endif } }; diff --git a/main/newsettings.h b/main/newsettings.h index a60aded..beb4c0d 100644 --- a/main/newsettings.h +++ b/main/newsettings.h @@ -7,12 +7,17 @@ #include #include +// esp-idf includes +#include + // 3rdparty lib includes #include #include #include +#include #include #include +#include #include using namespace espconfig; @@ -90,6 +95,13 @@ public: ConfigWrapper wifiApChannel {1, DoReset, {}, "wifiApChannel" }; ConfigWrapper wifiApAuthmode{WIFI_AUTH_WPA2_PSK, DoReset, {}, "wifiApAuthmode" }; + ConfigWrapper timeServerEnabled {false, DoReset, {}, "timeServerEnabl" }; + ConfigWrapper timeServer {"europe.pool.ntp.org", NoReset, StringMaxSize<64>, "timeServer" }; + ConfigWrapper timeSyncMode {SNTP_SYNC_MODE_IMMED, NoReset, {}, "timeSyncMode" }; + ConfigWrapper timeSyncInterval{espchrono::milliseconds32{CONFIG_LWIP_SNTP_UPDATE_DELAY}, NoReset, MinTimeSyncInterval, "timeSyncInterva" }; + ConfigWrapper timezoneOffset{espchrono::minutes32{60}, DoReset, {}, "timezoneOffset" }; // MinMaxValue + ConfigWrappertimeDst{espchrono::DayLightSavingMode::EuropeanSummerTime, DoReset, {}, "time_dst" }; + ConfigWrapper canBusResetOnError {false, DoReset, {}, "canBusRstErr" }; std::array wireless_door_configs { @@ -232,6 +244,13 @@ public: x(wifiApChannel) \ x(wifiApAuthmode) \ \ + x(timeServerEnabled) \ + x(timeServer) \ + x(timeSyncMode) \ + x(timeSyncInterval) \ + x(timezoneOffset) \ + x(timeDst) \ + \ x(canBusResetOnError) \ \ x(wireless_door_configs[0].doorId) \ diff --git a/main/presets.cpp b/main/presets.cpp index 7facaaa..3ea53e7 100644 --- a/main/presets.cpp +++ b/main/presets.cpp @@ -9,9 +9,6 @@ StringSettings makeDefaultStringSettings() #endif return { -#ifdef FEATURE_NTP - .timeServer = "europe.pool.ntp.org", -#endif #ifdef FEATURE_OTA .otaServers = std::array { ConfiguredOtaServer { .name = {}, .url = {} }, diff --git a/main/presets.h b/main/presets.h index 9b1af6e..a7a7fe7 100644 --- a/main/presets.h +++ b/main/presets.h @@ -34,16 +34,6 @@ constexpr Settings::Limits kidsLimits { .phaseAdvMax = 20 }; -constexpr Settings::TimeSettings defaultTimeSettings { - .timezoneOffset = 60min, - .daylightSavingMode = espchrono::DayLightSavingMode::EuropeanSummerTime, -#ifdef FEATURE_NTP - .timeServerEnabled = true, - .timeSyncMode = SNTP_SYNC_MODE_IMMED, - .timeSyncInterval = espchrono::milliseconds32{CONFIG_LWIP_SNTP_UPDATE_DELAY}, -#endif -}; - constexpr Settings::ControllerHardware defaultControllerHardware { .enableFrontLeft = true, .enableFrontRight = true, @@ -313,7 +303,6 @@ constexpr Settings defaultSettings { #ifdef FEATURE_BLE .bleSettings = defaultBleSettings, #endif - .timeSettings = defaultTimeSettings, .controllerHardware = defaultControllerHardware, .boardcomputerHardware = defaultBoardcomputerHardware, #ifdef FEATURE_CLOUD diff --git a/main/settings.h b/main/settings.h index 40b759c..a13a908 100644 --- a/main/settings.h +++ b/main/settings.h @@ -52,17 +52,6 @@ struct Settings } bleSettings; #endif - struct TimeSettings { - espchrono::minutes32 timezoneOffset; - espchrono::DayLightSavingMode daylightSavingMode; - -#ifdef FEATURE_NTP - bool timeServerEnabled; - sntp_sync_mode_t timeSyncMode; - espchrono::milliseconds32 timeSyncInterval; -#endif - } timeSettings; - struct ControllerHardware { bool enableFrontLeft, enableFrontRight, enableBackLeft, enableBackRight; bool invertFrontLeft, invertFrontRight, invertBackLeft, invertBackRight; @@ -244,14 +233,6 @@ void Settings::executeForEveryCommonSetting(T &&callable) callable("bleEnabled", bleSettings.bleEnabled); #endif - callable("timezoneOffset", timeSettings.timezoneOffset); - callable("daylightSaving", timeSettings.daylightSavingMode); -#ifdef FEATURE_NTP - callable("timeServerEnab", timeSettings.timeServerEnabled); - callable("timeSyncMode", timeSettings.timeSyncMode); - callable("timeSyncInterv", timeSettings.timeSyncInterval); -#endif - callable("wheelDiameter", controllerHardware.wheelDiameter); callable("numMagnetPoles", controllerHardware.numMagnetPoles); callable("swapFrontBack", controllerHardware.swapFrontBack); diff --git a/main/stringsettings.h b/main/stringsettings.h index bafb0c3..9014c84 100644 --- a/main/stringsettings.h +++ b/main/stringsettings.h @@ -6,10 +6,6 @@ struct StringSettings { -#ifdef FEATURE_NTP - std::string timeServer; -#endif - template void executeForEveryCommonSetting(T &&callable); @@ -29,9 +25,6 @@ struct StringSettings template void StringSettings::executeForEveryCommonSetting(T &&callable) { -#ifdef FEATURE_NTP - callable("timeServer", timeServer); -#endif #ifdef FEATURE_OTA callable("otaName0", otaServers[0].name); callable("otaUrl0", otaServers[0].url); diff --git a/main/time_bobbycar.cpp b/main/time_bobbycar.cpp index f95c6a4..a0a4cc9 100644 --- a/main/time_bobbycar.cpp +++ b/main/time_bobbycar.cpp @@ -11,12 +11,12 @@ #include // local includes -#include "globals.h" +#include "newsettings.h" espchrono::time_zone get_default_timezone() noexcept { using namespace espchrono; - return time_zone{minutes32{settings.timeSettings.timezoneOffset}, settings.timeSettings.daylightSavingMode}; + return time_zone{configs.timezoneOffset.value, configs.timeDst.value}; } #ifdef CONFIG_ESPCHRONO_SUPPORT_DEFAULT_TIMEZONE @@ -37,11 +37,11 @@ void initTime() { sntp_setoperatingmode(SNTP_OPMODE_POLL); static_assert(SNTP_MAX_SERVERS >= 1); - sntp_setservername(0, stringSettings.timeServer.c_str()); + sntp_setservername(0, configs.timeServer.value.c_str()); sntp_set_time_sync_notification_cb(time_sync_notification_cb); - sntp_set_sync_mode(settings.timeSettings.timeSyncMode); - sntp_set_sync_interval(espchrono::milliseconds32{settings.timeSettings.timeSyncInterval}.count()); - if (settings.timeSettings.timeServerEnabled) + sntp_set_sync_mode(configs.timeSyncMode.value); + sntp_set_sync_interval(espchrono::milliseconds32{configs.timeSyncInterval.value}.count()); + if (configs.timeServerEnabled.value) { ESP_LOGI("BOBBY", "sntp_init() ..."); sntp_init(); @@ -54,9 +54,9 @@ void initTime() void updateTime() { - if (bool(sntp_enabled()) != settings.timeSettings.timeServerEnabled) + if (bool(sntp_enabled()) != configs.timeServerEnabled.value) { - if (settings.timeSettings.timeServerEnabled) + if (configs.timeServerEnabled.value) { ESP_LOGD("BOBBY", "calling sntp_init()..."); sntp_init(); @@ -70,24 +70,24 @@ void updateTime() } } - if (stringSettings.timeServer != sntp_getservername(0)) + if (configs.timeServer.value != sntp_getservername(0)) { - ESP_LOGD("BOBBY", "calling sntp_getservername() with %s...", stringSettings.timeServer.c_str()); - sntp_setservername(0, stringSettings.timeServer.c_str()); + ESP_LOGD("BOBBY", "calling sntp_getservername() with %s...", configs.timeServer.value.c_str()); + sntp_setservername(0, configs.timeServer.value.c_str()); ESP_LOGI("BOBBY", "sntp_getservername() finished"); } - if (settings.timeSettings.timeSyncMode != sntp_get_sync_mode()) + if (configs.timeSyncMode.value != sntp_get_sync_mode()) { - ESP_LOGD("BOBBY", "calling sntp_set_sync_mode() with %s...", espcpputils::toString(settings.timeSettings.timeSyncMode).c_str()); - sntp_set_sync_mode(settings.timeSettings.timeSyncMode); + ESP_LOGD("BOBBY", "calling sntp_set_sync_mode() with %s...", espcpputils::toString(configs.timeSyncMode.value).c_str()); + sntp_set_sync_mode(configs.timeSyncMode.value); ESP_LOGI("BOBBY", "sntp_set_sync_mode() finished"); } - if (settings.timeSettings.timeSyncInterval != espchrono::milliseconds32{sntp_get_sync_interval()}) + if (configs.timeSyncInterval.value != espchrono::milliseconds32{sntp_get_sync_interval()}) { - ESP_LOGD("BOBBY", "calling sntp_set_sync_interval() with %s...", espchrono::toString(settings.timeSettings.timeSyncInterval).c_str()); - sntp_set_sync_interval(espchrono::milliseconds32{settings.timeSettings.timeSyncInterval}.count()); + ESP_LOGD("BOBBY", "calling sntp_set_sync_interval() with %s...", espchrono::toString(configs.timeSyncInterval.value).c_str()); + sntp_set_sync_interval(espchrono::milliseconds32{configs.timeSyncInterval.value}.count()); ESP_LOGI("BOBBY", "sntp_set_sync_interval() finished"); } } @@ -108,3 +108,20 @@ void time_sync_notification_cb(struct timeval *tv) ESP_LOGI("BOBBY", "nullptr"); } #endif + +void time_set_now(espchrono::utc_clock::time_point now) +{ + using namespace espchrono; + // ESP_LOGI("BOBBY", "%s (%lld)%s", toString(toDateTime(now)).c_str(), std::chrono::floor(now.time_since_epoch()).count(), time_valid(now) ? "":" (probably invalid)"); + const auto seconds = std::chrono::floor(now.time_since_epoch()); + + timeval ts { + .tv_sec = (long int)seconds.count(), + .tv_usec = (long int)std::chrono::floor(now.time_since_epoch() - seconds).count() + }; + timezone tz { + .tz_minuteswest = 0, + .tz_dsttime = 0 + }; + settimeofday(&ts, &tz); +} diff --git a/main/time_bobbycar.h b/main/time_bobbycar.h index 2d9d88c..5e38aa0 100644 --- a/main/time_bobbycar.h +++ b/main/time_bobbycar.h @@ -11,3 +11,5 @@ espchrono::time_zone get_default_timezone() noexcept; void initTime(); void updateTime(); #endif + +void time_set_now(espchrono::utc_clock::time_point now); diff --git a/main/utils.cpp b/main/utils.cpp index 3fe823b..a8d79a0 100644 --- a/main/utils.cpp +++ b/main/utils.cpp @@ -276,25 +276,13 @@ uint8_t time_to_percent(espchrono::milliseconds32 repeat, espchrono::millisecond return invert ? numLeds : 0; } -void time_set_now(espchrono::utc_clock::time_point now) -{ - using namespace espchrono; - // ESP_LOGI("BOBBY", "%s (%lld)%s", toString(toDateTime(now)).c_str(), std::chrono::floor(now.time_since_epoch()).count(), time_valid(now) ? "":" (probably invalid)"); - const auto seconds = std::chrono::floor(now.time_since_epoch()); - - timeval ts { - .tv_sec = (long int)seconds.count(), - .tv_usec = (long int)std::chrono::floor(now.time_since_epoch() - seconds).count() - }; - timezone tz { - .tz_minuteswest = 0, - .tz_dsttime = 0 - }; - settimeofday(&ts, &tz); -} - std::string local_clock_string() { - const auto dt = espchrono::toDateTime(espchrono::utc_clock::now() + settings.timeSettings.timezoneOffset); +#ifdef CONFIG_ESPCHRONO_SUPPORT_DEFAULT_TIMEZONE + const auto now = espchrono::local_clock::now(); +#else // mir egal ob die lokalzeit richtig is + const auto now = espchrono::utc_clock::now() + configs.timezoneOffset.value; +#endif + const auto dt = espchrono::toDateTime(now); return fmt::format("{:02d}:{:02d}:{:02d}", dt.hour, dt.minute, dt.second); } diff --git a/main/utils.h b/main/utils.h index ba20812..2cc4a8f 100644 --- a/main/utils.h +++ b/main/utils.h @@ -59,5 +59,4 @@ void readPotis(); float wattToAmpere(float watt); float wattToMotorCurrent(float watt); uint8_t time_to_percent(espchrono::milliseconds32 repeat, espchrono::milliseconds32 riseTime, espchrono::milliseconds32 fullTime, size_t numLeds, bool invert); -void time_set_now(espchrono::utc_clock::time_point now); std::string local_clock_string();