mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
@@ -46,8 +46,10 @@ void NTPSettingsService::WiFiEvent(WiFiEvent_t event) {
|
||||
|
||||
// https://werner.rothschopf.net/microcontroller/202103_arduino_esp32_ntp_en.htm
|
||||
void NTPSettingsService::configureNTP() {
|
||||
emsesp::EMSESP::system_.ntp_connected(false);
|
||||
if (connected_ && _state.enabled) {
|
||||
emsesp::EMSESP::logger().info(F("Starting NTP"));
|
||||
sntp_set_time_sync_notification_cb(ntp_received);
|
||||
configTzTime(_state.tzFormat.c_str(), _state.server.c_str());
|
||||
} else {
|
||||
setenv("TZ", _state.tzFormat.c_str(), 1);
|
||||
@@ -75,3 +77,8 @@ void NTPSettingsService::configureTime(AsyncWebServerRequest * request, JsonVari
|
||||
AsyncWebServerResponse * response = request->beginResponse(400);
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
void NTPSettingsService::ntp_received(struct timeval * tv) {
|
||||
// emsesp::EMSESP::logger().info(F("NTP sync to %d sec"), tv->tv_sec);
|
||||
emsesp::EMSESP::system_.ntp_connected(true);
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@ class NTPSettingsService : public StatefulService<NTPSettings> {
|
||||
NTPSettingsService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
|
||||
|
||||
void begin();
|
||||
static void ntp_received(struct timeval * tv);
|
||||
|
||||
private:
|
||||
HttpEndpoint<NTPSettings> _httpEndpoint;
|
||||
@@ -67,6 +68,7 @@ class NTPSettingsService : public StatefulService<NTPSettings> {
|
||||
void WiFiEvent(WiFiEvent_t event);
|
||||
void configureNTP();
|
||||
void configureTime(AsyncWebServerRequest * request, JsonVariant & json);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <NTPStatus.h>
|
||||
#include "../../src/emsesp_stub.hpp" // proddy added
|
||||
|
||||
using namespace std::placeholders; // for `_1` etc
|
||||
|
||||
@@ -35,7 +36,7 @@ void NTPStatus::ntpStatus(AsyncWebServerRequest * request) {
|
||||
time_t now = time(nullptr);
|
||||
|
||||
// only provide enabled/disabled status for now
|
||||
root["status"] = sntp_enabled() ? 1 : 0;
|
||||
root["status"] = sntp_enabled() && emsesp::EMSESP::system_.ntp_connected() ? 1 : 0;
|
||||
|
||||
// the current time in UTC
|
||||
root["utc_time"] = toUTCTimeString(gmtime(&now));
|
||||
|
||||
Reference in New Issue
Block a user