diff --git a/Network.cpp b/Network.cpp index 7c7fb7c..70ee897 100644 --- a/Network.cpp +++ b/Network.cpp @@ -8,14 +8,17 @@ #include #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(*jsonOut)); + serializeJson(json, reinterpret_cast(*_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(*jsonOut)); + serializeJson(json, reinterpret_cast(*_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); } } diff --git a/Network.h b/Network.h index 2df975e..d3ec444 100644 --- a/Network.h +++ b/Network.h @@ -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 _keepAliveCallback = nullptr; std::vector> _reconnectedCallbacks; diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index 72ec80c..fd76a8b 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -4,11 +4,14 @@ #include "PreferencesKeys.h" #include "Logger.h" #include "Config.h" +#include "CharBuffer.h" #include -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(*jsonOut)); - publishString(mqtt_topic_lock_log, jsonOut); - - delete jsonOut; + serializeJson(json, reinterpret_cast(*_buffer)); + publishString(mqtt_topic_lock_log, _buffer); if(authFound) { diff --git a/NetworkOpener.h b/NetworkOpener.h index 9c1c2e4..ffd8840 100644 --- a/NetworkOpener.h +++ b/NetworkOpener.h @@ -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; diff --git a/main.cpp b/main.cpp index ea7725e..7fe8644 100644 --- a/main.cpp +++ b/main.cpp @@ -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(); }