From 6519e1ab7a645b28ae8835556098dbe960be3cee Mon Sep 17 00:00:00 2001 From: DTTerastar Date: Sun, 10 Oct 2021 22:49:56 -0400 Subject: [PATCH] Implement a id type hierachy --- CHANGELOG.md | 8 +++- lib/BleFingerprint/BleFingerprint.cpp | 62 ++++++++++++++------------- lib/BleFingerprint/BleFingerprint.h | 36 +++++++++++++--- lib/BleFingerprint/GUI.cpp | 2 +- src/main.cpp | 2 +- 5 files changed, 73 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd232f2..ba4e2e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0.1] + +- Query bugs squashed! Needs a bit more testing, but it's MUCH better than 2.0 +- Apple model fingerprint updated to apple:model (with the , turned into a -) +- BH1750 sensor support added [lux sensor](https://amzn.to/3AoKlQ4) + ## [2.0.0] - Add `Query` switch to mqtt and webapp setup (defaults to off so as to not break existing fingerprints) - Add `Active Scan` switch to mqtt and webapp setup -- Add device model to apple fingerprints +- Add device model to apple fingerprints - Add support [room assistant app](https://apps.apple.com/us/app/room-assistant/id1538642237?itsct=apps_box_link&itscg=30200) @DTTerastar (#69) - Stats reworked diff --git a/lib/BleFingerprint/BleFingerprint.cpp b/lib/BleFingerprint/BleFingerprint.cpp index 163b69e..bdd11db 100644 --- a/lib/BleFingerprint/BleFingerprint.cpp +++ b/lib/BleFingerprint/BleFingerprint.cpp @@ -55,29 +55,30 @@ void BleFingerprint::fingerprint(BLEAdvertisedDevice *advertisedDevice) if (advertisedDevice->isAdvertisingService(tileUUID)) { calRssi = _parent->getRefRssi() + TILE_TX; - if (!pidOverriden) pid = "tile:" + getMac(); + setId("tile:" + getMac(), ID_TYPE_TILE); } else if (advertisedDevice->isAdvertisingService(exposureUUID)) { // found covid exposure tracker std::string strServiceData = advertisedDevice->getServiceData(exposureUUID); calRssi = _parent->getRefRssi() + EXPOSURE_TX; - if (!pidOverriden) pid = "exp:" + String(strServiceData.length()); + setId("exp:" + String(strServiceData.length()), ID_TYPE_EXPOSURE); name = hexStr(strServiceData).c_str(); } else if (advertisedDevice->isAdvertisingService(sonosUUID)) { asRssi = advertisedDevice->haveTXPower() ? _parent->getRefRssi() + advertisedDevice->getTXPower() : NO_RSSI; - if (!pidOverriden) pid = "sonos:" + getMac(); + setId("sonos:" + getMac(), ID_TYPE_SONOS); } else if (advertisedDevice->isAdvertisingService(itagUUID)) { asRssi = _parent->getRefRssi() + (advertisedDevice->haveTXPower() ? advertisedDevice->getTXPower() : ITAG_TX); - if (!pidOverriden) pid = "itag:" + getMac(); + setId("itag:" + getMac(), ID_TYPE_ITAG); } else if (advertisedDevice->isAdvertisingService(roomAssistantService)) { asRssi = advertisedDevice->haveTXPower() ? advertisedDevice->getTXPower() - 65 : NO_RSSI; shouldQuery = true; + rmAsst = true; } else if (advertisedDevice->isAdvertisingService(eddystoneUUID)) { @@ -86,7 +87,6 @@ void BleFingerprint::fingerprint(BLEAdvertisedDevice *advertisedDevice) { BLEEddystoneURL oBeacon = BLEEddystoneURL(); oBeacon.setData(strServiceData); - url = String(oBeacon.getDecodedURL().c_str()); calRssi = EDDYSTONE_ADD_1M + oBeacon.getPower(); } else if (strServiceData[0] == EDDYSTONE_TLM_FRAME_TYPE) @@ -111,7 +111,7 @@ void BleFingerprint::fingerprint(BLEAdvertisedDevice *advertisedDevice) } if (advertisedDevice->haveTXPower()) fingerprint = fingerprint + String(-advertisedDevice->getTXPower()); - sid = fingerprint; + setId(fingerprint, ID_TYPE_SID); } } @@ -131,28 +131,30 @@ void BleFingerprint::fingerprint(BLEAdvertisedDevice *advertisedDevice) { BLEBeacon oBeacon = BLEBeacon(); oBeacon.setData(strManufacturerData); - if (!pidOverriden) pid = Sprintf("iBeacon:%s-%d-%d", std::string(oBeacon.getProximityUUID()).c_str(), ENDIAN_CHANGE_U16(oBeacon.getMajor()), ENDIAN_CHANGE_U16(oBeacon.getMinor())); + setId(Sprintf("iBeacon:%s-%d-%d", std::string(oBeacon.getProximityUUID()).c_str(), ENDIAN_CHANGE_U16(oBeacon.getMajor()), ENDIAN_CHANGE_U16(oBeacon.getMinor())), ID_TYPE_IBEACON); calRssi = oBeacon.getSignalPower(); } else if (strManufacturerData.length() >= 4 && strManufacturerData[2] == 0x10) { shouldQuery = true; ignore = false; - if (!pidOverriden) { + String pid; if (advertisedDevice->haveTXPower()) pid = Sprintf("apple:%02x%02x:%d%d", strManufacturerData[2], strManufacturerData[3], strManufacturerData.length(), -advertisedDevice->getTXPower()); else pid = Sprintf("apple:%02x%02x:%d", strManufacturerData[2], strManufacturerData[3], strManufacturerData.length()); + setId(pid, ID_TYPE_APPLE_NEARBY); } + disc = hexStr(strManufacturerData.substr(4)).c_str(); mdRssi = _parent->getRefRssi() + APPLE_TX; } else { if (advertisedDevice->haveTXPower()) - sid = Sprintf("apple:%02x%02x:%d%d", strManufacturerData[2], strManufacturerData[3], strManufacturerData.length(), -advertisedDevice->getTXPower()); + setId(Sprintf("apple:%02x%02x:%d%d", strManufacturerData[2], strManufacturerData[3], strManufacturerData.length(), -advertisedDevice->getTXPower()), ID_TYPE_MISC_APPLE + ID_TYPE_TX_POW); else - sid = Sprintf("apple:%02x%02x:%d", strManufacturerData[2], strManufacturerData[3], strManufacturerData.length()); + setId(Sprintf("apple:%02x%02x:%d", strManufacturerData[2], strManufacturerData[3], strManufacturerData.length()), ID_TYPE_MISC_APPLE); mdRssi = _parent->getRefRssi() + APPLE_TX; ignore = true; } @@ -160,18 +162,18 @@ void BleFingerprint::fingerprint(BLEAdvertisedDevice *advertisedDevice) else if (manuf == "05a7") //Sonos { mdRssi = advertisedDevice->haveTXPower() ? _parent->getRefRssi() + advertisedDevice->getTXPower() : NO_RSSI; - if (!pidOverriden) pid = "sonos:" + getMac(); + setId("sonos:" + getMac(), ID_TYPE_SONOS); } else if (manuf == "0157") //Mi-fit { mdRssi = advertisedDevice->haveTXPower() ? _parent->getRefRssi() + advertisedDevice->getTXPower() : NO_RSSI; - pid = "mifit:" + getMac(); + setId("mifit:" + getMac(), ID_TYPE_MIFIT); } else if (manuf == "0006" && strManufacturerData.length() == 29) //microsoft { mdRssi = advertisedDevice->haveTXPower() ? _parent->getRefRssi() + advertisedDevice->getTXPower() : NO_RSSI; - pid = "md:microsoft:29"; - name = Sprintf("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + setId("md:microsoft:29", ID_TYPE_MSFT); + disc = Sprintf("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", strManufacturerData[6], strManufacturerData[7], strManufacturerData[8], strManufacturerData[9], strManufacturerData[10], strManufacturerData[11], strManufacturerData[12], strManufacturerData[13], strManufacturerData[14], strManufacturerData[15], strManufacturerData[16], strManufacturerData[17], strManufacturerData[18], strManufacturerData[19], strManufacturerData[20], strManufacturerData[21], strManufacturerData[22], strManufacturerData[23], @@ -181,7 +183,7 @@ void BleFingerprint::fingerprint(BLEAdvertisedDevice *advertisedDevice) else if (manuf == "0075") //samsung { mdRssi = advertisedDevice->haveTXPower() ? _parent->getRefRssi() + advertisedDevice->getTXPower() : NO_RSSI; - if (!pidOverriden) pid = "samsung:" + getMac(); + setId("samsung:" + getMac(), ID_TYPE_MISC); } else { @@ -189,7 +191,7 @@ void BleFingerprint::fingerprint(BLEAdvertisedDevice *advertisedDevice) String fingerprint = Sprintf("md:%s:%d", manuf.c_str(), strManufacturerData.length()); if (advertisedDevice->haveTXPower()) fingerprint = fingerprint + String(-advertisedDevice->getTXPower()); - sid = fingerprint; + setId(fingerprint, ID_TYPE_MD); } } } @@ -256,7 +258,7 @@ void BleFingerprint::setInitial(int initalRssi, float initalDistance) bool BleFingerprint::report(JsonDocument *doc, float maxDistance) { - if (pid.isEmpty() && sid.isEmpty() && !macPublic) + if (ignore || (idType == 0 && !macPublic)) return false; if (reported || !hasValue) @@ -277,6 +279,7 @@ bool BleFingerprint::report(JsonDocument *doc, float maxDistance) (*doc)[F("id")] = getId(); if (!name.isEmpty()) (*doc)[F("name")] = name; + if (!disc.isEmpty()) (*doc)[F("disc")] = disc; (*doc)[F("rssi@1m")] = get1mRssi(); (*doc)[F("rssi")] = rssi; @@ -304,33 +307,34 @@ bool BleFingerprint::query() pClient->setConnectTimeout(5); if (pClient->connect(address)) { - auto sName = pClient->getValue(genericAccessService, nameChar); - if (!sName.empty()) + if (name.isEmpty()) { - Serial.printf("%d Name | MAC: %s, ID: %-50s%s\n", xPortGetCoreID(), getMac().c_str(), getId().c_str(), sName.c_str()); - if (name.length() == 0) name = sName.c_str(); + auto sName = pClient->getValue(genericAccessService, nameChar); + if (!sName.empty()) + { + Serial.printf("%d Name | MAC: %s, ID: %-60s %s\n", xPortGetCoreID(), getMac().c_str(), getId().c_str(), sName.c_str()); + if (name.length() == 0) name = sName.c_str(); + } } auto sRmAst = pClient->getValue(roomAssistantService, rootAssistantCharacteristic); if (!sRmAst.empty()) { - Serial.printf("%d RmAst | MAC: %s, ID: %-50s%s\n", xPortGetCoreID(), getMac().c_str(), getId().c_str(), sRmAst.c_str()); - if (!pidOverriden) pid = String("roomAssistant:") + kebabify(sRmAst).c_str(); - pidOverriden = true; + Serial.printf("%d RmAst | MAC: %s, ID: %-60s %s\n", xPortGetCoreID(), getMac().c_str(), getId().c_str(), sRmAst.c_str()); + setId(String("roomAssistant:") + kebabify(sRmAst).c_str(), ID_TYPE_RM_ASST); } else { auto sMdl = pClient->getValue(deviceInformationService, modelChar); if (!sMdl.empty()) { - Serial.printf("%d Model | MAC: %s, ID: %-50s%s\n", xPortGetCoreID(), getMac().c_str(), getId().c_str(), sMdl.c_str()); - if (!pidOverriden) pid = pid + String("-") + kebabify(sMdl).c_str(); - pidOverriden = true; + Serial.printf("%d Model | MAC: %s, ID: %-60s %s\n", xPortGetCoreID(), getMac().c_str(), getId().c_str(), sMdl.c_str()); + setId(String("apple:") + kebabify(sMdl).c_str(), ID_TYPE_APPLE_MODEL); + if (name.isEmpty()) name = sMdl.c_str(); } else { - if (name.length() > 0 && !pidOverriden) pid = pid + String("-") + kebabify(name); - pidOverriden = true; + if (name.length() > 0) setId(String("name:") + kebabify(name), ID_TYPE_NAME); } } diff --git a/lib/BleFingerprint/BleFingerprint.h b/lib/BleFingerprint/BleFingerprint.h index adecef9..df79105 100644 --- a/lib/BleFingerprint/BleFingerprint.h +++ b/lib/BleFingerprint/BleFingerprint.h @@ -12,6 +12,23 @@ #define NO_RSSI -32768 +#define ID_TYPE_TX_POW short(1) +#define ID_TYPE_SID short(7) +#define ID_TYPE_MD short(8) +#define ID_TYPE_MISC_APPLE short(9) +#define ID_TYPE_NAME short(10) +#define ID_TYPE_MISC short(11) +#define ID_TYPE_MSFT short(12) +#define ID_TYPE_SONOS short(13) +#define ID_TYPE_MIFIT short(14) +#define ID_TYPE_EXPOSURE short(15) +#define ID_TYPE_ITAG short(16) +#define ID_TYPE_TILE short(17) +#define ID_TYPE_APPLE_NEARBY short(35) +#define ID_TYPE_APPLE_MODEL short(40) +#define ID_TYPE_IBEACON short(99) +#define ID_TYPE_RM_ASST short(100) + class BleFingerprintCollection; class BleFingerprint @@ -26,13 +43,22 @@ public: String getId() { - if (!pid.isEmpty()) return pid; + if (!id.isEmpty() && idType > 10) return id; if (macPublic) return getMac(); - if (!sid.isEmpty()) return sid; + if (!id.isEmpty()) return id; return getMac(); } + + void setId(String newId, short int newIdType) + { + if (newIdType < idType) return; + id = newId; + idType = newIdType; + } + String getMac(); int get1mRssi(); + String getDiscriminator() { return disc; } float getDistance() { return output.value.position; } int getRssi() { return rssi; } @@ -54,10 +80,10 @@ private: void fingerprint(BLEAdvertisedDevice *advertisedDevice); BleFingerprintCollection *_parent; - bool hasValue = false, close = false, reported = false, macPublic = false, ignore = false, shouldQuery = false, didQuery = false, pidOverriden = false; + bool hasValue = false, close = false, reported = false, macPublic = false, ignore = false, shouldQuery = false, didQuery = false, rmAsst = false; NimBLEAddress address; - String pid, sid, name, url; - int rssi = -100, calRssi = NO_RSSI, mdRssi = NO_RSSI, asRssi = NO_RSSI; + String id, name, disc; + short int idType = 0, rssi = -100, calRssi = NO_RSSI, mdRssi = NO_RSSI, asRssi = NO_RSSI; int newest = -100, recent = -100, oldest = -100; int qryAttempts = 0, seenCount = 1; diff --git a/lib/BleFingerprint/GUI.cpp b/lib/BleFingerprint/GUI.cpp index 72d2368..691484b 100644 --- a/lib/BleFingerprint/GUI.cpp +++ b/lib/BleFingerprint/GUI.cpp @@ -59,7 +59,7 @@ void GUI::connected(bool wifi = false, bool mqtt = false) void GUI::added(BleFingerprint *f) { if (f->getIgnore()) return; - Serial.printf("%d New | MAC: %s, ID: %s\n", xPortGetCoreID(), f->getMac().c_str(), f->getId().c_str()); + Serial.printf("%d New | MAC: %s, ID: %-60s %s\n", xPortGetCoreID(), f->getMac().c_str(), f->getId().c_str(), f->getDiscriminator().c_str()); } void GUI::removed(BleFingerprint *f, long age) diff --git a/src/main.cpp b/src/main.cpp index f285619..f8f5c99 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -449,7 +449,7 @@ void setup() // Init BH1750 (witch default l2c adres) int rc; // Returncode long m; // milli for calibration - bool state; + bool state = false; // if (! BH1750.begin(BH1750_TO_GROUND)) if (BH1750_I2c == "0x23")