Skip to content

Commit

Permalink
Remove direct transformation of JsonDocument as JsonObject. Closes #222
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Gmür committed Sep 18, 2024
1 parent 3677a66 commit 435cdf7
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ class Custom_API_Implementation : public IAPI_Implementation {
// Nothing to do
}

void Process_Json_Response(char * const topic, JsonObjectConst & data) override {
void Process_Json_Response(char * const topic, JsonDocument const & data) override {
// Nothing to do
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void setup() {

/// @brief Process the provisioning response received from the server
/// @param data Reference to the object containing the provisioning response
void processProvisionResponse(const JsonObjectConst &data) {
void processProvisionResponse(const JsonDocument &data) {
const size_t jsonSize = Helper::Measure_Json(data);
char buffer[jsonSize];
serializeJson(data, buffer, jsonSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void requestTimedOut() {
/// @brief Processes function for RPC response of "getCurrentTime".
/// If no response is set the callback is called with {"error": "timeout"}, after a few seconds
/// @param data Data containing the rpc response that was sent by the cloud
void processTime(const JsonVariantConst &data) {
void processTime(JsonDocument const & data) {
serializeJsonPretty(data, Serial);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void InitWiFi() {

/// @brief Process the provisioning response received from the server
/// @param data Reference to the object containing the provisioning response
void processProvisionResponse(const JsonObjectConst &data) {
void processProvisionResponse(const JsonDocument &data) {
const size_t jsonSize = Helper::Measure_Json(data);
char buffer[jsonSize];
serializeJson(data, buffer, jsonSize);
Expand Down
18 changes: 7 additions & 11 deletions src/Attribute_Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ char constexpr SHARED_REQUEST_KEY[] = "sharedKeys";
// Log messages.
#if THINGSBOARD_ENABLE_DEBUG
char constexpr NO_KEYS_TO_REQUEST[] = "No keys to request were given";
char constexpr ATT_KEY_NOT_FOUND[] = "Attribute key not found";
char constexpr ATT_KEY_NOT_FOUND[] = "Attribute key in Attribute_Request_Callback is NULL";
char constexpr ATT_KEY_IS_NULL[] = "Requested attribute key is NULL";
#endif // THINGSBOARD_ENABLE_DEBUG
#if !THINGSBOARD_ENABLE_DYNAMIC
Expand Down Expand Up @@ -81,8 +81,9 @@ class Attribute_Request : public IAPI_Implementation {
// Nothing to do
}

void Process_Json_Response(char * const topic, JsonObjectConst & data) override {
void Process_Json_Response(char * const topic, JsonDocument const & data) override {
size_t const request_id = Helper::parseRequestId(ATTRIBUTE_RESPONSE_TOPIC, topic);
JsonObjectConst object = data.template as<JsonObjectConst>();

for (auto it = m_attribute_request_callbacks.begin(); it != m_attribute_request_callbacks.end(); ++it) {
auto & attribute_request = *it;
Expand All @@ -97,18 +98,13 @@ class Attribute_Request : public IAPI_Implementation {
#endif // THINGSBOARD_ENABLE_DEBUG
goto delete_callback;
}
else if (!data) {
#if THINGSBOARD_ENABLE_DEBUG
Logger::println(ATT_KEY_NOT_FOUND);
#endif // THINGSBOARD_ENABLE_DEBUG
goto delete_callback;
}

if (data.containsKey(attribute_response_key)) {
data = data[attribute_response_key];
if (object.containsKey(attribute_response_key)) {
object = object[attribute_response_key];
}

attribute_request.Stop_Timeout_Timer();
attribute_request.Call_Callback(data);
attribute_request.Call_Callback(object);

delete_callback:
// Delete callback because the changes have been requested and the callback is no longer needed
Expand Down
2 changes: 1 addition & 1 deletion src/Client_Side_RPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Client_Side_RPC : public IAPI_Implementation {
// Nothing to do
}

void Process_Json_Response(char * const topic, JsonObjectConst & data) override {
void Process_Json_Response(char * const topic, JsonDocument const & data) override {
size_t const request_id = Helper::parseRequestId(RPC_RESPONSE_TOPIC, topic);

for (auto it = m_rpc_request_callbacks.begin(); it != m_rpc_request_callbacks.end(); ++it) {
Expand Down
2 changes: 1 addition & 1 deletion src/IAPI_Implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class IAPI_Implementation {
/// and is responsible for handling the alredy serialized payload and calling the appropriate previously subscribed callbacks
/// @param topic Previously subscribed topic, we got the response over
/// @param data Payload sent by the server over our given topic, that contains our key value pairs
virtual void Process_Json_Response(char * const topic, JsonObjectConst & data) = 0;
virtual void Process_Json_Response(char * const topic, JsonDocument const & data) = 0;

/// @brief Returns a non-owning pointer to the respone topic string, that we should have received the actual data on.
/// Used to check, which API Implementation needs to handle the current response to a previously sent request
Expand Down
4 changes: 2 additions & 2 deletions src/OTA_Firmware_Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class OTA_Firmware_Update : public IAPI_Implementation {
m_ota.Process_Firmware_Packet(chunk, payload, length);
}

void Process_Json_Response(char * const topic, JsonObjectConst & data) override {
void Process_Json_Response(char * const topic, JsonDocument const & data) override {
// Nothing to do
}

Expand Down Expand Up @@ -405,7 +405,7 @@ class OTA_Firmware_Update : public IAPI_Implementation {
}

#if !THINGSBOARD_ENABLE_STL
static void onStaticFirmwareReceived(JsonObjectConst const & data) {
static void onStaticFirmwareReceived(JsonDocument const & data) {
if (m_subscribedInstance == nullptr) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Provision.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Provision : public IAPI_Implementation {
// Nothing to do
}

void Process_Json_Response(char * const topic, JsonObjectConst & data) override {
void Process_Json_Response(char * const topic, JsonDocument const & data) override {
m_provision_callback.Call_Callback(data);
// Unsubscribe from the provision response topic,
// Will be resubscribed if another request is sent anyway
Expand Down
2 changes: 1 addition & 1 deletion src/Provision_Callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct X509_Certificate{};
/// @brief Provisioning callback wrapper,
/// contains the needed configuration settings to create the request that should be sent to the server.
/// Documentation about the specific use of Provisioning devices in ThingsBoard can be found here https://thingsboard.io/docs/user-guide/device-provisioning/
class Provision_Callback : public Callback<void, JsonObjectConst const &> {
class Provision_Callback : public Callback<void, JsonDocument const &> {
public:
/// @brief Constructs empty callback, will result in never being called. Internals are simply default constructed as nullptr
Provision_Callback() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/Server_Side_RPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Server_Side_RPC : public IAPI_Implementation {
// Nothing to do
}

void Process_Json_Response(char * const topic, JsonObjectConst & data) override {
void Process_Json_Response(char * const topic, JsonDocument const & data) override {
char const * const method_name = data[RPC_METHOD_KEY];

if (method_name == nullptr) {
Expand Down
21 changes: 7 additions & 14 deletions src/Shared_Attribute_Update.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

// Log messages.
#if THINGSBOARD_ENABLE_DEBUG
char constexpr NOT_FOUND_ATT_UPDATE[] = "Shared attribute update key not found";
char constexpr ATT_CB_NO_KEYS[] = "No keys subscribed. Calling subscribed callback for any updated attributes, assumed to be subscribed to every possible key";
char constexpr ATT_NO_CHANGE[] = "No keys that we subscribed too were changed, skipping callback";
char constexpr SHARED_KEY_IS_NULL[] = "Subscribed shared attribute update key is NULL";
Expand Down Expand Up @@ -107,16 +106,10 @@ class Shared_Attribute_Update : public IAPI_Implementation {
// Nothing to do
}

void Process_Json_Response(char * const topic, JsonObjectConst & data) override {
if (!data) {
#if THINGSBOARD_ENABLE_DEBUG
Logger::println(NOT_FOUND_ATT_UPDATE);
#endif // THINGSBOARD_ENABLE_DEBUG
return;
}

if (data.containsKey(SHARED_RESPONSE_KEY)) {
data = data[SHARED_RESPONSE_KEY];
void Process_Json_Response(char * const topic, JsonDocument const & data) override {
JsonObjectConst object = data.template as<JsonObjectConst>();
if (object.containsKey(SHARED_RESPONSE_KEY)) {
object = object[SHARED_RESPONSE_KEY];
}

for (auto const & shared_attribute : m_shared_attribute_update_callbacks) {
Expand All @@ -125,7 +118,7 @@ class Shared_Attribute_Update : public IAPI_Implementation {
Logger::println(ATT_CB_NO_KEYS);
#endif // THINGSBOARD_ENABLE_DEBUG
// No specifc keys were subscribed so we call the callback anyway, assumed to be subscribed to any update
shared_attribute.Call_Callback(data);
shared_attribute.Call_Callback(object);
continue;
}

Expand All @@ -140,7 +133,7 @@ class Shared_Attribute_Update : public IAPI_Implementation {
}
// Check if the request contained any of our requested keys and
// break early if the key was requested from this callback.
if (data.containsKey(att)) {
if (object.containsKey(att)) {
requested_att = att;
break;
}
Expand All @@ -158,7 +151,7 @@ class Shared_Attribute_Update : public IAPI_Implementation {
#if THINGSBOARD_ENABLE_DEBUG
Logger::printfln(CALLING_ATT_CB, requested_att);
#endif // THINGSBOARD_ENABLE_DEBUG
shared_attribute.Call_Callback(data);
shared_attribute.Call_Callback(object);
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/ThingsBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,16 +752,12 @@ class ThingsBoardSized {
Logger::printfln(UNABLE_TO_DE_SERIALIZE_JSON, error.c_str());
return;
}
// .as() is used instead of .to(), because it is meant to cast the JsonDocument to the given type,
// but it does not change the actual content of the JsonDocument, we don't want that because it contains content
// and .to() would result in the data being cleared ()"null"), instead .as() which allows accessing the data over a JsonObjectConst instead
JsonObjectConst data = json_buffer.template as<JsonObjectConst>();

for (auto & api : m_api_implementations) {
if (api == nullptr || api->Get_Process_Type() != API_Process_Type::JSON || api->Get_Response_Topic_String() == nullptr || strncmp(api->Get_Response_Topic_String(), topic, strlen(api->Get_Response_Topic_String())) != 0) {
continue;
}
api->Process_Json_Response(topic, data);
api->Process_Json_Response(topic, json_buffer);
}
}

Expand Down

0 comments on commit 435cdf7

Please sign in to comment.