Doxygen with github
Loading...
Searching...
No Matches
ksf::comps::ksMqttConnector Class Reference

A component which is responsible for MQTT connection management. More...

#include <ksMqttConnector.h>

+ Inheritance diagram for ksf::comps::ksMqttConnector:

Public Types

enum class  QosLevel { QOS_AT_LEAST_ONCE , QOS_EXACTLY_ONCE }
 

Public Member Functions

virtual const size_t getInstanceType () const
 Retrieves type ID of the object.
 
virtual bool isA (const size_t id) const
 Checks whether object is of given type.
 
 ksMqttConnector (bool sendConnectionStatus=true, bool usePersistentSession=false)
 Constructs 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 (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.
 
virtual ~ksMqttConnector ()
 Destructor (for uniqueptr purposes).
 
- Public Member Functions inherited from ksf::ksRtti
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 size_t getClassType ()
 
- Static Public Member Functions inherited from ksf::ksComponent
static const 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

std::unique_ptr< WiFiClient > 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< ksWifiConnectorwifiConnWp
 Weak pointer to WiFi connector.
 
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} 
 
ksSimpleTimer reconnectTimer { 10000UL }
 Timer that counts time between reconnection attempts.
 
std::string login
 Saved MQTT login.
 
std::string password
 Saved MQTT password.
 
std::string prefix
 Saved MQTT prefix.
 
std::string broker
 Saved MQTT broker.
 
uint16_t portNumber {1883}
 Saved MQTT port number.
 
std::unique_ptr< ksCertFingerprintcertFingerprint
 Shared pointer to fingerprint validator.
 
- Protected Attributes inherited from ksf::ksComponent
ksComponentState::TYPE componentState { ksComponentState::NotInitialized }
 

Detailed Description

A component which is responsible for MQTT connection management.

The component is responsible for MQTT prefix handling, connection time counting as well as connection status reporting. This component provides public methods that can be used to publish a message.

The user can register event handlers to receive connection state events as well as message arrival event.

Constructor & Destructor Documentation

◆ ksMqttConnector()

ksf::comps::ksMqttConnector::ksMqttConnector ( bool sendConnectionStatus = true,
bool usePersistentSession = false )

Constructs ksMqttConnector object.

Parameters
sendConnectionStatusIndicates whether last will message should be used to signal device state.
usePersistentSessionIndicates whether the session should be persistent.

References sendConnectionStatus, and usePersistentSession.

Member Function Documentation

◆ connectToBroker()

bool ksf::comps::ksMqttConnector::connectToBroker ( )
protected

Connects to the MQTT broker.

Returns
True on success, false on fail.

References broker, certFingerprint, login, mqttClientUq, netClientUq, password, portNumber, and prefix.

Referenced by loop().

◆ getConnectionTimeSeconds()

uint32_t ksf::comps::ksMqttConnector::getConnectionTimeSeconds ( ) const

Retrieves connection time in seconds.

Returns
MQTT connection time in seconds.

References isConnected(), and lastSuccessConnectionTime.

◆ getInstanceType()

virtual const size_t ksf::comps::ksMqttConnector::getInstanceType ( ) const
inlinevirtual

Retrieves type ID of the object.

Returns
Object type ID.

Reimplemented from ksf::ksComponent.

◆ getReconnectCounter()

uint32_t ksf::comps::ksMqttConnector::getReconnectCounter ( ) const
inline

Retrieves MQTT reconnect counter.

Returns
Number of MQTT reconnects.

References reconnectCounter.

◆ init()

bool ksf::comps::ksMqttConnector::init ( ksApplication * app)
overridevirtual

Instantiates the MQTT connector component.

Parameters
appPointer to the parent ksApplication.
Returns
True on success, false on fail.

Reimplemented from ksf::ksComponent.

References ksf::ksComponent::init(), mqttClientUq, and ksf::comps::ksMqttConfigProvider::setupMqttConnector().

◆ isA()

virtual bool ksf::comps::ksMqttConnector::isA ( const size_t id) const
inlinevirtual

Checks whether object is of given type.

Parameters
idType ID to check against.
Returns
True if object is of given type, otherwise false.

Reimplemented from ksf::ksComponent.

◆ isConnected()

bool ksf::comps::ksMqttConnector::isConnected ( ) const

Retrieves connection state.

Returns
True if connected, otherwise false.

References mqttClientUq.

Referenced by getConnectionTimeSeconds().

◆ loop()

bool ksf::comps::ksMqttConnector::loop ( ksApplication * app)
overridevirtual

◆ mqttConnectedInternal()

void ksf::comps::ksMqttConnector::mqttConnectedInternal ( )
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().

◆ mqttMessageInternal()

void ksf::comps::ksMqttConnector::mqttMessageInternal ( const char * topic,
const uint8_t * payload,
uint32_t length )
protected

Called when MQTT message arrives (internal function).

Parameters
topicTopic that received the message.
payloadPayload of the message.
lengthLength of the payload.

References ksf::evt::ksEvent< Params >::broadcast(), ksf::evt::ksEvent< Params >::isBound(), onAnyMessage, onDeviceMessage, and prefix.

Referenced by mqttConnectedInternal().

◆ postInit()

bool ksf::comps::ksMqttConnector::postInit ( ksApplication * app)
overridevirtual

Method that handles component post-initialization.

Used to setup callbacks.

Parameters
appPointer to the parent ksApplication.
Returns
True on success, false on fail.

Reimplemented from ksf::ksComponent.

References ksf::ksApplication::findComponent(), and wifiConnWp.

◆ publish()

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.

Parameters
topicTarget topic name.
payloadPayload to be transmitted.
retainTrue if this publish should be retained, otherwise false.
skipDevicePrefixTrue if device prefix shouldn't be inserted to the topic, false otherwise.

References mqttClientUq, and prefix.

◆ setupConnection()

void ksf::comps::ksMqttConnector::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.

Parameters
brokerMQTT broker address. Can be IP or hostname
portMQTT broker port
loginMQTT user login
passwordMQTT user password
fingerprintMQTT broker certificate fingerprint - if empty, secure connection won't be used

References broker, certFingerprint, login, mqttClientUq, netClientUq, password, portNumber, and prefix.

Referenced by ksf::comps::ksMqttConfigProvider::setupMqttConnector().

◆ subscribe()

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.

Parameters
topicTopic to subscribe
skipDevicePrefixTrue if device prefix shouldn't be inserted before passed topic, false otherwise.
qosQuality of service level (QOS).

References mqttClientUq, and prefix.

◆ unsubscribe()

void ksf::comps::ksMqttConnector::unsubscribe ( const std::string & topic,
bool skipDevicePrefix = false )

Unsubscribes the MQTT topic.

Parameters
topicTopic to be unsubscribed.
skipDevicePrefixTrue if the device prefix shouldn't be inserted to the topic, false othereide.

References mqttClientUq, and prefix.


The documentation for this class was generated from the following files: