KanoopProtocolQt 1.2.3
Qt HTTP operations and MQTT client library
Loading...
Searching...
No Matches
mqttclient.h
1#ifndef MQTTCLIENT_H
2#define MQTTCLIENT_H
3#include <QObject>
4
5#include <Kanoop/timespan.h>
6#include <Kanoop/utility/loggingbaseclass.h>
7#include <Kanoop/mqtt/mqttparameters.h>
8#include <Kanoop/kanoopprotocol.h>
9
10/** @brief MQTT client wrapper providing connection management, auto-reconnect, and SSL support. */
11class LIBKANOOPPROTOCOL_EXPORT MqttClient : public QObject,
12 public LoggingBaseClass
13{
14 Q_OBJECT
15public:
16 /** @brief Construct an MqttClient with default parameters.
17 * @param parent The parent QObject. */
18 MqttClient(QObject* parent = nullptr);
19 /** @brief Construct an MqttClient with the given MQTT parameters.
20 * @param parameters The connection parameters.
21 * @param parent The parent QObject. */
22 MqttClient(const MqttParameters& parameters, QObject* parent = nullptr);
23 /** @brief Destroy the MqttClient and release resources. */
24 virtual ~MqttClient();
25
26 /** @brief Start the MQTT client and optionally connect to the broker. */
27 virtual void start();
28 /** @brief Stop the MQTT client and disconnect from the broker. */
29 virtual void stop();
30
31 /** @brief Get the current MQTT connection parameters.
32 * @return The connection parameters. */
33 MqttParameters parameters() const { return _parameters; }
34 /** @brief Set the MQTT connection parameters.
35 * @param value The connection parameters. */
36 void setParameters(const MqttParameters& value) { _parameters = value; }
37
38 /** @brief Get whether the client connects automatically when started.
39 * @return True if auto-connect at start is enabled. */
40 bool connectAtStart() const { return _connectAtStart; }
41 /** @brief Set whether the client connects automatically when started.
42 * @param value True to enable auto-connect at start. */
43 void setConnectAtStart(bool value) { _connectAtStart = value; }
44
45 /** @brief Get whether automatic reconnection is enabled.
46 * @return True if auto-reconnect is enabled. */
47 bool autoReconnect() const { return _autoReconnect; }
48 /** @brief Set whether automatic reconnection is enabled.
49 * @param value True to enable auto-reconnect. */
50 void setAutoReconnect(bool value) { _autoReconnect = value; }
51
52 /** @brief Get the delay between automatic reconnection attempts.
53 * @return The reconnect interval. */
54 TimeSpan autoReconnectTime() const { return _autoReconnectTime; }
55 /** @brief Set the delay between automatic reconnection attempts.
56 * @param value The reconnect interval. */
57 void setAutoReconnectTime(const TimeSpan& value) { _autoReconnectTime = value; }
58
59 /** @brief Get the MQTT client identifier.
60 * @return The client ID string. */
61 QString clientId() const { return _clientId; }
62 /** @brief Set the MQTT client identifier.
63 * @param value The client ID string. */
64 void setClientId(const QString& value) { _clientId = value; }
65
66 /** @brief Check whether the client is currently connected to the broker.
67 * @return True if connected. */
68 bool isConnected() const { return _client != nullptr && _connected; }
69
70 /** @brief Subscribe to an MQTT topic.
71 * @param topic The topic filter string.
72 * @param qos The quality-of-service level (0, 1, or 2).
73 * @return A pointer to the resulting QMqttSubscription, or nullptr on failure. */
74 QMqttSubscription* subscribe(const QString& topic, uint8_t qos = 0);
75 /** @brief Publish a message to an MQTT topic.
76 * @param topic The topic to publish to.
77 * @param payload The message payload.
78 * @param qos The quality-of-service level (0, 1, or 2).
79 * @param retain True to set the retain flag on the message.
80 * @return The packet identifier of the published message, or -1 on failure. */
81 int publish(const QString& topic, const QByteArray& payload, uint8_t qos = 0, bool retain = false);
82
83protected:
84 /** @brief Get the underlying QMqttClient instance.
85 * @return A pointer to the QMqttClient. */
86 QMqttClient* client() const { return _client; }
87
88 /** @brief Called when the client successfully connects to the broker. */
89 virtual void connected() {}
90 /** @brief Called when the client disconnects from the broker. */
91 virtual void disconnected() {}
92 /** @brief Called when a client error occurs.
93 * @param error The error code. */
94 virtual void error(QMqttClient::ClientError error) { Q_UNUSED(error) }
95
96private:
97 MqttParameters _parameters;
98 bool _connectAtStart = true;
99 bool _autoReconnect = true;
100 TimeSpan _autoReconnectTime = TimeSpan::fromSeconds(1);
101 QString _clientId;
102 bool _connected = false;
103
104 QSslConfiguration _sslConfig;
105 QMqttClient* _client = nullptr;
106
107signals:
108 /** @brief Emitted when the client connects to the broker. */
110 /** @brief Emitted when the client disconnects from the broker. */
112 /** @brief Emitted when a client error occurs.
113 * @param error The error code. */
114 void clientError(QMqttClient::ClientError error);
115
116private slots:
117 void doConnect();
118 void onClientConnected();
119 void onDisconnected();
120 void onErrorChanged(QMqttClient::ClientError error);
121};
122
123#endif // MQTTCLIENT_H
MQTT client wrapper providing connection management, auto-reconnect, and SSL support.
Definition mqttclient.h:13
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.
Definition mqttclient.h:68
TimeSpan autoReconnectTime() const
Get the delay between automatic reconnection attempts.
Definition mqttclient.h:54
MqttClient(QObject *parent=nullptr)
Construct an MqttClient with default parameters.
QMqttClient * client() const
Get the underlying QMqttClient instance.
Definition mqttclient.h:86
virtual void connected()
Called when the client successfully connects to the broker.
Definition mqttclient.h:89
bool autoReconnect() const
Get whether automatic reconnection is enabled.
Definition mqttclient.h:47
void setClientId(const QString &value)
Set the MQTT client identifier.
Definition mqttclient.h:64
MqttClient(const MqttParameters &parameters, QObject *parent=nullptr)
Construct an MqttClient with the given MQTT parameters.
MqttParameters parameters() const
Get the current MQTT connection parameters.
Definition mqttclient.h:33
virtual void error(QMqttClient::ClientError error)
Called when a client error occurs.
Definition mqttclient.h:94
void clientConnected()
Emitted when the client connects to the broker.
bool connectAtStart() const
Get whether the client connects automatically when started.
Definition mqttclient.h:40
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.
Definition mqttclient.h:50
virtual ~MqttClient()
Destroy the MqttClient and release resources.
void setParameters(const MqttParameters &value)
Set the MQTT connection parameters.
Definition mqttclient.h:36
void setConnectAtStart(bool value)
Set whether the client connects automatically when started.
Definition mqttclient.h:43
void setAutoReconnectTime(const TimeSpan &value)
Set the delay between automatic reconnection attempts.
Definition mqttclient.h:57
virtual void disconnected()
Called when the client disconnects from the broker.
Definition mqttclient.h:91
QString clientId() const
Get the MQTT client identifier.
Definition mqttclient.h:61
Encapsulates connection parameters for an MQTT broker.