From 5a5f5d8658878b842a079d643121aed757025d32 Mon Sep 17 00:00:00 2001 From: technyon Date: Wed, 8 Jun 2022 22:50:57 +0200 Subject: [PATCH] presence detection fixes --- Network.cpp | 8 +++++++- Network.h | 1 + PresenceDetection.cpp | 25 +++++++++++++++---------- Version.h | 2 +- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Network.cpp b/Network.cpp index 1446cc9..31af0ee 100644 --- a/Network.cpp +++ b/Network.cpp @@ -205,7 +205,7 @@ void Network::update() if(_presenceCsv != nullptr && strlen(_presenceCsv) > 0) { - publishString(mqtt_topic_presence, _presenceCsv); + publishString_P(mqtt_topic_presence, _presenceCsv); _presenceCsv = nullptr; } @@ -394,6 +394,12 @@ void Network::publishString(const char *topic, const char *value) _device->mqttClient()->publish(path, value); } +void Network::publishString_P(const char *topic, const char *value) +{ + char path[200] = {0}; + buildMqttPath(topic, path); + _device->mqttClient()->publish_P(path, value, true); +} bool Network::isMqttConnected() { diff --git a/Network.h b/Network.h index 3fea58b..05c4d8c 100644 --- a/Network.h +++ b/Network.h @@ -55,6 +55,7 @@ private: void publishUInt(const char* topic, const unsigned int value); void publishBool(const char* topic, const bool value); void publishString(const char* topic, const char* value); + void publishString_P(const char* topic, const char* value); void buildMqttPath(const char* path, char* outPath); void subscribe(const char* path); diff --git a/PresenceDetection.cpp b/PresenceDetection.cpp index c384433..681bcc0 100644 --- a/PresenceDetection.cpp +++ b/PresenceDetection.cpp @@ -37,7 +37,7 @@ void PresenceDetection::initialize() void PresenceDetection::update() { - vTaskDelay( 5000 / portTICK_PERIOD_MS); + delay(3000); if(_timeout < 0) return; if(_devices.size() == 0) @@ -65,6 +65,8 @@ void PresenceDetection::update() _csv[_csvIndex-1] = 0x00; +// Serial.print("Devices found: "); +// Serial.println(_devices.size()); _network->publishPresenceDetection(_csv); } @@ -108,12 +110,13 @@ void PresenceDetection::buildCsv(const PdDevice &device) _csvIndex++; } - void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) { std::string addressStr = device->getAddress().toString(); char addrArrComp[13] = {0}; +// Serial.println(addressStr.c_str()); + addrArrComp[0] = addressStr.at(0); addrArrComp[1] = addressStr.at(1); addrArrComp[2] = addressStr.at(3); @@ -132,6 +135,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) auto it = _devices.find(addr); if(it == _devices.end()) { + PdDevice pdDevice; int i=0; @@ -142,12 +146,19 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) ++i; } + if(device->haveRSSI()) + { + pdDevice.hasRssi = true; + pdDevice.rssi = device->getRSSI(); + } + + std::string nameStr = "-"; if(device->haveName()) { std::string nameStr = device->getName(); - int i=0; - size_t len = nameStr.length(); + i=0; + len = nameStr.length(); while(i < len && i < sizeof(pdDevice.name)-1) { pdDevice.name[i] = nameStr.at(i); @@ -158,12 +169,6 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) _devices[addr] = pdDevice; } - - if(device->haveRSSI()) - { - pdDevice.hasRssi = true; - pdDevice.rssi = device->getRSSI(); - } } else { diff --git a/Version.h b/Version.h index 0d6b3c3..ec6d5f2 100644 --- a/Version.h +++ b/Version.h @@ -1,3 +1,3 @@ #pragma once -#define nuki_hub_version "2.6" \ No newline at end of file +#define nuki_hub_version "3.0" \ No newline at end of file