remove NetworkDevice dependency from MqttLogger library

This commit is contained in:
Luca Oliano
2024-02-04 19:05:26 +01:00
parent de117f5a06
commit c716afb254
8 changed files with 38 additions and 13 deletions

View File

@@ -8,4 +8,4 @@ category=Communication
url=https://github.com/androbi-com/MqttLogger url=https://github.com/androbi-com/MqttLogger
architectures=* architectures=*
includes=MqttLogger.h includes=MqttLogger.h
depends=PubSubClient depends=espMqttClient

View File

@@ -7,7 +7,7 @@ MqttLogger::MqttLogger(MqttLoggerMode mode)
this->setBufferSize(MQTT_MAX_PACKET_SIZE); this->setBufferSize(MQTT_MAX_PACKET_SIZE);
} }
MqttLogger::MqttLogger(NetworkDevice* client, const char* topic, MqttLoggerMode mode) MqttLogger::MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode)
{ {
this->setClient(client); this->setClient(client);
this->setTopic(topic); this->setTopic(topic);
@@ -19,9 +19,9 @@ MqttLogger::~MqttLogger()
{ {
} }
void MqttLogger::setClient(NetworkDevice* client) void MqttLogger::setClient(MqttClient& client)
{ {
this->client = client; this->client = &client;
} }
void MqttLogger::setTopic(const char* topic) void MqttLogger::setTopic(const char* topic)
@@ -74,9 +74,9 @@ void MqttLogger::sendBuffer()
if (this->bufferCnt > 0) if (this->bufferCnt > 0)
{ {
bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial; bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial;
if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->mqttConnected()) if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->connected())
{ {
this->client->mqttPublish(topic, 0, true, (uint8_t*)this->buffer, this->bufferCnt); this->client->publish(topic, 0, true, this->buffer, this->bufferCnt);
} else if (this->mode == MqttLoggerMode::MqttAndSerialFallback) } else if (this->mode == MqttLoggerMode::MqttAndSerialFallback)
{ {
doSerial = true; doSerial = true;

View File

@@ -11,7 +11,7 @@
#include <Arduino.h> #include <Arduino.h>
#include <Print.h> #include <Print.h>
#include "../../../networkDevices/NetworkDevice.h" #include <espMqttClient.h>
#define MQTT_MAX_PACKET_SIZE 1024 #define MQTT_MAX_PACKET_SIZE 1024
@@ -29,16 +29,16 @@ private:
uint8_t* buffer; uint8_t* buffer;
uint8_t* bufferEnd; uint8_t* bufferEnd;
uint16_t bufferCnt = 0, bufferSize = 0; uint16_t bufferCnt = 0, bufferSize = 0;
NetworkDevice* client; MqttClient* client;
MqttLoggerMode mode; MqttLoggerMode mode;
void sendBuffer(); void sendBuffer();
public: public:
MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
MqttLogger(NetworkDevice* client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
~MqttLogger(); ~MqttLogger();
void setClient(NetworkDevice* client); void setClient(MqttClient& client);
void setTopic(const char* topic); void setTopic(const char* topic);
void setMode(MqttLoggerMode mode); void setMode(MqttLoggerMode mode);
void setRetained(boolean retained); void setRetained(boolean retained);

View File

@@ -57,7 +57,7 @@ EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* preferen
String pathStr = preferences->getString(preference_mqtt_lock_path); String pathStr = preferences->getString(preference_mqtt_lock_path);
pathStr.concat(mqtt_topic_log); pathStr.concat(mqtt_topic_log);
strcpy(_path, pathStr.c_str()); strcpy(_path, pathStr.c_str());
Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial); Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial);
} }
} }
@@ -342,3 +342,14 @@ void EthLan8720Device::disableMqtt()
_mqttEnabled = false; _mqttEnabled = false;
} }
MqttClient *EthLan8720Device::getMqttClient() const
{
if (_useEncryption)
{
return _mqttClientSecure;
}
else
{
return _mqttClient;
}
}

View File

@@ -69,6 +69,7 @@ public:
private: private:
void onDisconnected(); void onDisconnected();
MqttClient *getMqttClient() const;
espMqttClient* _mqttClient = nullptr; espMqttClient* _mqttClient = nullptr;
espMqttClientSecure* _mqttClientSecure = nullptr; espMqttClientSecure* _mqttClientSecure = nullptr;

View File

@@ -61,7 +61,7 @@ void W5500Device::initialize()
_path = new char[pathStr.length() + 1]; _path = new char[pathStr.length() + 1];
memset(_path, 0, sizeof(_path)); memset(_path, 0, sizeof(_path));
strcpy(_path, pathStr.c_str()); strcpy(_path, pathStr.c_str());
Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial); Log = new MqttLogger(_mqttClient, _path, MqttLoggerMode::MqttAndSerial);
} }
reconnect(); reconnect();

View File

@@ -51,7 +51,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const I
String pathStr = preferences->getString(preference_mqtt_lock_path); String pathStr = preferences->getString(preference_mqtt_lock_path);
pathStr.concat(mqtt_topic_log); pathStr.concat(mqtt_topic_log);
strcpy(_path, pathStr.c_str()); strcpy(_path, pathStr.c_str());
Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial); Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial);
} }
} }
@@ -367,3 +367,15 @@ void WifiDevice::disableMqtt()
_mqttEnabled = false; _mqttEnabled = false;
} }
MqttClient *WifiDevice::getMqttClient() const
{
if (_useEncryption)
{
return _mqttClientSecure;
}
else
{
return _mqttClient;
}
}

View File

@@ -61,6 +61,7 @@ private:
static void clearRtcInitVar(WiFiManager*); static void clearRtcInitVar(WiFiManager*);
void onDisconnected(); void onDisconnected();
MqttClient *getMqttClient() const;
WiFiManager _wm; WiFiManager _wm;
espMqttClient* _mqttClient = nullptr; espMqttClient* _mqttClient = nullptr;