switch PowerMeter implementations to DTU_LOG macros

This commit is contained in:
Bernhard Kirchen
2025-05-19 20:26:21 +02:00
committed by Bernhard Kirchen
parent 125043019f
commit a5de585ff7
11 changed files with 74 additions and 86 deletions

View File

@@ -16,13 +16,15 @@ namespace PowerMeters::Sml {
class Provider : public ::PowerMeters::Provider {
protected:
explicit Provider(char const* user)
: _user(user) { }
{
snprintf(_user, sizeof(_user), "%s/SML", user);
}
void reset();
void processSmlByte(uint8_t byte);
private:
std::string _user;
char _user[16];
DataPointContainer _dataInFlight;

View File

@@ -16,7 +16,7 @@ namespace PowerMeters::Sml::Http {
class Provider : public ::PowerMeters::Sml::Provider {
public:
explicit Provider(PowerMeterHttpSmlConfig const& cfg)
: ::PowerMeters::Sml::Provider("PowerMeterHttpSml")
: ::PowerMeters::Sml::Provider("HTTP")
, _cfg(cfg) { }
~Provider();

View File

@@ -9,7 +9,7 @@ namespace PowerMeters::Sml::Serial {
class Provider : public ::PowerMeters::Sml::Provider {
public:
Provider()
: ::PowerMeters::Sml::Provider("PowerMeterSerialSml") { }
: ::PowerMeters::Sml::Provider("Serial") { }
~Provider();

View File

@@ -1,12 +1,15 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <Utils.h>
#include <powermeter/json/http/Provider.h>
#include <MessageOutput.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <mbedtls/sha256.h>
#include <base64.h>
#include <ESPmDNS.h>
#include <LogHelper.h>
static const char* TAG = "powerMeter";
static const char* SUBTAG = "HTTP/JSON";
namespace PowerMeters::Json::Http {
@@ -45,8 +48,8 @@ bool Provider::init()
continue;
}
MessageOutput.printf("[PowerMeters::Json::Http] Initializing HTTP getter for value %d failed:\r\n", i + 1);
MessageOutput.printf("[PowerMeters::Json::Http] %s\r\n", _httpGetters[i]->getErrorText());
DTU_LOGE("Initializing HTTP getter for value %d failed: %s",
i + 1, _httpGetters[i]->getErrorText());
return false;
}
@@ -95,11 +98,11 @@ void Provider::pollingLoop()
lock.lock();
if (std::holds_alternative<String>(res)) {
MessageOutput.printf("[PowerMeters::Json::Http] %s\r\n", std::get<String>(res).c_str());
DTU_LOGE("%s", std::get<String>(res).c_str());
continue;
}
MessageOutput.printf("[PowerMeters::Json::Http] New total: %.2f\r\n", getPowerTotal());
DTU_LOGD("New total: %.2f", getPowerTotal());
}
}

View File

@@ -1,9 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <powermeter/json/mqtt/Provider.h>
#include <MqttSettings.h>
#include <MessageOutput.h>
#include <ArduinoJson.h>
#include <Utils.h>
#include <LogHelper.h>
static const char* TAG = "powerMeter";
static const char* SUBTAG = "MQTT";
namespace PowerMeters::Json::Mqtt {
@@ -80,10 +83,8 @@ void Provider::onMessage(Provider::MsgProperties const& properties,
}
}
if (_verboseLogging) {
MessageOutput.printf("[PowerMeters::Json::Mqtt] Topic '%s': new value: %5.2f, "
"total: %5.2f\r\n", topic, newValue, getPowerTotal());
}
DTU_LOGD("Topic '%s': new value: %5.2f, total: %5.2f",
topic, newValue, getPowerTotal());
}
} // namespace PowerMeters::Json::Mqtt

View File

@@ -1,7 +1,10 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <powermeter/sdm/serial/Provider.h>
#include <PinMapping.h>
#include <MessageOutput.h>
#include <LogHelper.h>
static const char* TAG = "powerMeter";
static const char* SUBTAG = "SDM";
namespace PowerMeters::Sdm::Serial {
@@ -30,12 +33,11 @@ bool Provider::init()
{
const PinMapping_t& pin = PinMapping.get();
MessageOutput.printf("[PowerMeters::Sdm::Serial] rx = %d, tx = %d, dere = %d, rxen = %d, txen = %d \r\n",
DTU_LOGI("rx = %d, tx = %d, dere = %d, rxen = %d, txen = %d",
pin.powermeter_rx, pin.powermeter_tx, pin.powermeter_dere, pin.powermeter_rxen, pin.powermeter_txen);
if (pin.powermeter_rx <= GPIO_NUM_NC || pin.powermeter_tx <= GPIO_NUM_NC) {
MessageOutput.println("[PowerMeters::Sdm::Serial] invalid pin config for SDM "
"power meter (RX and TX pins must be defined)");
DTU_LOGE("invalid pin config for SDM power meter (RX and TX pins must be defined)");
return false;
}
@@ -97,33 +99,25 @@ bool Provider::readValue(std::unique_lock<std::mutex>& lock, uint16_t reg, float
switch (err) {
case SDM_ERR_NO_ERROR:
if (_verboseLogging) {
MessageOutput.printf("[PowerMeters::Sdm::Serial]: read register %d "
"(0x%04x) successfully\r\n", reg, reg);
}
DTU_LOGD("read register %d (0x%04x) successfully", reg, reg);
targetVar = val;
return true;
break;
case SDM_ERR_CRC_ERROR:
MessageOutput.printf("[PowerMeters::Sdm::Serial]: CRC error "
"while reading register %d (0x%04x)\r\n", reg, reg);
DTU_LOGE("CRC error while reading register %d (0x%04x)", reg, reg);
break;
case SDM_ERR_WRONG_BYTES:
MessageOutput.printf("[PowerMeters::Sdm::Serial]: unexpected data in "
"message while reading register %d (0x%04x)\r\n", reg, reg);
DTU_LOGE("unexpected data in message while reading register %d (0x%04x)", reg, reg);
break;
case SDM_ERR_NOT_ENOUGHT_BYTES:
MessageOutput.printf("[PowerMeters::Sdm::Serial]: unexpected end of "
"message while reading register %d (0x%04x)\r\n", reg, reg);
DTU_LOGE("unexpected end of message while reading register %d (0x%04x)", reg, reg);
break;
case SDM_ERR_TIMEOUT:
MessageOutput.printf("[PowerMeters::Sdm::Serial]: timeout occured "
"while reading register %d (0x%04x)\r\n", reg, reg);
DTU_LOGE("timeout occured while reading register %d (0x%04x)", reg, reg);
break;
default:
MessageOutput.printf("[PowerMeters::Sdm::Serial]: unknown SDM error "
"code after reading register %d (0x%04x)\r\n", reg, reg);
DTU_LOGE("unknown SDM error code after reading register %d (0x%04x)", reg, reg);
break;
}
@@ -199,7 +193,7 @@ void Provider::pollingLoop()
}
}
MessageOutput.printf("[PowerMeters::Sdm::Serial] TotalPower: %5.2f\r\n", getPowerTotal());
DTU_LOGD("TotalPower: %5.2f", getPowerTotal());
}
}

View File

@@ -1,6 +1,9 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <powermeter/sml/Provider.h>
#include <MessageOutput.h>
#include <LogHelper.h>
static const char* TAG = "powerMeter";
#define SUBTAG _user
namespace PowerMeters::Sml {
@@ -20,10 +23,7 @@ void Provider::processSmlByte(uint8_t byte)
float helper = 0.0;
handler.decoder(helper);
if (_verboseLogging) {
MessageOutput.printf("[%s] decoded %s to %.2f\r\n",
_user.c_str(), handler.name, helper);
}
DTU_LOGD("decoded %s to %.2f", handler.name, helper);
switch (handler.target)
{
@@ -83,13 +83,11 @@ void Provider::processSmlByte(uint8_t byte)
case SML_FINAL:
_dataCurrent.updateFrom(_dataInFlight);
reset();
MessageOutput.printf("[%s] TotalPower: %5.2f\r\n",
_user.c_str(), getPowerTotal());
DTU_LOGD("TotalPower: %5.2f", getPowerTotal());
break;
case SML_CHECKSUM_ERROR:
reset();
MessageOutput.printf("[%s] checksum verification failed\r\n",
_user.c_str());
DTU_LOGE("checksum verification failed");
break;
default:
break;

View File

@@ -1,9 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <powermeter/sml/http/Provider.h>
#include <MessageOutput.h>
#include <WiFiClientSecure.h>
#include <base64.h>
#include <ESPmDNS.h>
#include <LogHelper.h>
static const char* TAG = "powerMeter";
static const char* SUBTAG = "HTTP/SML";
namespace PowerMeters::Sml::Http {
@@ -29,8 +32,7 @@ bool Provider::init()
if (_upHttpGetter->init()) { return true; }
MessageOutput.printf("[PowerMeters::Sml::Http] Initializing HTTP getter failed:\r\n");
MessageOutput.printf("[PowerMeters::Sml::Http] %s\r\n", _upHttpGetter->getErrorText());
DTU_LOGE("Initializing HTTP getter failed: %s", _upHttpGetter->getErrorText());
_upHttpGetter = nullptr;
@@ -79,7 +81,7 @@ void Provider::pollingLoop()
lock.lock();
if (!res.isEmpty()) {
MessageOutput.printf("[PowerMeters::Sml::Http] %s\r\n", res.c_str());
DTU_LOGE("%s", res.c_str());
continue;
}
}

View File

@@ -1,7 +1,10 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <powermeter/sml/serial/Provider.h>
#include <PinMapping.h>
#include <MessageOutput.h>
#include <LogHelper.h>
static const char* TAG = "powerMeter";
static const char* SUBTAG = "Serial/SML";
namespace PowerMeters::Sml::Serial {
@@ -9,11 +12,10 @@ bool Provider::init()
{
const PinMapping_t& pin = PinMapping.get();
MessageOutput.printf("[PowerMeters::Sml::Serial] rx = %d\r\n", pin.powermeter_rx);
DTU_LOGI("rx = %d", pin.powermeter_rx);
if (pin.powermeter_rx <= GPIO_NUM_NC) {
MessageOutput.println("[PowerMeters::Sml::Serial] invalid pin config "
"for serial SML power meter (RX pin must be defined)");
DTU_LOGE("invalid pin config for serial SML power meter (RX pin must be defined)");
return false;
}

View File

@@ -5,7 +5,10 @@
#include <powermeter/udp/smahm/Provider.h>
#include <Arduino.h>
#include <WiFiUdp.h>
#include <MessageOutput.h>
#include <LogHelper.h>
static const char* TAG = "powerMeter";
static const char* SUBTAG = "UDP/SMAHM";
namespace PowerMeters::Udp::SmaHM {
@@ -20,8 +23,7 @@ void Provider::Soutput(int kanal, int index, int art, int tarif,
{
if (!_verboseLogging) { return; }
MessageOutput.printf("[PowerMeters::Udp::SmaHM] %s = %.1f (timestamp %u)\r\n",
name, value, timestamp);
DTU_LOGD("%s = %.1f (timestamp %u)", name, value, timestamp);
}
bool Provider::init()
@@ -148,8 +150,7 @@ uint8_t* Provider::decodeGroup(uint8_t* offset, uint16_t grouplen)
continue;
}
MessageOutput.printf("[PowerMeters::Udp::SmaHM] Skipped unknown measurement: %d %d %d %d\r\n",
kanal, index, art, tarif);
DTU_LOGI("Skipped unknown measurement: %d %d %d %d", kanal, index, art, tarif);
offset += art;
}
@@ -169,7 +170,7 @@ void Provider::loop()
uint8_t buffer[1024];
int rSize = SMAUdp.read(buffer, 1024);
if (buffer[0] != 'S' || buffer[1] != 'M' || buffer[2] != 'A') {
MessageOutput.println("[PowerMeters::Udp::SmaHM] Not an SMA packet?");
DTU_LOGE("Not an SMA packet?");
return;
}
@@ -200,8 +201,7 @@ void Provider::loop()
continue;
}
MessageOutput.printf("[PowerMeters::Udp::SmaHM] Unhandled group 0x%04x with length %d\r\n",
grouptag, grouplen);
DTU_LOGW("Unhandled group 0x%04x with length %d", grouptag, grouplen);
offset += grouplen;
} while (grouplen > 0 && offset + 4 < buffer + rSize);
}

View File

@@ -5,7 +5,10 @@
#include <powermeter/udp/victron/Provider.h>
#include <Arduino.h>
#include <WiFiUdp.h>
#include <MessageOutput.h>
#include <LogHelper.h>
static const char* TAG = "powerMeter";
static const char* SUBTAG = "ModbusUDP/Victron";
namespace PowerMeters::Udp::Victron {
@@ -70,9 +73,7 @@ void Provider::sendModbusRequest()
_lastRequest = currentMillis;
if (_verboseLogging) {
MessageOutput.printf("[PowerMeters::Udp::Victron] sent modbus request\r\n");
}
DTU_LOGD("sent modbus request");
}
static float readInt16(uint8_t const** buffer, uint8_t factor)
@@ -109,18 +110,8 @@ void Provider::parseModbusResponse()
uint8_t const* p = buffer.data();
if (_verboseLogging) {
MessageOutput.printf("[PowerMeters::Udp::Victron] received %d bytes:", packetSize);
for (int i = 0; i < packetSize; i++) {
if (i % 16 == 0) {
MessageOutput.print("\r\n");
}
MessageOutput.printf("%02X ", buffer[i]);
}
MessageOutput.print("\r\n");
}
DTU_LOGD("received %d bytes", packetSize);
LogHelper::dumpBytes(TAG, SUBTAG, buffer.data(), packetSize);
uint16_t length = 0;
uint16_t protocolId = 0;
@@ -141,26 +132,24 @@ void Provider::parseModbusResponse()
p += 2;
if (!dataRemains(length)) {
MessageOutput.printf("[PowerMeters::Udp::Victron] unexpected end of packet\r\n");
DTU_LOGE("unexpected end of packet");
return;
}
if (currentTransactionId == sTransactionId) { break; }
MessageOutput.printf("[PowerMeters::Udp::Victron] skipping message "
"with unexpected transaction ID: %04X\r\n", currentTransactionId);
DTU_LOGI("skipping message with unexpected transaction ID: %04X", currentTransactionId);
p += length;
}
if (protocolId != 0x0000) {
MessageOutput.printf("[PowerMeters::Udp::Victron] invalid protocol ID: %04X\r\n", protocolId);
DTU_LOGE("invalid protocol ID: %04X", protocolId);
return;
}
uint16_t expectedLength = (sRegisterCount * 2) + 3;
if (length != expectedLength) {
MessageOutput.printf("[PowerMeters::Udp::Victron] unexpected length: %04X, "
"expected %04X\r\n", length, expectedLength);
DTU_LOGE("unexpected length: %04X, expected %04X", length, expectedLength);
return;
}
@@ -168,8 +157,7 @@ void Provider::parseModbusResponse()
p += 1;
if (unitId != sUnitId) {
MessageOutput.printf("[PowerMeters::Udp::Victron] unexpected unit ID: %02X, "
"expected %02X\r\n", unitId, sUnitId);
DTU_LOGE("unexpected unit ID: %02X, expected %02X", unitId, sUnitId);
return;
}
@@ -177,8 +165,7 @@ void Provider::parseModbusResponse()
p += 1;
if (functionCode != sFunctionCode) {
MessageOutput.printf("[PowerMeters::Udp::Victron] unexpected function code: %02X, "
"expected %02X\r\n", functionCode, sFunctionCode);
DTU_LOGE("unexpected function code: %02X, expected %02X", functionCode, sFunctionCode);
return;
}
@@ -187,8 +174,7 @@ void Provider::parseModbusResponse()
uint8_t expectedByteCount = sRegisterCount * 2;
if (byteCount != expectedByteCount) {
MessageOutput.printf("[PowerMeters::Udp::Victron] unexpected byte count: %02X, "
"expected %02X\r\n", byteCount, expectedByteCount);
DTU_LOGE("unexpected byte count: %02X, expected %02X", byteCount, expectedByteCount);
return;
}