moved time related settings into newsettings

This commit is contained in:
2021-12-29 15:49:52 +01:00
parent 2c1aca079f
commit aeb89a8d96
11 changed files with 82 additions and 86 deletions

View File

@ -90,17 +90,29 @@ struct CloudTransmitTimeoutAccessor : public RefAccessorSaveSettings<int16_t> {
// Time
struct TimezoneOffsetAccessor : public virtual espgui::AccessorInterface<int32_t>
{
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>
{
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> { espchrono::DayLightSavingMode &getRef() const override { return settings.timeSettings.daylightSavingMode; } };
#ifdef FEATURE_NTP
struct TimeServerEnabledAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.timeSettings.timeServerEnabled; } };
struct TimeSyncModeAccessor : public RefAccessorSaveSettings<sntp_sync_mode_t> { sntp_sync_mode_t &getRef() const override { return settings.timeSettings.timeSyncMode; } };
struct TimeServerEnabledAccessor : public virtual espgui::AccessorInterface<bool>
{
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>
{
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>
{
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

View File

@ -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
}
};

View File

@ -7,12 +7,17 @@
#include <array>
#include <optional>
// esp-idf includes
#include <esp_sntp.h>
// 3rdparty lib includes
#include <fmt/core.h>
#include <configmanager.h>
#include <configconstraints_base.h>
#include <configconstraints_espchrono.h>
#include <configwrapper.h>
#include <espwifiutils.h>
#include <espchrono.h>
#include <makearray.h>
using namespace espconfig;
@ -90,6 +95,13 @@ public:
ConfigWrapper<uint8_t> wifiApChannel {1, DoReset, {}, "wifiApChannel" };
ConfigWrapper<wifi_auth_mode_t> wifiApAuthmode{WIFI_AUTH_WPA2_PSK, DoReset, {}, "wifiApAuthmode" };
ConfigWrapper<bool> timeServerEnabled {false, DoReset, {}, "timeServerEnabl" };
ConfigWrapper<std::string> timeServer {"europe.pool.ntp.org", NoReset, StringMaxSize<64>, "timeServer" };
ConfigWrapper<sntp_sync_mode_t> timeSyncMode {SNTP_SYNC_MODE_IMMED, NoReset, {}, "timeSyncMode" };
ConfigWrapper<espchrono::milliseconds32> timeSyncInterval{espchrono::milliseconds32{CONFIG_LWIP_SNTP_UPDATE_DELAY}, NoReset, MinTimeSyncInterval, "timeSyncInterva" };
ConfigWrapper<espchrono::minutes32> timezoneOffset{espchrono::minutes32{60}, DoReset, {}, "timezoneOffset" }; // MinMaxValue<minutes32, -1440m, 1440m>
ConfigWrapper<espchrono::DayLightSavingMode>timeDst{espchrono::DayLightSavingMode::EuropeanSummerTime, DoReset, {}, "time_dst" };
ConfigWrapper<bool> canBusResetOnError {false, DoReset, {}, "canBusRstErr" };
std::array<WirelessDoorsConfig, 5> 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) \

View File

@ -9,9 +9,6 @@ StringSettings makeDefaultStringSettings()
#endif
return {
#ifdef FEATURE_NTP
.timeServer = "europe.pool.ntp.org",
#endif
#ifdef FEATURE_OTA
.otaServers = std::array<ConfiguredOtaServer, 5> {
ConfiguredOtaServer { .name = {}, .url = {} },

View File

@ -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

View File

@ -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);

View File

@ -6,10 +6,6 @@
struct StringSettings
{
#ifdef FEATURE_NTP
std::string timeServer;
#endif
template<typename T>
void executeForEveryCommonSetting(T &&callable);
@ -29,9 +25,6 @@ struct StringSettings
template<typename T>
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);

View File

@ -11,12 +11,12 @@
#include <espstrutils.h>
// 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<std::chrono::seconds>(now.time_since_epoch()).count(), time_valid(now) ? "":" (probably invalid)");
const auto seconds = std::chrono::floor<std::chrono::seconds>(now.time_since_epoch());
timeval ts {
.tv_sec = (long int)seconds.count(),
.tv_usec = (long int)std::chrono::floor<std::chrono::microseconds>(now.time_since_epoch() - seconds).count()
};
timezone tz {
.tz_minuteswest = 0,
.tz_dsttime = 0
};
settimeofday(&ts, &tz);
}

View File

@ -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);

View File

@ -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<std::chrono::seconds>(now.time_since_epoch()).count(), time_valid(now) ? "":" (probably invalid)");
const auto seconds = std::chrono::floor<std::chrono::seconds>(now.time_since_epoch());
timeval ts {
.tv_sec = (long int)seconds.count(),
.tv_usec = (long int)std::chrono::floor<std::chrono::microseconds>(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);
}

View File

@ -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();