ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Shared_Attribute_Update.h
Go to the documentation of this file.
1#ifndef Shared_Attribute_Update_h
2#define Shared_Attribute_Update_h
3
4// Local includes.
7
8
9// Log messages.
10#if !THINGSBOARD_ENABLE_DYNAMIC
11char constexpr SHARED_ATTRIBUTE_UPDATE_SUBSCRIPTIONS[] = "shared attribute update";
12#endif // !THINGSBOARD_ENABLE_DYNAMIC
13
14
18#if THINGSBOARD_ENABLE_DYNAMIC
19template <typename Logger = DefaultLogger>
20#else
24template<size_t MaxSubscriptions = DEFAULT_SUBSCRIPTION_AMOUNT, size_t MaxAttributes = DEFAULT_ATTRIBUTES_AMOUNT, typename Logger = DefaultLogger>
25#endif // THINGSBOARD_ENABLE_DYNAMIC
27#if THINGSBOARD_ENABLE_DYNAMIC
30#else
33#endif // THINGSBOARD_ENABLE_DYNAMIC
34
35 public:
38
39 ~Shared_Attribute_Update() override = default;
40
54 template<typename InputIterator>
55 bool Shared_Attributes_Subscribe(InputIterator const & first, InputIterator const & last) {
56#if !THINGSBOARD_ENABLE_DYNAMIC
57 auto const size = Helper::distance(first, last);
58 if (m_shared_attribute_update_callbacks.size() + size > m_shared_attribute_update_callbacks.capacity()) {
60 return false;
61 }
62#endif // !THINGSBOARD_ENABLE_DYNAMIC
63 (void)m_subscribe_topic_callback.Call_Callback(ATTRIBUTE_TOPIC);
64 // Push back complete vector into our local m_shared_attribute_update_callbacks vector.
65 m_shared_attribute_update_callbacks.insert(m_shared_attribute_update_callbacks.end(), first, last);
66 return true;
67 }
68
79#if !THINGSBOARD_ENABLE_DYNAMIC
80 if (m_shared_attribute_update_callbacks.size() + 1U > m_shared_attribute_update_callbacks.capacity()) {
82 return false;
83 }
84#endif // !THINGSBOARD_ENABLE_DYNAMIC
85 (void)m_subscribe_topic_callback.Call_Callback(ATTRIBUTE_TOPIC);
86 m_shared_attribute_update_callbacks.push_back(callback);
87 return true;
88 }
89
95 m_shared_attribute_update_callbacks.clear();
96 return m_unsubscribe_topic_callback.Call_Callback(ATTRIBUTE_TOPIC);
97 }
98
100 return API_Process_Type::JSON;
101 }
102
103 void Process_Response(char const * topic, uint8_t * payload, uint32_t length) override {
104 // Nothing to do
105 }
106
107 void Process_Json_Response(char const * topic, JsonDocument const & data) override {
108 JsonObjectConst object = data.template as<JsonObjectConst>();
109 if (object.containsKey(SHARED_RESPONSE_KEY)) {
110 object = object[SHARED_RESPONSE_KEY];
111 }
112
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) {
116#else
117 Callback_Container filtered_shared_attribute_update_callbacks = {};
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) {
119#endif // THINGSBOARD_ENABLE_CXX20
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);
122 }) != shared_attribute.Get_Attributes().end());
123 });
124
125 for (auto const & shared_attribute : filtered_shared_attribute_update_callbacks) {
126#else
127 for (auto const & shared_attribute : m_shared_attribute_update_callbacks) {
128 if (shared_attribute.Get_Attributes().empty()) {
129 // No specifc keys were subscribed so we call the callback anyway, assumed to be subscribed to every shared attribute update
130 shared_attribute.Call_Callback(object);
131 continue;
132 }
133
134 char const * requested_att = nullptr;
135
136 for (auto const & att : shared_attribute.Get_Attributes()) {
138 continue;
139 }
140 // Check if the shared attribute update contains any of our subscribed keys and break early if they do,
141 // because we want to receive every update that chagned atleast one of our subscribed keys
142 if (object.containsKey(att)) {
143 requested_att = att;
144 break;
145 }
146 }
147
148 // Check if the shared attribute update contained any of our subscribed keys.
149 // Do not inform the callback if it did not
150 if (requested_att == nullptr) {
151 continue;
152 }
153#endif // THINGSBOARD_ENABLE_STL
154 shared_attribute.Call_Callback(object);
155 }
156 }
157
158 bool Is_Response_Topic_Matching(char const * topic) const override {
159 return strncmp(ATTRIBUTE_TOPIC, topic, strlen(ATTRIBUTE_TOPIC) + 1) == 0;
160 }
161
162 bool Unsubscribe() override {
164 }
165
167 if (!m_shared_attribute_update_callbacks.empty() && !m_subscribe_topic_callback.Call_Callback(ATTRIBUTE_TOPIC)) {
168 Logger::printfln(SUBSCRIBE_TOPIC_FAILED, ATTRIBUTE_TOPIC);
169 return false;
170 }
171 return true;
172 }
173
174#if !THINGSBOARD_USE_ESP_TIMER
175 void loop() override {
176 // Nothing to do
177 }
178#endif // !THINGSBOARD_USE_ESP_TIMER
179
180 void Initialize() override {
181 // Nothing to do
182 }
183
185 m_subscribe_topic_callback.Set_Callback(subscribe_topic_callback);
186 m_unsubscribe_topic_callback.Set_Callback(unsubscribe_topic_callback);
187 }
188
189 private:
190 Callback<bool, char const * const> m_subscribe_topic_callback = {}; // Subscribe mqtt topic client callback
191 Callback<bool, char const * const> m_unsubscribe_topic_callback = {}; // Unubscribe mqtt topic client callback
192 Callback_Container m_shared_attribute_update_callbacks = {}; // Shared attribute update callbacks array
193};
194
195#endif // Shared_Attribute_Update_h
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