🪄 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);
+7 -11
View File
@@ -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<NTPSettings>
{
public:
class NTPSettingsService : public StatefulService<NTPSettings> {
public:
NTPSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager);
void begin();
private:
private:
PsychicHttpServer *_server;
SecurityManager *_securityManager;
HttpEndpoint<NTPSettings> _httpEndpoint;
+8 -20
View File
@@ -14,15 +14,11 @@
#include <NTPStatus.h>
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();
+3 -4
View File
@@ -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);