use shared_ptr for presence detection device objects
This commit is contained in:
@@ -25,6 +25,7 @@ add_compile_definitions(TLS_KEY_MAX_SIZE=1800)
|
|||||||
add_compile_definitions(ESP_PLATFORM)
|
add_compile_definitions(ESP_PLATFORM)
|
||||||
add_compile_definitions(ESP32)
|
add_compile_definitions(ESP32)
|
||||||
add_compile_definitions(ARDUINO_ARCH_ESP32)
|
add_compile_definitions(ARDUINO_ARCH_ESP32)
|
||||||
|
# add_compile_definitions(CONFIG_BT_NIMBLE_PINNED_TO_CORE=1)
|
||||||
|
|
||||||
include_directories(${PROJECT_NAME}
|
include_directories(${PROJECT_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
|
|||||||
@@ -59,13 +59,13 @@ void PresenceDetection::update()
|
|||||||
long ts = millis();
|
long ts = millis();
|
||||||
for(auto it : _devices)
|
for(auto it : _devices)
|
||||||
{
|
{
|
||||||
if(ts - _timeout < it.second.timestamp)
|
if(ts - _timeout < it.second->timestamp)
|
||||||
{
|
{
|
||||||
buildCsv(it.second);
|
buildCsv(it.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent csv buffer overflow
|
// Prevent csv buffer overflow
|
||||||
if(_csvIndex > _bufferSize - (sizeof(it.second.name) + sizeof(it.second.address) + 10))
|
if(_csvIndex > _bufferSize - (sizeof(it.second->name) + sizeof(it.second->address) + 10))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -79,20 +79,20 @@ void PresenceDetection::update()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PresenceDetection::buildCsv(const PdDevice &device)
|
void PresenceDetection::buildCsv(const std::shared_ptr<PdDevice>& device)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < 17; i++)
|
for(int i = 0; i < 17; i++)
|
||||||
{
|
{
|
||||||
_csv[_csvIndex] = device.address[i];
|
_csv[_csvIndex] = device->address[i];
|
||||||
++_csvIndex;
|
++_csvIndex;
|
||||||
}
|
}
|
||||||
_csv[_csvIndex] = ';';
|
_csv[_csvIndex] = ';';
|
||||||
++_csvIndex;
|
++_csvIndex;
|
||||||
|
|
||||||
int i=0;
|
int i=0;
|
||||||
while(device.name[i] != 0x00 && i < sizeof(device.name))
|
while(device->name[i] != 0x00 && i < sizeof(device->name))
|
||||||
{
|
{
|
||||||
_csv[_csvIndex] = device.name[i];
|
_csv[_csvIndex] = device->name[i];
|
||||||
++_csvIndex;
|
++_csvIndex;
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
@@ -100,10 +100,10 @@ void PresenceDetection::buildCsv(const PdDevice &device)
|
|||||||
_csv[_csvIndex] = ';';
|
_csv[_csvIndex] = ';';
|
||||||
++_csvIndex;
|
++_csvIndex;
|
||||||
|
|
||||||
if(device.hasRssi)
|
if(device->hasRssi)
|
||||||
{
|
{
|
||||||
char rssiStr[20] = {0};
|
char rssiStr[20] = {0};
|
||||||
itoa(device.rssi, rssiStr, 10);
|
itoa(device->rssi, rssiStr, 10);
|
||||||
|
|
||||||
int i=0;
|
int i=0;
|
||||||
while(rssiStr[i] != 0x00 && i < 20)
|
while(rssiStr[i] != 0x00 && i < 20)
|
||||||
@@ -144,20 +144,20 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
|||||||
if(it == _devices.end())
|
if(it == _devices.end())
|
||||||
{
|
{
|
||||||
|
|
||||||
PdDevice pdDevice;
|
std::shared_ptr<PdDevice> pdDevice = std::make_shared<PdDevice>();
|
||||||
|
|
||||||
int i=0;
|
int i=0;
|
||||||
size_t len = addressStr.length();
|
size_t len = addressStr.length();
|
||||||
while(i < len)
|
while(i < len)
|
||||||
{
|
{
|
||||||
pdDevice.address[i] = addressStr.at(i);
|
pdDevice->address[i] = addressStr.at(i);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(device->haveRSSI())
|
if(device->haveRSSI())
|
||||||
{
|
{
|
||||||
pdDevice.hasRssi = true;
|
pdDevice->hasRssi = true;
|
||||||
pdDevice.rssi = device->getRSSI();
|
pdDevice->rssi = device->getRSSI();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string nameStr = "-";
|
std::string nameStr = "-";
|
||||||
@@ -167,13 +167,13 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
|||||||
|
|
||||||
i=0;
|
i=0;
|
||||||
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);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
pdDevice.timestamp = millis();
|
pdDevice->timestamp = millis();
|
||||||
|
|
||||||
_devices[addr] = pdDevice;
|
_devices[addr] = pdDevice;
|
||||||
}
|
}
|
||||||
@@ -191,8 +191,8 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
|||||||
|
|
||||||
if(ENDIAN_CHANGE_U16(oBeacon.getMinor()) == 40004)
|
if(ENDIAN_CHANGE_U16(oBeacon.getMinor()) == 40004)
|
||||||
{
|
{
|
||||||
pdDevice.timestamp = millis();
|
pdDevice->timestamp = millis();
|
||||||
strcpy(pdDevice.name, oBeacon.getProximityUUID().toString().c_str());
|
strcpy(pdDevice->name, oBeacon.getProximityUUID().toString().c_str());
|
||||||
_devices[addr] = pdDevice;
|
_devices[addr] = pdDevice;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,11 +200,11 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
it->second.timestamp = millis();
|
it->second->timestamp = millis();
|
||||||
if(device->haveRSSI())
|
if(device->haveRSSI())
|
||||||
{
|
{
|
||||||
it->second.hasRssi = true;
|
it->second->hasRssi = true;
|
||||||
it->second.rssi = device->getRSSI();
|
it->second->rssi = device->getRSSI();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,14 +25,14 @@ public:
|
|||||||
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
|
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void buildCsv(const PdDevice& device);
|
void buildCsv(const std::shared_ptr<PdDevice>& device);
|
||||||
|
|
||||||
Preferences* _preferences;
|
Preferences* _preferences;
|
||||||
BleScanner::Scanner* _bleScanner;
|
BleScanner::Scanner* _bleScanner;
|
||||||
Network* _network;
|
Network* _network;
|
||||||
char* _csv = {0};
|
char* _csv = {0};
|
||||||
size_t _bufferSize = 0;
|
size_t _bufferSize = 0;
|
||||||
std::map<long long, PdDevice> _devices;
|
std::map<long long, std::shared_ptr<PdDevice>> _devices;
|
||||||
int _timeout = 20000;
|
int _timeout = 20000;
|
||||||
int _csvIndex = 0;
|
int _csvIndex = 0;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user