From 23b3123b971965706934a533391116cc350c15bf Mon Sep 17 00:00:00 2001 From: riegelbrau <73437730+riegelbrau@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:11:02 +0200 Subject: [PATCH] [RTL] Correct string handling for type key in NAMING_KEYS (#1960) * Correct string handling for type key in NAMING_KEYS Extend class structure RTL_433device and modify code to create correct state topic in discovery message to support sensors, that send type key as well as sensors, that don't. Actually only TPMS sensors use type=TPMS. * Try to correct format failure. * Remove trailing spaces --- main/ZgatewayRTL_433.ino | 31 ++++++++++++++++++++++--------- main/config_RF.h | 14 ++++++++------ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/main/ZgatewayRTL_433.ino b/main/ZgatewayRTL_433.ino index dc025a3..ac4afa0 100644 --- a/main/ZgatewayRTL_433.ino +++ b/main/ZgatewayRTL_433.ino @@ -66,11 +66,12 @@ void dumpRTL_433Devices() { RTL_433device* p = *it; DISCOVERY_TRACE_LOG(F("uniqueId %s" CR), p->uniqueId); DISCOVERY_TRACE_LOG(F("modelName %s" CR), p->modelName); + DISCOVERY_TRACE_LOG(F("type %s" CR), p->type); DISCOVERY_TRACE_LOG(F("isDisc %d" CR), p->isDisc); } } -void createOrUpdateDeviceRTL_433(const char* id, const char* model, uint8_t flags) { +void createOrUpdateDeviceRTL_433(const char* id, const char* model, const char* type, uint8_t flags) { if (xSemaphoreTake(semaphorecreateOrUpdateDeviceRTL_433, pdMS_TO_TICKS(30000)) == pdFALSE) { Log.error(F("[rtl_433] semaphorecreateOrUpdateDeviceRTL_433 Semaphore NOT taken" CR)); return; @@ -85,8 +86,12 @@ void createOrUpdateDeviceRTL_433(const char* id, const char* model, uint8_t flag Log.warning(F("[rtl_433] Device id %s exceeds available space" CR), id); // Remove from production release ? }; if (strlcpy(device->modelName, model, modelNameSize) > modelNameSize) { - Log.warning(F("[rtl_433] Device model %s exceeds available space" CR), id); // Remove from production release ? + Log.warning(F("[rtl_433] Device model %s exceeds available space" CR), model); // Remove from production release ? }; + if (strlcpy(device->type, type, typeSize) > typeSize) { + Log.warning(F("[rtl_433] Device type %s exceeds available space" CR), type); // Remove from production release ? + } + DISCOVERY_TRACE_LOG(F("[rtl_433] Device type is %s." CR), device->type); // Remove from production release ? device->isDisc = flags & device_flags_isDisc; RTL_433devices.push_back(device); newRTL_433Devices++; @@ -135,10 +140,16 @@ void launchRTL_433Discovery(bool overrideDiscovery) { # if valueAsATopic // Remove the key from the unique id to extract the device id String idWoKeyAndModel = idWoKey; - idWoKeyAndModel.remove(0, strlen(pdevice->modelName)); - idWoKeyAndModel.replace("-", "/"); + if (strcmp(pdevice->type, "null")) { + idWoKeyAndModel.remove(0, (strlen(pdevice->modelName) + strlen(pdevice->type) + 1)); // type is present + topic = topic + "/" + String(pdevice->type) + "/" + String(pdevice->modelName); + } else { + idWoKeyAndModel.remove(0, (strlen(pdevice->modelName))); + topic = topic + "/" + String(pdevice->modelName); + } DISCOVERY_TRACE_LOG(F("idWoKeyAndModel %s" CR), idWoKeyAndModel.c_str()); - topic = topic + "/" + String(pdevice->modelName) + idWoKeyAndModel; + idWoKeyAndModel.replace("-", "/"); + topic = topic + idWoKeyAndModel; # endif if (strcmp(parameters[i][0], "battery_ok") == 0) { if (strcmp(pdevice->modelName, "Govee-Water") == 0 || strcmp(pdevice->modelName, "Govee-Contact") == 0 || strcmp(pdevice->modelName, "Archos-TBH") == 0 || strcmp(pdevice->modelName, "FineOffset-WH31L") == 0 || strcmp(pdevice->modelName, "Fineoffset-WH45") == 0 || strcmp(pdevice->modelName, "Fineoffset-WN34") == 0 || strcmp(pdevice->modelName, "Fineoffset-WS80") == 0 || strcmp(pdevice->modelName, "Fineoffset-WH0290") == 0 || strcmp(pdevice->modelName, "Fineoffset-WH51") == 0 || strcmp(pdevice->modelName, "Kedsum-TH") == 0 || strcmp(pdevice->modelName, "AVE") == 0 || strcmp(pdevice->modelName, "TPMS") == 0) { @@ -232,7 +243,7 @@ void launchRTL_433Discovery(bool overrideDiscovery) { } } -void storeRTL_433Discovery(JsonObject& RFrtl_433_ESPdata, const char* model, const char* uniqueid) { +void storeRTL_433Discovery(JsonObject& RFrtl_433_ESPdata, const char* model, const char* type, const char* uniqueid) { //Sanitize model name String modelSanitized = model; modelSanitized.replace(" ", "_"); @@ -246,7 +257,7 @@ void storeRTL_433Discovery(JsonObject& RFrtl_433_ESPdata, const char* model, con for (int i = 0; i < numRows; i++) { if (RFrtl_433_ESPdata.containsKey(parameters[i][0])) { String key_id = String(uniqueid) + "-" + String(parameters[i][0]); - createOrUpdateDeviceRTL_433((char*)key_id.c_str(), (char*)modelSanitized.c_str(), device_flags_init); + createOrUpdateDeviceRTL_433((char*)key_id.c_str(), (char*)modelSanitized.c_str(), (char*)type, device_flags_init); } } } @@ -264,6 +275,8 @@ void rtl_433_Callback(char* message) { unsigned long MQTTvalue = (int)RFrtl_433_ESPdata["id"] + round((float)RFrtl_433_ESPdata["temperature_C"]); String topic = subjectRTL_433toMQTT; String model = RFrtl_433_ESPdata["model"]; + String type = RFrtl_433_ESPdata["type"]; + Log.notice(F("type: %s" CR), type.c_str()); String uniqueid; const char naming_keys[5][8] = {"type", "model", "subtype", "channel", "id"}; // from rtl_433_mqtt_hass.py @@ -284,11 +297,11 @@ void rtl_433_Callback(char* message) { uniqueid.replace("/", "-"); - // Log.notice(F("uniqueid: %s" CR), uniqueid.c_str()); + DISCOVERY_TRACE_LOG(F("uniqueid: %s" CR), uniqueid.c_str()); if (!isAduplicateSignal(MQTTvalue)) { # ifdef ZmqttDiscovery if (SYSConfig.discovery) - storeRTL_433Discovery(RFrtl_433_ESPdata, (char*)model.c_str(), (char*)uniqueid.c_str()); + storeRTL_433Discovery(RFrtl_433_ESPdata, (char*)model.c_str(), (char*)type.c_str(), (char*)uniqueid.c_str()); # endif //RFrtl_433_ESPdata["origin"] = (char*)topic.c_str(); //handleJsonEnqueue(RFrtl_433_ESPdata); diff --git a/main/config_RF.h b/main/config_RF.h index ad7e176..6dea2f1 100644 --- a/main/config_RF.h +++ b/main/config_RF.h @@ -1,15 +1,15 @@ -/* +/* Theengs OpenMQTTGateway - We Unite Sensors in One Open-Source Interface - Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an MQTT broker + Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an MQTT broker Send and receiving command by MQTT - + This files enables to set your parameter for the radiofrequency gateways (ZgatewayRF and ZgatewayRF2) with RCswitch and newremoteswitch library - + Copyright: (c)Florian ROBERT - + This file is part of OpenMQTTGateway. - + OpenMQTTGateway is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or @@ -73,10 +73,12 @@ extern void launchRTL_433Discovery(bool overrideDiscovery); # define uniqueIdSize 60 // longest model + longest key # define modelNameSize 31 // longest model +# define typeSize 10 // longest type struct RTL_433device { char uniqueId[uniqueIdSize]; char modelName[modelNameSize]; + char type[typeSize]; bool isDisc; };