move publish presence detection to Network class
This commit is contained in:
24
Network.cpp
24
Network.cpp
@@ -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();
|
_device->mqttClient()->loop();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -233,6 +244,12 @@ void Network::reconfigureDevice()
|
|||||||
_device->reconfigure();
|
_device->reconfigure();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::setMqttPresencePath(char *path)
|
||||||
|
{
|
||||||
|
memset(_mqttPresencePrefix, 0, sizeof(_mqttPresencePrefix));
|
||||||
|
strcpy(_mqttPresencePrefix, path);
|
||||||
|
}
|
||||||
|
|
||||||
bool Network::isMqttConnected()
|
bool Network::isMqttConnected()
|
||||||
{
|
{
|
||||||
return _mqttConnected;
|
return _mqttConnected;
|
||||||
@@ -372,4 +389,9 @@ void Network::removeHASSConfig(char* uidString)
|
|||||||
|
|
||||||
_device->mqttClient()->publish(path.c_str(), NULL, 0U, true);
|
_device->mqttClient()->publish(path.c_str(), NULL, 0U, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::publishPresenceDetection(char *csv)
|
||||||
|
{
|
||||||
|
_presenceCsv = csv;
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ public:
|
|||||||
int update();
|
int update();
|
||||||
void registerMqttReceiver(MqttReceiver* receiver);
|
void registerMqttReceiver(MqttReceiver* receiver);
|
||||||
void reconfigureDevice();
|
void reconfigureDevice();
|
||||||
|
void setMqttPresencePath(char* path);
|
||||||
|
|
||||||
void subscribe(const char* prefix, const 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);
|
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 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 removeHASSConfig(char* uidString);
|
||||||
|
|
||||||
|
void publishPresenceDetection(char* csv);
|
||||||
|
|
||||||
PubSubClient* mqttClient();
|
PubSubClient* mqttClient();
|
||||||
bool isMqttConnected();
|
bool isMqttConnected();
|
||||||
|
|
||||||
@@ -52,9 +55,11 @@ private:
|
|||||||
char _mqttBrokerAddr[101] = {0};
|
char _mqttBrokerAddr[101] = {0};
|
||||||
char _mqttUser[31] = {0};
|
char _mqttUser[31] = {0};
|
||||||
char _mqttPass[31] = {0};
|
char _mqttPass[31] = {0};
|
||||||
|
char _mqttPresencePrefix[181] = {0};
|
||||||
std::vector<String> _subscribedTopics;
|
std::vector<String> _subscribedTopics;
|
||||||
int _networkTimeout = 0;
|
int _networkTimeout = 0;
|
||||||
std::vector<MqttReceiver*> _mqttReceivers;
|
std::vector<MqttReceiver*> _mqttReceivers;
|
||||||
|
char* _presenceCsv = nullptr;
|
||||||
|
|
||||||
unsigned long _lastConnectedTs = 0;
|
unsigned long _lastConnectedTs = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ void NetworkLock::initialize()
|
|||||||
_preferences->putString(preference_mqtt_lock_path, _mqttPath);
|
_preferences->putString(preference_mqtt_lock_path, _mqttPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_network->setMqttPresencePath(_mqttPath);
|
||||||
|
|
||||||
_network->subscribe(_mqttPath, mqtt_topic_lock_action);
|
_network->subscribe(_mqttPath, mqtt_topic_lock_action);
|
||||||
for(const auto& topic : _configTopics)
|
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)
|
void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
|
||||||
{
|
{
|
||||||
char value[50] = {0};
|
char value[50] = {0};
|
||||||
@@ -181,11 +169,6 @@ void NetworkLock::publishAdvancedConfig(const NukiLock::AdvancedConfig &config)
|
|||||||
publishBool(mqtt_topic_config_auto_lock, config.autoLockEnabled == 1);
|
publishBool(mqtt_topic_config_auto_lock, config.autoLockEnabled == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkLock::publishPresenceDetection(char *csv)
|
|
||||||
{
|
|
||||||
_presenceCsv = csv;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkLock::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *))
|
void NetworkLock::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *))
|
||||||
{
|
{
|
||||||
_lockActionReceivedCallback = lockActionReceivedCallback;
|
_lockActionReceivedCallback = lockActionReceivedCallback;
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ public:
|
|||||||
virtual ~NetworkLock();
|
virtual ~NetworkLock();
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
void update();
|
|
||||||
|
|
||||||
void publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurnerState, const NukiLock::KeyTurnerState& lastKeyTurnerState);
|
void publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurnerState, const NukiLock::KeyTurnerState& lastKeyTurnerState);
|
||||||
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
|
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
|
||||||
@@ -26,7 +25,6 @@ public:
|
|||||||
void publishBatteryReport(const NukiLock::BatteryReport& batteryReport);
|
void publishBatteryReport(const NukiLock::BatteryReport& batteryReport);
|
||||||
void publishConfig(const NukiLock::Config& config);
|
void publishConfig(const NukiLock::Config& config);
|
||||||
void publishAdvancedConfig(const NukiLock::AdvancedConfig& 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 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 removeHASSConfig(char* uidString);
|
||||||
|
|
||||||
@@ -51,7 +49,6 @@ private:
|
|||||||
Network* _network;
|
Network* _network;
|
||||||
Preferences* _preferences;
|
Preferences* _preferences;
|
||||||
|
|
||||||
char* _presenceCsv = nullptr;
|
|
||||||
std::vector<char*> _configTopics;
|
std::vector<char*> _configTopics;
|
||||||
char _mqttPath[181] = {0};
|
char _mqttPath[181] = {0};
|
||||||
|
|
||||||
|
|||||||
@@ -38,18 +38,6 @@ void NetworkOpener::initialize()
|
|||||||
_network->registerMqttReceiver(this);
|
_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)
|
void NetworkOpener::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &length)
|
||||||
{
|
{
|
||||||
char value[50] = {0};
|
char value[50] = {0};
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ public:
|
|||||||
virtual ~NetworkOpener() = default;
|
virtual ~NetworkOpener() = default;
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
void update();
|
|
||||||
|
|
||||||
void publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState);
|
void publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState);
|
||||||
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
|
void publishAuthorizationInfo(const uint32_t authId, const char* authName);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "PresenceDetection.h"
|
#include "PresenceDetection.h"
|
||||||
#include "PreferencesKeys.h"
|
#include "PreferencesKeys.h"
|
||||||
|
|
||||||
PresenceDetection::PresenceDetection(Preferences* preferences, BleScanner::Scanner *bleScanner, NetworkLock* network)
|
PresenceDetection::PresenceDetection(Preferences* preferences, BleScanner::Scanner *bleScanner, Network* network)
|
||||||
: _preferences(preferences),
|
: _preferences(preferences),
|
||||||
_bleScanner(bleScanner),
|
_bleScanner(bleScanner),
|
||||||
_network(network)
|
_network(network)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "BleScanner.h"
|
#include "BleScanner.h"
|
||||||
#include "BleInterfaces.h"
|
#include "BleInterfaces.h"
|
||||||
#include "NetworkLock.h"
|
#include "Network.h"
|
||||||
|
|
||||||
struct PdDevice
|
struct PdDevice
|
||||||
{
|
{
|
||||||
@@ -18,7 +18,7 @@ struct PdDevice
|
|||||||
class PresenceDetection : public BleScanner::Subscriber
|
class PresenceDetection : public BleScanner::Subscriber
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PresenceDetection(Preferences* preferences, BleScanner::Scanner* bleScanner, NetworkLock* network);
|
PresenceDetection(Preferences* preferences, BleScanner::Scanner* bleScanner, Network* network);
|
||||||
virtual ~PresenceDetection();
|
virtual ~PresenceDetection();
|
||||||
|
|
||||||
void initialize();
|
void initialize();
|
||||||
@@ -31,7 +31,7 @@ private:
|
|||||||
|
|
||||||
Preferences* _preferences;
|
Preferences* _preferences;
|
||||||
BleScanner::Scanner* _bleScanner;
|
BleScanner::Scanner* _bleScanner;
|
||||||
NetworkLock* _network;
|
Network* _network;
|
||||||
char* _csv = {0};
|
char* _csv = {0};
|
||||||
std::map<long long, PdDevice> _devices;
|
std::map<long long, PdDevice> _devices;
|
||||||
int _timeout = 20000;
|
int _timeout = 20000;
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -36,8 +36,6 @@ void networkTask(void *pvParameters)
|
|||||||
// Network Device and MQTT is connected. Process all updates.
|
// Network Device and MQTT is connected. Process all updates.
|
||||||
case 0:
|
case 0:
|
||||||
network->update();
|
network->update();
|
||||||
networkLock->update();
|
|
||||||
networkOpener->update();
|
|
||||||
webCfgServer->update();
|
webCfgServer->update();
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
@@ -210,7 +208,7 @@ void setup()
|
|||||||
webCfgServer = new WebCfgServer(nuki, nukiOpener, network, ethServer, preferences, networkDevice == NetworkDeviceType::WiFi);
|
webCfgServer = new WebCfgServer(nuki, nukiOpener, network, ethServer, preferences, networkDevice == NetworkDeviceType::WiFi);
|
||||||
webCfgServer->initialize();
|
webCfgServer->initialize();
|
||||||
|
|
||||||
presenceDetection = new PresenceDetection(preferences, bleScanner, networkLock);
|
presenceDetection = new PresenceDetection(preferences, bleScanner, network);
|
||||||
presenceDetection->initialize();
|
presenceDetection->initialize();
|
||||||
|
|
||||||
setupTasks();
|
setupTasks();
|
||||||
|
|||||||
Reference in New Issue
Block a user