Skip to content

Commit

Permalink
[BT] Tracker discovery and timeout by "track" tag
Browse files Browse the repository at this point in the history
  • Loading branch information
DigiH committed Jan 28, 2024
1 parent a7f43ed commit 30936fc
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -402,17 +402,18 @@ void updateDevicesStatus() {
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
BLEdevice* p = *it;
unsigned long now = millis();
// Check for tracker status
bool isTracker = false;
std::string tag = decoder.getTheengAttribute(p->sensorModel_id, "tag");
if (tag.length() >= 3) {
uint8_t data = getBinaryData(tag[3]);
if (((data >> 3) & 0x01) == 1) {
isTracker = true;
Log.notice(F("Is Device Tracker for offline evaluation" CR));
}
}
// Device tracker devices
if (BTConfig.presenceEnable && (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::NUT ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::NUTALE ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::MIBAND ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TAGIT ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TILE ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TILEN ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::ITAG ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::BM2 ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::RUUVITAG_RAWV1 ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::RUUVITAG_RAWV2)) { // We apply the offline status only for tracking device, can be extended further to all the devices
if (isTracker) { // We apply the offline status only for tracking device, can be extended further to all the devices
if ((p->lastUpdate != 0) && (p->lastUpdate < (now - BTConfig.presenceAwayTimer) && (now > BTConfig.presenceAwayTimer)) &&
(BTConfig.ignoreWBlist || ((!oneWhite || isWhite(p)) && !isBlack(p)))) { // Only if WBlist is disabled OR ((no white MAC OR this MAC is white) AND not a black listed MAC)) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
Expand Down Expand Up @@ -587,6 +588,16 @@ std::string convertServiceData(std::string deviceServiceData) {
return spr;
}

uint8_t getBinaryData(char ch) {
uint8_t data = 0;
if (ch >= '0' && ch <= '9')
data = ch - '0';
else if (ch >= 'a' && ch <= 'f')
data = 10 + (ch - 'a');

return data;
}

void procBLETask(void* pvParameters) {
BLEAdvertisedDevice* advertisedDevice = nullptr;

Expand Down Expand Up @@ -971,20 +982,24 @@ void launchBTDiscovery(bool overrideDiscovery) {
std::string brand = decoder.getTheengAttribute(p->sensorModel_id, "brand");
std::string model = decoder.getTheengAttribute(p->sensorModel_id, "model");
std::string model_id = decoder.getTheengAttribute(p->sensorModel_id, "model_id");

// Check for tracker status
bool isTracker = false;
std::string tag = decoder.getTheengAttribute(p->sensorModel_id, "tag");
if (tag.length() >= 3) {
uint8_t data = getBinaryData(tag[3]);
if (((data >> 3) & 0x01) == 1) {
isTracker = true;
Log.notice(F("Is Device Tracker for discovery" CR));
}
}

String discovery_topic = String(subjectBTtoMQTT) + "/" + macWOdots;
if (!BTConfig.extDecoderEnable && // Do not decode if an external decoder is configured
p->sensorModel_id > TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL &&
p->sensorModel_id < TheengsDecoder::BLE_ID_NUM::BLE_ID_MAX &&
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC && p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::BM2) { // Exception on HHCCJCY01HHCC and BM2 as these ones are discoverable and connectable
if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::NUT ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::NUTALE ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::MIBAND ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TAGIT ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TILE ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TILEN ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::ITAG ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::RUUVITAG_RAWV1 ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::RUUVITAG_RAWV2) {
if (isTracker) {
String tracker_name = String(model_id.c_str()) + "-tracker";
String tracker_id = macWOdots + "-tracker";
createDiscovery("device_tracker",
Expand Down

0 comments on commit 30936fc

Please sign in to comment.