ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Telemetry.h
Go to the documentation of this file.
1#ifndef Telemetry_h
2#define Telemetry_h
3
4// Local includes.
5#include "Configuration.h"
6
7// Library includes.
8#include <ArduinoJson.h>
9#if THINGSBOARD_ENABLE_STL
10#include <type_traits>
11#endif // THINGSBOARD_ENABLE_STL
12
13
16class Telemetry {
17 public:
19 Telemetry();
20
26 template <typename T,
27#if THINGSBOARD_ENABLE_STL
28 // Standard library is_integral, includes bool, char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, and unsigned long long
29 typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
30#else
31 // Workaround for ArduinoJson version after 6.21.0, to still be able to access internal enable_if and is_integral declarations, previously accessible with ARDUINOJSON_NAMESPACE
32 typename ArduinoJson::ARDUINOJSON_VERSION_NAMESPACE::detail::enable_if<ArduinoJson::ARDUINOJSON_VERSION_NAMESPACE::detail::is_integral<T>::value>::type* = nullptr>
33#endif // THINGSBOARD_ENABLE_STL
34 Telemetry(char const * key, T const & value)
35 : m_type(DataType::TYPE_INT)
36 , m_key(key)
37 , m_value()
38 {
39 m_value.integer = value;
40 }
41
47 template <typename T,
48#if THINGSBOARD_ENABLE_STL
49 // Standard library is_floating_point, includes float and double
50 typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr>
51#else
52 // Workaround for ArduinoJson version after 6.21.0, to still be able to access internal enable_if and is_floating_point declarations, previously accessible with ARDUINOJSON_NAMESPACE
53 typename ArduinoJson::ARDUINOJSON_VERSION_NAMESPACE::detail::enable_if<ArduinoJson::ARDUINOJSON_VERSION_NAMESPACE::detail::is_floating_point<T>::value>::type* = nullptr>
54#endif // THINGSBOARD_ENABLE_STL
55 Telemetry(char const * key, T const & value)
56 : m_type(DataType::TYPE_REAL)
57 , m_key(key)
58 , m_value()
59 {
60 m_value.real = value;
61 }
62
66 Telemetry(char const * key, bool value);
67
71 Telemetry(char const * key, char const * value);
72
75 bool IsEmpty() const;
76
81 template <typename TSource>
82 bool SerializeKeyValue(TSource & source) const {
83 switch (m_type) {
84 case DataType::TYPE_BOOL:
85 if (m_key) {
86 source[m_key] = m_value.boolean;
87 return source.containsKey(m_key);
88 }
89 return source.set(m_value.boolean);
90 case DataType::TYPE_INT:
91 if (m_key) {
92 source[m_key] = m_value.integer;
93 return source.containsKey(m_key);
94 }
95 return source.set(m_value.integer);
96 case DataType::TYPE_REAL:
97 if (m_key) {
98 source[m_key] = m_value.real;
99 return source.containsKey(m_key);
100 }
101 return source.set(m_value.real);
102 case DataType::TYPE_STR:
103 if (m_key) {
104 source[m_key] = m_value.str;
105 return source.containsKey(m_key);
106 }
107 return source.set(m_value.str);
108 default:
109 // Nothing to do
110 break;
111 }
112 return false;
113 }
114
115 private:
117 union Data {
118 const char *str;
119 bool boolean;
120 int64_t integer;
121 double real;
122 };
123
125 enum class DataType: uint8_t {
126 TYPE_NONE,
127 TYPE_BOOL,
128 TYPE_INT,
129 TYPE_REAL,
130 TYPE_STR
131 };
132
133 DataType m_type = {}; // Data type flag, showing which value is saved in the class instance
134 const char *m_key = {}; // Data key of the key-value pair
135 Data m_value = {}; // Data value of the key-value pair
136};
137
140
141#endif // Telemetry_h
Telemetry record class, allows to store different data using a common interface.
Definition: Telemetry.h:16
bool IsEmpty() const
Whether this record is empty or not.
Definition: Telemetry.cpp:28
Telemetry()
Creates an empty Telemetry record containg neither a key nor value.
Definition: Telemetry.cpp:4
Telemetry(char const *key, T const &value)
Constructs a telemetry record from integral value.
Definition: Telemetry.h:34
bool SerializeKeyValue(TSource &source) const
Serializes a key-value pair or only a value, depending on the constructor used.
Definition: Telemetry.h:82