1#ifndef ThingsBoard_Http_h
2#define ThingsBoard_Http_h
20char constexpr POST[] =
"POST";
21char constexpr GET[] =
"GET";
22char constexpr HTTP_FAILED[] =
"(%s) failed HTTP response (%d)";
30template<
typename Logger = DefaultLogger>
56 , m_max_stack(max_stack_size)
57 , m_token(access_token)
60 if (m_client.
connect(host, port) != 0) {
73 m_max_stack = max_stack_size;
89 bool Send_Json(
char const * topic, JsonDocument
const & source) {
92 if (source.isNull()) {
98 if (source.overflowed()) {
106 char * json =
new char[json_size]();
107 if (serializeJson(source, json, json_size) < json_size - 1) {
117 char json[json_size] = {};
118 if (serializeJson(source, json, json_size) < json_size - 1) {
134 if (json ==
nullptr || m_token ==
nullptr) {
139 (void)snprintf(path,
sizeof(path), topic, m_token);
140 return Post_Message(path, json);
155 return Send_Key_Value_Pair(key, value);
169#if THINGSBOARD_ENABLE_DYNAMIC
170 template<
typename InputIterator>
172 template<
size_t MaxKeyValuePairAmount,
typename InputIterator>
175#if THINGSBOARD_ENABLE_DYNAMIC
176 return Send_Data_Array(first, last,
true);
178 return Send_Data_Array<MaxKeyValuePairAmount>(first, last,
true);
212 return Send_Key_Value_Pair(key, value,
false);
226#if THINGSBOARD_ENABLE_DYNAMIC
227 template<
typename InputIterator>
231 template<
size_t MaxKeyValuePairAmount,
typename InputIterator>
234#if THINGSBOARD_ENABLE_DYNAMIC
235 return Send_Data_Array(first, last,
false);
237 return Send_Data_Array<MaxKeyValuePairAmount>(first, last,
false);
267#if THINGSBOARD_ENABLE_STL
272 return Get_Message(path, response);
280 return Post_Message(path, json);
285 void Clear_Connection() {
295 bool Post_Message(
char const * path,
char const * json) {
313#if THINGSBOARD_ENABLE_STL
314 bool Get_Message(
char const * path, std::string& response) {
316 bool Get_Message(
char const * path, String& response) {
318 bool success = m_client.
get(path);
344#if THINGSBOARD_ENABLE_DYNAMIC
345 template<
typename InputIterator>
347 template<
size_t MaxKeyValuePairAmount,
typename InputIterator>
349 bool Send_Data_Array(InputIterator
const & first, InputIterator
const & last,
bool telemetry) {
351#if THINGSBOARD_ENABLE_DYNAMIC
352 TBJsonDocument json_buffer(JSON_OBJECT_SIZE(size));
354 if (size > MaxKeyValuePairAmount) {
358 StaticJsonDocument<JSON_OBJECT_SIZE(MaxKeyValuePairAmount)> json_buffer;
361#if THINGSBOARD_ENABLE_STL
367 for (
auto it = first; it != last; ++it) {
368 auto const & data = *it;
385 bool Send_Key_Value_Pair(
char const * key, T value,
bool telemetry =
true) {
392 StaticJsonDocument<JSON_OBJECT_SIZE(1)> json_buffer;
393 if (!t.SerializeKeyValue(json_buffer)) {
401 size_t m_max_stack = {};
402 char const *m_token = {};
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
uint16_t constexpr DEFAULT_MAX_STACK_SIZE
Definition: Constants.h:21
char constexpr CONNECT_FAILED[]
Definition: Constants.h:35
char constexpr HTTP_FAILED[]
Definition: ThingsBoardHttp.h:22
char constexpr GET[]
Definition: ThingsBoardHttp.h:21
char constexpr HTTP_POST_PATH[]
Definition: ThingsBoardHttp.h:15
char constexpr HTTP_TELEMETRY_TOPIC[]
Definition: ThingsBoardHttp.h:13
int constexpr HTTP_RESPONSE_SUCCESS_RANGE_END
Definition: ThingsBoardHttp.h:17
char constexpr POST[]
Definition: ThingsBoardHttp.h:20
char constexpr HTTP_ATTRIBUTES_TOPIC[]
Definition: ThingsBoardHttp.h:14
int constexpr HTTP_RESPONSE_SUCCESS_RANGE_START
Definition: ThingsBoardHttp.h:16
static size_t Calculate_Print_Size(char const *format, Args const &... args)
Returns the total amount of bytes needed to store the formatted string with null termination,...
Definition: Helper.h:32
static size_t distance(InputIterator const &first, InputIterator const &last)
Calculates the distance between two iterators.
Definition: Helper.h:85
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
HTTP Client interface that contains the method that a class that can be used to send and receive data...
Definition: IHTTP_Client.h:22
virtual int post(char const *url_path, char const *content_type, char const *request_body)=0
Connects to the server and sends a POST request with a body and content type.
virtual void stop()=0
Disconnects the given device from the current host and clears about any remaining bytes still in the ...
virtual int get(const char *url_path)=0
Connects to the server and sends a GET request.
virtual std::string get_response_body()=0
Returns the response body of a previously sent message as a string object, skips any response headers...
virtual void set_keep_alive(bool keep_alive)=0
Sets whether to close the HTTP connection for every single request and reconnect once a new request i...
virtual int connect(char const *host, uint16_t port)=0
Connects to the given server instance over the defined port.
virtual int get_response_status_code()=0
Gets the HTTP status code contained in the server response.
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 the ArduinoHttpClient to allow connecting and sending / retrieving data from ThingsBoa...
Definition: ThingsBoardHttp.h:31
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: ThingsBoardHttp.h:211
bool Send_Json(char const *topic, JsonDocument const &source)
Sends key-value pairs from the given JsonDocument over the given topic.
Definition: ThingsBoardHttp.h:89
bool Send_Json_String(char const *topic, char const *json)
Sends key-value pairs from the given json string over the given topic.
Definition: ThingsBoardHttp.h:133
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: ThingsBoardHttp.h:196
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: ThingsBoardHttp.h:78
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: ThingsBoardHttp.h:154
bool Send_Post_Request(char const *path, char const *json)
Attempts to send a POST request over HTTP or HTTPS.
Definition: ThingsBoardHttp.h:279
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: ThingsBoardHttp.h:255
bool Send_Attribute_String(char const *json)
Send string containing json as attribute data. See https://thingsboard.io/docs/user-guide/attribute/ ...
Definition: ThingsBoardHttp.h:246
bool Send_Telemetry_String(char const *json)
Send string containing json as telemetry data. See https://thingsboard.io/docs/user-guide/telemetry/ ...
Definition: ThingsBoardHttp.h:187
bool Send_Get_Request(char const *path, std::string &response)
Attempts to send a GET request over HTTP or HTTPS.
Definition: ThingsBoardHttp.h:268
bool Send_Attributes(InputIterator const &first, InputIterator const &last)
Send aggregated key-value pair as attribute data.
Definition: ThingsBoardHttp.h:233
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: ThingsBoardHttp.h:72
ThingsBoardHttpSized(IHTTP_Client &client, char const *access_token, char const *host, uint16_t port=80U, bool keep_alive=true, size_t const &max_stack_size=DEFAULT_MAX_STACK_SIZE)
Constructs a instance with the given network client that should be used to establish the connection t...
Definition: ThingsBoardHttp.h:54
bool Send_Telemetry(InputIterator const &first, InputIterator const &last)
Send aggregated key-value pair as telemetry data.
Definition: ThingsBoardHttp.h:174