[BT] Device Tracker sync across gateways (#2094)

Device Tracker sync across gateways - OpenMQTTGateway & Theengs Gateway
This commit is contained in:
DigiH authored and GitHub committed 2024-10-28 11:25:38 -05:00
1 parent 952a65dd0f
commit 67e50642dd
4 files changed
+39 -3

No files matched your search

+29
View File
@@ -1269,6 +1269,27 @@ void PublishDeviceData(JsonObject& BLEdata) {
Log.notice(F("Not a sensor device filtered" CR));
return;
}
# if BLEDecoder
if (enableMultiGTWSync && BLEdata.containsKey("model_id") && BLEdata.containsKey("id")) {
// Publish tracker sync message
bool isTracker = false;
std::string tag = decoder.getTheengAttribute(BLEdata["model_id"].as<const char*>(), "tag");
if (tag.length() >= 4) {
isTracker = checkIfIsTracker(tag[3]);
}
if (isTracker) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject TrackerSyncdata = BLEdataBuffer.to<JsonObject>();
TrackerSyncdata["gatewayid"] = gateway_name;
TrackerSyncdata["trackerid"] = BLEdata["id"].as<const char*>();
String topic = String(mqtt_topic) + String(subjectTrackerSync);
TrackerSyncdata["topic"] = topic.c_str();
enqueueJsonObject(TrackerSyncdata);
}
}
# endif
} else {
Log.notice(F("Low rssi, device filtered" CR));
return;
@@ -1553,6 +1574,14 @@ void XtoBT(const char* topicOri, JsonObject& BTdata) { // json object decoding
Log.error(F("BLE busy - command not sent" CR));
gatewayState = GatewayState::ERROR;
}
} else if (strstr(topicOri, subjectTrackerSync) != NULL) {
if (BTdata.containsKey("gatewayid") && BTdata.containsKey("trackerid") && BTdata["gatewayid"] != gateway_name) {
BLEdevice* device = getDeviceByMac(BTdata["trackerid"].as<const char*>());
if (device != &NO_BT_DEVICE_FOUND && device->lastUpdate != 0) {
device->lastUpdate = 0;
Log.notice(F("Tracker %s disassociated by gateway %s" CR), BTdata["trackerid"].as<const char*>(), BTdata["gatewayid"].as<const char*>());
}
}
}
}
#endif
+5
View File
@@ -40,6 +40,7 @@ extern String stateBTMeasures(bool);
#define subjectBTtoMQTT "/BTtoMQTT"
#define subjectMQTTtoBTset "/commands/MQTTtoBT/config"
#define subjectMQTTtoBT "/commands/MQTTtoBT"
#define subjectTrackerSync "internal/trackersync"
// Uncomment to send undecoded device data to another gateway device for decoding
// #define MQTTDecodeTopic "undecoded"
#ifndef UseExtDecoder
@@ -143,6 +144,10 @@ unsigned long scanCount = 0;
# define useBeaconUuidForTopic false // define true to use iBeacon UUID as topic, instead of sender (random) MAC address
#endif
#ifndef enableMultiGTWSync
# define enableMultiGTWSync true // //define true to use tracker and closest control devices sync across OpenMQTTGateway and Theengs Gateway gateways
#endif
/*--------------HOME ASSISTANT ROOM PRESENCE--------------*/
#define subjectHomePresence "presence/" // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name)
+4
View File
@@ -1134,6 +1134,10 @@ void setupMQTT() {
mqtt->subscribe(subjectMultiGTWIR, receivingDATA, mqtt_max_payload_size);
# endif
# if enableMultiGTWSync
mqtt->subscribe(String(mqtt_topic) + subjectTrackerSync, receivingDATA, mqtt_max_payload_size);
# endif
mqtt->begin();
}
#endif