Skip to content

Commit 19cd34a

Browse files
committed
feat: update NTP for both WiFi and ethernet
1 parent fb9a460 commit 19cd34a

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/framework/NTPSettingsService.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
**/
1414

1515
#include <NTPSettingsService.h>
16+
#if FT_ENABLED(FT_ETHERNET)
17+
#include <ETH.h>
18+
#endif
1619

1720
NTPSettingsService::NTPSettingsService(PsychicHttpServer *server,
1821
FS *fs,
@@ -29,11 +32,19 @@ NTPSettingsService::NTPSettingsService(PsychicHttpServer *server,
2932
void NTPSettingsService::begin()
3033
{
3134
WiFi.onEvent(
32-
std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
35+
std::bind(&NTPSettingsService::onNetworkDisconnected, this, std::placeholders::_1, std::placeholders::_2),
3336
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
34-
WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2),
37+
WiFi.onEvent(std::bind(&NTPSettingsService::onNetworkGotIP, this, std::placeholders::_1, std::placeholders::_2),
3538
WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
3639

40+
#if FT_ENABLED(FT_ETHERNET)
41+
WiFi.onEvent(
42+
std::bind(&NTPSettingsService::onNetworkDisconnected, this, std::placeholders::_1, std::placeholders::_2),
43+
WiFiEvent_t::ARDUINO_EVENT_ETH_DISCONNECTED);
44+
WiFi.onEvent(std::bind(&NTPSettingsService::onNetworkGotIP, this, std::placeholders::_1, std::placeholders::_2),
45+
WiFiEvent_t::ARDUINO_EVENT_ETH_GOT_IP);
46+
#endif
47+
3748
_httpEndpoint.begin();
3849
_server->on(TIME_PATH,
3950
HTTP_POST,
@@ -47,25 +58,29 @@ void NTPSettingsService::begin()
4758
configureNTP();
4859
}
4960

50-
void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
61+
void NTPSettingsService::onNetworkGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
5162
{
5263
#ifdef SERIAL_INFO
5364
Serial.println(F("Got IP address, starting NTP Synchronization"));
5465
#endif
5566
configureNTP();
5667
}
5768

58-
void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info)
69+
void NTPSettingsService::onNetworkDisconnected(WiFiEvent_t event, WiFiEventInfo_t info)
5970
{
6071
#ifdef SERIAL_INFO
61-
Serial.println(F("WiFi connection dropped, stopping NTP."));
72+
Serial.println(F("Network connection dropped, stopping NTP."));
6273
#endif
6374
configureNTP();
6475
}
6576

6677
void NTPSettingsService::configureNTP()
6778
{
68-
if (WiFi.isConnected() && _state.enabled)
79+
bool networkConnected = WiFi.isConnected();
80+
#if FT_ENABLED(FT_ETHERNET)
81+
networkConnected = networkConnected || ETH.connected();
82+
#endif
83+
if (networkConnected && _state.enabled)
6984
{
7085
#ifdef SERIAL_INFO
7186
Serial.println(F("Starting NTP..."));

lib/framework/NTPSettingsService.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ class NTPSettingsService : public StatefulService<NTPSettings>
8686
HttpEndpoint<NTPSettings> _httpEndpoint;
8787
FSPersistence<NTPSettings> _fsPersistence;
8888

89-
void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
90-
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
89+
void onNetworkGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
90+
void onNetworkDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
9191
void configureNTP();
9292
esp_err_t configureTime(PsychicRequest *request, JsonVariant &json);
9393
};

0 commit comments

Comments
 (0)