From 3bdb30e99881b6a9346a54356e2a9713676ea214 Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 18 Aug 2024 09:42:30 +0200 Subject: [PATCH] remove PresenceDetection code --- clion/CMakeLists.txt | 1 - src/NukiNetwork.cpp | 27 +---- src/NukiNetwork.h | 10 +- src/NukiNetworkLock.cpp | 4 - src/NukiWrapper.cpp | 5 +- src/PresenceDetection.cpp | 242 -------------------------------------- src/PresenceDetection.h | 43 ------- src/WebCfgServer.cpp | 15 --- src/main.cpp | 15 +-- 9 files changed, 5 insertions(+), 357 deletions(-) delete mode 100644 src/PresenceDetection.cpp delete mode 100644 src/PresenceDetection.h diff --git a/clion/CMakeLists.txt b/clion/CMakeLists.txt index 9e044d2..2a62b19 100644 --- a/clion/CMakeLists.txt +++ b/clion/CMakeLists.txt @@ -27,7 +27,6 @@ set(SRCFILES ../src/MqttTopics.h ../src/WebCfgServerConstants.h ../src/WebCfgServer.cpp - ../src/PresenceDetection.cpp ../src/PreferencesKeys.h ../src/Gpio.cpp ../src/Logger.cpp diff --git a/src/NukiNetwork.cpp b/src/NukiNetwork.cpp index f192206..0731a33 100644 --- a/src/NukiNetwork.cpp +++ b/src/NukiNetwork.cpp @@ -23,9 +23,8 @@ extern const uint8_t x509_crt_imported_bundle_bin_start[] asm("_binary_x509_crt_ extern const uint8_t x509_crt_imported_bundle_bin_end[] asm("_binary_x509_crt_bundle_end"); #ifndef NUKI_HUB_UPDATER -NukiNetwork::NukiNetwork(Preferences *preferences, PresenceDetection* presenceDetection, Gpio* gpio, const String& maintenancePathPrefix, char* buffer, size_t bufferSize) +NukiNetwork::NukiNetwork(Preferences *preferences, Gpio* gpio, const String& maintenancePathPrefix, char* buffer, size_t bufferSize) : _preferences(preferences), - _presenceDetection(presenceDetection), _gpio(gpio), _buffer(buffer), _bufferSize(bufferSize) @@ -399,22 +398,6 @@ bool NukiNetwork::update() _lastConnectedTs = ts; - #if PRESENCE_DETECTION_ENABLED - if(_presenceDetection != nullptr && (_lastPresenceTs == 0 || (ts - _lastPresenceTs) > 3000)) - { - char* presenceCsv = _presenceDetection->generateCsv(); - bool success = publishString(_mqttPresencePrefix, mqtt_topic_presence, presenceCsv, true); - - if(!success) - { - Log->println(F("Failed to publish presence CSV data.")); - Log->println(presenceCsv); - } - - _lastPresenceTs = ts; - } - #endif - if(_device->signalStrength() != 127 && _rssiPublishInterval > 0 && ts - _lastRssiTs > _rssiPublishInterval) { _lastRssiTs = ts; @@ -753,14 +736,6 @@ void NukiNetwork::gpioActionCallback(const GpioAction &action, const int &pin) _gpioTs[pin] = (esp_timer_get_time() / 1000); } -#if PRESENCE_DETECTION_ENABLED -void NukiNetwork::setMqttPresencePath(char *path) -{ - memset(_mqttPresencePrefix, 0, sizeof(_mqttPresencePrefix)); - strcpy(_mqttPresencePrefix, path); -} -#endif - void NukiNetwork::disableAutoRestarts() { _networkTimeout = 0; diff --git a/src/NukiNetwork.h b/src/NukiNetwork.h index 6fd2830..ec0d20e 100644 --- a/src/NukiNetwork.h +++ b/src/NukiNetwork.h @@ -14,7 +14,6 @@ #include "Gpio.h" #include #include "NukiConstants.h" -#include "PresenceDetection.h" #endif #define JSON_BUFFER_SIZE 1024 @@ -37,12 +36,9 @@ public: #ifdef NUKI_HUB_UPDATER explicit NukiNetwork(Preferences* preferences); #else - explicit NukiNetwork(Preferences* preferences, PresenceDetection* presenceDetection, Gpio* gpio, const String& maintenancePathPrefix, char* buffer, size_t bufferSize); + explicit NukiNetwork(Preferences* preferences, Gpio* gpio, const String& maintenancePathPrefix, char* buffer, size_t bufferSize); void registerMqttReceiver(MqttReceiver* receiver); - #if PRESENCE_DETECTION_ENABLED - void setMqttPresencePath(char* path); - #endif void disableAutoRestarts(); // disable on OTA start void disableMqtt(); String localIP(); @@ -148,7 +144,6 @@ private: String _lockPath; String _discoveryTopic; - PresenceDetection* _presenceDetection; Gpio* _gpio; int _mqttConnectionState = 0; @@ -177,9 +172,6 @@ private: int64_t _lastConnectedTs = 0; int64_t _lastMaintenanceTs = 0; int64_t _lastUpdateCheckTs = 0; - #if PRESENCE_DETECTION_ENABLED - int64_t _lastPresenceTs = 0; - #endif int64_t _lastRssiTs = 0; bool _mqttEnabled = true; int _rssiPublishInterval = 0; diff --git a/src/NukiNetworkLock.cpp b/src/NukiNetworkLock.cpp index e246b7d..5bf97c7 100644 --- a/src/NukiNetworkLock.cpp +++ b/src/NukiNetworkLock.cpp @@ -60,10 +60,6 @@ void NukiNetworkLock::initialize() _preferences->putString(preference_mqtt_lock_path, _mqttPath); } - #if PRESENCE_DETECTION_ENABLED - _network->setMqttPresencePath(_mqttPath); - #endif - _haEnabled = _preferences->getString(preference_mqtt_hass_discovery, "") != ""; _disableNonJSON = _preferences->getBool(preference_disable_non_json, false); _offEnabled = _preferences->getBool(preference_official_hybrid, false); diff --git a/src/NukiWrapper.cpp b/src/NukiWrapper.cpp index 02aaec0..b1597be 100644 --- a/src/NukiWrapper.cpp +++ b/src/NukiWrapper.cpp @@ -51,6 +51,8 @@ NukiWrapper::~NukiWrapper() void NukiWrapper::initialize(const bool& firstStart) { + _preferences->remove(preference_presence_detection_timeout); + _nukiLock.initialize(); esp_power_level_t powerLevel; @@ -134,9 +136,6 @@ void NukiWrapper::initialize(const bool& firstStart) _preferences->putInt(preference_query_interval_configuration, 3600); _preferences->putInt(preference_query_interval_battery, 1800); _preferences->putInt(preference_query_interval_keypad, 1800); - #if PRESENCE_DETECTION_ENABLED - _preferences->putInt(preference_presence_detection_timeout, -1); - #endif } if(_nrOfRetries < 0 || _nrOfRetries == 200) diff --git a/src/PresenceDetection.cpp b/src/PresenceDetection.cpp deleted file mode 100644 index 791f033..0000000 --- a/src/PresenceDetection.cpp +++ /dev/null @@ -1,242 +0,0 @@ -#include "PresenceDetection.h" -#include "PreferencesKeys.h" -#include "Logger.h" -#include "CharBuffer.h" -#include -#include -#include "NimBLEBeacon.h" -#include "NukiUtils.h" - -PresenceDetection::PresenceDetection(Preferences* preferences, BleScanner::Scanner *bleScanner, char* buffer, size_t bufferSize) -: _preferences(preferences), - _bleScanner(bleScanner), - _csv(buffer), - _bufferSize(bufferSize) -{ - _timeout = _preferences->getInt(preference_presence_detection_timeout, 0) * 1000; - if(_timeout == 0) - { - _timeout = 60000; - _preferences->putInt(preference_presence_detection_timeout, 60); - } - - Log->print(F("Presence detection timeout (ms): ")); - Log->println(_timeout); -} - -PresenceDetection::~PresenceDetection() -{ - _bleScanner->unsubscribe(this); - _bleScanner = nullptr; - - delete _csv; - _csv = nullptr; -} - -void PresenceDetection::initialize() -{ - _bleScanner->subscribe(this); -} - -char* PresenceDetection::generateCsv() -{ - if(!enabled()) return nullptr; - memset(_csv, 0, _bufferSize); - - _csvIndex = 0; - int64_t ts = esp_timer_get_time() / 1000; - { - std::lock_guard lock(mtx); - - for (auto it: _devices) - { - if (ts - _timeout < it.second->timestamp) - { - buildCsv(it.second); - } - - // Prevent csv buffer overflow - if (_csvIndex > _bufferSize - (sizeof(it.second->name) + sizeof(it.second->address) + 10)) - { - break; - } - } - } - - if(_csvIndex == 0) - { - strcpy(_csv, ";;"); - return _csv; - } - - _csv[_csvIndex-1] = 0x00; - return _csv; -} - - -void PresenceDetection::buildCsv(const std::shared_ptr& device) -{ - for(int i = 0; i < 17; i++) - { - _csv[_csvIndex] = device->address[i]; - ++_csvIndex; - } - _csv[_csvIndex] = ';'; - ++_csvIndex; - - int i=0; - while(device->name[i] != 0x00 && i < sizeof(device->name)) - { - _csv[_csvIndex] = device->name[i]; - ++_csvIndex; - ++i; - } - - _csv[_csvIndex] = ';'; - ++_csvIndex; - - char rssiStr[20] = {0}; - itoa(device->rssi, rssiStr, 10); - - int j=0; - while(rssiStr[j] != 0x00 && j < 20) - { - _csv[_csvIndex] = rssiStr[j]; - ++_csvIndex; - ++j; - } - - _csv[_csvIndex] = '\n'; - _csvIndex++; -} - -void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) -{ - std::string addressStr = device->getAddress().toString(); - char addrArrComp[13] = {0}; - -// Log->println(addressStr.c_str()); - - addrArrComp[0] = addressStr.at(0); - addrArrComp[1] = addressStr.at(1); - addrArrComp[2] = addressStr.at(3); - addrArrComp[3] = addressStr.at(4); - addrArrComp[4] = addressStr.at(6); - addrArrComp[5] = addressStr.at(7); - addrArrComp[6] = addressStr.at(9); - addrArrComp[7] = addressStr.at(10); - addrArrComp[8] = addressStr.at(12); - addrArrComp[9] = addressStr.at(13); - addrArrComp[10] = addressStr.at(15); - addrArrComp[11] = addressStr.at(16); - - int64_t addr = strtoll(addrArrComp, nullptr, 16); - - bool found; - { - std::lock_guard lock(mtx); - auto it = _devices.find(addr); - found = (it != _devices.end()); - - if(found) - { - it->second->timestamp = esp_timer_get_time() / 1000; - it->second->rssi = device->getRSSI(); - } - } - - if(!found) - { - std::shared_ptr pdDevice = std::make_shared(); - - int i=0; - size_t len = addressStr.length(); - while(i < len) - { - pdDevice->address[i] = addressStr.at(i); - ++i; - } - - pdDevice->rssi = device->getRSSI(); - - std::string nameStr = "-"; - if(device->haveName()) - { - std::string nameStr = device->getName(); - - i=0; - len = nameStr.length(); - while(i < len && i < sizeof(pdDevice->name)-1) - { - pdDevice->name[i] = nameStr.at(i); - ++i; - } - - pdDevice->timestamp = esp_timer_get_time() / 1000; - - { - std::lock_guard lock(mtx); - _devices[addr] = pdDevice; - } - } - else if (device->haveManufacturerData()) - { - std::string strManufacturerData = device->getManufacturerData(); - - uint8_t cManufacturerData[100]; - strManufacturerData.copy((char *)cManufacturerData, std::min(strManufacturerData.length(), sizeof(cManufacturerData)), 0); - - if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00) - { - BLEBeacon oBeacon = BLEBeacon(); - oBeacon.setData(strManufacturerData); - -// if(ENDIAN_CHANGE_U16(oBeacon.getMinor()) == 40004) -// { - pdDevice->timestamp = esp_timer_get_time() / 1000; - strcpy(pdDevice->name, oBeacon.getProximityUUID().toString().c_str()); - { - std::lock_guard lock(mtx); - _devices[addr] = pdDevice; - } -// } - } - } - else - { - std::string nameStr = "-"; - i=0; - len = nameStr.length(); - while(i < len && i < sizeof(pdDevice->name)-1) - { - pdDevice->name[i] = nameStr.at(i); - ++i; - } - - pdDevice->timestamp = esp_timer_get_time() / 1000; - - { - std::lock_guard lock(mtx); - _devices[addr] = pdDevice; - } - } - } - -// if(device->haveName()) -// { -// Log->print(" | "); -// Log->print(device->getName().c_str()); -// if(device->haveRSSI()) -// { -// Log->print(" | "); -// Log->print(device->getRSSI()); -// } -// } -// Log->println(); - -} - -bool PresenceDetection::enabled() -{ - return _timeout > 0; -} \ No newline at end of file diff --git a/src/PresenceDetection.h b/src/PresenceDetection.h deleted file mode 100644 index dbdf8d3..0000000 --- a/src/PresenceDetection.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include -#include "BleScanner.h" -#include "BleInterfaces.h" -#include -#include -#include - -struct PdDevice -{ - char address[18] = {0}; - char name[37] = {0}; - int64_t timestamp = 0; - int rssi = 0; - bool hasRssi = false; -}; - -class PresenceDetection : public BleScanner::Subscriber -{ -public: - PresenceDetection(Preferences* preferences, BleScanner::Scanner* bleScanner, char* buffer, size_t bufferSize); - virtual ~PresenceDetection(); - - void initialize(); - char* generateCsv(); - bool enabled(); - - void onResult(NimBLEAdvertisedDevice* advertisedDevice) override; - -private: - void buildCsv(const std::shared_ptr& device); - - std::mutex mtx; - - Preferences* _preferences; - BleScanner::Scanner* _bleScanner; - char* _csv = {0}; - size_t _bufferSize = 0; - std::map> _devices; - int _timeout = 20000; - int _csvIndex = 0; -}; diff --git a/src/WebCfgServer.cpp b/src/WebCfgServer.cpp index fe6acf3..8142ec1 100644 --- a/src/WebCfgServer.cpp +++ b/src/WebCfgServer.cpp @@ -1378,18 +1378,6 @@ bool WebCfgServer::processArgs(AsyncWebServerRequest *request, String& message) } } } - #if PRESENCE_DETECTION_ENABLED - else if(key == "PRDTMO") - { - if(_preferences->getInt(preference_presence_detection_timeout, 60) != value.toInt()) - { - _preferences->putInt(preference_presence_detection_timeout, value.toInt()); - Log->print(F("Setting changed: ")); - Log->println(key); - configChanged = true; - } - } - #endif else if(key == "RSBC") { if(_preferences->getInt(preference_restart_ble_beacon_lost, 60) != value.toInt()) @@ -3103,9 +3091,6 @@ void WebCfgServer::buildNukiConfigHtml(AsyncWebServerRequest *request) printInputField(response, "TRYDLY", "Delay between retries (milliseconds)", _preferences->getInt(preference_command_retry_delay), 10, ""); if(_preferences->getBool(preference_lock_enabled, true)) printCheckBox(response, "REGAPP", "Lock: Nuki Bridge is running alongside Nuki Hub (needs re-pairing if changed)", _preferences->getBool(preference_register_as_app), ""); if(_preferences->getBool(preference_opener_enabled, false)) printCheckBox(response, "REGAPPOPN", "Opener: Nuki Bridge is running alongside Nuki Hub (needs re-pairing if changed)", _preferences->getBool(preference_register_opener_as_app), ""); -#if PRESENCE_DETECTION_ENABLED - printInputField(response, "PRDTMO", "Presence detection timeout (seconds; -1 to disable)", _preferences->getInt(preference_presence_detection_timeout), 10, ""); -#endif printInputField(response, "RSBC", "Restart if bluetooth beacons not received (seconds; -1 to disable)", _preferences->getInt(preference_restart_ble_beacon_lost), 10, ""); printInputField(response, "TXPWR", "BLE transmit power in dB (minimum -12, maximum 9)", _preferences->getInt(preference_ble_tx_power, 9), 10, ""); diff --git a/src/main.cpp b/src/main.cpp index 46fa427..7858f0b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,6 @@ #ifndef NUKI_HUB_UPDATER #include "NukiWrapper.h" #include "NukiNetworkLock.h" -#include "PresenceDetection.h" #include "NukiOpenerWrapper.h" #include "Gpio.h" #include "CharBuffer.h" @@ -33,7 +32,6 @@ NukiNetworkOpener* networkOpener = nullptr; BleScanner::Scanner* bleScanner = nullptr; NukiWrapper* nuki = nullptr; NukiOpenerWrapper* nukiOpener = nullptr; -PresenceDetection* presenceDetection = nullptr; NukiDeviceId* deviceIdLock = nullptr; NukiDeviceId* deviceIdOpener = nullptr; Gpio* gpio = nullptr; @@ -42,7 +40,6 @@ bool lockEnabled = false; bool openerEnabled = false; TaskHandle_t nukiTaskHandle = nullptr; -TaskHandle_t presenceDetectionTaskHandle = nullptr; int64_t restartTs = ((2^64) - (5 * 1000 * 60000)) / 1000; @@ -175,7 +172,6 @@ void nukiTask(void *pvParameters) { delay(5000); } - #ifndef PRESENCE_DETECTION_ENABLED else if (!whiteListed) { whiteListed = true; @@ -188,7 +184,6 @@ void nukiTask(void *pvParameters) bleScanner->whitelist(nukiOpener->getBleAddress()); } } - #endif if(lockEnabled) { @@ -459,20 +454,12 @@ void setup() bleScanner->initialize("NukiHub", true, 40, 40); bleScanner->setScanDuration(0); - #if PRESENCE_DETECTION_ENABLED - if(preferences->getInt(preference_presence_detection_timeout) >= 0) - { - presenceDetection = new PresenceDetection(preferences, bleScanner, CharBuffer::get(), buffer_size); - presenceDetection->initialize(); - } - #endif - lockEnabled = preferences->getBool(preference_lock_enabled); openerEnabled = preferences->getBool(preference_opener_enabled); const String mqttLockPath = preferences->getString(preference_mqtt_lock_path); - network = new NukiNetwork(preferences, presenceDetection, gpio, mqttLockPath, CharBuffer::get(), buffer_size); + network = new NukiNetwork(preferences, gpio, mqttLockPath, CharBuffer::get(), buffer_size); network->initialize(); networkLock = new NukiNetworkLock(network, preferences, CharBuffer::get(), buffer_size);