5#include <Kanoop/timespan.h>
6#include <Kanoop/utility/loggingbaseclass.h>
7#include <Kanoop/mqtt/mqttparameters.h>
8#include <Kanoop/kanoopprotocol.h>
11class LIBKANOOPPROTOCOL_EXPORT
MqttClient :
public QObject,
12 public LoggingBaseClass
68 bool isConnected()
const {
return _client !=
nullptr && _connected; }
74 QMqttSubscription*
subscribe(
const QString& topic, uint8_t qos = 0);
81 int publish(
const QString& topic,
const QByteArray& payload, uint8_t qos = 0,
bool retain =
false);
86 QMqttClient*
client()
const {
return _client; }
94 virtual void error(QMqttClient::ClientError error) { Q_UNUSED(error) }
98 bool _connectAtStart =
true;
99 bool _autoReconnect =
true;
100 TimeSpan _autoReconnectTime = TimeSpan::fromSeconds(1);
102 bool _connected =
false;
104 QSslConfiguration _sslConfig;
105 QMqttClient* _client =
nullptr;
118 void onClientConnected();
119 void onDisconnected();
120 void onErrorChanged(QMqttClient::ClientError error);
MQTT client wrapper providing connection management, auto-reconnect, and SSL support.
int publish(const QString &topic, const QByteArray &payload, uint8_t qos=0, bool retain=false)
Publish a message to an MQTT topic.
bool isConnected() const
Check whether the client is currently connected to the broker.
TimeSpan autoReconnectTime() const
Get the delay between automatic reconnection attempts.
MqttClient(QObject *parent=nullptr)
Construct an MqttClient with default parameters.
QMqttClient * client() const
Get the underlying QMqttClient instance.
virtual void connected()
Called when the client successfully connects to the broker.
bool autoReconnect() const
Get whether automatic reconnection is enabled.
void setClientId(const QString &value)
Set the MQTT client identifier.
MqttClient(const MqttParameters ¶meters, QObject *parent=nullptr)
Construct an MqttClient with the given MQTT parameters.
MqttParameters parameters() const
Get the current MQTT connection parameters.
virtual void error(QMqttClient::ClientError error)
Called when a client error occurs.
void clientConnected()
Emitted when the client connects to the broker.
bool connectAtStart() const
Get whether the client connects automatically when started.
void clientError(QMqttClient::ClientError error)
Emitted when a client error occurs.
virtual void stop()
Stop the MQTT client and disconnect from the broker.
void clientDisconnected()
Emitted when the client disconnects from the broker.
virtual void start()
Start the MQTT client and optionally connect to the broker.
QMqttSubscription * subscribe(const QString &topic, uint8_t qos=0)
Subscribe to an MQTT topic.
void setAutoReconnect(bool value)
Set whether automatic reconnection is enabled.
virtual ~MqttClient()
Destroy the MqttClient and release resources.
void setParameters(const MqttParameters &value)
Set the MQTT connection parameters.
void setConnectAtStart(bool value)
Set whether the client connects automatically when started.
void setAutoReconnectTime(const TimeSpan &value)
Set the delay between automatic reconnection attempts.
virtual void disconnected()
Called when the client disconnects from the broker.
QString clientId() const
Get the MQTT client identifier.
Encapsulates connection parameters for an MQTT broker.