12#if THINGSBOARD_ENABLE_STREAM_UTILS
13#include <StreamUtils.h>
21char constexpr INVALID_BUFFER_SIZE[] =
"Send buffer size (%u) to small for the given payloads size (%u), increase with Set_Buffer_Size accordingly or install the StreamUtils library";
24#if THINGSBOARD_ENABLE_DYNAMIC
25char constexpr MAXIMUM_RESPONSE_EXCEEDED[] =
"Prevented allocation on the heap (%u) for JsonDocument. Discarding message that is bigger than maximum response size (%u)";
26char constexpr HEAP_ALLOCATION_FAILED[] =
"Failed allocating required size (%u) for JsonDocument. Ensure there is enough heap memory left";
30#if THINGSBOARD_ENABLE_DEBUG
31char constexpr RECEIVE_MESSAGE[] =
"Received (%u) bytes of data from server over topic (%s)";
32char constexpr ALLOCATING_JSON[] =
"Allocated internal JsonDocument for MQTT server response with size (%u)";
33char constexpr SEND_MESSAGE[] =
"Sending data to server over topic (%s) with data (%s)";
34char constexpr SEND_SERIALIZED[] =
"Hidden, because json data is bigger than buffer, therefore showing in console is skipped";
43#if THINGSBOARD_ENABLE_DYNAMIC
52template <
typename Logger = DefaultLogger>
64template<
size_t MaxResponse = DEFAULT_RESPONSE_AMOUNT,
size_t MaxEndpo
intsAmount = DEFAULT_ENDPOINT_AMOUNT,
typename Logger = DefaultLogger>
113 template<
typename... Args>
114#if THINGSBOARD_ENABLE_DYNAMIC
115#if THINGSBOARD_ENABLE_STREAM_UTILS
121#if THINGSBOARD_ENABLE_STREAM_UTILS
128 , m_max_stack(max_stack_size)
129#if THINGSBOARD_ENABLE_STREAM_UTILS
130 , m_buffering_size(buffering_size)
132#if THINGSBOARD_ENABLE_DYNAMIC
133 , m_max_response_size(max_response_size)
135 , m_api_implementations(args...)
137 for (
auto & api : m_api_implementations) {
138 if (api ==
nullptr) {
141#if THINGSBOARD_ENABLE_STL
142 api->Set_Client_Callbacks(std::bind(&
ThingsBoardSized::Subscribe_API_Implementation,
this, std::placeholders::_1), std::bind(&
ThingsBoardSized::Send_Json,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&
ThingsBoardSized::Send_Json_String,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::Subscribe_Topic,
this, std::placeholders::_1), std::bind(&ThingsBoardSized::Unsubscribe_Topic,
this, std::placeholders::_1), std::bind(&
ThingsBoardSized::Get_Receive_Buffer_Size,
this), std::bind(&
ThingsBoardSized::Get_Send_Buffer_Size,
this), std::bind(&
ThingsBoardSized::Set_Buffer_Size,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::Get_Last_Request_ID,
this));
144 api->Set_Client_Callbacks(ThingsBoardSized::Static_Subscribe_Implementation, ThingsBoardSized::Static_Send_Json, ThingsBoardSized::Static_Send_Json_String, ThingsBoardSized::Static_Subscribe_Topic, ThingsBoardSized::Static_Unsubscribe_Topic, ThingsBoardSized::Static_Get_Receive_Buffer_Size, ThingsBoardSized::Static_Get_Send_Buffer_Size, ThingsBoardSized::Static_Set_Buffer_Size, ThingsBoardSized::Static_Get_Last_Request_ID);
150#if THINGSBOARD_ENABLE_STL
151 m_client.
set_data_callback(std::bind(&ThingsBoardSized::On_MQTT_Message,
this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
152 m_client.
set_connect_callback(std::bind(&ThingsBoardSized::Resubscribe_Permanent_Subscriptionss,
this));
156 m_subscribedInstance =
this;
177 m_max_stack = max_stack_size;
186#if THINGSBOARD_ENABLE_STREAM_UTILS
190 size_t const & Get_Buffering_Size()
const {
191 return m_buffering_size;
200 void Set_Buffering_Size(
size_t const & buffering_size) {
201 m_buffering_size = buffering_size;
205#if THINGSBOARD_ENABLE_DYNAMIC
213 void Set_Max_Response_Size(
size_t const & max_response_size) {
214 m_max_response_size = max_response_size;
224 size_t const & Get_Max_Response_Size() {
225 return m_max_response_size;
231 bool const result = m_client.
set_buffer_size(receive_buffer_size, send_buffer_size);
256 for (
auto & api : m_api_implementations) {
257 if (api ==
nullptr) {
261 (void)api->Unsubscribe();
288 if (host ==
nullptr) {
322#if !THINGSBOARD_USE_ESP_TIMER
323 for (
auto & api : m_api_implementations) {
324 if (api ==
nullptr) {
330 return m_client.
loop();
342 bool Send_Json(
char const * topic, JsonDocument
const & source) {
345 if (source.isNull()) {
351 if (source.overflowed()) {
358#if THINGSBOARD_ENABLE_STREAM_UTILS
362#if THINGSBOARD_ENABLE_DEBUG
363 Logger::printfln(SEND_MESSAGE, topic, SEND_SERIALIZED);
365 result = Serialize_Json(topic, source, json_size - 1);
370 char* json =
new char[json_size]();
371 if (serializeJson(source, json, json_size) < json_size - 1) {
381 char json[json_size] = {};
382 if (serializeJson(source, json, json_size) < json_size - 1) {
399 if (json ==
nullptr) {
404 auto const json_size = strlen(json);
406 if (current_send_buffer_size < json_size) {
411#if THINGSBOARD_ENABLE_DEBUG
412 Logger::printfln(SEND_MESSAGE, topic, json);
414 return m_client.
publish(topic,
reinterpret_cast<uint8_t const *
>(json), json_size);
422#if !THINGSBOARD_ENABLE_DYNAMIC
423 if (m_api_implementations.
size() + 1 > m_api_implementations.
capacity()) {
428#if THINGSBOARD_ENABLE_STL
429 api.
Set_Client_Callbacks(std::bind(&
ThingsBoardSized::Subscribe_API_Implementation,
this, std::placeholders::_1), std::bind(&
ThingsBoardSized::Send_Json,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&
ThingsBoardSized::Send_Json_String,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::Subscribe_Topic,
this, std::placeholders::_1), std::bind(&ThingsBoardSized::Unsubscribe_Topic,
this, std::placeholders::_1), std::bind(&
ThingsBoardSized::Get_Receive_Buffer_Size,
this), std::bind(&
ThingsBoardSized::Get_Send_Buffer_Size,
this), std::bind(&
ThingsBoardSized::Set_Buffer_Size,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::Get_Last_Request_ID,
this));
431 api.
Set_Client_Callbacks(ThingsBoardSized::Static_Subscribe_Implementation, ThingsBoardSized::Static_Send_Json, ThingsBoardSized::Static_Send_Json_String, ThingsBoardSized::Static_Subscribe_Topic, ThingsBoardSized::Static_Unsubscribe_Topic, ThingsBoardSized::Static_Get_Receive_Buffer_Size, ThingsBoardSized::Static_Get_Send_Buffer_Size, ThingsBoardSized::Static_Set_Buffer_Size, ThingsBoardSized::Static_Get_Last_Request_ID);
446 template <
typename InputIterator>
448#if !THINGSBOARD_ENABLE_DYNAMIC
450 if (m_api_implementations.
size() + size > m_api_implementations.
capacity()) {
455 for (
auto it = first; it != last; ++it) {
457 if (api ==
nullptr) {
460#if THINGSBOARD_ENABLE_STL
461 api->Set_Client_Callbacks(std::bind(&
ThingsBoardSized::Subscribe_API_Implementation,
this, std::placeholders::_1), std::bind(&
ThingsBoardSized::Send_Json,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&
ThingsBoardSized::Send_Json_String,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::Subscribe_Topic,
this, std::placeholders::_1), std::bind(&ThingsBoardSized::Unsubscribe_Topic,
this, std::placeholders::_1), std::bind(&
ThingsBoardSized::Get_Receive_Buffer_Size,
this), std::bind(&
ThingsBoardSized::Get_Send_Buffer_Size,
this), std::bind(&
ThingsBoardSized::Set_Buffer_Size,
this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::Get_Last_Request_ID,
this));
463 api->Set_Client_Callbacks(ThingsBoardSized::Static_Subscribe_Implementation, ThingsBoardSized::Static_Send_Json, ThingsBoardSized::Static_Send_Json_String, ThingsBoardSized::Static_Subscribe_Topic, ThingsBoardSized::Static_Unsubscribe_Topic, ThingsBoardSized::Static_Get_Receive_Buffer_Size, ThingsBoardSized::Static_Get_Send_Buffer_Size, ThingsBoardSized::Static_Set_Buffer_Size, ThingsBoardSized::Static_Get_Last_Request_ID);
467 m_api_implementations.
insert(m_api_implementations.
end(), first, last);
485 bool Claim_Request(
size_t const & duration_ms,
char const * secret_key =
nullptr) {
486 StaticJsonDocument<JSON_OBJECT_SIZE(2)> request_buffer;
507 return Send_Key_Value_Pair(key, value);
521#if THINGSBOARD_ENABLE_DYNAMIC
522 template<
typename InputIterator>
524 template<
size_t MaxKeyValuePairAmount,
typename InputIterator>
527#if THINGSBOARD_ENABLE_DYNAMIC
528 return Send_Data_Array(first, last,
true);
530 return Send_Data_Array<MaxKeyValuePairAmount>(first, last,
true);
564 return Send_Key_Value_Pair(key, value,
false);
578#if THINGSBOARD_ENABLE_DYNAMIC
579 template<
typename InputIterator>
581 template<
size_t MaxKeyValuePairAmount,
typename InputIterator>
584#if THINGSBOARD_ENABLE_DYNAMIC
585 return Send_Data_Array(first, last,
false);
587 return Send_Data_Array<MaxKeyValuePairAmount>(first, last,
false);
610#if THINGSBOARD_ENABLE_DYNAMIC
616#if THINGSBOARD_ENABLE_STREAM_UTILS
625 bool Serialize_Json(
char const * topic, JsonDocument
const & source) {
627 if (!m_client.begin_publish(topic, json_size - 1)) {
631 BufferingPrint buffered_print(m_client, Get_Buffering_Size());
632 auto const bytes_serialized = serializeJson(source, buffered_print);
633 if (bytes_serialized < json_size - 1) {
637 buffered_print.flush();
638 return m_client.end_publish();
643 bool Subscribe_Topic(
char const * topic) {
648 bool Unsubscribe_Topic(
char const * topic) {
656 size_t * Get_Last_Request_ID() {
657 return &m_request_id;
669 bool Connect_To_Host(
char const * access_token,
char const * client_id,
char const * password) {
670 bool const connection_result = m_client.
connect(client_id, access_token, password);
671 if (!connection_result) {
674 return connection_result;
683 void Resubscribe_Permanent_Subscriptionss() {
684 for (
auto & api : m_api_implementations) {
685 if (api ==
nullptr) {
689 (void)api->Resubscribe_Permanent_Subscriptions();
700 bool Send_Key_Value_Pair(
char const * key, T
const & value,
bool telemetry =
true) {
706 StaticJsonDocument<JSON_OBJECT_SIZE(1)> json_buffer;
707 if (!t.SerializeKeyValue(json_buffer)) {
725#if THINGSBOARD_ENABLE_DYNAMIC
726 template<
typename InputIterator>
728 template<
size_t MaxKeyValuePairAmount,
typename InputIterator>
730 bool Send_Data_Array(InputIterator
const & first, InputIterator
const & last,
bool telemetry) {
732#if THINGSBOARD_ENABLE_DYNAMIC
736 TBJsonDocument json_buffer(JSON_OBJECT_SIZE(size));
738 if (size > MaxKeyValuePairAmount) {
742 StaticJsonDocument<JSON_OBJECT_SIZE(MaxKeyValuePairAmount)> json_buffer;
745#if THINGSBOARD_ENABLE_STL
751 for (
auto it = first; it != last; ++it) {
752 auto const & data = *it;
774 void On_MQTT_Message(
char * topic,
uint8_t * payload,
unsigned int length) {
775#if THINGSBOARD_ENABLE_DEBUG
776 Logger::printfln(RECEIVE_MESSAGE, length, topic);
779#if THINGSBOARD_ENABLE_STL
780#if THINGSBOARD_ENABLE_CXX20
781 auto filtered_raw_api_implementations = m_api_implementations | std::views::filter([&topic](
IAPI_Implementation const * api) {
783 IAPI_Container filtered_raw_api_implementations = {};
784 std::copy_if(m_api_implementations.begin(), m_api_implementations.end(), std::back_inserter(filtered_raw_api_implementations), [&topic](
IAPI_Implementation const * api) {
786 return (api != nullptr && api->Get_Process_Type() == API_Process_Type::RAW && api->Is_Response_Topic_Matching(topic));
789 for (
auto & api : filtered_raw_api_implementations) {
796 if (!filtered_raw_api_implementations.empty()) {
800 bool processed_response_as_raw =
false;
801 for (
auto & api : m_api_implementations) {
806 processed_response_as_raw =
true;
809 if (processed_response_as_raw) {
817#if THINGSBOARD_ENABLE_DYNAMIC
821 auto const document_size = JSON_OBJECT_SIZE(size);
822 auto const & max_response_size = Get_Max_Response_Size();
823 if (max_response_size != 0U && document_size > max_response_size) {
824 Logger::printfln(MAXIMUM_RESPONSE_EXCEEDED, document_size, max_response_size);
827 TBJsonDocument json_buffer(document_size);
830 if (json_buffer.capacity() != document_size) {
831 Logger::printfln(HEAP_ALLOCATION_FAILED, document_size);
835 if (size > MaxResponse) {
839 auto constexpr document_size = JSON_OBJECT_SIZE(MaxResponse);
840 StaticJsonDocument<document_size> json_buffer;
842#if THINGSBOARD_ENABLE_DEBUG
843 Logger::printfln(ALLOCATING_JSON, document_size);
849 DeserializationError
const error = deserializeJson(json_buffer, payload, length);
855#if THINGSBOARD_ENABLE_STL
856#if THINGSBOARD_ENABLE_CXX20
857 auto filtered_json_api_implementations = m_api_implementations | std::views::filter([&topic](
IAPI_Implementation const * api) {
859 IAPI_Container filtered_json_api_implementations = {};
860 std::copy_if(m_api_implementations.begin(), m_api_implementations.end(), std::back_inserter(filtered_json_api_implementations), [&topic](
IAPI_Implementation const * api) {
862 return (api != nullptr && api->Get_Process_Type() == API_Process_Type::JSON && api->Is_Response_Topic_Matching(topic));
865 for (
auto & api : filtered_json_api_implementations) {
869 for (
auto & api : m_api_implementations) {
878#if !THINGSBOARD_ENABLE_STL
879 static void On_Static_MQTT_Message(
char * topic,
uint8_t * payload,
unsigned int length) {
880 if (m_subscribedInstance ==
nullptr) {
883 m_subscribedInstance->On_MQTT_Message(topic, payload, length);
886 static void Static_MQTT_Connect() {
887 if (m_subscribedInstance ==
nullptr) {
890 m_subscribedInstance->Resubscribe_Permanent_Subscriptionss();
894 if (m_subscribedInstance ==
nullptr) {
897 m_subscribedInstance->Subscribe_API_Implementation(api);
900 static bool Static_Send_Json(
char const * topic, JsonDocument
const & source) {
901 if (m_subscribedInstance ==
nullptr) {
904 return m_subscribedInstance->Send_Json(topic, source);
907 static bool Static_Send_Json_String(
char const * topic,
char const * json) {
908 if (m_subscribedInstance ==
nullptr) {
911 return m_subscribedInstance->Send_Json_String(topic, json);
914 static bool Static_Subscribe_Topic(
char const * topic) {
915 if (m_subscribedInstance ==
nullptr) {
918 return m_subscribedInstance->Subscribe_Topic(topic);
921 static bool Static_Unsubscribe_Topic(
char const * topic) {
922 if (m_subscribedInstance ==
nullptr) {
925 return m_subscribedInstance->Unsubscribe_Topic(topic);
928 static size_t * Static_Get_Last_Request_ID() {
929 if (m_subscribedInstance ==
nullptr) {
932 return m_subscribedInstance->Get_Last_Request_ID();
935 static uint16_t Static_Get_Receive_Buffer_Size() {
936 if (m_subscribedInstance ==
nullptr) {
939 return m_subscribedInstance->Get_Receive_Buffer_Size();
942 static uint16_t Static_Get_Send_Buffer_Size() {
943 if (m_subscribedInstance ==
nullptr) {
946 return m_subscribedInstance->Get_Send_Buffer_Size();
949 static bool Static_Set_Buffer_Size(uint16_t receive_buffer_size, uint16_t send_buffer_size) {
950 if (m_subscribedInstance ==
nullptr) {
953 return m_subscribedInstance->Set_Buffer_Size(receive_buffer_size, send_buffer_size);
960 size_t m_max_stack = {};
961 size_t m_request_id = {};
962#if THINGSBOARD_ENABLE_STREAM_UTILS
963 size_t m_buffering_size = {};
965#if THINGSBOARD_ENABLE_DYNAMIC
966 size_t m_max_response_size = {};
968 IAPI_Container m_api_implementations = {};
971#if !THINGSBOARD_ENABLE_STL
972#if !THINGSBOARD_ENABLE_DYNAMIC
973template<
size_t MaxResponse,
size_t MaxEndpo
intsAmount,
typename Logger>
976template<
typename Logger>
char constexpr TOO_MANY_JSON_FIELDS[]
Definition: Constants.h:32
char constexpr JSON_SIZE_TO_SMALL[]
Definition: Constants.h:38
char constexpr UNABLE_TO_ALLOCATE_JSON[]
Definition: Constants.h:37
char constexpr UNABLE_TO_SERIALIZE_JSON[]
Definition: Constants.h:36
char constexpr UNABLE_TO_SERIALIZE[]
Definition: Constants.h:34
uint8_t constexpr DEFAULT_PAYLOAD_SIZE
Definition: Constants.h:20
uint16_t constexpr DEFAULT_MAX_STACK_SIZE
Definition: Constants.h:21
char constexpr CONNECT_FAILED[]
Definition: Constants.h:35
char constexpr MAX_SUBSCRIPTIONS_EXCEEDED[]
Definition: IAPI_Implementation.h:20
char constexpr ATTRIBUTE_TOPIC[]
Definition: IAPI_Implementation.h:29
char constexpr TELEMETRY_TOPIC[]
Definition: IAPI_Implementation.h:33
MQTT_Connection_Error
Possible error states the MQTT broker connection can be in, if connecting the device to the MQTT brok...
Definition: MQTT_Connection_Error.h:9
MQTT_Connection_State
Possible states the MQTT broker connection can be in.
Definition: MQTT_Connection_State.h:11
uint16_t constexpr DEFAULT_MQTT_PORT
Definition: ThingsBoard.h:17
char constexpr INVALID_BUFFER_SIZE[]
Definition: ThingsBoard.h:21
char constexpr PROV_ACCESS_TOKEN[]
Definition: ThingsBoard.h:18
char constexpr UNABLE_TO_DE_SERIALIZE_JSON[]
Definition: ThingsBoard.h:20
char constexpr UNABLE_TO_ALLOCATE_BUFFER[]
Definition: ThingsBoard.h:22
char constexpr DURATION_KEY[]
Definition: ThingsBoard.h:40
char constexpr SECRET_KEY[]
Definition: ThingsBoard.h:39
char constexpr MAX_ENDPOINTS_AMOUNT_TEMPLATE_NAME[]
Definition: ThingsBoard.h:23
char constexpr CLAIM_TOPIC[]
Definition: ThingsBoard.h:37
char constexpr API_SUBSCRIPTIONS[]
Definition: ThingsBoard.h:28
std::function< return_type(argument_types... arguments)> function
Callback signature.
Definition: Callback.h:34
Custom std::array or std::vector implementation that contains a partial vector-like interface impleme...
Definition: Container.h:29
size_type size() const
Gets the current amount of elements in the underlying data container.
Definition: Container.h:147
void insert(iterator position, InputIterator const &first, InputIterator const &last)
Copies all elements from the given start to exclusively the given end iterator into the underlying da...
Definition: Container.h:257
size_type constexpr capacity() const
Gets the maximum amount of elements that can be stored in the underlying data container.
Definition: Container.h:157
void push_back(const_reference element)
Appends the given element at the end of the underlying data container.
Definition: Container.h:230
iterator end()
Returns a iterator to one-past-the-last element of the underlying data container.
Definition: Container.h:209
static size_t distance(InputIterator const &first, InputIterator const &last)
Calculates the distance between two iterators.
Definition: Helper.h:85
static bool String_IsNull_Or_Empty(char const *str)
Returns wheter the given string is either a nullptr or is an empty string, meaning it only contains a...
Definition: Helper.cpp:27
static size_t Measure_Json(TSource const &source)
Calculates the total size of the string the serializeJson method would produce including the null end...
Definition: Helper.h:73
static size_t Calculate_Symbol_Occurences(uint8_t const *bytes, char symbol, uint32_t length)
Returns the amount of occurences of the given smybol in the given byte payload.
Definition: Helper.cpp:10
Base functionality required by all API implementation.
Definition: IAPI_Implementation.h:37
virtual void Initialize()=0
Method that allows to construct internal objects, after the required callback member methods have bee...
virtual void Process_Json_Response(char const *topic, JsonDocument const &data)=0
Process callback that will be called upon response arrival.
virtual void Set_Client_Callbacks(Callback< void, IAPI_Implementation & >::function subscribe_api_callback, Callback< bool, char const *const, JsonDocument const & >::function send_json_callback, Callback< bool, char const *const, char const *const >::function send_json_string_callback, Callback< bool, char const *const >::function subscribe_topic_callback, Callback< bool, char const *const >::function unsubscribe_topic_callback, Callback< uint16_t >::function get_receive_size_callback, Callback< uint16_t >::function get_send_size_callback, Callback< bool, uint16_t, uint16_t >::function set_buffer_size_callback, Callback< size_t * >::function get_request_id_callback)=0
Sets the underlying callbacks that are required for the different API Implementation to communicate w...
virtual API_Process_Type Get_Process_Type() const =0
Returns the way the server response should be processed.
virtual bool Is_Response_Topic_Matching(char const *topic) const =0
Compares received response topic and the topic this api implementation handles responses on,...
virtual void Process_Response(char const *topic, uint8_t *payload, uint32_t length)=0
Process callback that will be called upon response arrival.
MQTT Client interface that contains the method that a class that can be used to send and receive data...
Definition: IMQTT_Client.h:36
virtual void disconnect()=0
Force disconnects from the previously connected server and should release all used resources.
virtual bool loop()=0
Receives / sends any outstanding messages from and to the MQTT broker.
virtual bool connect(char const *client_id, char const *user_name, char const *password)=0
Connects to the previously with set_server configured server instance and port with the given credent...
virtual void subscribe_connection_state_changed_callback(Callback< void, MQTT_Connection_State, MQTT_Connection_Error >::function callback)=0
Sets the callback that is called, whenever the underlying state of our connection with the MQTT broke...
virtual MQTT_Connection_Error get_last_connection_error() const =0
Allows to deciper the reason for a failure, while attempting to establish a connection to the MQTT br...
virtual uint16_t get_receive_buffer_size()=0
Gets the previously set size of the internal buffer size meant for incoming MQTT data.
virtual void set_data_callback(Callback< void, char *, uint8_t *, unsigned int >::function callback)=0
Sets the callback that is called, if any message is received by the MQTT broker.
virtual bool unsubscribe(char const *topic)=0
Unsubscribes to previously subscribed MQTT messages of the given topic.
virtual bool set_buffer_size(uint16_t receive_buffer_size, uint16_t send_buffer_size)=0
Changes the size of the buffer for sent and received MQTT messages.
virtual MQTT_Connection_State get_connection_state() const =0
Get the current connection state to the server includes possible intermediate states between connecti...
virtual void set_server(char const *domain, uint16_t port)=0
Configures the server and port that the client should connect with over MQTT.
virtual bool publish(char const *topic, uint8_t const *payload, size_t const &length)=0
Sends the given payload over the previously established connection.
virtual uint16_t get_send_buffer_size()=0
Gets the previously set size of the internal buffer size meant for outgoing MQTT data.
virtual bool connected()=0
Returns our current connection status to MQTT, true meaning we are connected, false meaning we have b...
virtual bool subscribe(char const *topic)=0
Subscribes to MQTT message on the given topic, which will cause an internal callback to be called for...
virtual void set_connect_callback(Callback< void >::function callback)=0
Sets the callback that is called, if we have successfully established a connection with the MQTT brok...
Telemetry record class, allows to store different data using a common interface.
Definition: Telemetry.h:16
bool SerializeKeyValue(TSource &source) const
Serializes a key-value pair or only a value, depending on the constructor used.
Definition: Telemetry.h:82
Wrapper around any arbitrary MQTT Client implementing the IMQTT_Client interface, to allow connecting...
Definition: ThingsBoard.h:66
size_t const & Get_Maximum_Stack_Size() const
Returns the maximum amount of bytes that we want to allocate on the stack, before the memory is alloc...
Definition: ThingsBoard.h:182
bool Send_Telemetry_String(char const *json)
Send string containing json as telemetry data. See https://thingsboard.io/docs/user-guide/telemetry/ ...
Definition: ThingsBoard.h:539
bool Send_Attributes(InputIterator const &first, InputIterator const &last)
Send aggregated key-value pair as attribute data.
Definition: ThingsBoard.h:583
void Set_Maximum_Stack_Size(size_t const &max_stack_size)
Sets the maximum amount of bytes that we want to allocate on the stack, before the memory is allocate...
Definition: ThingsBoard.h:176
bool Send_Json_String(char const *topic, char const *json)
Sends key-value pairs from the given json string over the given topic.
Definition: ThingsBoard.h:398
bool Send_Json(char const *topic, JsonDocument const &source)
Sends key-value pairs from the given JsonDocument over the given topic.
Definition: ThingsBoard.h:342
bool connect(char const *host, char const *access_token=PROV_ACCESS_TOKEN, uint16_t port=DEFAULT_MQTT_PORT, char const *client_id=nullptr, char const *password=nullptr)
Connects to the given server instance and port with the given credentials.
Definition: ThingsBoard.h:287
bool Send_Attribute_Json(JsonDocument const &source)
Send key-value pairs as attribute data. See https://thingsboard.io/docs/user-guide/attribute/ for mor...
Definition: ThingsBoard.h:605
bool Claim_Request(size_t const &duration_ms, char const *secret_key=nullptr)
Send a claiming request for this device.
Definition: ThingsBoard.h:485
void Subscribe_Connection_State_Changed_Callback(Callback< void, MQTT_Connection_State, MQTT_Connection_Error >::function callback)
Sets the callback that is called, whenever the underlying state of our connection with the MQTT broke...
Definition: ThingsBoard.h:316
MQTT_Connection_Error Get_Last_Connection_Error()
Allows to deciper the reason for a failure, while attempting to establish a connection to the MQTT br...
Definition: ThingsBoard.h:311
bool Set_Buffer_Size(uint16_t receive_buffer_size, uint16_t send_buffer_size)
Changes the size of the buffer for sent and received MQTT messages.
Definition: ThingsBoard.h:230
ThingsBoardSized(IMQTT_Client &client, uint16_t receive_buffer_size=DEFAULT_PAYLOAD_SIZE, uint16_t send_buffer_size=DEFAULT_PAYLOAD_SIZE, size_t const &max_stack_size=DEFAULT_MAX_STACK_SIZE, Args const &... args)
Constructs a instance with the given network client that should be used to establish the connection t...
Definition: ThingsBoard.h:124
uint16_t Get_Receive_Buffer_Size()
Gets the previously set size of the internal buffer size meant for incoming MQTT data.
Definition: ThingsBoard.h:239
bool loop()
Receives / sends any outstanding messages from and to the MQTT broker.
Definition: ThingsBoard.h:321
bool Send_Attribute_Data(char const *key, T const &value)
Sends the given key-value pair as attribute data. See https://thingsboard.io/docs/user-guide/attribut...
Definition: ThingsBoard.h:563
IMQTT_Client & Get_Client()
Gets the registered underlying MQTT Client implementation.
Definition: ThingsBoard.h:165
MQTT_Connection_State Get_Connection_State()
Get the current connection state to the server includes possible intermediate states between connecti...
Definition: ThingsBoard.h:306
bool Send_Telemetry(InputIterator const &first, InputIterator const &last)
Send aggregated key-value pair as telemetry data.
Definition: ThingsBoard.h:526
void Subscribe_API_Implementations(InputIterator const &first, InputIterator const &last)
Subscribes the given API implementation.
Definition: ThingsBoard.h:447
uint16_t Get_Send_Buffer_Size()
Gets the previously set size of the internal buffer size meant for outgoing MQTT data.
Definition: ThingsBoard.h:244
void Subscribe_API_Implementation(IAPI_Implementation &api)
Subscribes the given API implementation.
Definition: ThingsBoard.h:421
void disconnect()
Force disconnects from the previously connected server and should release all used resources.
Definition: ThingsBoard.h:296
bool connected()
Returns our current connection status to MQTT, true meaning we are connected, false meaning we have b...
Definition: ThingsBoard.h:301
bool Send_Telemetry_Data(char const *key, T const &value)
Sends the given key-value pair as telemetry data. See https://thingsboard.io/docs/user-guide/telemetr...
Definition: ThingsBoard.h:506
bool Send_Attribute_String(char const *json)
Send string containing json as attribute data. See https://thingsboard.io/docs/user-guide/attribute/ ...
Definition: ThingsBoard.h:596
bool Send_Telemetry_Json(JsonDocument const &source)
Send key-value pairs as telemetry data. See https://thingsboard.io/docs/user-guide/telemetry/ for mor...
Definition: ThingsBoard.h:548
void Cleanup_Subscriptions()
Clears all currently subscribed callbacks and unsubscribed from all currently subscribed MQTT topics.
Definition: ThingsBoard.h:255