Doxygen with github
Loading...
Searching...
No Matches
ksMqttConnector.h
1/*
2 * Copyright (c) 2021-2023, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFramework library.
5 * All licensing information can be found inside LICENSE.md file.
6 *
7 * https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
8 */
9
10#pragma once
11
12#include <string>
13#include <string_view>
14
15#include "../ksComponent.h"
16#include "../evt/ksEvent.h"
17#include "../ksSimpleTimer.h"
18
19#if (defined(ESP32) && ESP_ARDUINO_VERSION_MAJOR >= 3)
20 #define ksMqttConnectorNetClient_t NetworkClient
21 #define ksMqttConnectorNetClientSecure_t NetworkClientSecure
22#else
23 #define ksMqttConnectorNetClient_t WiFiClient
24 #define ksMqttConnectorNetClientSecure_t WiFiClientSecure
25#endif
26
27class PubSubClient;
28class ksCertFingerprint;
29class ksMqttConnectorNetClient_t;
30
31namespace ksf
32{
33 namespace comps
34 {
35 class ksWifiConnector;
45 {
46 KSF_RTTI_DECLARATIONS(ksMqttConnector, ksComponent)
47
48 protected:
49#if APP_LOG_ENABLED
50 ksApplication* app{nullptr};
51#endif
52 std::unique_ptr<ksMqttConnectorNetClient_t> netClientUq;
53 std::unique_ptr<PubSubClient> mqttClientUq;
54
55 std::weak_ptr<ksWifiConnector> wifiConnWp;
56
58 uint32_t reconnectCounter{0};
59
60 struct {
63 bool wasConnected : 1;
64 } bitflags = {true, false, true};
65
66 ksSimpleTimer reconnectTimer{KSF_MQTT_RECONNECT_DELAY_MS};
67
68 std::string login;
69 std::string password;
70 std::string prefix;
71 std::string broker;
72 uint16_t portNumber{1883};
73
74 std::unique_ptr<ksCertFingerprint> certFingerprint;
75
81 bool connectToBroker();
82
89
96 void mqttMessageInternal(const char* topic, const uint8_t* payload, uint32_t length);
97
98 public:
99 enum class QosLevel
100 {
101 QOS_AT_LEAST_ONCE,
102 QOS_EXACTLY_ONCE
103 };
104
105 DECLARE_KS_EVENT(onDeviceMessage, const std::string_view&, const std::string_view&)
106 DECLARE_KS_EVENT(onAnyMessage, const std::string_view&, const std::string_view&)
107
108 DECLARE_KS_EVENT(onConnected)
109 DECLARE_KS_EVENT(onDisconnected)
110
118
125 bool init(ksApplication* app) override;
126
135 bool postInit(ksApplication* app) override;
136
141 bool loop(ksApplication* app) override;
142
147 bool isConnected() const;
148
153 uint32_t getConnectionTimeSeconds() const;
154
159 uint32_t getReconnectCounter() const { return reconnectCounter; }
160
167 void subscribe(const std::string& topic, bool skipDevicePrefix = false, ksMqttConnector::QosLevel = ksMqttConnector::QosLevel::QOS_AT_LEAST_ONCE);
168
174 void unsubscribe(const std::string& topic, bool skipDevicePrefix = false);
175
183 void publish(const std::string& topic, const std::string& payload, bool retain = false, bool skipDevicePrefix = false);
184
193 void setupConnection(const std::string broker, const std::string& port, std::string login, std::string password, std::string prefix, const std::string& fingerprint);
194
199 };
200 }
201}
A component which is responsible for MQTT connection management.
Definition ksMqttConnector.h:45
std::weak_ptr< ksWifiConnector > wifiConnWp
Weak pointer to WiFi connector.
Definition ksMqttConnector.h:55
std::unique_ptr< WiFiClient > netClientUq
Shared pointer to WiFiClient used to connect to MQTT.
Definition ksMqttConnector.h:52
std::string prefix
Saved MQTT prefix.
Definition ksMqttConnector.h:70
void subscribe(const std::string &topic, bool skipDevicePrefix=false, ksMqttConnector::QosLevel=ksMqttConnector::QosLevel::QOS_AT_LEAST_ONCE)
Subscribes to MQTT topic.
Definition ksMqttConnector.cpp:134
uint32_t getReconnectCounter() const
Retrieves MQTT reconnect counter.
Definition ksMqttConnector.h:159
bool init(ksApplication *app) override
Instantiates the MQTT connector component.
Definition ksMqttConnector.cpp:44
uint64_t lastSuccessConnectionTime
Time of connection to MQTT broker in seconds.
Definition ksMqttConnector.h:57
std::unique_ptr< PubSubClient > mqttClientUq
Shared pointer to PubSubClient used to connect to MQTT.
Definition ksMqttConnector.h:53
virtual ~ksMqttConnector()
Destructor (for uniqueptr purposes).
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, const std::string_view & > > onAnyMessage
onAnyMessage event that user can bind to.
Definition ksMqttConnector.h:106
bool loop(ksApplication *app) override
Executes MQTT connection logic.
Definition ksMqttConnector.cpp:225
std::unique_ptr< ksCertFingerprint > certFingerprint
Shared pointer to fingerprint validator.
Definition ksMqttConnector.h:74
std::string login
Saved MQTT login.
Definition ksMqttConnector.h:68
bool usePersistentSession
Use persistent session or not.
Definition ksMqttConnector.h:62
uint32_t getConnectionTimeSeconds() const
Retrieves connection time in seconds.
Definition ksMqttConnector.cpp:260
bool sendConnectionStatus
Send connection status to MQTT or not.
Definition ksMqttConnector.h:61
void unsubscribe(const std::string &topic, bool skipDevicePrefix=false)
Unsubscribes the MQTT topic.
Definition ksMqttConnector.cpp:140
bool isConnected() const
Retrieves connection state.
Definition ksMqttConnector.cpp:255
void setupConnection(const std::string broker, const std::string &port, std::string login, std::string password, std::string prefix, const std::string &fingerprint)
Sets up MQTT connection.
Definition ksMqttConnector.cpp:68
std::shared_ptr< ksf::evt::ksEvent<> > onDisconnected
onDisconnected event that user can bind to.
Definition ksMqttConnector.h:109
void publish(const std::string &topic, const std::string &payload, bool retain=false, bool skipDevicePrefix=false)
Publishes a message to the MQTT topic.
Definition ksMqttConnector.cpp:145
uint16_t portNumber
Saved MQTT port number.
Definition ksMqttConnector.h:72
void mqttConnectedInternal()
Connects to the MQTT broker (internal function).
Definition ksMqttConnector.cpp:97
bool connectToBroker()
Connects to the MQTT broker.
Definition ksMqttConnector.cpp:162
ksSimpleTimer reconnectTimer
Timer that counts time between reconnection attempts.
Definition ksMqttConnector.h:66
bool postInit(ksApplication *app) override
Method that handles component post-initialization.
Definition ksMqttConnector.cpp:62
std::string broker
Saved MQTT broker.
Definition ksMqttConnector.h:71
std::shared_ptr< ksf::evt::ksEvent<> > onConnected
onConnected event that user can bind to.
Definition ksMqttConnector.h:108
void mqttMessageInternal(const char *topic, const uint8_t *payload, uint32_t length)
Called when MQTT message arrives (internal function).
Definition ksMqttConnector.cpp:104
std::string password
Saved MQTT password.
Definition ksMqttConnector.h:69
uint32_t reconnectCounter
MQTT reconnection counter.
Definition ksMqttConnector.h:58
bool wasConnected
True if connected in previous loop.
Definition ksMqttConnector.h:63
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, const std::string_view & > > onDeviceMessage
onDeviceMessage event that user can bind to.
Definition ksMqttConnector.h:105
A class that is a base for user-defined application.
Definition ksApplication.h:43
Base component class.
Definition ksComponent.h:51
Simple timer class, without using component architecture.
Definition ksSimpleTimer.h:27