-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathNTPSettingsService.cpp
More file actions
142 lines (122 loc) · 3.94 KB
/
NTPSettingsService.cpp
File metadata and controls
142 lines (122 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <NTPSettingsService.h>
NTPSettingsService::NTPSettingsService(AsyncWebServer *server, FS *fs, ActiveStatus *activeStatus) : AdminSettingsService(server, fs, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE),
_activeStatus(activeStatus)
{
#if defined(ESP8266)
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1));
#elif defined(ESP_PLATFORM)
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
#endif
NTP.onNTPSyncEvent([this](NTPSyncEvent_t ntpEvent) {
_ntpEvent = ntpEvent;
_syncEventTriggered = true;
});
}
NTPSettingsService::~NTPSettingsService() {}
void NTPSettingsService::loop()
{
// detect when we need to re-configure NTP and do it in the main loop
if (_reconfigureNTP)
{
_reconfigureNTP = false;
configureNTP();
}
// output sync event to serial
if (_syncEventTriggered)
{
processSyncEvent(_ntpEvent);
_syncEventTriggered = false;
}
// keep time synchronized in background
now();
}
void NTPSettingsService::readFromJsonObject(JsonObject &root)
{
_server = root["server"] | NTP_SETTINGS_SERVICE_DEFAULT_SERVER;
_interval = root["interval"];
// validate server is specified, resorting to default
_server.trim();
if (!_server)
{
_server = NTP_SETTINGS_SERVICE_DEFAULT_SERVER;
}
// make sure interval is in bounds
if (_interval < NTP_SETTINGS_MIN_INTERVAL)
{
_interval = NTP_SETTINGS_MIN_INTERVAL;
}
else if (_interval > NTP_SETTINGS_MAX_INTERVAL)
{
_interval = NTP_SETTINGS_MAX_INTERVAL;
}
}
void NTPSettingsService::writeToJsonObject(JsonObject &root)
{
root["server"] = _server;
root["interval"] = _interval;
}
void NTPSettingsService::onConfigUpdated()
{
_reconfigureNTP = true;
}
#if defined(ESP8266)
void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP &event)
{
Serial.printf("Got IP address, starting NTP Synchronization\n");
_reconfigureNTP = true;
}
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected &event)
{
Serial.printf("WiFi connection dropped, stopping NTP.\n");
_activeStatus->TimeNotSetted();
_reconfigureNTP = false;
NTP.stop();
}
#elif defined(ESP_PLATFORM)
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
{
Serial.printf("Got IP address, starting NTP Synchronization\n");
_reconfigureNTP = true;
}
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info)
{
Serial.printf("WiFi connection dropped, stopping NTP.\n");
_activeStatus->TimeNotSetted();
_reconfigureNTP = false;
NTP.stop();
}
#endif
void NTPSettingsService::configureNTP()
{
if (_activeStatus->BrewStarted)
{
Serial.println("Brew started, NTP won't sync");
return;
}
Serial.println("Configuring NTP...");
// disable sync
NTP.stop();
// enable sync
NTP.begin(_server);
NTP.setInterval(_interval);
}
void NTPSettingsService::processSyncEvent(NTPSyncEvent_t ntpEvent)
{
if (ntpEvent)
{
_activeStatus->TimeNotSetted();
Serial.print("Time Sync error: ");
if (ntpEvent == noResponse)
Serial.println("NTP server not reachable");
else if (ntpEvent == invalidAddress)
Serial.println("Invalid NTP server address");
}
else
{
_activeStatus->TimeSetted();
Serial.print("Got NTP time: ");
Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
}
}