add locks in presence detection for _device dictionary
This commit is contained in:
@@ -45,6 +45,9 @@ char* PresenceDetection::generateCsv()
|
|||||||
|
|
||||||
_csvIndex = 0;
|
_csvIndex = 0;
|
||||||
long ts = millis();
|
long ts = millis();
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
|
||||||
for (auto it: _devices)
|
for (auto it: _devices)
|
||||||
{
|
{
|
||||||
if (ts - _timeout < it.second->timestamp)
|
if (ts - _timeout < it.second->timestamp)
|
||||||
@@ -58,6 +61,7 @@ char* PresenceDetection::generateCsv()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(_csvIndex == 0)
|
if(_csvIndex == 0)
|
||||||
{
|
{
|
||||||
@@ -131,8 +135,24 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
|||||||
|
|
||||||
long long addr = strtoll(addrArrComp, nullptr, 16);
|
long long addr = strtoll(addrArrComp, nullptr, 16);
|
||||||
|
|
||||||
|
bool found;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
auto it = _devices.find(addr);
|
auto it = _devices.find(addr);
|
||||||
if(it == _devices.end())
|
found = (it != _devices.end());
|
||||||
|
|
||||||
|
if(found)
|
||||||
|
{
|
||||||
|
it->second->timestamp = millis();
|
||||||
|
if(device->haveRSSI())
|
||||||
|
{
|
||||||
|
it->second->hasRssi = true;
|
||||||
|
it->second->rssi = device->getRSSI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!found)
|
||||||
{
|
{
|
||||||
|
|
||||||
std::shared_ptr<PdDevice> pdDevice = std::make_shared<PdDevice>();
|
std::shared_ptr<PdDevice> pdDevice = std::make_shared<PdDevice>();
|
||||||
@@ -166,8 +186,11 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
|||||||
|
|
||||||
pdDevice->timestamp = millis();
|
pdDevice->timestamp = millis();
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
_devices[addr] = pdDevice;
|
_devices[addr] = pdDevice;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (device->haveManufacturerData())
|
else if (device->haveManufacturerData())
|
||||||
{
|
{
|
||||||
std::string strManufacturerData = device->getManufacturerData();
|
std::string strManufacturerData = device->getManufacturerData();
|
||||||
@@ -184,19 +207,13 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
|||||||
{
|
{
|
||||||
pdDevice->timestamp = millis();
|
pdDevice->timestamp = millis();
|
||||||
strcpy(pdDevice->name, oBeacon.getProximityUUID().toString().c_str());
|
strcpy(pdDevice->name, oBeacon.getProximityUUID().toString().c_str());
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
_devices[addr] = pdDevice;
|
_devices[addr] = pdDevice;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
it->second->timestamp = millis();
|
|
||||||
if(device->haveRSSI())
|
|
||||||
{
|
|
||||||
it->second->hasRssi = true;
|
|
||||||
it->second->rssi = device->getRSSI();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if(device->haveName())
|
// if(device->haveName())
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "BleScanner.h"
|
#include "BleScanner.h"
|
||||||
#include "BleInterfaces.h"
|
#include "BleInterfaces.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
struct PdDevice
|
struct PdDevice
|
||||||
{
|
{
|
||||||
@@ -29,6 +30,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
void buildCsv(const std::shared_ptr<PdDevice>& device);
|
void buildCsv(const std::shared_ptr<PdDevice>& device);
|
||||||
|
|
||||||
|
std::mutex mtx;
|
||||||
|
|
||||||
Preferences* _preferences;
|
Preferences* _preferences;
|
||||||
BleScanner::Scanner* _bleScanner;
|
BleScanner::Scanner* _bleScanner;
|
||||||
char* _csv = {0};
|
char* _csv = {0};
|
||||||
|
|||||||
Reference in New Issue
Block a user