ESPresense/lib/BleFingerprint/BleFingerprint.h

89 lines
2.8 KiB
C
Raw Normal View History

#ifndef _BLEFINGERPRINT_
#define _BLEFINGERPRINT_
#include "GUI.h"
2021-04-05 04:12:46 +02:00
#include <ArduinoJson.h>
#include <NimBLEAdvertisedDevice.h>
#include <NimBLEBeacon.h>
2021-04-05 04:12:46 +02:00
#include <NimBLEDevice.h>
#include <NimBLEEddystoneTLM.h>
#include <NimBLEEddystoneURL.h>
2021-03-22 23:56:29 +01:00
#include <SoftFilters.h>
#define NO_RSSI -32768
#ifdef TX_DEFAULT
static const int defaultTxPower = TX_DEFAULT;
#else
static const int defaultTxPower = -59;
#endif
2021-09-15 18:11:32 +02:00
#define Sprintf(f, ...) ( \
{ \
char *s; \
asprintf(&s, f, __VA_ARGS__); \
String r = s; \
free(s); \
r; \
})
#define SMacf(f) ( \
{ \
auto nativeAddress = f.getNative(); \
Sprintf("%02x%02x%02x%02x%02x%02x", nativeAddress[5], nativeAddress[4], nativeAddress[3], nativeAddress[2], nativeAddress[1], nativeAddress[0]); \
})
class BleFingerprint
{
public:
2021-04-05 04:12:46 +02:00
BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float fcmin, float beta, float dcutoff);
void seen(BLEAdvertisedDevice *advertisedDevice);
2021-09-25 05:05:12 +02:00
bool report(JsonDocument *doc, float maxDistance);
2021-09-15 18:11:32 +02:00
String getId()
{
if (!pid.isEmpty()) return pid;
if (macPublic) return getMac();
if (!sid.isEmpty()) return sid;
return getMac();
}
String getMac() { return SMacf(address); }
int get1mRssi()
{
if (calRssi != NO_RSSI) return calRssi;
if (calcRssi != NO_RSSI) return calcRssi;
return defaultTxPower;
}
2021-04-07 02:13:13 +02:00
float getDistance() { return output.value.position; }
int getRSSI() { return rssi; }
void setInitial(int rssi, float distance);
NimBLEAddress getAddress() { return address; }
2021-04-02 21:16:30 +02:00
long getLastSeen() { return lastSeenMicros; };
private:
2021-09-15 18:11:32 +02:00
void fingerprint(BLEAdvertisedDevice *advertisedDevice);
bool hasValue = false, close = false, reported = false, macPublic = false;
NimBLEAddress address;
2021-09-15 18:11:32 +02:00
String pid, sid, name, url;
int rssi = -100, calRssi = NO_RSSI, calcRssi = NO_RSSI;
2021-04-07 02:13:13 +02:00
int newest = -100;
int recent = -100;
int oldest = -100;
float raw = 0, lastReported = 0, temp = 0;
2021-04-02 13:56:27 +02:00
long firstSeenMicros, lastSeenMicros = 0, lastReportedMicros = 0;
uint16_t volts = 0;
2021-03-29 20:50:23 +02:00
2021-03-22 23:56:29 +01:00
Reading<Differential<float>> output;
TimestampFilter<float> tsFilter;
2021-04-05 04:12:46 +02:00
one_euro_filter<double, unsigned long> oneEuro;
2021-03-22 23:56:29 +01:00
DifferentialFilter<float> diffFilter;
2021-04-07 02:13:13 +02:00
bool filter();
};
#endif