use common buffer
This commit is contained in:
25
Network.cpp
25
Network.cpp
@@ -8,14 +8,17 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include "RestartReason.h"
|
#include "RestartReason.h"
|
||||||
#include "networkDevices/EthLan8720Device.h"
|
#include "networkDevices/EthLan8720Device.h"
|
||||||
|
#include "CharBuffer.h"
|
||||||
|
|
||||||
Network* Network::_inst = nullptr;
|
Network* Network::_inst = nullptr;
|
||||||
unsigned long Network::_ignoreSubscriptionsTs = 0;
|
unsigned long Network::_ignoreSubscriptionsTs = 0;
|
||||||
|
|
||||||
RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];
|
RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];
|
||||||
|
|
||||||
Network::Network(Preferences *preferences, const String& maintenancePathPrefix)
|
Network::Network(Preferences *preferences, const String& maintenancePathPrefix, char* buffer, size_t bufferSize)
|
||||||
: _preferences(preferences)
|
: _preferences(preferences),
|
||||||
|
_buffer(buffer),
|
||||||
|
_bufferSize(bufferSize)
|
||||||
{
|
{
|
||||||
_inst = this;
|
_inst = this;
|
||||||
_hostname = _preferences->getString(preference_hostname);
|
_hostname = _preferences->getString(preference_hostname);
|
||||||
@@ -578,7 +581,6 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
|
|
||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
char* jsonOut = new char[JSON_BUFFER_SIZE];
|
|
||||||
DynamicJsonDocument json(JSON_BUFFER_SIZE);
|
DynamicJsonDocument json(JSON_BUFFER_SIZE);
|
||||||
|
|
||||||
auto dev = json.createNestedObject("dev");
|
auto dev = json.createNestedObject("dev");
|
||||||
@@ -599,16 +601,14 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n
|
|||||||
json["stat_unlocked"] = unlockedState;
|
json["stat_unlocked"] = unlockedState;
|
||||||
json["opt"] = "false";
|
json["opt"] = "false";
|
||||||
|
|
||||||
serializeJson(json, reinterpret_cast<char(&)[JSON_BUFFER_SIZE]>(*jsonOut));
|
serializeJson(json, reinterpret_cast<char(&)[CHAR_BUFFER_SIZE]>(*_buffer));
|
||||||
|
|
||||||
String path = discoveryTopic;
|
String path = discoveryTopic;
|
||||||
path.concat("/lock/");
|
path.concat("/lock/");
|
||||||
path.concat(uidString);
|
path.concat(uidString);
|
||||||
path.concat("/smartlock/config");
|
path.concat("/smartlock/config");
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer);
|
||||||
|
|
||||||
delete jsonOut;
|
|
||||||
|
|
||||||
// Battery critical
|
// Battery critical
|
||||||
publishHassTopic("binary_sensor",
|
publishHassTopic("binary_sensor",
|
||||||
@@ -910,7 +910,7 @@ void Network::publishHASSConfigKeypadAttemptInfo(char *deviceType, const char *b
|
|||||||
"diagnostic",
|
"diagnostic",
|
||||||
"",
|
"",
|
||||||
{ { "ic", "mdi:drag-vertical" },
|
{ { "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)
|
void Network::publishHASSWifiRssiConfig(char *deviceType, const char *baseTopic, char *name, char *uidString)
|
||||||
@@ -984,8 +984,7 @@ void Network::publishHassTopic(const String& mqttDeviceType,
|
|||||||
|
|
||||||
if (discoveryTopic != "")
|
if (discoveryTopic != "")
|
||||||
{
|
{
|
||||||
char *jsonOut = new char[JSON_BUFFER_SIZE];
|
DynamicJsonDocument json(CHAR_BUFFER_SIZE);
|
||||||
DynamicJsonDocument json(JSON_BUFFER_SIZE);
|
|
||||||
|
|
||||||
// Battery level
|
// Battery level
|
||||||
json.clear();
|
json.clear();
|
||||||
@@ -1022,7 +1021,7 @@ void Network::publishHassTopic(const String& mqttDeviceType,
|
|||||||
json[entry.first] = entry.second;
|
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;
|
String path = discoveryTopic;
|
||||||
path.concat("/");
|
path.concat("/");
|
||||||
@@ -1033,9 +1032,7 @@ void Network::publishHassTopic(const String& mqttDeviceType,
|
|||||||
path.concat(mattDeviceName);
|
path.concat(mattDeviceName);
|
||||||
path.concat("/config");
|
path.concat("/config");
|
||||||
|
|
||||||
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, jsonOut);
|
_device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, _buffer);
|
||||||
|
|
||||||
delete jsonOut;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ enum class NetworkDeviceType
|
|||||||
class Network
|
class Network
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit Network(Preferences* preferences, const String& maintenancePathPrefix);
|
explicit Network(Preferences* preferences, const String& maintenancePathPrefix, char* buffer, size_t bufferSize);
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
bool update();
|
bool update();
|
||||||
@@ -127,6 +127,10 @@ private:
|
|||||||
bool _mqttEnabled = true;
|
bool _mqttEnabled = true;
|
||||||
static unsigned long _ignoreSubscriptionsTs;
|
static unsigned long _ignoreSubscriptionsTs;
|
||||||
long _rssiPublishInterval = 0;
|
long _rssiPublishInterval = 0;
|
||||||
|
|
||||||
|
char* _buffer;
|
||||||
|
size_t _bufferSize;
|
||||||
|
|
||||||
std::function<void()> _keepAliveCallback = nullptr;
|
std::function<void()> _keepAliveCallback = nullptr;
|
||||||
std::vector<std::function<void()>> _reconnectedCallbacks;
|
std::vector<std::function<void()>> _reconnectedCallbacks;
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,14 @@
|
|||||||
#include "PreferencesKeys.h"
|
#include "PreferencesKeys.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
|
#include "CharBuffer.h"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
NetworkOpener::NetworkOpener(Network* network, Preferences* preferences)
|
NetworkOpener::NetworkOpener(Network* network, Preferences* preferences, char* buffer, size_t bufferSize)
|
||||||
: _preferences(preferences),
|
: _preferences(preferences),
|
||||||
_network(network)
|
_network(network),
|
||||||
|
_buffer(buffer),
|
||||||
|
_bufferSize(bufferSize)
|
||||||
{
|
{
|
||||||
_configTopics.reserve(5);
|
_configTopics.reserve(5);
|
||||||
_configTopics.push_back(mqtt_topic_config_button_enabled);
|
_configTopics.push_back(mqtt_topic_config_button_enabled);
|
||||||
@@ -269,8 +272,7 @@ void NetworkOpener::publishAuthorizationInfo(const std::list<NukiOpener::LogEntr
|
|||||||
char authName[33];
|
char authName[33];
|
||||||
memset(authName, 0, sizeof(authName));
|
memset(authName, 0, sizeof(authName));
|
||||||
|
|
||||||
char *jsonOut = new char[OPENER_LOG_JSON_BUFFER_SIZE];
|
DynamicJsonDocument json(CHAR_BUFFER_SIZE);
|
||||||
DynamicJsonDocument json(OPENER_LOG_JSON_BUFFER_SIZE);
|
|
||||||
|
|
||||||
for(const auto& log : logEntries)
|
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));
|
serializeJson(json, reinterpret_cast<char(&)[CHAR_BUFFER_SIZE]>(*_buffer));
|
||||||
publishString(mqtt_topic_lock_log, jsonOut);
|
publishString(mqtt_topic_lock_log, _buffer);
|
||||||
|
|
||||||
delete jsonOut;
|
|
||||||
|
|
||||||
if(authFound)
|
if(authFound)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,12 +9,10 @@
|
|||||||
#include "NukiOpenerConstants.h"
|
#include "NukiOpenerConstants.h"
|
||||||
#include "NetworkLock.h"
|
#include "NetworkLock.h"
|
||||||
|
|
||||||
#define OPENER_LOG_JSON_BUFFER_SIZE 2048
|
|
||||||
|
|
||||||
class NetworkOpener : public MqttReceiver
|
class NetworkOpener : public MqttReceiver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit NetworkOpener(Network* network, Preferences* preferences);
|
explicit NetworkOpener(Network* network, Preferences* preferences, char* buffer, size_t bufferSize);
|
||||||
virtual ~NetworkOpener() = default;
|
virtual ~NetworkOpener() = default;
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
@@ -85,6 +83,9 @@ private:
|
|||||||
unsigned long _resetLockStateTs = 0;
|
unsigned long _resetLockStateTs = 0;
|
||||||
uint8_t _queryCommands = 0;
|
uint8_t _queryCommands = 0;
|
||||||
|
|
||||||
|
char* _buffer;
|
||||||
|
size_t _bufferSize;
|
||||||
|
|
||||||
bool (*_lockActionReceivedCallback)(const char* value) = nullptr;
|
bool (*_lockActionReceivedCallback)(const char* value) = nullptr;
|
||||||
void (*_configUpdateReceivedCallback)(const char* path, 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;
|
void (*_keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code, const int& enabled) = nullptr;
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -182,7 +182,7 @@ void setup()
|
|||||||
openerEnabled = preferences->getBool(preference_opener_enabled);
|
openerEnabled = preferences->getBool(preference_opener_enabled);
|
||||||
|
|
||||||
const String mqttLockPath = preferences->getString(preference_mqtt_lock_path);
|
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();
|
network->initialize();
|
||||||
|
|
||||||
networkLock = new NetworkLock(network, preferences, CharBuffer::get(), CHAR_BUFFER_SIZE);
|
networkLock = new NetworkLock(network, preferences, CharBuffer::get(), CHAR_BUFFER_SIZE);
|
||||||
@@ -190,7 +190,7 @@ void setup()
|
|||||||
|
|
||||||
if(openerEnabled)
|
if(openerEnabled)
|
||||||
{
|
{
|
||||||
networkOpener = new NetworkOpener(network, preferences);
|
networkOpener = new NetworkOpener(network, preferences, CharBuffer::get(), CHAR_BUFFER_SIZE);
|
||||||
networkOpener->initialize();
|
networkOpener->initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user