diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index a72c48939e..e14d92e5d1 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -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) { @@ -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); @@ -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); @@ -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 @@ -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()).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()); 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"]) { @@ -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); } } } diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index 6b76684bc3..9bda9b369d 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -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; diff --git a/main/config_BT.h b/main/config_BT.h index f7e1768181..11c2f16eec 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -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; @@ -210,6 +204,7 @@ struct BLEAction { struct BLEdevice { char macAdr[18]; + char name[20]; int macType; bool isDisc; bool isWhtL; diff --git a/main/config_mqttDiscovery.h b/main/config_mqttDiscovery.h index ac8dc905c5..0962088f07 100644 --- a/main/config_mqttDiscovery.h +++ b/main/config_mqttDiscovery.h @@ -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