Doxygen with github
|
A component which is responsible for MQTT connection management. More...
#include <ksMqttConnector.h>
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< ksWifiConnector > | wifiConnWp | |
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< ksCertFingerprint > | certFingerprint | |
Shared pointer to fingerprint validator. | ||
Protected Attributes inherited from ksf::ksComponent | ||
ksComponentState::TYPE | componentState { ksComponentState::NotInitialized } | |
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.
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.
Referenced by getInstanceType().
|
protected |
Connects to the MQTT broker.
References broker, certFingerprint, 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 |
Retrieves type ID of the object.
Reimplemented from ksf::ksComponent.
References ksMqttConnector().
|
inline |
Retrieves MQTT reconnect counter.
References getReconnectCounter(), and reconnectCounter.
Referenced by getReconnectCounter().
|
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 connectToBroker(), mqttClientUq, mqttConnectedInternal(), onDisconnected, reconnectCounter, reconnectTimer, and wifiConnWp.
|
protected |
Connects to the MQTT broker (internal function).
Binds onMessage callback, calls bound onConnected callbacks and is used to configures parameters.
References 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 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 | ( | 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.
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 broker, certFingerprint, login, mqttClientUq, netClientUq, password, portNumber, and prefix.
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.