From e838d3cee008f64360ab7701c0c3dca198301c38 Mon Sep 17 00:00:00 2001 From: h2zero <32826625+h2zero@users.noreply.github.com> Date: Mon, 24 Jan 2022 19:09:15 -0700 Subject: [PATCH] Remove std::string as the model id and use decoder enum. (#1143) * Remove std::string as the model id and use decoder enum. * Update decoder version * Check model id is in decoder range when launching discovery. * Add BLEconnect enum MIN/MAX --- main/ZgatewayBLEConnect.ino | 6 +-- main/ZgatewayBT.ino | 84 ++++++++++++++++++++++--------------- main/config_BT.h | 13 +++++- main/main.ino | 6 --- platformio.ini | 2 +- 5 files changed, 67 insertions(+), 44 deletions(-) diff --git a/main/ZgatewayBLEConnect.ino b/main/ZgatewayBLEConnect.ino index f2f12f2..9b27b45 100644 --- a/main/ZgatewayBLEConnect.ino +++ b/main/ZgatewayBLEConnect.ino @@ -145,10 +145,10 @@ void LYWSD03MMC_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pD for (std::vector::iterator it = devices.begin(); it != devices.end(); ++it) { BLEdevice* p = *it; if ((strcmp(p->macAdr, (char*)mac_address.c_str()) == 0)) { - if (p->sensorModel_id.compare("LYWSD03MMC") == 0) + if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC) BLEdata["model"] = "LYWSD03MMC"; - else if (p->sensorModel_id.compare("MHO_C401") == 0) - BLEdata["model"] = "MHO_C401"; + else if (p->sensorModel_id == BLEconectable::id::MHO_C401) + BLEdata["model"] = "MHO-C401"; } } BLEdata["id"] = (char*)mac_address.c_str(); diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 0bf850b..b845401 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -81,7 +81,12 @@ vector BLEactions; vector devices; int newDevices = 0; -static BLEdevice NO_DEVICE_FOUND = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false, false, false, false, ""}; +static BLEdevice NO_DEVICE_FOUND = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + false, + false, + false, + false, + TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL}; static bool oneWhite = false; int minRssi = abs(MinimumRSSI); //minimum rssi value @@ -196,7 +201,7 @@ void pubBT(JsonObject& data) { bool ProcessLock = false; // Process lock when we want to use a critical function like OTA for example BLEdevice* getDeviceByMac(const char* mac); -void createOrUpdateDevice(const char* mac, uint8_t flags, std::string model); +void createOrUpdateDevice(const char* mac, uint8_t flags, int model); BLEdevice* getDeviceByMac(const char* mac) { Log.trace(F("getDeviceByMac %s" CR), mac); @@ -219,13 +224,14 @@ bool updateWorB(JsonObject& BTdata, bool isWhite) { for (int i = 0; i < size; i++) { const char* mac = BTdata[jsonKey][i]; - createOrUpdateDevice(mac, (isWhite ? device_flags_isWhiteL : device_flags_isBlackL), ""); + createOrUpdateDevice(mac, (isWhite ? device_flags_isWhiteL : device_flags_isBlackL), + TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL); } return true; } -void createOrUpdateDevice(const char* mac, uint8_t flags, std::string model) { +void createOrUpdateDevice(const char* mac, uint8_t flags, int model) { # ifdef ESP32 if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(30000)) == pdFALSE) { Log.error(F("Semaphore NOT taken" CR)); @@ -256,7 +262,8 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, std::string model) { device->connect = true; } - if (!model.empty()) device->sensorModel_id = model; + if (model != TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL) + device->sensorModel_id = model; if (flags & device_flags_isWhiteL || flags & device_flags_isBlackL) { device->isWhtL = flags & device_flags_isWhiteL; @@ -284,7 +291,7 @@ void dumpDevices() { Log.trace(F("isWhtL %d" CR), p->isWhtL); Log.trace(F("isBlkL %d" CR), p->isBlkL); Log.trace(F("connect %d" CR), p->connect); - Log.trace(F("sensorModel_id %s" CR), p->sensorModel_id.c_str()); + Log.trace(F("sensorModel_id %d" CR), p->sensorModel_id); } } @@ -467,15 +474,16 @@ void BLEconnect() { if (p->connect) { Log.trace(F("Model to connect found: %s" CR), p->macAdr); NimBLEAddress addr(std::string(p->macAdr)); - if (p->sensorModel_id.compare("LYWSD03MMC") == 0 || p->sensorModel_id.compare("MHO-C401") == 0) { + if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC || + p->sensorModel_id == BLEconectable::id::MHO_C401) { LYWSD03MMC_connect BLEclient(addr); BLEclient.processActions(BLEactions); BLEclient.publishData(); - } else if (p->sensorModel_id.compare("DT24-BLE") == 0) { + } else if (p->sensorModel_id == BLEconectable::id::DT24_BLE) { DT24_connect BLEclient(addr); BLEclient.processActions(BLEactions); BLEclient.publishData(); - } else if (p->sensorModel_id.compare("HHCCJCY01HHCC") == 0) { + } else if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { HHCCJCY01HHCC_connect BLEclient(addr); BLEclient.processActions(BLEactions); BLEclient.publishData(); @@ -493,10 +501,10 @@ void BLEconnect() { if (!it.complete && --it.ttl) { swap.push_back(it); } else if (memcmp(it.addr, p->macAdr, sizeof(it.addr)) == 0) { - if (p->sensorModel_id != "DT24-BLE" && - p->sensorModel_id != "HHCCJCY01HHCC" && - p->sensorModel_id != "LYWSD03MMC" && - p->sensorModel_id != "MHO-C401") { + if (p->sensorModel_id != BLEconectable::id::DT24_BLE && + p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC && + p->sensorModel_id != BLEconectable::id::LYWSD03MMC && + p->sensorModel_id != BLEconectable::id::MHO_C401) { // if irregulary connected to and connection failed clear the connect flag. p->connect = false; } @@ -809,14 +817,16 @@ void launchBTDiscovery() { # endif BLEdevice* p = *it; Log.trace(F("Device mac %s" CR), p->macAdr); - if (p->sensorModel_id.compare("") != 0 && !isDiscovered(p)) { + if (p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL && + p->sensorModel_id < BLEconectable::id::MIN && !isDiscovered(p)) { String macWOdots = String(p->macAdr); macWOdots.replace(":", ""); - Log.trace(F("Looking for Model_id: %s" CR), p->sensorModel_id.c_str()); - std::string properties = decoder.getTheengProperties(p->sensorModel_id.c_str()); + Log.trace(F("Looking for Model_id: %d" CR), p->sensorModel_id); + std::string properties = decoder.getTheengProperties(p->sensorModel_id); Log.trace(F("properties: %s" CR), properties.c_str()); - std::string brand = decoder.getTheengAttribute(p->sensorModel_id.c_str(), "brand"); - std::string model = decoder.getTheengAttribute(p->sensorModel_id.c_str(), "model"); + 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"); if (!properties.empty()) { StaticJsonDocument jsonBuffer; deserializeJson(jsonBuffer, properties); @@ -825,7 +835,7 @@ void launchBTDiscovery() { Log.trace("Unit: %s", prop.value()["unit"].as()); Log.trace("Name: %s", prop.value()["name"].as()); String discovery_topic = String(subjectBTtoMQTT) + "/" + macWOdots; - String entity_name = String(p->sensorModel_id.c_str()) + "-" + String(prop.key().c_str()); + String entity_name = String(model_id.c_str()) + "-" + String(prop.key().c_str()); String unique_id = macWOdots + "-" + entity_name; # if OpenHABDiscovery String value_template = "{{ value_json." + String(prop.key().c_str()) + "}}"; @@ -837,19 +847,19 @@ void launchBTDiscovery() { will_Topic, prop.value()["name"], value_template.c_str(), "", "", prop.value()["unit"], 0, "", "", false, "", - model.c_str(), brand.c_str(), p->sensorModel_id.c_str(), macWOdots.c_str(), false, + model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false, stateClassMeasurement); } } else { // Discovery of sensors from which we retrieve data only by connect - if (p->sensorModel_id.compare("DT24-BLE") == 0) { - DT24Discovery(macWOdots.c_str(), p->sensorModel_id.c_str()); + if (p->sensorModel_id == BLEconectable::id::DT24_BLE) { + DT24Discovery(macWOdots.c_str(), "DT24-BLE"); } - if (p->sensorModel_id.compare("LYWSD03MMC") == 0) { - LYWSD03MMCDiscovery(macWOdots.c_str(), p->sensorModel_id.c_str()); + if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC) { + LYWSD03MMCDiscovery(macWOdots.c_str(), "LYWSD03MMC"); } - if (p->sensorModel_id.compare("MHO-C401") == 0) { - MHO_C401Discovery(macWOdots.c_str(), p->sensorModel_id.c_str()); + if (p->sensorModel_id == BLEconectable::id::MHO_C401) { + MHO_C401Discovery(macWOdots.c_str(), "MHO-C401"); } } p->isDisc = true; // we don't need the semaphore and all the search magic via createOrUpdateDevice @@ -897,19 +907,26 @@ void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) { void process_bledata(JsonObject& BLEdata) { const char* mac = BLEdata["id"].as(); - if (decoder.decodeBLEJson(BLEdata)) { // Broadcaster devices - std::string model_id = BLEdata["model_id"]; - Log.trace(F("Decoder found device: %s" CR), model_id.c_str()); - if (model_id.compare("HHCCJCY01HHCC") == 0) { + int model_id = decoder.decodeBLEJson(BLEdata); + 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) { createOrUpdateDevice(mac, device_flags_connect, model_id); // Device that broadcast and can be connected } else { createOrUpdateDevice(mac, device_flags_init, model_id); } } else if (BLEdata.containsKey("name")) { // Connectable devices std::string name = BLEdata["name"]; - if (name.compare("LYWSD03MMC") == 0 || name.compare("DT24-BLE") == 0 || name.compare("MHO-C401") == 0) { + if (name.compare("LYWSD03MMC") == 0) + model_id = BLEconectable::id::LYWSD03MMC; + else if (name.compare("DT24-BLE") == 0) + model_id = BLEconectable::id::DT24_BLE; + else if (name.compare("MHO-C401") == 0) + model_id = BLEconectable::id::MHO_C401; + + if (model_id > 0) { Log.trace(F("Connectable device found: %s" CR), name.c_str()); - createOrUpdateDevice(mac, device_flags_connect, name); + createOrUpdateDevice(mac, device_flags_connect, model_id); } } else { Log.trace(F("No device found " CR)); @@ -991,7 +1008,8 @@ void MQTTtoBTAction(JsonObject& BTdata) { return; } - createOrUpdateDevice(action.addr, device_flags_connect, ""); + createOrUpdateDevice(action.addr, device_flags_connect, + TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL); if (BTdata.containsKey("immediate") && BTdata["immediate"].as()) { // Immediate action; we need to prevent the normal connection action and stop scanning diff --git a/main/config_BT.h b/main/config_BT.h index 140726f..fcebb60 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -168,7 +168,18 @@ struct BLEdevice { bool isWhtL; bool isBlkL; bool connect; - std::string sensorModel_id; + int sensorModel_id; +}; + +class BLEconectable { +public: + enum id { + MIN = 1000, + LYWSD03MMC, + MHO_C401, + DT24_BLE, + MAX, + }; }; JsonObject& getBTJsonObject(const char* json = NULL, bool haPresenceEnabled = true); diff --git a/main/main.ino b/main/main.ino index 6d38d1e..e82fb3e 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1154,9 +1154,7 @@ void setup_wifimanager(bool reset_settings) { WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, parameters_size * 2); WiFiManagerParameter custom_mqtt_topic("topic", "mqtt base topic", mqtt_topic, mqtt_topic_max_size); WiFiManagerParameter custom_mqtt_secure("secure", "mqtt secure", "1", 1, mqtt_secure ? "type=\"checkbox\" checked" : "type=\"checkbox\""); -# ifdef ESP32 // not enough memory on ESP8266 WiFiManagerParameter custom_mqtt_cert("cert", "mqtt broker cert", mqtt_cert.c_str(), 2048); -# endif WiFiManagerParameter custom_gateway_name("name", "gateway name", gateway_name, parameters_size * 2); # endif //WiFiManager @@ -1185,9 +1183,7 @@ void setup_wifimanager(bool reset_settings) { wifiManager.addParameter(&custom_mqtt_user); wifiManager.addParameter(&custom_mqtt_pass); wifiManager.addParameter(&custom_mqtt_secure); -# ifdef ESP32 // not enough memory on ESP8266 wifiManager.addParameter(&custom_mqtt_cert); -# endif wifiManager.addParameter(&custom_gateway_name); wifiManager.addParameter(&custom_mqtt_topic); # endif @@ -1241,7 +1237,6 @@ void setup_wifimanager(bool reset_settings) { strcpy(gateway_name, custom_gateway_name.getValue()); mqtt_secure = *custom_mqtt_secure.getValue(); -# ifdef ESP32 // not enough memory on ESP8266 int cert_len = strlen(custom_mqtt_cert.getValue()); if (cert_len) { char* cert_in = (char*)custom_mqtt_cert.getValue(); @@ -1259,7 +1254,6 @@ void setup_wifimanager(bool reset_settings) { mqtt_cert = cert_begin; } -# endif # endif //save the custom parameters to FS saveMqttConfig(); diff --git a/platformio.ini b/platformio.ini index ddaef28..b5ebff5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -129,7 +129,7 @@ somfy_remote=Somfy_Remote_Lib@0.3.0 rtl_433_ESP = https://github.com/NorthernMan54/rtl_433_ESP.git#v0.0.3 emodbus = miq19/eModbus@1.0.0 gfSunInverter = https://github.com/BlackSmith/GFSunInverter.git#v1.0.1 -decoder = https://github.com/theengs/decoder.git#v0.1.6 +decoder = https://github.com/theengs/decoder.git#v0.1.7 [env] framework = arduino