avoid using intermediate buffer to store config to publish

This commit is contained in:
Nicu Hodos 2025-10-11 16:13:44 +02:00
parent fd40969c20
commit d18d7553b9
3 changed files with 18 additions and 8 deletions

View File

@ -6,7 +6,7 @@
using namespace std; using namespace std;
#define JSON_SIZE 512 #define JSON_SIZE 1024
#define TOPIC_SIZE 255 #define TOPIC_SIZE 255
#define CONFIG_TOPIC "homeassistant/%s/" MAIN_DEVICE_ID "/%s" #define CONFIG_TOPIC "homeassistant/%s/" MAIN_DEVICE_ID "/%s"
#define BASE_TOPIC MAIN_DEVICE_ID "/%s" #define BASE_TOPIC MAIN_DEVICE_ID "/%s"
@ -92,12 +92,8 @@ namespace Ha {
void publishConfig() { void publishConfig() {
StaticJsonDocument<JSON_SIZE> jsonDoc; StaticJsonDocument<JSON_SIZE> jsonDoc;
buildConfig(jsonDoc); buildConfig(jsonDoc);
char message[JSON_SIZE] = {};
serializeJson(jsonDoc, message);
auto configTopic = buildConfigTopic(); auto configTopic = buildConfigTopic();
publisher(configTopic.get(), message); publisher(configTopic.get(), jsonDoc.as<string>().c_str());
} }
void publishCleanupConfig() { void publishCleanupConfig() {

View File

@ -30,10 +30,10 @@ namespace WebServer {
server.on("/commands", HTTP_GET, [](AsyncWebServerRequest *request) { server.on("/commands", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream("application/json"); AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonDocument jsonResponse(JSON_SIZE*10); DynamicJsonDocument jsonResponse(5120);
JsonArray array = jsonResponse.to<JsonArray>(); JsonArray array = jsonResponse.to<JsonArray>();
for (auto it = Command::mapCommandIds.begin(); it != Command::mapCommandIds.end(); ++it) { for (auto it = Command::mapCommandIds.begin(); it != Command::mapCommandIds.end(); ++it) {
StaticJsonDocument<JSON_SIZE/2> jsonDoc; StaticJsonDocument<256> jsonDoc;
it->second->toJson(jsonDoc); it->second->toJson(jsonDoc);
array.add(jsonDoc); array.add(jsonDoc);
} }

View File

@ -1,5 +1,6 @@
#include <unity.h> #include <unity.h>
#include <unordered_map> #include <unordered_map>
#include <memory>
#define MAIN_DEVICE_ID "test" #define MAIN_DEVICE_ID "test"
@ -208,6 +209,18 @@ void testBinarySensor(void) {
TEST_ASSERT_NOT_NULL(GenericSensor::mapSensors["id"]); TEST_ASSERT_NOT_NULL(GenericSensor::mapSensors["id"]);
} }
void testPublisher(void) {
Ha::publisher = [](const char* topic, const char* message) -> uint16_t {
TEST_ASSERT_EQUAL_STRING("{\"name\":\"a_name\",\"unique_id\":\"test_id\",\"command_topic\":\"test/id/set\"}", message);
return 0;
};
Switch s("a_name", "id");
StaticJsonDocument<256> doc;
s.buildConfig(doc);
s.publishConfig();
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
UNITY_BEGIN(); UNITY_BEGIN();
RUN_TEST(testDevice); RUN_TEST(testDevice);
@ -224,5 +237,6 @@ int main(int argc, char **argv) {
RUN_TEST(testSwitchWithState); RUN_TEST(testSwitchWithState);
RUN_TEST(testNumber); RUN_TEST(testNumber);
RUN_TEST(testBinarySensor); RUN_TEST(testBinarySensor);
RUN_TEST(testPublisher);
return UNITY_END(); return UNITY_END();
} }