Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DISC] Add the capability to use the device BLE name #1936

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void PublishDeviceData(JsonObject& BLEdata);

atomic_int forceBTScan;

void createOrUpdateDevice(const char* mac, uint8_t flags, int model, int mac_type = 0);
void createOrUpdateDevice(const char* mac, uint8_t flags, int model, int mac_type = 0, const char* name = "");

BLEdevice* getDeviceByMac(const char* mac); // Declared here to avoid pre-compilation issue (misplaced auto declaration by pio)
BLEdevice* getDeviceByMac(const char* mac) {
Expand Down Expand Up @@ -354,12 +354,11 @@ bool updateWorB(JsonObject& BTdata, bool isWhite) {
return true;
}

void createOrUpdateDevice(const char* mac, uint8_t flags, int model, int mac_type) {
void createOrUpdateDevice(const char* mac, uint8_t flags, int model, int mac_type, const char* name) {
if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(30000)) == pdFALSE) {
Log.error(F("Semaphore NOT taken" CR));
return;
}

BLEdevice* device = getDeviceByMac(mac);
if (device == &NO_BT_DEVICE_FOUND) {
Log.trace(F("add %s" CR), mac);
Expand All @@ -371,6 +370,14 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, int model, int mac_typ
device->isBlkL = flags & device_flags_isBlackL;
device->connect = flags & device_flags_connect;
device->macType = mac_type;
// Check name length
if (strlen(name) > 20) {
Log.warning(F("Name too long, truncating" CR));
strncpy(device->name, name, 20);
device->name[20] = '\0';
} else {
strcpy(device->name, name);
}
device->sensorModel_id = model;
device->lastUpdate = millis();
devices.push_back(device);
Expand Down Expand Up @@ -992,6 +999,11 @@ void launchBTDiscovery(bool overrideDiscovery) {
Log.trace(F("properties: %s" CR), properties.c_str());
std::string brand = decoder.getTheengAttribute(p->sensorModel_id, "brand");
std::string model = decoder.getTheengAttribute(p->sensorModel_id, "model");
# ifdef ForceDeviceName
if (p->name[0] != '\0') {
model = p->name;
}
# endif
std::string model_id = decoder.getTheengAttribute(p->sensorModel_id, "model_id");

// Check for tracker status
Expand Down Expand Up @@ -1225,14 +1237,15 @@ void process_bledata(JsonObject& BLEdata) {
BLEdata["type"] = "RMAC";
Log.trace(F("Potential RMAC (prmac) converted to RMAC" CR));
}
const char* deviceName = BLEdata["name"] | "";

if ((BLEdata["type"].as<string>()).compare("RMAC") != 0 && model_id != TheengsDecoder::BLE_ID_NUM::IBEACON) { // Do not store in memory the random mac devices and iBeacons
if (model_id >= 0) { // Broadcaster devices
Log.trace(F("Decoder found device: %s" CR), BLEdata["model_id"].as<const char*>());
if (model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC || model_id == TheengsDecoder::BLE_ID_NUM::BM2) { // Device that broadcast and can be connected
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type);
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type, deviceName);
} else {
createOrUpdateDevice(mac, device_flags_init, model_id, mac_type);
createOrUpdateDevice(mac, device_flags_init, model_id, mac_type, deviceName);
if (BTConfig.adaptiveScan == true && (BTConfig.BLEinterval != MinTimeBtwScan || BTConfig.intervalActiveScan != MinTimeBtwScan)) {
if (BLEdata.containsKey("acts") && BLEdata.containsKey("cont")) {
if (BLEdata["acts"] && BLEdata["cont"]) {
Expand Down Expand Up @@ -1268,14 +1281,14 @@ void process_bledata(JsonObject& BLEdata) {

if (model_id > 0) {
Log.trace(F("Connectable device found: %s" CR), name.c_str());
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type);
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type, deviceName);
}
} else if (BTConfig.extDecoderEnable && model_id < 0 && BLEdata.containsKey("servicedata")) {
const char* service_data = (const char*)(BLEdata["servicedata"] | "");
if (strstr(service_data, "209800") != NULL) {
model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC;
Log.trace(F("Connectable device found: HHCCJCY01HHCC" CR));
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type);
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type, deviceName);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ void createDiscovery(const char* sensor_type,
device["mdl"] = device_model;
}

// generate unique device name by adding the second half of the device_id only if device_name and device_id are different
// generate unique device name by adding the second half of the device_id only if device_name and device_id are different and we don't want to use the BLE name
if (device_name[0]) {
if (strcmp(device_id, device_name) != 0 && device_id[0]) {
if (strcmp(device_id, device_name) != 0 && device_id[0] && !ForceDeviceName) {
device["name"] = device_name + String("-") + String(device_id + 6);
} else {
device["name"] = device_name;
Expand Down
13 changes: 4 additions & 9 deletions main/config_BT.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,9 @@ extern String stateBTMeasures(bool);
# define HassPresence false //true if we publish into Home Assistant presence topic
#endif

#define HMSerialSpeed 9600 // Communication speed with the HM module, softwareserial doesn't support 115200
//#define HM_BLUE_LED_STOP true //uncomment to stop the blue led light of HM1X

#define BLEdelimiter "4f4b2b444953413a" // OK+DISA:
#define BLEEndOfDiscovery "4f4b2b4449534345" // OK+DISCE
#define BLEdelimiterLength 16
#define CRLR "0d0a"
#define CRLR_Length 4
#define BLE_CNCT_TIMEOUT 3000
#ifndef BLE_CNCT_TIMEOUT
# define BLE_CNCT_TIMEOUT 3000
#endif

unsigned long scanCount = 0;

Expand Down Expand Up @@ -210,6 +204,7 @@ struct BLEAction {

struct BLEdevice {
char macAdr[18];
char name[20];
int macType;
bool isDisc;
bool isWhtL;
Expand Down
4 changes: 4 additions & 0 deletions main/config_mqttDiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ void announceDeviceTrigger(bool use_gateway_info,
# define GATEWAY_MANUFACTURER "OMG_community"
#endif

#ifndef ForceDeviceName
# define ForceDeviceName false // Set to true to force the device name to be from the name of the device and not the model
#endif

/*-------------- Auto discovery macros-----------------*/
// Set the line below to true so as to have autodiscovery working with OpenHAB
#ifndef OpenHABDiscovery
Expand Down
Loading