From d6a8563cc75e0fc55c16afb8022cd242f280889c Mon Sep 17 00:00:00 2001 From: Proddy Date: Tue, 29 Nov 2022 21:14:31 +0100 Subject: [PATCH] fixes for #751 --- lib/framework/NTPSettingsService.cpp | 1 - src/system.cpp | 11 ++++++----- src/system.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index 3df0d3118..f20bd8d6a 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -83,5 +83,4 @@ void NTPSettingsService::configureTime(AsyncWebServerRequest * request, JsonVari void NTPSettingsService::ntp_received(struct timeval * tv) { // emsesp::EMSESP::logger().info("NTP sync to %d sec", tv->tv_sec); emsesp::EMSESP::system_.ntp_connected(true); - emsesp::EMSESP::system_.send_info_mqtt("connected"); } diff --git a/src/system.cpp b/src/system.cpp index cdeba2322..a8a521ae0 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -536,17 +536,17 @@ void System::loop() { } // send MQTT info topic appended with the version information as JSON, as a retained flag -void System::send_info_mqtt(const char * event_str) { +void System::send_info_mqtt(const char * event_str, bool send_ntp) { StaticJsonDocument doc; doc["event"] = event_str; doc["version"] = EMSESP_APP_VERSION; // if NTP is enabled send the boot_time in local time in ISO 8601 format (eg: 2022-11-15 20:46:38) // https://github.com/emsesp/EMS-ESP32/issues/751 - if (ntp_connected()) { + if (send_ntp) { char time_string[25]; - time_t now = time(nullptr); // grab the current instant in unix seconds - strftime(time_string, 25, "%F %T", localtime(&now)); + time_t now = time(nullptr) - uuid::get_uptime_sec(); + strftime(time_string, 25, "%FT%T%z", localtime(&now)); doc["boot_time"] = time_string; } @@ -1414,7 +1414,8 @@ std::string System::reset_reason(uint8_t cpu) const { // set NTP status void System::ntp_connected(bool b) { if (b != ntp_connected_) { - LOG_INFO(b ? "NTP connected" : "NTP disconnected"); // if changed report it + LOG_INFO(b ? "NTP connected" : "NTP disconnected"); // if changed report it + emsesp::EMSESP::system_.send_info_mqtt("connected", true); // send info topic, but only once } ntp_connected_ = b; ntp_last_check_ = b ? uuid::get_uptime_sec() : 0; diff --git a/src/system.h b/src/system.h index e13efd3a8..f8ee51dc2 100644 --- a/src/system.h +++ b/src/system.h @@ -77,7 +77,7 @@ class System { bool check_upgrade(); bool heartbeat_json(JsonObject & output); void send_heartbeat(); - void send_info_mqtt(const char * event_str); + void send_info_mqtt(const char * event_str, bool send_ntp = false); bool syslog_enabled() { return syslog_enabled_;