Skip to content

Commit

Permalink
Prevent empty HASS auto discovery topics if memory allocation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody authored and helgeerbe committed Jan 5, 2024
1 parent 88d7567 commit f00cd1b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/Utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <ArduinoJson.h>
#include <cstdint>

class Utils {
Expand All @@ -9,4 +10,5 @@ class Utils {
static uint64_t generateDtuSerial();
static int getTimezoneOffset();
static void restartDtu();
static bool checkJsonAlloc(const DynamicJsonDocument& doc, const char* function, const uint16_t line);
};
24 changes: 24 additions & 0 deletions src/MqttHandleHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ void MqttHandleHassClass::publishInverterField(std::shared_ptr<InverterAbstract>
}

DynamicJsonDocument root(1024);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

root["name"] = name;
root["stat_t"] = stateTopic;
root["uniq_id"] = serial + "_ch" + chanNum + "_" + fieldName;
Expand Down Expand Up @@ -179,6 +183,10 @@ void MqttHandleHassClass::publishInverterButton(std::shared_ptr<InverterAbstract
const String cmdTopic = MqttSettings.getPrefix() + serial + "/" + subTopic;

DynamicJsonDocument root(1024);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

root["name"] = caption;
root["uniq_id"] = serial + "_" + buttonId;
if (strcmp(icon, "")) {
Expand Down Expand Up @@ -217,6 +225,10 @@ void MqttHandleHassClass::publishInverterNumber(
const String statTopic = MqttSettings.getPrefix() + serial + "/" + stateTopic;

DynamicJsonDocument root(1024);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

root["name"] = caption;
root["uniq_id"] = serial + "_" + buttonId;
if (strcmp(icon, "")) {
Expand Down Expand Up @@ -251,6 +263,10 @@ void MqttHandleHassClass::publishInverterBinarySensor(std::shared_ptr<InverterAb
const String statTopic = MqttSettings.getPrefix() + serial + "/" + subTopic;

DynamicJsonDocument root(1024);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

root["name"] = caption;
root["uniq_id"] = serial + "_" + sensorId;
root["stat_t"] = statTopic;
Expand All @@ -275,6 +291,10 @@ void MqttHandleHassClass::publishDtuSensor(const char* name, const char* device_
}

DynamicJsonDocument root(1024);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

root["name"] = name;
root["uniq_id"] = getDtuUniqueId() + "_" + id;
if (strcmp(device_class, "")) {
Expand Down Expand Up @@ -317,6 +337,10 @@ void MqttHandleHassClass::publishDtuBinarySensor(const char* name, const char* d
}

DynamicJsonDocument root(1024);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}

root["name"] = name;
root["uniq_id"] = getDtuUniqueId() + "_" + id;
root["stat_t"] = MqttSettings.getPrefix() + topic;
Expand Down
11 changes: 11 additions & 0 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Utils.h"
#include "Display_Graphic.h"
#include "Led_Single.h"
#include "MessageOutput.h"
#include <Esp.h>

uint32_t Utils::getChipId()
Expand Down Expand Up @@ -65,3 +66,13 @@ void Utils::restartDtu()
yield();
ESP.restart();
}

bool Utils::checkJsonAlloc(const DynamicJsonDocument& doc, const char* function, const uint16_t line)
{
if (doc.capacity() == 0) {
MessageOutput.printf("Alloc failed: %s, %d\r\n", function, line);
return false;
}

return true;
}

0 comments on commit f00cd1b

Please sign in to comment.