Skip to content

Commit

Permalink
implement and use Utils::checkJsonOverflow()
Browse files Browse the repository at this point in the history
this method calls the overflowed() method on the respective
DynamicJsonDocument and prints a respective message if not all
data could be added to the DynamicJsonDocument.
  • Loading branch information
schlimmchen committed Mar 23, 2024
1 parent 1fb2d42 commit 8bfb5c6
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class Utils {
static int getTimezoneOffset();
static void restartDtu();
static bool checkJsonAlloc(const DynamicJsonDocument& doc, const char* function, const uint16_t line);
static bool checkJsonOverflow(const DynamicJsonDocument& doc, const char* function, const uint16_t line);
static void removeAllFiles();
};
4 changes: 4 additions & 0 deletions src/MqttHandlVedirectHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ void MqttHandleVedirectHassClass::publishSensor(const char *caption, const char
root["stat_cla"] = stateClass;
}

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

char buffer[512];
serializeJson(root, buffer);
publish(configTopic, buffer);
Expand Down Expand Up @@ -195,6 +197,8 @@ void MqttHandleVedirectHassClass::publishBinarySensor(const char *caption, const
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj, spMpptData);

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

char buffer[512];
serializeJson(root, buffer);
publish(configTopic, buffer);
Expand Down
4 changes: 4 additions & 0 deletions src/MqttHandleBatteryHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ void MqttHandleBatteryHassClass::publishSensor(const char* caption, const char*
root["stat_cla"] = stateClass;
}

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

char buffer[512];
serializeJson(root, buffer);
publish(configTopic, buffer);
Expand Down Expand Up @@ -216,6 +218,8 @@ void MqttHandleBatteryHassClass::publishBinarySensor(const char* caption, const
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj);

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

char buffer[512];
serializeJson(root, buffer);
publish(configTopic, buffer);
Expand Down
4 changes: 4 additions & 0 deletions src/MqttHandlePowerLimiterHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ void MqttHandlePowerLimiterHassClass::publishSelect(
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj);

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
Expand Down Expand Up @@ -179,6 +181,8 @@ void MqttHandlePowerLimiterHassClass::publishNumber(
JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj);

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
Expand Down
10 changes: 10 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ bool Utils::checkJsonAlloc(const DynamicJsonDocument& doc, const char* function,
return true;
}

bool Utils::checkJsonOverflow(const DynamicJsonDocument& doc, const char* function, const uint16_t line)
{
if (doc.overflowed()) {
MessageOutput.printf("DynamicJsonDocument overflowed: %s, %d\r\n", function, line);
return true;
}

return false;
}

/// @brief Remove all files but the PINMAPPING_FILENAME
void Utils::removeAllFiles()
{
Expand Down
2 changes: 2 additions & 0 deletions src/WebApi_ws_Huawei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ void WebApiWsHuaweiLiveClass::sendDataTaskCb()
JsonVariant var = root;
generateJsonResponse(var);

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

String buffer;
serializeJson(root, buffer);

Expand Down
2 changes: 2 additions & 0 deletions src/WebApi_ws_battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ void WebApiWsBatteryLiveClass::sendDataTaskCb()
JsonVariant var = root;
generateJsonResponse(var);

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

// battery provider does not generate a card, e.g., MQTT provider
if (root.isNull()) { return; }

Expand Down
2 changes: 2 additions & 0 deletions src/WebApi_ws_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ void WebApiWsLiveClass::sendOnBatteryStats()

if (root.isNull()) { return; }

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

String buffer;
serializeJson(root, buffer);

Expand Down
2 changes: 2 additions & 0 deletions src/WebApi_ws_vedirect_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ void WebApiWsVedirectLiveClass::sendDataTaskCb()
JsonVariant var = root;
generateJsonResponse(var, fullUpdate);

if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }

String buffer;
serializeJson(root, buffer);

Expand Down

0 comments on commit 8bfb5c6

Please sign in to comment.