#include "./diagnostics.h" #include #include namespace homekit::mqtt { static const char TOPIC_DIAGNOSTICS[] = "diag"; static const char TOPIC_INITIAL_DIAGNOSTICS[] = "d1ag"; void MqttDiagnosticsModule::onConnect(Mqtt &mqtt) { sendDiagnostics(mqtt); } void MqttDiagnosticsModule::onDisconnect(Mqtt &mqtt, espMqttClientTypes::DisconnectReason reason) { initialSent = false; } void MqttDiagnosticsModule::tick(Mqtt& mqtt) { if (!tickElapsed()) return; sendDiagnostics(mqtt); } void MqttDiagnosticsModule::sendDiagnostics(Mqtt& mqtt) { auto cfg = config::read(); if (!initialSent) { MqttInitialDiagnosticsPayload stat{ .ip = wifi::getIPAsInteger(), .fw_version = CONFIG_FW_VERSION, .rssi = wifi::getRSSI(), .free_heap = ESP.getFreeHeap(), .flags = DiagnosticsFlags{ .state = 1, .config_changed_value_present = 1, .config_changed = static_cast(cfg.flags.node_configured || cfg.flags.wifi_configured ? 1 : 0) } }; mqtt.publish(TOPIC_INITIAL_DIAGNOSTICS, reinterpret_cast(&stat), sizeof(stat)); initialSent = true; } else { MqttDiagnosticsPayload stat{ .rssi = wifi::getRSSI(), .free_heap = ESP.getFreeHeap(), .flags = DiagnosticsFlags{ .state = 1, .config_changed_value_present = 0, .config_changed = 0 } }; mqtt.publish(TOPIC_DIAGNOSTICS, reinterpret_cast(&stat), sizeof(stat)); } } }