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
architectures=*
includes=MqttLogger.h
depends=PubSubClient
depends=espMqttClient

View File

@@ -7,7 +7,7 @@ MqttLogger::MqttLogger(MqttLoggerMode mode)
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->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)
@@ -74,9 +74,9 @@ void MqttLogger::sendBuffer()
if (this->bufferCnt > 0)
{
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)
{
doSerial = true;

View File

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