Set initial distance from previous item with same id
This commit is contained in:
parent
270e3c475b
commit
9f4ce708b1
|
@ -38,16 +38,19 @@ static String getProximityUUIDString(BLEBeacon beacon)
|
|||
return returnedString;
|
||||
}
|
||||
|
||||
BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float initalDistance)
|
||||
BleFingerprint::~BleFingerprint()
|
||||
{
|
||||
Serial.printf("Del | MAC: %s, ID: %s\n", SMacf(address).c_str(), id.c_str());
|
||||
}
|
||||
|
||||
BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice)
|
||||
{
|
||||
firstSeen = time(nullptr);
|
||||
address = advertisedDevice->getAddress();
|
||||
|
||||
auto nativeAddress = address.getNative();
|
||||
String mac_address = Sprintf("%02x%02x%02x%02x%02x%02x", nativeAddress[5], nativeAddress[4], nativeAddress[3], nativeAddress[2], nativeAddress[1], nativeAddress[0]);
|
||||
String mac_address = SMacf(address);
|
||||
|
||||
Serial.print("New | MAC: ");
|
||||
Serial.print(mac_address);
|
||||
Serial.printf("New | MAC: %s", mac_address.c_str());
|
||||
|
||||
if (advertisedDevice->haveName())
|
||||
name = String(advertisedDevice->getName().c_str());
|
||||
|
@ -60,13 +63,13 @@ BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float init
|
|||
{
|
||||
id = "tile:" + mac_address;
|
||||
Serial.printf(", ID: %s", id.c_str());
|
||||
setCalRssi(advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0);
|
||||
calRssi = advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0;
|
||||
}
|
||||
else if (advertisedDevice->haveServiceUUID() && advertisedDevice->getServiceDataUUID().equals(BLEUUID(exposureUUID)) == true)
|
||||
{ // found covid exposure tracker
|
||||
id = "exp:" + String(strServiceData.length());
|
||||
Serial.printf(", ID: %s", id.c_str());
|
||||
setCalRssi(advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0);
|
||||
calRssi = advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0;
|
||||
|
||||
//char *sdHex = NimBLEUtils::buildHexData(nullptr, (uint8_t *)strServiceData.data(), strServiceData.length());
|
||||
//doc["tek"] = String(sdHex).substring(4, 20);
|
||||
|
@ -75,16 +78,15 @@ BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float init
|
|||
else if (advertisedDevice->haveServiceUUID() && advertisedDevice->getServiceDataUUID().equals(BLEUUID(beaconUUID)) == true)
|
||||
{ // found Eddystone UUID
|
||||
Serial.print(", Eddystone");
|
||||
// Update distance v ariable for Eddystone BLE devices
|
||||
if (cServiceData[0] == 0x10)
|
||||
{
|
||||
BLEEddystoneURL oBeacon = BLEEddystoneURL();
|
||||
oBeacon.setData(strServiceData);
|
||||
// Serial.printf("Eddystone Frame Type (Eddystone-URL) ");
|
||||
// Serial.printf(oBeacon.getDecodedURL().c_str());
|
||||
url = String(oBeacon.getDecodedURL().c_str());
|
||||
Serial.print(" URL: ");
|
||||
Serial.print(oBeacon.getDecodedURL().c_str());
|
||||
setCalRssi(oBeacon.getPower());
|
||||
Serial.print(url.c_str());
|
||||
calRssi = oBeacon.getPower();
|
||||
}
|
||||
else if (cServiceData[0] == 0x20)
|
||||
{
|
||||
|
@ -126,7 +128,7 @@ BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float init
|
|||
|
||||
id = "iBeacon:" + proximityUUID + "-" + String(major) + "-" + String(minor);
|
||||
Serial.printf(", ID: %s", id.c_str());
|
||||
setCalRssi(oBeacon.getSignalPower());
|
||||
calRssi = oBeacon.getSignalPower();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -137,7 +139,7 @@ BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float init
|
|||
id = fingerprint;
|
||||
Serial.printf(", ID: %s", id.c_str());
|
||||
|
||||
setCalRssi(advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0);
|
||||
calRssi = advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -151,13 +153,13 @@ BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float init
|
|||
Serial.printf(", ID: %s", id.c_str());
|
||||
}
|
||||
|
||||
setCalRssi(advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0);
|
||||
calRssi = advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0;
|
||||
}
|
||||
free(mdHex);
|
||||
}
|
||||
else
|
||||
{
|
||||
setCalRssi(advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0);
|
||||
calRssi = (advertisedDevice->haveTXPower() ? (-advertisedDevice->getTXPower()) - 41 : 0);
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
|
@ -172,9 +174,12 @@ void BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice)
|
|||
calRssi = defaultTxPower;
|
||||
|
||||
float ratio = (calRssi - rssi) / 35.0f;
|
||||
float distFl = pow(10, ratio);
|
||||
raw = distFl;
|
||||
raw = pow(10, ratio);
|
||||
setDistance(raw);
|
||||
}
|
||||
|
||||
void BleFingerprint::setDistance(float distFl)
|
||||
{
|
||||
Reading<float> inter1, inter2;
|
||||
|
||||
if (tsFilter.push(&distFl, &inter1))
|
||||
|
@ -186,7 +191,12 @@ void BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice)
|
|||
}
|
||||
}
|
||||
|
||||
bool BleFingerprint::shouldReport()
|
||||
float BleFingerprint::getDistance()
|
||||
{
|
||||
return output.value.position;
|
||||
}
|
||||
|
||||
bool BleFingerprint::report(JsonDocument *doc)
|
||||
{
|
||||
if (id == nullptr && name == nullptr)
|
||||
return false;
|
||||
|
@ -194,40 +204,36 @@ bool BleFingerprint::shouldReport()
|
|||
if (!hasValue)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
StaticJsonDocument<512> BleFingerprint::report()
|
||||
{
|
||||
StaticJsonDocument<512> doc;
|
||||
String mac = SMacf(address);
|
||||
if (output.value.position < 0.5)
|
||||
{
|
||||
if (!enroll)
|
||||
{
|
||||
Serial.printf("Enter | %-50s %lu %5.1f %5.1f %5.1f\n", id.c_str(), output.timestamp, output.value.position, output.value.speed * 1e6, output.value.acceleration * 1e12);
|
||||
Serial.printf("Enter | MAC: %s, ID: %-50s %lu %5.1f %5.1f %5.1f\n", mac.c_str(), id.c_str(), output.timestamp, output.value.position, output.value.speed * 1e6, output.value.acceleration * 1e12);
|
||||
enroll = true;
|
||||
}
|
||||
}
|
||||
else if (enroll && output.value.position > 1.5)
|
||||
{
|
||||
Serial.printf("Left | %-50s %lu %5.1f %5.1f %5.1f\n", id.c_str(), output.timestamp, output.value.position, output.value.speed * 1e6, output.value.acceleration * 1e12);
|
||||
Serial.printf("Left | MAC: %s, ID: %-50s %lu %5.1f %5.1f %5.1f\n", mac.c_str(), id.c_str(), output.timestamp, output.value.position, output.value.speed * 1e6, output.value.acceleration * 1e12);
|
||||
enroll = false;
|
||||
}
|
||||
|
||||
if (id != nullptr)
|
||||
doc[F("id")] = id;
|
||||
(*doc)[F("id")] = id;
|
||||
if (name != nullptr)
|
||||
doc[F("name")] = name;
|
||||
(*doc)[F("name")] = name;
|
||||
|
||||
doc[F("rssi@1m")] = calRssi;
|
||||
doc[F("rssi")] = rssi;
|
||||
(*doc)[F("rssi@1m")] = calRssi;
|
||||
(*doc)[F("rssi")] = rssi;
|
||||
|
||||
doc[F("mac")] = SMacf(address);
|
||||
doc[F("raw")] = round(raw * 100.0f) / 100.0f;
|
||||
doc[F("distance")] = round(output.value.position * 100.0f) / 100.0f;
|
||||
doc[F("speed")] = round(output.value.speed * 1e7f) / 10.0f;
|
||||
(*doc)[F("mac")] = mac;
|
||||
(*doc)[F("raw")] = round(raw * 100.0f) / 100.0f;
|
||||
(*doc)[F("distance")] = round(output.value.position * 100.0f) / 100.0f;
|
||||
(*doc)[F("speed")] = round(output.value.speed * 1e7f) / 10.0f;
|
||||
|
||||
doc[F("first")] = SDateTimef(firstSeen);
|
||||
doc[F("last")] = SDateTimef(lastSeen);
|
||||
return doc;
|
||||
(*doc)[F("first")] = SDateTimef(firstSeen);
|
||||
(*doc)[F("last")] = SDateTimef(lastSeen);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -13,15 +13,19 @@ class BleFingerprint
|
|||
{
|
||||
|
||||
public:
|
||||
BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float initalDistance);
|
||||
BleFingerprint(BLEAdvertisedDevice *advertisedDevice);
|
||||
~BleFingerprint();
|
||||
|
||||
void seen(BLEAdvertisedDevice *advertisedDevice);
|
||||
bool shouldReport();
|
||||
StaticJsonDocument<512> report();
|
||||
bool report(JsonDocument *doc);
|
||||
|
||||
void setCalRssi(int rssi) { calRssi = rssi; }
|
||||
String getId() { return id; }
|
||||
|
||||
float getDistance();
|
||||
void setDistance(float distFl);
|
||||
|
||||
NimBLEAddress getAddress() { return address; }
|
||||
void setAddress(NimBLEAddress newAddr) { address = newAddr; }
|
||||
|
||||
private:
|
||||
bool hasValue = false, enroll = false;
|
||||
|
|
22
src/main.cpp
22
src/main.cpp
|
@ -79,21 +79,26 @@ BleFingerprint *getFingerprint(BLEAdvertisedDevice *advertisedDevice)
|
|||
{
|
||||
auto mac = advertisedDevice->getAddress();
|
||||
|
||||
auto is_even = [mac](BleFingerprint *f) { return f->getAddress() == mac; };
|
||||
|
||||
auto it = std::find_if(fingerprints.begin(), fingerprints.end(), is_even);
|
||||
auto it = std::find_if(fingerprints.begin(), fingerprints.end(), [mac](BleFingerprint *f) { return f->getAddress() == mac; });
|
||||
if (it != fingerprints.end())
|
||||
{
|
||||
return *it;
|
||||
}
|
||||
|
||||
if (fingerprints.size() > MAX_MAC_ADDRESSES)
|
||||
if (fingerprints.size() >= MAX_MAC_ADDRESSES)
|
||||
{
|
||||
delete fingerprints.back();
|
||||
fingerprints.pop_back();
|
||||
}
|
||||
|
||||
auto created = new BleFingerprint(advertisedDevice, MAX_DISTANCE);
|
||||
auto created = new BleFingerprint(advertisedDevice);
|
||||
auto it2 = std::find_if(fingerprints.begin(), fingerprints.end(), [created](BleFingerprint *f) { return f->getId() == created->getId(); });
|
||||
if (it2 != fingerprints.end())
|
||||
{
|
||||
auto found = *it2;
|
||||
created->setDistance(found->getDistance());
|
||||
}
|
||||
|
||||
fingerprints.push_front(created);
|
||||
return created;
|
||||
}
|
||||
|
@ -257,7 +262,6 @@ void reconnect(TimerHandle_t xTimer)
|
|||
{
|
||||
retryAttempts++;
|
||||
}
|
||||
|
||||
if (!WiFi.isConnected())
|
||||
if (!WiFiSettings.connect(true, 60))
|
||||
ESP.restart();
|
||||
|
@ -292,11 +296,11 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
|||
bool reportDevice(BLEAdvertisedDevice advertisedDevice)
|
||||
{
|
||||
BleFingerprint *f = getFingerprint(&advertisedDevice);
|
||||
StaticJsonDocument<512> doc;
|
||||
|
||||
if (!f->shouldReport())
|
||||
if (!f->report(&doc))
|
||||
return false;
|
||||
|
||||
auto doc = f->report();
|
||||
char JSONmessageBuffer[512];
|
||||
serializeJson(doc, JSONmessageBuffer);
|
||||
String id = doc["id"];
|
||||
|
@ -439,7 +443,7 @@ void firmwareUpdate(void)
|
|||
{
|
||||
#ifdef VERSION
|
||||
static long lastFirmwareCheck;
|
||||
if (millis() - lastFirmwareCheck < CHECK_FOR_UPDATES_MILI || WiFi.status() != WL_CONNECTED)
|
||||
if (millis() - lastFirmwareCheck < CHECK_FOR_UPDATES_MILI)
|
||||
return;
|
||||
|
||||
lastFirmwareCheck = millis();
|
||||
|
|
Loading…
Reference in New Issue