2018-02-26 00:11:31 +00:00
|
|
|
#ifndef NTPSettingsService_h
|
|
|
|
#define NTPSettingsService_h
|
|
|
|
|
2020-05-14 22:23:45 +00:00
|
|
|
#include <HttpEndpoint.h>
|
|
|
|
#include <FSPersistence.h>
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2020-01-20 11:14:46 +00:00
|
|
|
#include <time.h>
|
|
|
|
#ifdef ESP32
|
|
|
|
#include <lwip/apps/sntp.h>
|
|
|
|
#elif defined(ESP8266)
|
|
|
|
#include <sntp.h>
|
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2020-05-19 23:32:49 +00:00
|
|
|
#ifndef FACTORY_NTP_ENABLED
|
|
|
|
#define FACTORY_NTP_ENABLED true
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FACTORY_NTP_TIME_ZONE_LABEL
|
|
|
|
#define FACTORY_NTP_TIME_ZONE_LABEL "Europe/London"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FACTORY_NTP_TIME_ZONE_FORMAT
|
|
|
|
#define FACTORY_NTP_TIME_ZONE_FORMAT "GMT0BST,M3.5.0/1,M10.5.0"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FACTORY_NTP_SERVER
|
|
|
|
#define FACTORY_NTP_SERVER "time.google.com"
|
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
#define NTP_SETTINGS_FILE "/config/ntpSettings.json"
|
2018-04-01 09:35:23 +00:00
|
|
|
#define NTP_SETTINGS_SERVICE_PATH "/rest/ntpSettings"
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2020-06-16 21:24:15 +00:00
|
|
|
#define MAX_TIME_SIZE 256
|
|
|
|
#define TIME_PATH "/rest/time"
|
|
|
|
|
2020-02-01 08:44:26 +00:00
|
|
|
class NTPSettings {
|
|
|
|
public:
|
|
|
|
bool enabled;
|
|
|
|
String tzLabel;
|
|
|
|
String tzFormat;
|
|
|
|
String server;
|
2020-05-14 22:23:45 +00:00
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
static void read(NTPSettings& settings, JsonObject& root) {
|
2020-05-14 22:23:45 +00:00
|
|
|
root["enabled"] = settings.enabled;
|
|
|
|
root["server"] = settings.server;
|
|
|
|
root["tz_label"] = settings.tzLabel;
|
|
|
|
root["tz_format"] = settings.tzFormat;
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
static StateUpdateResult update(JsonObject& root, NTPSettings& settings) {
|
2020-05-19 23:32:49 +00:00
|
|
|
settings.enabled = root["enabled"] | FACTORY_NTP_ENABLED;
|
|
|
|
settings.server = root["server"] | FACTORY_NTP_SERVER;
|
|
|
|
settings.tzLabel = root["tz_label"] | FACTORY_NTP_TIME_ZONE_LABEL;
|
|
|
|
settings.tzFormat = root["tz_format"] | FACTORY_NTP_TIME_ZONE_FORMAT;
|
2020-05-29 19:18:43 +00:00
|
|
|
return StateUpdateResult::CHANGED;
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
2020-02-01 08:44:26 +00:00
|
|
|
};
|
|
|
|
|
2020-05-14 22:23:45 +00:00
|
|
|
class NTPSettingsService : public StatefulService<NTPSettings> {
|
2019-12-03 23:16:06 +00:00
|
|
|
public:
|
|
|
|
NTPSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2020-05-14 22:23:45 +00:00
|
|
|
void begin();
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
private:
|
2020-05-14 22:23:45 +00:00
|
|
|
HttpEndpoint<NTPSettings> _httpEndpoint;
|
|
|
|
FSPersistence<NTPSettings> _fsPersistence;
|
2020-06-16 21:24:15 +00:00
|
|
|
AsyncCallbackJsonWebHandler _timeHandler;
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
|
|
|
|
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
|
|
|
|
#elif defined(ESP8266)
|
2019-12-03 23:16:06 +00:00
|
|
|
WiFiEventHandler _onStationModeDisconnectedHandler;
|
|
|
|
WiFiEventHandler _onStationModeGotIPHandler;
|
2018-11-11 17:47:44 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
void onStationModeGotIP(const WiFiEventStationModeGotIP& event);
|
|
|
|
void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);
|
2018-11-11 17:47:44 +00:00
|
|
|
#endif
|
2019-12-03 23:16:06 +00:00
|
|
|
void configureNTP();
|
2020-06-16 21:24:15 +00:00
|
|
|
void configureTime(AsyncWebServerRequest* request, JsonVariant& json);
|
2018-02-26 00:11:31 +00:00
|
|
|
};
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
#endif // end NTPSettingsService_h
|