Use ESP Logging Macros for mqtt

This commit is contained in:
Thomas Basler
2025-04-18 18:09:06 +02:00
parent d0e8dbe61f
commit c5e97a5c7b
2 changed files with 25 additions and 21 deletions

View File

@@ -3,10 +3,12 @@
* Copyright (C) 2022-2025 Thomas Basler and others
*/
#include "MqttHandleInverter.h"
#include "MessageOutput.h"
#include "MqttSettings.h"
#include <ctime>
#undef TAG
static const char* TAG = "mqtt";
#define PUBLISH_MAX_INTERVAL 60000
MqttHandleInverterClass MqttHandleInverter;
@@ -169,7 +171,7 @@ void MqttHandleInverterClass::onMqttMessage(Topic t, const espMqttClientTypes::M
auto inv = Hoymiles.getInverterBySerial(serial);
if (inv == nullptr) {
MessageOutput.printf("Inverter not found\n");
ESP_LOGW(TAG, "Inverter not found");
return;
}
@@ -178,7 +180,7 @@ void MqttHandleInverterClass::onMqttMessage(Topic t, const espMqttClientTypes::M
try {
payload_val = std::stof(strValue);
} catch (std::invalid_argument const& e) {
MessageOutput.printf("MQTT handler: cannot parse payload of topic '%s' as float: %s\n",
ESP_LOGW(TAG, "MQTT handler: cannot parse payload of topic '%s' as float: %s",
topic, strValue.c_str());
return;
}
@@ -186,59 +188,59 @@ void MqttHandleInverterClass::onMqttMessage(Topic t, const espMqttClientTypes::M
switch (t) {
case Topic::LimitPersistentRelative:
// Set inverter limit relative persistent
MessageOutput.printf("Limit Persistent: %.1f %%\n", payload_val);
ESP_LOGI(TAG, "Limit Persistent: %.1f %%", payload_val);
inv->sendActivePowerControlRequest(payload_val, PowerLimitControlType::RelativPersistent);
break;
case Topic::LimitPersistentAbsolute:
// Set inverter limit absolute persistent
MessageOutput.printf("Limit Persistent: %.1f W\n", payload_val);
ESP_LOGI(TAG, "Limit Persistent: %.1f W", payload_val);
inv->sendActivePowerControlRequest(payload_val, PowerLimitControlType::AbsolutPersistent);
break;
case Topic::LimitNonPersistentRelative:
// Set inverter limit relative non persistent
MessageOutput.printf("Limit Non-Persistent: %.1f %%\n", payload_val);
ESP_LOGI(TAG, "Limit Non-Persistent: %.1f %%", payload_val);
if (!properties.retain) {
inv->sendActivePowerControlRequest(payload_val, PowerLimitControlType::RelativNonPersistent);
} else {
MessageOutput.printf("Ignored because retained\n");
ESP_LOGW(TAG, "Ignored because retained");
}
break;
case Topic::LimitNonPersistentAbsolute:
// Set inverter limit absolute non persistent
MessageOutput.printf("Limit Non-Persistent: %.1f W\n", payload_val);
ESP_LOGI(TAG, "Limit Non-Persistent: %.1f W", payload_val);
if (!properties.retain) {
inv->sendActivePowerControlRequest(payload_val, PowerLimitControlType::AbsolutNonPersistent);
} else {
MessageOutput.printf("Ignored because retained\n");
ESP_LOGW(TAG, "Ignored because retained");
}
break;
case Topic::Power:
// Turn inverter on or off
MessageOutput.printf("Set inverter power to: %" PRId32 "\n", static_cast<int32_t>(payload_val));
ESP_LOGI(TAG, "Set inverter power to: %" PRId32 "", static_cast<int32_t>(payload_val));
inv->sendPowerControlRequest(static_cast<int32_t>(payload_val) > 0);
break;
case Topic::Restart:
// Restart inverter
MessageOutput.printf("Restart inverter\n");
ESP_LOGI(TAG, "Restart inverter");
if (!properties.retain && payload_val == 1) {
inv->sendRestartControlRequest();
} else {
MessageOutput.printf("Ignored because retained or numeric value not '1'\n");
ESP_LOGW(TAG, "Ignored because retained or numeric value not '1'");
}
break;
case Topic::ResetRfStats:
// Reset RF Stats
MessageOutput.printf("Reset RF stats\n");
ESP_LOGI(TAG, "Reset RF stats");
if (!properties.retain && payload_val == 1) {
inv->resetRadioStats();
} else {
MessageOutput.printf("Ignored because retained or numeric value not '1'\n");
ESP_LOGW(TAG, "Ignored because retained or numeric value not '1'");
}
}
}

View File

@@ -4,10 +4,12 @@
*/
#include "MqttSettings.h"
#include "Configuration.h"
#include "MessageOutput.h"
#include <frozen/map.h>
#include <frozen/string.h>
#undef TAG
static const char* TAG = "mqtt";
MqttSettingsClass::MqttSettingsClass()
{
}
@@ -16,11 +18,11 @@ void MqttSettingsClass::NetworkEvent(network_event event)
{
switch (event) {
case network_event::NETWORK_GOT_IP:
MessageOutput.printf("Network connected\n");
ESP_LOGD(TAG, "Network connected");
performConnect();
break;
case network_event::NETWORK_DISCONNECTED:
MessageOutput.printf("Network lost connection\n");
ESP_LOGD(TAG, "Network lost connection");
_mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
break;
default:
@@ -30,7 +32,7 @@ void MqttSettingsClass::NetworkEvent(network_event event)
void MqttSettingsClass::onMqttConnect(const bool sessionPresent)
{
MessageOutput.printf("Connected to MQTT.\n");
ESP_LOGI(TAG, "Connected to MQTT.");
const CONFIG_T& config = Configuration.get();
publish(config.Mqtt.Lwt.Topic, config.Mqtt.Lwt.Value_Online);
@@ -76,7 +78,7 @@ void MqttSettingsClass::onMqttDisconnect(espMqttClientTypes::DisconnectReason re
auto it = reasons.find(reason);
const char* reasonStr = (it != reasons.end()) ? it->second.data() : "Unknown";
MessageOutput.printf("Disconnected from MQTT. Reason: %s\n", reasonStr);
ESP_LOGW(TAG, "Disconnected from MQTT. Reason: %s", reasonStr);
_mqttReconnectTimer.once(
2, +[](MqttSettingsClass* instance) { instance->performConnect(); }, this);
@@ -84,7 +86,7 @@ void MqttSettingsClass::onMqttDisconnect(espMqttClientTypes::DisconnectReason re
void MqttSettingsClass::onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, const size_t len, const size_t index, const size_t total)
{
MessageOutput.printf("Received MQTT message on topic: %s\n", topic);
ESP_LOGD(TAG, "Received MQTT message on topic: %s", topic);
_mqttSubscribeParser.handle_message(properties, topic, payload, len);
}
@@ -104,7 +106,7 @@ void MqttSettingsClass::performConnect()
return;
}
MessageOutput.printf("Connecting to MQTT...\n");
ESP_LOGI(TAG, "Connecting to MQTT...");
const CONFIG_T& config = Configuration.get();
const String willTopic = getPrefix() + config.Mqtt.Lwt.Topic;
String clientId = getClientId();