1#ifndef Shared_Attribute_Update_h
2#define Shared_Attribute_Update_h
10#if !THINGSBOARD_ENABLE_DYNAMIC
18#if THINGSBOARD_ENABLE_DYNAMIC
19template <
typename Logger = DefaultLogger>
24template<
size_t MaxSubscriptions = DEFAULT_SUBSCRIPTION_AMOUNT,
size_t MaxAttributes = DEFAULT_ATTRIBUTES_AMOUNT,
typename Logger = DefaultLogger>
27#if THINGSBOARD_ENABLE_DYNAMIC
54 template<
typename InputIterator>
56#if !THINGSBOARD_ENABLE_DYNAMIC
58 if (m_shared_attribute_update_callbacks.
size() + size > m_shared_attribute_update_callbacks.
capacity()) {
65 m_shared_attribute_update_callbacks.
insert(m_shared_attribute_update_callbacks.
end(), first, last);
79#if !THINGSBOARD_ENABLE_DYNAMIC
80 if (m_shared_attribute_update_callbacks.
size() + 1U > m_shared_attribute_update_callbacks.
capacity()) {
86 m_shared_attribute_update_callbacks.
push_back(callback);
95 m_shared_attribute_update_callbacks.
clear();
100 return API_Process_Type::JSON;
108 JsonObjectConst
object = data.template as<JsonObjectConst>();
113#if THINGSBOARD_ENABLE_STL
114#if THINGSBOARD_ENABLE_CXX20
115 auto filtered_shared_attribute_update_callbacks = m_shared_attribute_update_callbacks | std::views::filter([&
object](
Callback_Value const & shared_attribute) {
118 std::copy_if(m_shared_attribute_update_callbacks.
begin(), m_shared_attribute_update_callbacks.
end(), std::back_inserter(filtered_shared_attribute_update_callbacks), [&
object](
Callback_Value const & shared_attribute) {
120 return (shared_attribute.Get_Attributes().empty() || std::find_if(shared_attribute.Get_Attributes().begin(), shared_attribute.Get_Attributes().end(), [&object](const char * att) {
121 return object.containsKey(att);
125 for (
auto const & shared_attribute : filtered_shared_attribute_update_callbacks) {
127 for (
auto const & shared_attribute : m_shared_attribute_update_callbacks) {
134 char const * requested_att =
nullptr;
142 if (
object.containsKey(att)) {
150 if (requested_att ==
nullptr) {
167 if (!m_shared_attribute_update_callbacks.
empty() && !m_subscribe_topic_callback.Call_Callback(
ATTRIBUTE_TOPIC)) {
174#if !THINGSBOARD_USE_ESP_TIMER
184 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)
override {
185 m_subscribe_topic_callback.Set_Callback(subscribe_topic_callback);
186 m_unsubscribe_topic_callback.Set_Callback(unsubscribe_topic_callback);
192 Callback_Container m_shared_attribute_update_callbacks = {};
API_Process_Type
Possible processing types an API Implementation uses to handle responses from the server.
Definition: API_Process_Type.h:19
char constexpr MAX_SUBSCRIPTIONS_TEMPLATE_NAME[]
Definition: IAPI_Implementation.h:22
char constexpr SHARED_RESPONSE_KEY[]
Definition: IAPI_Implementation.h:31
char constexpr SUBSCRIBE_TOPIC_FAILED[]
Definition: IAPI_Implementation.h:23
char constexpr MAX_SUBSCRIPTIONS_EXCEEDED[]
Definition: IAPI_Implementation.h:20
char constexpr ATTRIBUTE_TOPIC[]
Definition: IAPI_Implementation.h:29
char constexpr SHARED_ATTRIBUTE_UPDATE_SUBSCRIPTIONS[]
Definition: Shared_Attribute_Update.h:11
General purpose safe callback wrapper. Expects either c-style or c++ style function pointer,...
Definition: Callback.h:30
std::function< return_type(argument_types... arguments)> function
Callback signature.
Definition: Callback.h:34
return_type Call_Callback(argument_types const &... arguments) const
Calls the callback that was subscribed, when this class instance was initally created.
Definition: Callback.h:62
size_type size() const
Gets the current amount of elements in the underlying data container.
Definition: Container.h:147
iterator begin()
Returns an iterator to the first element of the underlying data container. If the array is empty,...
Definition: Container.h:165
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
bool empty() const
Returns whether there are any elements in the underlying data container.
Definition: Container.h:141
void clear()
Erases all elements from the container. After this call, size() returns zero.
Definition: Container.h:330
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
Base functionality required by all API implementation.
Definition: IAPI_Implementation.h:37
Shared attribute update callback wrapper, contains the needed configuration settings to create the re...
Definition: Shared_Attribute_Callback.h:20
CString_Container const & Get_Attributes() const
Gets all the subscribed shared attributes that will result in the callback method being called if any...
Definition: Shared_Attribute_Callback.h:57
Handles the internal implementation of the ThingsBoard shared Attribute Update API....
Definition: Shared_Attribute_Update.h:26
void loop() override
Internal loop method to update inernal timers for API calls that can timeout.
Definition: Shared_Attribute_Update.h:175
bool Shared_Attributes_Subscribe(InputIterator const &first, InputIterator const &last)
Subscribes shared attribute callbacks, that will be called if an update for the containing shared att...
Definition: Shared_Attribute_Update.h:55
bool Resubscribe_Permanent_Subscriptions() override
Forwards the call to let the API clear up any ongoing single-event subscriptions (Provision,...
Definition: Shared_Attribute_Update.h:166
bool Unsubscribe() override
Unsubcribes all callbacks, to clear up any ongoing subscriptions and stop receiving information over ...
Definition: Shared_Attribute_Update.h:162
void Initialize() override
Method that allows to construct internal objects, after the required callback member methods have bee...
Definition: Shared_Attribute_Update.h:180
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) override
Sets the underlying callbacks that are required for the different API Implementation to communicate w...
Definition: Shared_Attribute_Update.h:184
Shared_Attribute_Update()=default
Constructor.
void Process_Response(char const *topic, uint8_t *payload, uint32_t length) override
Process callback that will be called upon response arrival.
Definition: Shared_Attribute_Update.h:103
API_Process_Type Get_Process_Type() const override
Returns the way the server response should be processed.
Definition: Shared_Attribute_Update.h:99
void Process_Json_Response(char const *topic, JsonDocument const &data) override
Process callback that will be called upon response arrival.
Definition: Shared_Attribute_Update.h:107
bool Shared_Attributes_Unsubscribe()
Unsubcribes all shared attribute callbacks. See https://thingsboard.io/docs/reference/mqtt-api/#subsc...
Definition: Shared_Attribute_Update.h:94
bool Shared_Attributes_Subscribe(Callback_Value const &callback)
Subscribes a shared attribute callback, that will be called if an update for the containing shared at...
Definition: Shared_Attribute_Update.h:78
~Shared_Attribute_Update() override=default
bool Is_Response_Topic_Matching(char const *topic) const override
Compares received response topic and the topic this api implementation handles responses on,...
Definition: Shared_Attribute_Update.h:158