🪄 Formats NTP related service

This commit is contained in:
Rune Harlyk
2024-07-09 20:06:21 +02:00
committed by Rune Harlyk
parent 33e1a28223
commit 42eafde631
4 changed files with 34 additions and 70 deletions
@@ -14,20 +14,15 @@
#include <NTPSettingsService.h>
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<JsonObject>())
{
esp_err_t NTPSettingsService::configureTime(PsychicRequest *request, JsonVariant &json) {
if (!sntp_enabled() && json.is<JsonObject>()) {
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);