🪄 Formats NTP related service
This commit is contained in:
@@ -14,20 +14,15 @@
|
|||||||
|
|
||||||
#include <NTPSettingsService.h>
|
#include <NTPSettingsService.h>
|
||||||
|
|
||||||
NTPSettingsService::NTPSettingsService(PsychicHttpServer *server,
|
NTPSettingsService::NTPSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager)
|
||||||
FS *fs,
|
: _server(server),
|
||||||
SecurityManager *securityManager) : _server(server),
|
_securityManager(securityManager),
|
||||||
_securityManager(securityManager),
|
_httpEndpoint(NTPSettings::read, NTPSettings::update, this, server, NTP_SETTINGS_SERVICE_PATH, securityManager),
|
||||||
_httpEndpoint(NTPSettings::read, NTPSettings::update, this, server, NTP_SETTINGS_SERVICE_PATH, securityManager),
|
_fsPersistence(NTPSettings::read, NTPSettings::update, this, fs, NTP_SETTINGS_FILE) {
|
||||||
_fsPersistence(NTPSettings::read, NTPSettings::update, this, fs, NTP_SETTINGS_FILE)
|
addUpdateHandler([&](const String &originId) { configureNTP(); }, false);
|
||||||
{
|
|
||||||
addUpdateHandler([&](const String &originId)
|
|
||||||
{ configureNTP(); },
|
|
||||||
false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NTPSettingsService::begin()
|
void NTPSettingsService::begin() {
|
||||||
{
|
|
||||||
WiFi.onEvent(
|
WiFi.onEvent(
|
||||||
std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
|
std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
|
||||||
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
|
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
|
||||||
@@ -35,8 +30,7 @@ void NTPSettingsService::begin()
|
|||||||
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
|
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
|
||||||
|
|
||||||
_httpEndpoint.begin();
|
_httpEndpoint.begin();
|
||||||
_server->on(TIME_PATH,
|
_server->on(TIME_PATH, HTTP_POST,
|
||||||
HTTP_POST,
|
|
||||||
_securityManager->wrapCallback(
|
_securityManager->wrapCallback(
|
||||||
std::bind(&NTPSettingsService::configureTime, this, std::placeholders::_1, std::placeholders::_2),
|
std::bind(&NTPSettingsService::configureTime, this, std::placeholders::_1, std::placeholders::_2),
|
||||||
AuthenticationPredicates::IS_ADMIN));
|
AuthenticationPredicates::IS_ADMIN));
|
||||||
@@ -47,39 +41,26 @@ void NTPSettingsService::begin()
|
|||||||
configureNTP();
|
configureNTP();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
|
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { configureNTP(); }
|
||||||
{
|
|
||||||
configureNTP();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info)
|
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { configureNTP(); }
|
||||||
{
|
|
||||||
configureNTP();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NTPSettingsService::configureNTP()
|
void NTPSettingsService::configureNTP() {
|
||||||
{
|
if (WiFi.isConnected() && _state.enabled) {
|
||||||
if (WiFi.isConnected() && _state.enabled)
|
|
||||||
{
|
|
||||||
configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
|
configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
setenv("TZ", _state.tzFormat.c_str(), 1);
|
setenv("TZ", _state.tzFormat.c_str(), 1);
|
||||||
tzset();
|
tzset();
|
||||||
sntp_stop();
|
sntp_stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NTPSettingsService::configureTime(PsychicRequest *request, JsonVariant &json)
|
esp_err_t NTPSettingsService::configureTime(PsychicRequest *request, JsonVariant &json) {
|
||||||
{
|
if (!sntp_enabled() && json.is<JsonObject>()) {
|
||||||
if (!sntp_enabled() && json.is<JsonObject>())
|
|
||||||
{
|
|
||||||
struct tm tm = {0};
|
struct tm tm = {0};
|
||||||
String timeLocal = json["local_time"];
|
String timeLocal = json["local_time"];
|
||||||
char *s = strptime(timeLocal.c_str(), "%Y-%m-%dT%H:%M:%S", &tm);
|
char *s = strptime(timeLocal.c_str(), "%Y-%m-%dT%H:%M:%S", &tm);
|
||||||
if (s != nullptr)
|
if (s != nullptr) {
|
||||||
{
|
|
||||||
time_t time = mktime(&tm);
|
time_t time = mktime(&tm);
|
||||||
struct timeval now = {.tv_sec = time};
|
struct timeval now = {.tv_sec = time};
|
||||||
settimeofday(&now, nullptr);
|
settimeofday(&now, nullptr);
|
||||||
|
|||||||
@@ -43,24 +43,21 @@
|
|||||||
|
|
||||||
#define TIME_PATH "/api/time"
|
#define TIME_PATH "/api/time"
|
||||||
|
|
||||||
class NTPSettings
|
class NTPSettings {
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
bool enabled;
|
bool enabled;
|
||||||
String tzLabel;
|
String tzLabel;
|
||||||
String tzFormat;
|
String tzFormat;
|
||||||
String server;
|
String server;
|
||||||
|
|
||||||
static void read(NTPSettings &settings, JsonObject &root)
|
static void read(NTPSettings &settings, JsonObject &root) {
|
||||||
{
|
|
||||||
root["enabled"] = settings.enabled;
|
root["enabled"] = settings.enabled;
|
||||||
root["server"] = settings.server;
|
root["server"] = settings.server;
|
||||||
root["tz_label"] = settings.tzLabel;
|
root["tz_label"] = settings.tzLabel;
|
||||||
root["tz_format"] = settings.tzFormat;
|
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.enabled = root["enabled"] | FACTORY_NTP_ENABLED;
|
||||||
settings.server = root["server"] | FACTORY_NTP_SERVER;
|
settings.server = root["server"] | FACTORY_NTP_SERVER;
|
||||||
settings.tzLabel = root["tz_label"] | FACTORY_NTP_TIME_ZONE_LABEL;
|
settings.tzLabel = root["tz_label"] | FACTORY_NTP_TIME_ZONE_LABEL;
|
||||||
@@ -69,14 +66,13 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class NTPSettingsService : public StatefulService<NTPSettings>
|
class NTPSettingsService : public StatefulService<NTPSettings> {
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
NTPSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager);
|
NTPSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager);
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PsychicHttpServer *_server;
|
PsychicHttpServer *_server;
|
||||||
SecurityManager *_securityManager;
|
SecurityManager *_securityManager;
|
||||||
HttpEndpoint<NTPSettings> _httpEndpoint;
|
HttpEndpoint<NTPSettings> _httpEndpoint;
|
||||||
|
|||||||
@@ -14,15 +14,11 @@
|
|||||||
|
|
||||||
#include <NTPStatus.h>
|
#include <NTPStatus.h>
|
||||||
|
|
||||||
NTPStatus::NTPStatus(PsychicHttpServer *server, SecurityManager *securityManager) : _server(server),
|
NTPStatus::NTPStatus(PsychicHttpServer *server, SecurityManager *securityManager)
|
||||||
_securityManager(securityManager)
|
: _server(server), _securityManager(securityManager) {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void NTPStatus::begin()
|
void NTPStatus::begin() {
|
||||||
{
|
_server->on(NTP_STATUS_SERVICE_PATH, HTTP_GET,
|
||||||
_server->on(NTP_STATUS_SERVICE_PATH,
|
|
||||||
HTTP_GET,
|
|
||||||
_securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1),
|
_securityManager->wrapRequest(std::bind(&NTPStatus::ntpStatus, this, std::placeholders::_1),
|
||||||
AuthenticationPredicates::IS_AUTHENTICATED));
|
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.
|
* 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];
|
char time_string[25];
|
||||||
strftime(time_string, 25, format, time);
|
strftime(time_string, 25, format, time);
|
||||||
return String(time_string);
|
return String(time_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
String toUTCTimeString(tm *time)
|
String toUTCTimeString(tm *time) { return formatTime(time, "%FT%TZ"); }
|
||||||
{
|
|
||||||
return formatTime(time, "%FT%TZ");
|
|
||||||
}
|
|
||||||
|
|
||||||
String toLocalTimeString(tm *time)
|
String toLocalTimeString(tm *time) { return formatTime(time, "%FT%T"); }
|
||||||
{
|
|
||||||
return formatTime(time, "%FT%T");
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t NTPStatus::ntpStatus(PsychicRequest *request)
|
esp_err_t NTPStatus::ntpStatus(PsychicRequest *request) {
|
||||||
{
|
|
||||||
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||||
JsonObject root = response.getRoot();
|
JsonObject root = response.getRoot();
|
||||||
|
|
||||||
|
|||||||
@@ -25,14 +25,13 @@
|
|||||||
|
|
||||||
#define NTP_STATUS_SERVICE_PATH "/api/ntpStatus"
|
#define NTP_STATUS_SERVICE_PATH "/api/ntpStatus"
|
||||||
|
|
||||||
class NTPStatus
|
class NTPStatus {
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
NTPStatus(PsychicHttpServer *server, SecurityManager *securityManager);
|
NTPStatus(PsychicHttpServer *server, SecurityManager *securityManager);
|
||||||
|
|
||||||
void begin();
|
void begin();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PsychicHttpServer *_server;
|
PsychicHttpServer *_server;
|
||||||
SecurityManager *_securityManager;
|
SecurityManager *_securityManager;
|
||||||
esp_err_t ntpStatus(PsychicRequest *request);
|
esp_err_t ntpStatus(PsychicRequest *request);
|
||||||
|
|||||||
Reference in New Issue
Block a user