Skip to content

Commit 78c0abd

Browse files
authored
Merge pull request #131 from imbeacon/master
Telemetry class updates & RPC_Response backward compatibility
2 parents a7a94d0 + a62cf04 commit 78c0abd

9 files changed

Lines changed: 116 additions & 62 deletions

File tree

examples/0002-arduino_rpc/0002-arduino_rpc.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ RPC_Response processTemperatureChange(const RPC_Data &data) {
178178
// Just an response example
179179
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
180180
doc[RPC_RESPONSE_KEY] = 42;
181-
return doc;
181+
return RPC_Response(doc);
182182
}
183183

184184
/// @brief Processes function for RPC call "example_set_switch"
@@ -202,7 +202,7 @@ RPC_Response processSwitchChange(const RPC_Data &data) {
202202
// Just an response example
203203
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
204204
doc[RPC_RESPONSE_KEY] = 22.02;
205-
return doc;
205+
return RPC_Response(doc);
206206
}
207207

208208
const uint8_t callback_size = 2U;

examples/0010-esp8266_esp32_rpc/0010-esp8266_esp32_rpc.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ RPC_Response processTemperatureChange(const RPC_Data &data) {
244244
// Just an response example
245245
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
246246
doc[RPC_RESPONSE_KEY] = 42;
247-
return doc;
247+
return RPC_Response(doc);
248248
}
249249

250250
/// @brief Processes function for RPC call "example_set_switch"
@@ -272,7 +272,7 @@ RPC_Response processSwitchChange(const RPC_Data &data) {
272272
// Just an response example
273273
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
274274
doc[RPC_RESPONSE_KEY] = 22.02;
275-
return doc;
275+
return RPC_Response(doc);
276276
}
277277

278278
const std::array<RPC_Callback, 2U> callbacks = {

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/thingsboard/thingsboard-arduino-sdk"
88
},
99
"dependencies": {
10-
"bblanchon/ArduinoJson": "^6.20.1",
10+
"bblanchon/ArduinoJson": "^6.21.1",
1111
"thingsboard/TBPubSubClient": "https://github.com/thingsboard/pubsubclient.git#v2.9.1",
1212
"arduino-libraries/ArduinoHttpClient" : "^0.4.0"
1313
},

src/Attribute_Request_Callback.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#ifndef Attribute_Request_Callback_h
88
#define Attribute_Request_Callback_h
99

10+
// Local includes.
11+
#include "Configuration.h"
12+
1013
// Library includes.
1114
#include <ArduinoJson.h>
12-
#include "Configuration.h"
1315
#if THINGSBOARD_ENABLE_STL
1416
#include <functional>
1517
#include <vector>

src/RPC_Callback.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
// Local includes.
1111
#include "Configuration.h"
12+
#include "RPC_Response.h"
1213

1314
// Library includes.
14-
#include <Telemetry.h>
1515
#if THINGSBOARD_ENABLE_STL
1616
#include <functional>
1717
#endif // THINGSBOARD_ENABLE_STL
@@ -25,8 +25,6 @@ constexpr char RPC_CB_NULL[] PROGMEM = "Server-side RPC callback is NULL";
2525
constexpr char RPC_CB_NULL[] = "Server-side RPC callback is NULL";
2626
#endif // THINGSBOARD_ENABLE_PROGMEM
2727

28-
// Convenient aliases
29-
using RPC_Response = JsonVariant;
3028
// JSON variant const (read only twice as small as JSON variant), is used to communicate RPC parameters to the client
3129
using RPC_Data = const JsonVariantConst;
3230

src/RPC_Response.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
RPC_Response.h - Library API for sending data to the ThingsBoard
3+
Based on PubSub MQTT library.
4+
Created by Olender M. Oct 2018.
5+
Released into the public domain.
6+
*/
7+
#ifndef RPC_RESPONSE_H
8+
#define RPC_RESPONSE_H
9+
10+
#include <Telemetry.h>
11+
12+
class RPC_Response : public JsonVariant {
13+
public:
14+
15+
RPC_Response()
16+
: JsonVariant() {
17+
}
18+
19+
RPC_Response(JsonVariant variant)
20+
: JsonVariant(variant) {
21+
}
22+
23+
RPC_Response(Telemetry telemetry) {
24+
StaticJsonDocument<JSON_OBJECT_SIZE(1)> jsonBuffer;
25+
const JsonVariant object = jsonBuffer.to<JsonVariant>();
26+
if (!telemetry.SerializeKeyValue(object)) {
27+
return;
28+
}
29+
this->set(object);
30+
}
31+
32+
template <typename T>
33+
RPC_Response(const char *key, T value)
34+
: RPC_Response(Telemetry(key, value)) {
35+
}
36+
};
37+
38+
#endif //RPC_RESPONSE_H

src/Telemetry.h

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
#ifndef Telemetry_h
88
#define Telemetry_h
99

10+
// Local includes.
11+
#include "Configuration.h"
12+
1013
// Library includes.
1114
#include <ArduinoJson.h>
15+
#if THINGSBOARD_ENABLE_STL
16+
#include <type_traits>
17+
#endif // THINGSBOARD_ENABLE_STL
1218

1319
/// @brief Telemetry record class, allows to store different data using a common interface.
1420
class Telemetry {
@@ -17,24 +23,23 @@ class Telemetry {
1723
inline Telemetry()
1824
: m_type(DataType::TYPE_NONE), m_key(NULL), m_value() { }
1925

26+
/// @brief Constructs telemetry record from integer value
2027
/// @brief Constructs telemetry record from integer value
2128
/// @tparam T Type of the passed value, is required to be integral,
2229
/// to ensure this constructor isn't used instead of the float one by mistake
2330
/// @param key Key of the key value pair we want to create
2431
/// @param val Value of the key value pair we want to create
2532
template <typename T,
26-
typename = ARDUINOJSON_NAMESPACE::enable_if<ARDUINOJSON_NAMESPACE::is_integral<T>::value>>
33+
#if THINGSBOARD_ENABLE_STL
34+
// 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 longy
35+
typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
36+
#else
37+
// 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
38+
typename ArduinoJson::ARDUINOJSON_VERSION_NAMESPACE::detail::enable_if<ArduinoJson::ARDUINOJSON_VERSION_NAMESPACE::detail::is_integral<T>::value>::type* = nullptr>
39+
#endif // THINGSBOARD_ENABLE_STL
2740
inline Telemetry(const char *key, T val)
28-
: m_type(DataType::TYPE_INT), m_key(key), m_value() {
29-
m_value.integer = val;
30-
}
31-
32-
/// @brief Constructs telemetry record from boolean value
33-
/// @param key Key of the key value pair we want to create
34-
/// @param val Value of the key value pair we want to create
35-
inline Telemetry(const char *key, bool val)
36-
: m_type(DataType::TYPE_BOOL), m_key(key), m_value() {
37-
m_value.boolean = val;
41+
: m_type(DataType::TYPE_INT), m_key(key), m_value() {
42+
m_value.integer = val;
3843
}
3944

4045
/// @brief Constructs telemetry record from float value
@@ -49,8 +54,8 @@ class Telemetry {
4954
/// @param key Key of the key value pair we want to create
5055
/// @param val Value of the key value pair we want to create
5156
inline Telemetry(const char *key, const char *val)
52-
: m_type(DataType::TYPE_STR), m_key(key), m_value() {
53-
m_value.str = val;
57+
: m_type(DataType::TYPE_STR), m_key(key), m_value() {
58+
m_value.str = val;
5459
}
5560

5661
/// @brief Whether this record is empty or not
@@ -65,10 +70,6 @@ class Telemetry {
6570
inline bool SerializeKeyValue(const JsonVariant &jsonObj) const {
6671
if (m_key) {
6772
switch (m_type) {
68-
case DataType::TYPE_BOOL:
69-
jsonObj[m_key] = m_value.boolean;
70-
return true;
71-
break;
7273
case DataType::TYPE_INT:
7374
jsonObj[m_key] = m_value.integer;
7475
return true;
@@ -89,9 +90,6 @@ class Telemetry {
8990
}
9091

9192
switch (m_type) {
92-
case DataType::TYPE_BOOL:
93-
return jsonObj.set(m_value.boolean);
94-
break;
9593
case DataType::TYPE_INT:
9694
return jsonObj.set(m_value.integer);
9795
break;
@@ -112,15 +110,13 @@ class Telemetry {
112110
// Data container
113111
union Data {
114112
const char *str;
115-
bool boolean;
116113
int integer;
117114
float real;
118115
};
119116

120117
// Data type inside a container
121118
enum class DataType: const uint8_t {
122119
TYPE_NONE,
123-
TYPE_BOOL,
124120
TYPE_INT,
125121
TYPE_REAL,
126122
TYPE_STR

src/ThingsBoard.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -637,11 +637,11 @@ class ThingsBoardSized {
637637
// Telemetry API
638638

639639
/// @brief Attempts to send telemetry data with the given key and value of the given type
640-
/// @tparam T Type of the data value we want to send
640+
/// @tparam T Type of the passed value
641641
/// @param key Key of the key value pair we want to send
642642
/// @param value Value of the key value pair we want to send
643643
/// @return Whether sending the data was successful or not
644-
template<class T>
644+
template<typename T>
645645
inline bool sendTelemetryData(const char *key, T value) {
646646
return sendKeyValue(key, value);
647647
}
@@ -811,45 +811,45 @@ class ThingsBoardSized {
811811
// Attribute API
812812

813813
/// @brief Attempts to send attribute data with the given key and value of the given type
814-
/// @tparam T Type of the data value we want to send
814+
/// @tparam T Type of the passed value
815815
/// @param key Key of the key value pair we want to send
816816
/// @param value Value of the key value pair we want to send
817817
/// @return Whether sending the data was successful or not
818-
template<class T>
819-
inline bool sendAttributeData(const char *attrName, T value) {
820-
return sendKeyValue(attrName, value, false);
818+
template<typename T>
819+
inline bool sendAttributeData(const char *key, T value) {
820+
return sendKeyValue(key, value, false);
821821
}
822822

823823
/// @brief Attempts to send integer attribute data with the given key and value
824824
/// @param key Key of the key value pair we want to send
825825
/// @param value Value of the key value pair we want to send
826826
/// @return Whether sending the data was successful or not
827-
inline bool sendAttributeInt(const char *attrName, int value) {
828-
return sendKeyValue(attrName, value, false);
827+
inline bool sendAttributeInt(const char *key, int value) {
828+
return sendKeyValue(key, value, false);
829829
}
830830

831831
/// @brief Attempts to send boolean attribute data with the given key and value
832832
/// @param key Key of the key value pair we want to send
833833
/// @param value Value of the key value pair we want to send
834834
/// @return Whether sending the data was successful or not
835-
inline bool sendAttributeBool(const char *attrName, bool value) {
836-
return sendKeyValue(attrName, value, false);
835+
inline bool sendAttributeBool(const char *key, bool value) {
836+
return sendKeyValue(key, value, false);
837837
}
838838

839839
/// @brief Attempts to send float attribute data with the given key and value
840840
/// @param key Key of the key value pair we want to send
841841
/// @param value Value of the key value pair we want to send
842842
/// @return Whether sending the data was successful or not
843-
inline bool sendAttributeFloat(const char *attrName, float value) {
844-
return sendKeyValue(attrName, value, false);
843+
inline bool sendAttributeFloat(const char *key, float value) {
844+
return sendKeyValue(key, value, false);
845845
}
846846

847847
/// @brief Attempts to send string attribute data with the given key and value
848848
/// @param key Key of the key value pair we want to send
849849
/// @param value Value of the key value pair we want to send
850850
/// @return Whether sending the data was successful or not
851-
inline bool sendAttributeString(const char *attrName, const char *value) {
852-
return sendKeyValue(attrName, value, false);
851+
inline bool sendAttributeString(const char *key, const char *value) {
852+
return sendKeyValue(key, value, false);
853853
}
854854

855855
/// @brief Attempts to send aggregated attribute data
@@ -1845,7 +1845,7 @@ class ThingsBoardSized {
18451845
}
18461846

18471847
/// @brief Attempts to send a single key-value pair with the given key and value of the given type
1848-
/// @tparam T Type of the data value we want to send
1848+
/// @tparam T Type of the passed value
18491849
/// @param key Key of the key value pair we want to send
18501850
/// @param value Value of the key value pair we want to send
18511851
/// @param telemetry Whether the data we want to send should be sent as an attribute or telemetry data value

0 commit comments

Comments
 (0)