Merge pull request #427 from MichaelDvP/dev

add NTP status to mqtt/ha (heartbeat) and log
This commit is contained in:
Proddy
2022-03-29 20:28:47 +02:00
committed by GitHub
3 changed files with 26 additions and 5 deletions

View File

@@ -648,6 +648,7 @@ void Mqtt::ha_status() {
} }
publish_system_ha_sensor_config(DeviceValueType::INT, F("Uptime"), F("uptime"), DeviceValueUOM::NONE); publish_system_ha_sensor_config(DeviceValueType::INT, F("Uptime"), F("uptime"), DeviceValueUOM::NONE);
publish_system_ha_sensor_config(DeviceValueType::INT, F("Uptime (sec)"), F("uptime_sec"), DeviceValueUOM::SECONDS); publish_system_ha_sensor_config(DeviceValueType::INT, F("Uptime (sec)"), F("uptime_sec"), DeviceValueUOM::SECONDS);
publish_system_ha_sensor_config(DeviceValueType::BOOL, F("NTP status"), F("ntp_status"), DeviceValueUOM::NONE);
publish_system_ha_sensor_config(DeviceValueType::INT, F("Free memory"), F("freemem"), DeviceValueUOM::KB); publish_system_ha_sensor_config(DeviceValueType::INT, F("Free memory"), F("freemem"), DeviceValueUOM::KB);
publish_system_ha_sensor_config(DeviceValueType::INT, F("MQTT fails"), F("mqttfails"), DeviceValueUOM::NONE); publish_system_ha_sensor_config(DeviceValueType::INT, F("MQTT fails"), F("mqttfails"), DeviceValueUOM::NONE);
publish_system_ha_sensor_config(DeviceValueType::INT, F("Rx received"), F("rxreceived"), DeviceValueUOM::NONE); publish_system_ha_sensor_config(DeviceValueType::INT, F("Rx received"), F("rxreceived"), DeviceValueUOM::NONE);

View File

@@ -543,6 +543,18 @@ bool System::heartbeat_json(JsonObject & output) {
output["uptime"] = uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3); output["uptime"] = uuid::log::format_timestamp_ms(uuid::get_uptime_ms(), 3);
output["uptime_sec"] = uuid::get_uptime_sec(); output["uptime_sec"] = uuid::get_uptime_sec();
bool value_b = EMSESP::system_.ntp_connected();
if (Mqtt::ha_enabled()) {
char s[7];
output["ntp_status"] = Helpers::render_boolean(s, value_b); // for HA always render as string
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
output["ntp_status"] = value_b;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
output["ntp_status"] = value_b ? 1 : 0;
} else {
char s[7];
output["ntp_status"] = Helpers::render_boolean(s, value_b);
}
output["rxreceived"] = EMSESP::rxservice_.telegram_count(); output["rxreceived"] = EMSESP::rxservice_.telegram_count();
output["rxfails"] = EMSESP::rxservice_.telegram_error_count(); output["rxfails"] = EMSESP::rxservice_.telegram_error_count();
output["txreads"] = EMSESP::txservice_.telegram_read_count(); output["txreads"] = EMSESP::txservice_.telegram_read_count();
@@ -1323,4 +1335,13 @@ std::string System::reset_reason(uint8_t cpu) const {
return ("Unkonwn"); return ("Unkonwn");
} }
// set NTP status
void System::ntp_connected(bool b) {
if (b != ntp_connected_) {
LOG_INFO(b ? F("NTP connected") : F("NTP disconnected"));
}
ntp_connected_ = b;
ntp_last_check_ = b ? uuid::get_uptime_sec() : 0;
}
} // namespace emsesp } // namespace emsesp

View File

@@ -154,14 +154,13 @@ class System {
ethernet_connected_ = b; ethernet_connected_ = b;
} }
void ntp_connected(bool b) { void ntp_connected(bool b);
ntp_connected_ = b;
ntp_last_check_ = b ? uuid::get_uptime_sec() : 0;
}
bool ntp_connected() { bool ntp_connected() {
// timeout 2 hours, ntp sync is normally every hour. // timeout 2 hours, ntp sync is normally every hour.
ntp_connected_ = (uuid::get_uptime_sec() - ntp_last_check_ > 7201) ? false : ntp_connected_; if ((uuid::get_uptime_sec() - ntp_last_check_ > 7201) && ntp_connected_) {
ntp_connected(false);
}
return ntp_connected_; return ntp_connected_;
} }