implement and use Utils::checkJsonOverflow()

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.
This commit is contained in:
Bernhard Kirchen 2024-03-23 17:49:27 +01:00
parent 1fb2d4262e
commit 8bfb5c6523
9 changed files with 31 additions and 0 deletions

View File

@ -11,5 +11,6 @@ public:
static int getTimezoneOffset(); static int getTimezoneOffset();
static void restartDtu(); static void restartDtu();
static bool checkJsonAlloc(const DynamicJsonDocument& doc, const char* function, const uint16_t line); 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(); static void removeAllFiles();
}; };

View File

@ -151,6 +151,8 @@ void MqttHandleVedirectHassClass::publishSensor(const char *caption, const char
root["stat_cla"] = stateClass; root["stat_cla"] = stateClass;
} }
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
char buffer[512]; char buffer[512];
serializeJson(root, buffer); serializeJson(root, buffer);
publish(configTopic, buffer); publish(configTopic, buffer);
@ -195,6 +197,8 @@ void MqttHandleVedirectHassClass::publishBinarySensor(const char *caption, const
JsonObject deviceObj = root.createNestedObject("dev"); JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj, spMpptData); createDeviceInfo(deviceObj, spMpptData);
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
char buffer[512]; char buffer[512];
serializeJson(root, buffer); serializeJson(root, buffer);
publish(configTopic, buffer); publish(configTopic, buffer);

View File

@ -173,6 +173,8 @@ void MqttHandleBatteryHassClass::publishSensor(const char* caption, const char*
root["stat_cla"] = stateClass; root["stat_cla"] = stateClass;
} }
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
char buffer[512]; char buffer[512];
serializeJson(root, buffer); serializeJson(root, buffer);
publish(configTopic, buffer); publish(configTopic, buffer);
@ -216,6 +218,8 @@ void MqttHandleBatteryHassClass::publishBinarySensor(const char* caption, const
JsonObject deviceObj = root.createNestedObject("dev"); JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj); createDeviceInfo(deviceObj);
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
char buffer[512]; char buffer[512];
serializeJson(root, buffer); serializeJson(root, buffer);
publish(configTopic, buffer); publish(configTopic, buffer);

View File

@ -133,6 +133,8 @@ void MqttHandlePowerLimiterHassClass::publishSelect(
JsonObject deviceObj = root.createNestedObject("dev"); JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj); createDeviceInfo(deviceObj);
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
String buffer; String buffer;
serializeJson(root, buffer); serializeJson(root, buffer);
publish(configTopic, buffer); publish(configTopic, buffer);
@ -179,6 +181,8 @@ void MqttHandlePowerLimiterHassClass::publishNumber(
JsonObject deviceObj = root.createNestedObject("dev"); JsonObject deviceObj = root.createNestedObject("dev");
createDeviceInfo(deviceObj); createDeviceInfo(deviceObj);
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
String buffer; String buffer;
serializeJson(root, buffer); serializeJson(root, buffer);
publish(configTopic, buffer); publish(configTopic, buffer);

View File

@ -79,6 +79,16 @@ bool Utils::checkJsonAlloc(const DynamicJsonDocument& doc, const char* function,
return true; 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 /// @brief Remove all files but the PINMAPPING_FILENAME
void Utils::removeAllFiles() void Utils::removeAllFiles()
{ {

View File

@ -64,6 +64,8 @@ void WebApiWsHuaweiLiveClass::sendDataTaskCb()
JsonVariant var = root; JsonVariant var = root;
generateJsonResponse(var); generateJsonResponse(var);
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
String buffer; String buffer;
serializeJson(root, buffer); serializeJson(root, buffer);

View File

@ -67,6 +67,8 @@ void WebApiWsBatteryLiveClass::sendDataTaskCb()
JsonVariant var = root; JsonVariant var = root;
generateJsonResponse(var); generateJsonResponse(var);
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
// battery provider does not generate a card, e.g., MQTT provider // battery provider does not generate a card, e.g., MQTT provider
if (root.isNull()) { return; } if (root.isNull()) { return; }

View File

@ -123,6 +123,8 @@ void WebApiWsLiveClass::sendOnBatteryStats()
if (root.isNull()) { return; } if (root.isNull()) { return; }
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
String buffer; String buffer;
serializeJson(root, buffer); serializeJson(root, buffer);

View File

@ -91,6 +91,8 @@ void WebApiWsVedirectLiveClass::sendDataTaskCb()
JsonVariant var = root; JsonVariant var = root;
generateJsonResponse(var, fullUpdate); generateJsonResponse(var, fullUpdate);
if (Utils::checkJsonOverflow(root, __FUNCTION__, __LINE__)) { return; }
String buffer; String buffer;
serializeJson(root, buffer); serializeJson(root, buffer);