Doxygen with github
|
A component responsible for managing MQTT connections. More...
#include <ksMqttConnector.h>
Public Types | |
enum class | QosLevel { QOS_AT_LEAST_ONCE , QOS_EXACTLY_ONCE } |
Public Member Functions | |
virtual const std::size_t | getInstanceType () const |
Retrieves type ID of the object. | |
virtual bool | isA (const std::size_t id) const |
Checks whether object is of given type. | |
ksMqttConnector (bool sendConnectionStatus=true, bool usePersistentSession=false) | |
Constructs ksMqttConnector object. | |
virtual | ~ksMqttConnector () |
Destructs ksMqttConnector object. | |
bool | init (ksApplication *app) override |
Instantiates the MQTT connector component. | |
bool | postInit (ksApplication *app) override |
Method that handles component post-initialization. | |
bool | loop (ksApplication *app) override |
Executes MQTT connection logic. | |
bool | isConnected () const |
Retrieves connection state. | |
uint32_t | getConnectionTimeSeconds () const |
Retrieves connection time in seconds. | |
uint32_t | getReconnectCounter () const |
Retrieves MQTT reconnect counter. | |
void | subscribe (const std::string &topic, bool skipDevicePrefix=false, ksMqttConnector::QosLevel=ksMqttConnector::QosLevel::QOS_AT_LEAST_ONCE) |
Subscribes to MQTT topic. | |
void | unsubscribe (const std::string &topic, bool skipDevicePrefix=false) |
Unsubscribes the MQTT topic. | |
void | publish (const std::string &topic, const std::string &payload, bool retain=false, bool skipDevicePrefix=false) |
Publishes a message to the MQTT topic. | |
void | setupConnection (std::string broker, const std::string &port, std::string login, std::string password, std::string prefix, const std::string &fingerprint) |
Sets up MQTT connection. | |
![]() | |
![]() | |
virtual | ~ksRtti ()=default |
Destructor. | |
template<typename TType > | |
TType * | as () |
Tries to cast object to the type provided as a template parameter. | |
template<typename TType > | |
const TType * | as () const |
Tries to cast object to the type provided as a template parameter (const version). | |
Static Public Member Functions | |
static const std::size_t | getClassType () |
![]() | |
static const std::size_t | getClassType () |
Public Attributes | |
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, const std::string_view & > > | onDeviceMessage {std::make_shared<ksf::evt::ksEvent<const std::string_view&, const std::string_view&>>()} |
onDeviceMessage event that user can bind to. | |
std::shared_ptr< ksf::evt::ksEvent< const std::string_view &, const std::string_view & > > | onAnyMessage {std::make_shared<ksf::evt::ksEvent<const std::string_view&, const std::string_view&>>()} |
onAnyMessage event that user can bind to. | |
std::shared_ptr< ksf::evt::ksEvent<> > | onConnected {std::make_shared<ksf::evt::ksEvent<>>()} |
onConnected event that user can bind to. | |
std::shared_ptr< ksf::evt::ksEvent<> > | onDisconnected {std::make_shared<ksf::evt::ksEvent<>>()} |
onDisconnected event that user can bind to. | |
Protected Member Functions | |
bool | connectToBroker () |
Connects to the MQTT broker. | |
void | mqttConnectedInternal () |
Connects to the MQTT broker (internal function). | |
void | mqttMessageInternal (const char *topic, const uint8_t *payload, uint32_t length) |
Called when MQTT message arrives (internal function). | |
Protected Attributes | ||
misc::ksDomainQuery | domainResolver | |
Domain query used to resolve MQTT broker address. | ||
std::unique_ptr< ksMqttConnectorNetClient_t > | netClientUq | |
Shared pointer to WiFiClient used to connect to MQTT. | ||
std::unique_ptr< PubSubClient > | mqttClientUq | |
Shared pointer to PubSubClient used to connect to MQTT. | ||
std::weak_ptr< ksWifiConnector > | wifiConnWp | |
Weak pointer to WiFi connector. | ||
misc::ksSimpleTimer | reconnectTimer | |
Timer that counts time between reconnection attempts. | ||
uint64_t | lastSuccessConnectionTime {0} | |
Time of connection to MQTT broker in seconds. | ||
uint32_t | reconnectCounter {0} | |
MQTT reconnection counter. | ||
struct { | ||
bool sendConnectionStatus: 1 | ||
Send connection status to MQTT or not. | ||
bool usePersistentSession: 1 | ||
Use persistent session or not. | ||
bool wasConnected: 1 | ||
True if connected in previous loop. | ||
} | bitflags = {true, false, true} | |
std::string | login | |
Saved MQTT login. | ||
std::string | password | |
Saved MQTT password. | ||
std::string | prefix | |
Saved MQTT prefix. | ||
uint16_t | portNumber {1883} | |
Saved MQTT port number. | ||
std::unique_ptr< misc::ksCertFingerprint > | certFingerprint | |
Shared pointer to fingerprint validator. | ||
![]() | ||
ksComponentState::TYPE | componentState { ksComponentState::NotInitialized } | |
Holds current state of the component. | ||
A component responsible for managing MQTT connections.
This component handles MQTT prefix management, connection time tracking, and status reporting. It also provides public methods for publishing messages.
Users can register event handlers to receive connection state updates and message arrival events.
ksf::comps::ksMqttConnector::ksMqttConnector | ( | bool | sendConnectionStatus = true, |
bool | usePersistentSession = false ) |
Constructs ksMqttConnector object.
sendConnectionStatus | Indicates whether last will message should be used to signal device state. |
usePersistentSession | Indicates whether the session should be persistent. |
References sendConnectionStatus, and usePersistentSession.
|
protected |
Connects to the MQTT broker.
References certFingerprint, domainResolver, ksf::misc::ksDomainQuery::getResolvedIP(), login, mqttClientUq, netClientUq, password, portNumber, and prefix.
Referenced by loop().
uint32_t ksf::comps::ksMqttConnector::getConnectionTimeSeconds | ( | ) | const |
Retrieves connection time in seconds.
References isConnected(), and lastSuccessConnectionTime.
|
inlinevirtual |
|
inline |
|
overridevirtual |
Instantiates the MQTT connector component.
app | Pointer to the parent ksApplication. |
Reimplemented from ksf::ksComponent.
References ksf::ksComponent::init(), mqttClientUq, and ksf::comps::ksMqttConfigProvider::setupMqttConnector().
|
inlinevirtual |
Checks whether object is of given type.
id | Type ID to check against. |
Reimplemented from ksf::ksComponent.
bool ksf::comps::ksMqttConnector::isConnected | ( | ) | const |
Retrieves connection state.
References mqttClientUq.
Referenced by getConnectionTimeSeconds().
|
overridevirtual |
Executes MQTT connection logic.
Reimplemented from ksf::ksComponent.
References ksf::evt::ksEvent< Params >::broadcast(), connectToBroker(), domainResolver, ksf::misc::ksSimpleTimer::hasTimePassed(), ksf::misc::ksDomainQuery::invalidate(), mqttClientUq, mqttConnectedInternal(), onDisconnected, ksf::misc::ksDomainQuery::process(), reconnectCounter, reconnectTimer, ksf::misc::ksSimpleTimer::restart(), and wifiConnWp.
|
protected |
Connects to the MQTT broker (internal function).
Binds onMessage callback, calls bound onConnected callbacks and is used to configures parameters.
References ksf::evt::ksEvent< Params >::broadcast(), lastSuccessConnectionTime, mqttClientUq, mqttMessageInternal(), and onConnected.
Referenced by loop().
|
protected |
Called when MQTT message arrives (internal function).
topic | Topic that received the message. |
payload | Payload of the message. |
length | Length of the payload. |
References ksf::evt::ksEvent< Params >::broadcast(), ksf::evt::ksEvent< Params >::isBound(), onAnyMessage, onDeviceMessage, and prefix.
Referenced by mqttConnectedInternal().
|
overridevirtual |
Method that handles component post-initialization.
Used to setup callbacks.
app | Pointer to the parent ksApplication. |
Reimplemented from ksf::ksComponent.
References ksf::ksApplication::findComponent(), and wifiConnWp.
void ksf::comps::ksMqttConnector::publish | ( | const std::string & | topic, |
const std::string & | payload, | ||
bool | retain = false, | ||
bool | skipDevicePrefix = false ) |
Publishes a message to the MQTT topic.
topic | Target topic name. |
payload | Payload to be transmitted. |
retain | True if this publish should be retained, otherwise false. |
skipDevicePrefix | True if device prefix shouldn't be inserted to the topic, false otherwise. |
References mqttClientUq, and prefix.
void ksf::comps::ksMqttConnector::setupConnection | ( | std::string | broker, |
const std::string & | port, | ||
std::string | login, | ||
std::string | password, | ||
std::string | prefix, | ||
const std::string & | fingerprint ) |
Sets up MQTT connection.
broker | MQTT broker address. Can be IP or hostname |
port | MQTT broker port |
login | MQTT user login |
password | MQTT user password |
fingerprint | MQTT broker certificate fingerprint - if empty, secure connection won't be used |
References certFingerprint, domainResolver, login, mqttClientUq, netClientUq, password, portNumber, prefix, and ksf::misc::ksDomainQuery::setDomain().
Referenced by ksf::comps::ksMqttConfigProvider::setupMqttConnector().
void ksf::comps::ksMqttConnector::subscribe | ( | const std::string & | topic, |
bool | skipDevicePrefix = false, | ||
ksMqttConnector::QosLevel | qos = ksMqttConnector::QosLevel::QOS_AT_LEAST_ONCE ) |
Subscribes to MQTT topic.
topic | Topic to subscribe |
skipDevicePrefix | True if device prefix shouldn't be inserted before passed topic, false otherwise. |
qos | Quality of service level (QOS). |
References mqttClientUq, and prefix.
void ksf::comps::ksMqttConnector::unsubscribe | ( | const std::string & | topic, |
bool | skipDevicePrefix = false ) |
Unsubscribes the MQTT topic.
topic | Topic to be unsubscribed. |
skipDevicePrefix | True if the device prefix shouldn't be inserted to the topic, false othereide. |
References mqttClientUq, and prefix.