move publish presence detection to Network class

This commit is contained in:
technyon
2022-07-02 12:46:18 +02:00
parent beb87a959f
commit 4f442d7b1d
9 changed files with 35 additions and 43 deletions

View File

@@ -129,6 +129,17 @@ int Network::update()
}
}
if(_presenceCsv != nullptr && strlen(_presenceCsv) > 0)
{
bool success = publishString(_mqttPresencePrefix, mqtt_topic_presence, _presenceCsv);
if(!success)
{
Serial.println(F("Failed to publish presence CSV data."));
Serial.println(_presenceCsv);
}
_presenceCsv = nullptr;
}
_device->mqttClient()->loop();
return 0;
}
@@ -233,6 +244,12 @@ void Network::reconfigureDevice()
_device->reconfigure();
}
void Network::setMqttPresencePath(char *path)
{
memset(_mqttPresencePrefix, 0, sizeof(_mqttPresencePrefix));
strcpy(_mqttPresencePrefix, path);
}
bool Network::isMqttConnected()
{
return _mqttConnected;
@@ -372,4 +389,9 @@ void Network::removeHASSConfig(char* uidString)
_device->mqttClient()->publish(path.c_str(), NULL, 0U, true);
}
}
}
void Network::publishPresenceDetection(char *csv)
{
_presenceCsv = csv;
}

View File

@@ -20,6 +20,7 @@ public:
int update();
void registerMqttReceiver(MqttReceiver* receiver);
void reconfigureDevice();
void setMqttPresencePath(char* path);
void subscribe(const char* prefix, const char* path);
void publishFloat(const char* prefix, const char* topic, const float value, const uint8_t precision = 2);
@@ -31,6 +32,8 @@ public:
void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState);
void removeHASSConfig(char* uidString);
void publishPresenceDetection(char* csv);
PubSubClient* mqttClient();
bool isMqttConnected();
@@ -52,9 +55,11 @@ private:
char _mqttBrokerAddr[101] = {0};
char _mqttUser[31] = {0};
char _mqttPass[31] = {0};
char _mqttPresencePrefix[181] = {0};
std::vector<String> _subscribedTopics;
int _networkTimeout = 0;
std::vector<MqttReceiver*> _mqttReceivers;
char* _presenceCsv = nullptr;
unsigned long _lastConnectedTs = 0;
};

View File

@@ -40,6 +40,8 @@ void NetworkLock::initialize()
_preferences->putString(preference_mqtt_lock_path, _mqttPath);
}
_network->setMqttPresencePath(_mqttPath);
_network->subscribe(_mqttPath, mqtt_topic_lock_action);
for(const auto& topic : _configTopics)
{
@@ -47,20 +49,6 @@ void NetworkLock::initialize()
}
}
void NetworkLock::update()
{
if(_presenceCsv != nullptr && strlen(_presenceCsv) > 0)
{
bool success = publishString(mqtt_topic_presence, _presenceCsv);
if(!success)
{
Serial.println(F("Failed to publish presence CSV data."));
Serial.println(_presenceCsv);
}
_presenceCsv = nullptr;
}
}
void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
{
char value[50] = {0};
@@ -181,11 +169,6 @@ void NetworkLock::publishAdvancedConfig(const NukiLock::AdvancedConfig &config)
publishBool(mqtt_topic_config_auto_lock, config.autoLockEnabled == 1);
}
void NetworkLock::publishPresenceDetection(char *csv)
{
_presenceCsv = csv;
}
void NetworkLock::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *))
{
_lockActionReceivedCallback = lockActionReceivedCallback;

View File

@@ -18,7 +18,6 @@ public:
virtual ~NetworkLock();
void initialize();
void update();
void publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurnerState, const NukiLock::KeyTurnerState& lastKeyTurnerState);
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
@@ -26,7 +25,6 @@ public:
void publishBatteryReport(const NukiLock::BatteryReport& batteryReport);
void publishConfig(const NukiLock::Config& config);
void publishAdvancedConfig(const NukiLock::AdvancedConfig& config);
void publishPresenceDetection(char* csv);
void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction, char* lockedState, char* unlockedState);
void removeHASSConfig(char* uidString);
@@ -51,7 +49,6 @@ private:
Network* _network;
Preferences* _preferences;
char* _presenceCsv = nullptr;
std::vector<char*> _configTopics;
char _mqttPath[181] = {0};

View File

@@ -38,18 +38,6 @@ void NetworkOpener::initialize()
_network->registerMqttReceiver(this);
}
void NetworkOpener::update()
{
bool connected = _network->mqttClient()->connected();
if(!_isConnected && connected)
{
subscribe(mqtt_topic_lock_action);
}
_isConnected = connected;
}
void NetworkOpener::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
{
char value[50] = {0};

View File

@@ -18,7 +18,6 @@ public:
virtual ~NetworkOpener() = default;
void initialize();
void update();
void publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState);
void publishAuthorizationInfo(const uint32_t authId, const char* authName);

View File

@@ -1,7 +1,7 @@
#include "PresenceDetection.h"
#include "PreferencesKeys.h"
PresenceDetection::PresenceDetection(Preferences* preferences, BleScanner::Scanner *bleScanner, NetworkLock* network)
PresenceDetection::PresenceDetection(Preferences* preferences, BleScanner::Scanner *bleScanner, Network* network)
: _preferences(preferences),
_bleScanner(bleScanner),
_network(network)

View File

@@ -2,7 +2,7 @@
#include "BleScanner.h"
#include "BleInterfaces.h"
#include "NetworkLock.h"
#include "Network.h"
struct PdDevice
{
@@ -18,7 +18,7 @@ struct PdDevice
class PresenceDetection : public BleScanner::Subscriber
{
public:
PresenceDetection(Preferences* preferences, BleScanner::Scanner* bleScanner, NetworkLock* network);
PresenceDetection(Preferences* preferences, BleScanner::Scanner* bleScanner, Network* network);
virtual ~PresenceDetection();
void initialize();
@@ -31,7 +31,7 @@ private:
Preferences* _preferences;
BleScanner::Scanner* _bleScanner;
NetworkLock* _network;
Network* _network;
char* _csv = {0};
std::map<long long, PdDevice> _devices;
int _timeout = 20000;

View File

@@ -36,8 +36,6 @@ void networkTask(void *pvParameters)
// Network Device and MQTT is connected. Process all updates.
case 0:
network->update();
networkLock->update();
networkOpener->update();
webCfgServer->update();
break;
case 1:
@@ -210,7 +208,7 @@ void setup()
webCfgServer = new WebCfgServer(nuki, nukiOpener, network, ethServer, preferences, networkDevice == NetworkDeviceType::WiFi);
webCfgServer->initialize();
presenceDetection = new PresenceDetection(preferences, bleScanner, networkLock);
presenceDetection = new PresenceDetection(preferences, bleScanner, network);
presenceDetection->initialize();
setupTasks();