use common buffer

This commit is contained in:
technyon
2023-03-22 23:06:13 +01:00
parent ff4bd2618f
commit 04ebf17b1a
5 changed files with 30 additions and 28 deletions

View File

@@ -8,14 +8,17 @@
#include <ArduinoJson.h>
#include "RestartReason.h"
#include "networkDevices/EthLan8720Device.h"
#include "CharBuffer.h"
Network* Network::_inst = nullptr;
unsigned long Network::_ignoreSubscriptionsTs = 0;
RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];
Network::Network(Preferences *preferences, const String& maintenancePathPrefix)
: _preferences(preferences)
Network::Network(Preferences *preferences, const String& maintenancePathPrefix, char* buffer, size_t bufferSize)
: _preferences(preferences),
_buffer(buffer),
_bufferSize(bufferSize)
{
_inst = this;
_hostname = _preferences->getString(preference_hostname);
@@ -578,7 +581,6 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
if (discoveryTopic != "")
{
char* jsonOut = new char[JSON_BUFFER_SIZE];
DynamicJsonDocument json(JSON_BUFFER_SIZE);
auto dev = json.createNestedObject("dev");
@@ -599,16 +601,14 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
json["stat_unlocked"] = unlockedState;
json["opt"] = "false";
serializeJson(json, reinterpret_cast<char(&)[JSON_BUFFER_SIZE]>(*jsonOut));
serializeJson(json, reinterpret_cast<char(&)[CHAR_BUFFER_SIZE]>(*_buffer));
String path = discoveryTopic;
path.concat("/lock/");
path.concat(uidString);
path.concat("/smartlock/config");
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
delete jsonOut;
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer);
// Battery critical
publishHassTopic("binary_sensor",
@@ -910,7 +910,7 @@ void Network::publishHASSConfigKeypadAttemptInfo(char *deviceType, const char *b
"diagnostic",
"",
{ { "ic", "mdi:drag-vertical" },
{ "value_template", "{% for state in value_json %} {% if state.type == 'KeypadAction' %} {{ state.completionStatus }} {% endif %} {% endfor %}" }});
{ "value_template", "{{ (value_json|selectattr('type', 'eq', 'KeypadAction')|first).completionStatus }}" }});
}
void Network::publishHASSWifiRssiConfig(char *deviceType, const char *baseTopic, char *name, char *uidString)
@@ -984,8 +984,7 @@ void Network::publishHassTopic(const String& mqttDeviceType,
if (discoveryTopic != "")
{
char *jsonOut = new char[JSON_BUFFER_SIZE];
DynamicJsonDocument json(JSON_BUFFER_SIZE);
DynamicJsonDocument json(CHAR_BUFFER_SIZE);
// Battery level
json.clear();
@@ -1022,7 +1021,7 @@ void Network::publishHassTopic(const String& mqttDeviceType,
json[entry.first] = entry.second;
}
serializeJson(json, reinterpret_cast<char (&)[JSON_BUFFER_SIZE]>(*jsonOut));
serializeJson(json, reinterpret_cast<char (&)[JSON_BUFFER_SIZE]>(*_buffer));
String path = discoveryTopic;
path.concat("/");
@@ -1033,9 +1032,7 @@ void Network::publishHassTopic(const String& mqttDeviceType,
path.concat(mattDeviceName);
path.concat("/config");
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
delete jsonOut;
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer);
}
}

View File

@@ -21,7 +21,7 @@ enum class NetworkDeviceType
class Network
{
public:
explicit Network(Preferences* preferences, const String& maintenancePathPrefix);
explicit Network(Preferences* preferences, const String& maintenancePathPrefix, char* buffer, size_t bufferSize);
void initialize();
bool update();
@@ -127,6 +127,10 @@ private:
bool _mqttEnabled = true;
static unsigned long _ignoreSubscriptionsTs;
long _rssiPublishInterval = 0;
char* _buffer;
size_t _bufferSize;
std::function<void()> _keepAliveCallback = nullptr;
std::vector<std::function<void()>> _reconnectedCallbacks;

View File

@@ -4,11 +4,14 @@
#include "PreferencesKeys.h"
#include "Logger.h"
#include "Config.h"
#include "CharBuffer.h"
#include <ArduinoJson.h>
NetworkOpener::NetworkOpener(Network* network, Preferences* preferences)
NetworkOpener::NetworkOpener(Network* network, Preferences* preferences, char* buffer, size_t bufferSize)
: _preferences(preferences),
_network(network)
_network(network),
_buffer(buffer),
_bufferSize(bufferSize)
{
_configTopics.reserve(5);
_configTopics.push_back(mqtt_topic_config_button_enabled);
@@ -269,8 +272,7 @@ void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntr
char authName[33];
memset(authName, 0, sizeof(authName));
char *jsonOut = new char[OPENER_LOG_JSON_BUFFER_SIZE];
DynamicJsonDocument json(OPENER_LOG_JSON_BUFFER_SIZE);
DynamicJsonDocument json(CHAR_BUFFER_SIZE);
for(const auto& log : logEntries)
{
@@ -373,10 +375,8 @@ void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntr
}
}
serializeJson(json, reinterpret_cast<char(&)[OPENER_LOG_JSON_BUFFER_SIZE]>(*jsonOut));
publishString(mqtt_topic_lock_log, jsonOut);
delete jsonOut;
serializeJson(json, reinterpret_cast<char(&)[CHAR_BUFFER_SIZE]>(*_buffer));
publishString(mqtt_topic_lock_log, _buffer);
if(authFound)
{

View File

@@ -9,12 +9,10 @@
#include "NukiOpenerConstants.h"
#include "NetworkLock.h"
#define OPENER_LOG_JSON_BUFFER_SIZE 2048
class NetworkOpener : public MqttReceiver
{
public:
explicit NetworkOpener(Network* network, Preferences* preferences);
explicit NetworkOpener(Network* network, Preferences* preferences, char* buffer, size_t bufferSize);
virtual ~NetworkOpener() = default;
void initialize();
@@ -85,6 +83,9 @@ private:
unsigned long _resetLockStateTs = 0;
uint8_t _queryCommands = 0;
char* _buffer;
size_t _bufferSize;
bool (*_lockActionReceivedCallback)(const char* value) = nullptr;
void (*_configUpdateReceivedCallback)(const char* path, const char* value) = nullptr;
void (*_keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code, const int& enabled) = nullptr;

View File

@@ -182,7 +182,7 @@ void setup()
openerEnabled = preferences->getBool(preference_opener_enabled);
const String mqttLockPath = preferences->getString(preference_mqtt_lock_path);
network = new Network(preferences, mqttLockPath);
network = new Network(preferences, mqttLockPath, CharBuffer::get(), CHAR_BUFFER_SIZE);
network->initialize();
networkLock = new NetworkLock(network, preferences, CharBuffer::get(), CHAR_BUFFER_SIZE);
@@ -190,7 +190,7 @@ void setup()
if(openerEnabled)
{
networkOpener = new NetworkOpener(network, preferences);
networkOpener = new NetworkOpener(network, preferences, CharBuffer::get(), CHAR_BUFFER_SIZE);
networkOpener->initialize();
}