From c18640eab083f2d372bd66df51525321f262d037 Mon Sep 17 00:00:00 2001 From: Proddy Date: Mon, 20 Feb 2023 18:09:32 +0100 Subject: [PATCH] brand_to_string to brand_to_char. use const char * where possible --- src/emsdevice.cpp | 59 ++++++++++++++++---------------------- src/emsdevice.h | 2 +- src/mqtt.cpp | 2 +- src/mqtt.h | 3 +- src/web/WebDataService.cpp | 2 +- 5 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index 3c1cbe402..657175d03 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -79,7 +79,7 @@ const char * EMSdevice::uom_to_string(uint8_t uom) { } } -const std::string EMSdevice::brand_to_string() { +const char * EMSdevice::brand_to_char() { switch (brand_) { case EMSdevice::Brand::BOSCH: return "Bosch"; @@ -267,8 +267,8 @@ const std::string EMSdevice::to_string() { return std::string(name_) + " (DeviceID:" + Helpers::hextoa(device_id_) + ", ProductID:" + Helpers::itoa(product_id_) + ", Version:" + version_ + ")"; } - return brand_to_string() + " " + name_ + " (DeviceID:" + Helpers::hextoa(device_id_) + ", ProductID:" + Helpers::itoa(product_id_) + ", Version:" + version_ - + ")"; + return std::string(brand_to_char()) + " " + name_ + " (DeviceID:" + Helpers::hextoa(device_id_) + ", ProductID:" + Helpers::itoa(product_id_) + + ", Version:" + version_ + ")"; } // returns out brand + device name @@ -278,7 +278,7 @@ const std::string EMSdevice::to_string_short() { return std::string(device_type_2_device_name_translated()) + ": " + name_; } - return std::string(device_type_2_device_name_translated()) + ": " + brand_to_string() + " " + name_; + return std::string(device_type_2_device_name_translated()) + ": " + brand_to_char() + " " + name_; } // for each telegram that has the fetch value set (true) do a read request @@ -458,30 +458,18 @@ void EMSdevice::register_telegram_type(const uint16_t telegram_type_id, const ch } // add to device value library, also know now as a "device entity" -// arguments are: -// tag: to be used to group mqtt together, either as separate topics as a nested object -// value_p: pointer to the value from the .h file -// type: one of DeviceValueType -// options: options for enum, which are translated as a list of lists -// options_single: list of names -// numeric_operator: to divide or multiply, see DeviceValueNumOps:: -// short_name: used in MQTT as keys -// fullname: used in Web and Console unless empty (nullptr) - can be translated -// uom: unit of measure from DeviceValueUOM -// has_cmd: true if this is an associated command -// min: min allowed value -// max: max allowed value -void EMSdevice::add_device_value(uint8_t tag, - void * value_p, - uint8_t type, - const char * const ** options, - const char * const * options_single, - int8_t numeric_operator, - const char * const * name, - uint8_t uom, - const cmd_function_p f, - int16_t min, - uint16_t max) { +void EMSdevice::add_device_value(uint8_t tag, // to be used to group mqtt together, either as separate topics as a nested object + void * value_p, // pointer to the value from the .h file + uint8_t type, // one of DeviceValueType + const char * const ** options, // options for enum, which are translated as a list of lists + const char * const * options_single, // list of names + int8_t numeric_operator, // to divide or multiply, see DeviceValueNumOps:: + const char * const * name, // shortname, used in MQTT as the key + uint8_t uom, // unit of measure from DeviceValueUOM + const cmd_function_p f, // command function pointer + int16_t min, // min allowed value + uint16_t max // max allowed value +) { // initialize the device value depending on it's type // ignoring DeviceValueType::CMD and DeviceValueType::TIME @@ -512,7 +500,12 @@ void EMSdevice::add_device_value(uint8_t tag, // get fullname, getting translation if it exists const char * const * fullname; if (Helpers::count_items(name) == 1) { - fullname = nullptr; // no translations available, use empty to prevent crash +#ifdef EMSESP_DEBUG + // when compiling in debug, we don't use all the translations to save on Flash memory + fullname = &name[0]; // use shortname when debugging +#else + fullname = nullptr; // no translations available, use empty +#endif } else { fullname = &name[1]; // translations start at index 1 } @@ -1533,11 +1526,7 @@ bool EMSdevice::generate_values(JsonObject & output, const uint8_t tag_filter, c for (auto & dv : devicevalues_) { // check if it exists, there is a value for the entity. Set the flag to ACTIVE // not that this will override any previously removed states - if (dv.hasValue()) { - dv.add_state(DeviceValueState::DV_ACTIVE); - } else { - dv.remove_state(DeviceValueState::DV_ACTIVE); - } + (dv.hasValue()) ? dv.add_state(DeviceValueState::DV_ACTIVE) : dv.remove_state(DeviceValueState::DV_ACTIVE); auto fullname = dv.get_fullname(); @@ -1720,7 +1709,7 @@ void EMSdevice::mqtt_ha_entity_config_create() { if (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) && (dv.type != DeviceValueType::CMD) && dv.has_state(DeviceValueState::DV_ACTIVE) && !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE)) { // create_device_config is only done once for the EMS device. It can added to any entity, so we take the first - Mqtt::publish_ha_sensor_config(dv, name(), brand_to_string(), false, create_device_config); + Mqtt::publish_ha_sensor_config(dv, name(), brand_to_char(), false, create_device_config); dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); create_device_config = false; // only create the main config once } diff --git a/src/emsdevice.h b/src/emsdevice.h index d69468421..e088ebbdc 100644 --- a/src/emsdevice.h +++ b/src/emsdevice.h @@ -178,7 +178,7 @@ class EMSdevice { } } - const std::string brand_to_string(); + const char * brand_to_char(); const std::string to_string(); const std::string to_string_short(); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 103cc3354..c6f9367a3 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -912,7 +912,7 @@ void Mqtt::process_queue() { // create's a ha sensor config topic from a device value object // and also takes a flag (create_device_config) used to also create the main HA device config. This is only needed for one entity -void Mqtt::publish_ha_sensor_config(DeviceValue & dv, const std::string & model, const std::string & brand, const bool remove, const bool create_device_config) { +void Mqtt::publish_ha_sensor_config(DeviceValue & dv, const char * model, const char * brand, const bool remove, const bool create_device_config) { StaticJsonDocument dev_json; // always create the ids diff --git a/src/mqtt.h b/src/mqtt.h index c19e72d74..80e5828e6 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -89,8 +89,7 @@ class Mqtt { static void publish_ha(const char * topic, const JsonObject & payload); static void publish_ha(const char * topic); - static void - publish_ha_sensor_config(DeviceValue & dv, const std::string & model, const std::string & brand, const bool remove, const bool create_device_config = false); + static void publish_ha_sensor_config(DeviceValue & dv, const char * model, const char * brand, const bool remove, const bool create_device_config = false); static void publish_ha_sensor_config(uint8_t type, uint8_t tag, const char * const fullname, diff --git a/src/web/WebDataService.cpp b/src/web/WebDataService.cpp index 84e95b671..703837369 100644 --- a/src/web/WebDataService.cpp +++ b/src/web/WebDataService.cpp @@ -83,7 +83,7 @@ void WebDataService::core_data(AsyncWebServerRequest * request) { obj["id"] = Helpers::smallitoa(buffer, emsdevice->unique_id()); // a unique id as a string obj["tn"] = emsdevice->device_type_2_device_name_translated(); // translated device type name obj["t"] = emsdevice->device_type(); // device type number - obj["b"] = emsdevice->brand_to_string(); // brand + obj["b"] = emsdevice->brand_to_char(); // brand obj["n"] = emsdevice->name(); // name obj["d"] = emsdevice->device_id(); // deviceid obj["p"] = emsdevice->product_id(); // productid