presence detection fixes

This commit is contained in:
technyon
2022-06-08 22:50:57 +02:00
parent 0dd1612f39
commit 5a5f5d8658
4 changed files with 24 additions and 12 deletions

View File

@@ -205,7 +205,7 @@ void Network::update()
if(_presenceCsv != nullptr && strlen(_presenceCsv) > 0) if(_presenceCsv != nullptr && strlen(_presenceCsv) > 0)
{ {
publishString(mqtt_topic_presence, _presenceCsv); publishString_P(mqtt_topic_presence, _presenceCsv);
_presenceCsv = nullptr; _presenceCsv = nullptr;
} }
@@ -394,6 +394,12 @@ void Network::publishString(const char *topic, const char *value)
_device->mqttClient()->publish(path, 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() bool Network::isMqttConnected()
{ {

View File

@@ -55,6 +55,7 @@ private:
void publishUInt(const char* topic, const unsigned int value); void publishUInt(const char* topic, const unsigned int value);
void publishBool(const char* topic, const bool value); void publishBool(const char* topic, const bool value);
void publishString(const char* topic, const char* 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 buildMqttPath(const char* path, char* outPath);
void subscribe(const char* path); void subscribe(const char* path);

View File

@@ -37,7 +37,7 @@ void PresenceDetection::initialize()
void PresenceDetection::update() void PresenceDetection::update()
{ {
vTaskDelay( 5000 / portTICK_PERIOD_MS); delay(3000);
if(_timeout < 0) return; if(_timeout < 0) return;
if(_devices.size() == 0) if(_devices.size() == 0)
@@ -65,6 +65,8 @@ void PresenceDetection::update()
_csv[_csvIndex-1] = 0x00; _csv[_csvIndex-1] = 0x00;
// Serial.print("Devices found: ");
// Serial.println(_devices.size());
_network->publishPresenceDetection(_csv); _network->publishPresenceDetection(_csv);
} }
@@ -108,12 +110,13 @@ void PresenceDetection::buildCsv(const PdDevice &device)
_csvIndex++; _csvIndex++;
} }
void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
{ {
std::string addressStr = device->getAddress().toString(); std::string addressStr = device->getAddress().toString();
char addrArrComp[13] = {0}; char addrArrComp[13] = {0};
// Serial.println(addressStr.c_str());
addrArrComp[0] = addressStr.at(0); addrArrComp[0] = addressStr.at(0);
addrArrComp[1] = addressStr.at(1); addrArrComp[1] = addressStr.at(1);
addrArrComp[2] = addressStr.at(3); addrArrComp[2] = addressStr.at(3);
@@ -132,6 +135,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
auto it = _devices.find(addr); auto it = _devices.find(addr);
if(it == _devices.end()) if(it == _devices.end())
{ {
PdDevice pdDevice; PdDevice pdDevice;
int i=0; int i=0;
@@ -142,12 +146,19 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
++i; ++i;
} }
if(device->haveRSSI())
{
pdDevice.hasRssi = true;
pdDevice.rssi = device->getRSSI();
}
std::string nameStr = "-";
if(device->haveName()) if(device->haveName())
{ {
std::string nameStr = device->getName(); std::string nameStr = device->getName();
int i=0; i=0;
size_t len = nameStr.length(); len = nameStr.length();
while(i < len && i < sizeof(pdDevice.name)-1) while(i < len && i < sizeof(pdDevice.name)-1)
{ {
pdDevice.name[i] = nameStr.at(i); pdDevice.name[i] = nameStr.at(i);
@@ -158,12 +169,6 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
_devices[addr] = pdDevice; _devices[addr] = pdDevice;
} }
if(device->haveRSSI())
{
pdDevice.hasRssi = true;
pdDevice.rssi = device->getRSSI();
}
} }
else else
{ {

View File

@@ -1,3 +1,3 @@
#pragma once #pragma once
#define nuki_hub_version "2.6" #define nuki_hub_version "3.0"