From 3929ac07af54f447249be1fc8b3fded3aae59f14 Mon Sep 17 00:00:00 2001 From: proddy Date: Sat, 27 Feb 2021 21:37:50 +0100 Subject: [PATCH] uom made optional --- src/emsdevice.cpp | 10 ++++------ src/emsdevice.h | 15 +++++++++++---- src/mqtt.cpp | 22 +++++++++++----------- src/mqtt.h | 4 ++-- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index cbad0a163..6ef018016 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -59,7 +59,7 @@ const std::string EMSdevice::uom_to_string(uint8_t uom) { if (uom == DeviceValueUOM::NONE) { return std::string{}; } - return uuid::read_flash_string(DeviceValueUOM_s[uom]); + return uuid::read_flash_string(DeviceValueUOM_s[uom - 1]); // offset by 1 to account for NONE } const std::string EMSdevice::tag_to_string(uint8_t tag) { @@ -286,7 +286,7 @@ bool EMSdevice::get_toggle_fetch(uint16_t telegram_id) { } // list device values, only for EMSESP_DEBUG mode -void EMSdevice::show_device_values(uuid::console::Shell & shell) { +void EMSdevice::show_device_values_debug(uuid::console::Shell & shell) { size_t total_s = 0; uint8_t count = 0; for (const auto & dv : devicevalues_) { @@ -367,9 +367,8 @@ void EMSdevice::register_telegram_type(const uint16_t telegram_type_id, const __ // type: one of DeviceValueType // options: options for enum or a divider for int (e.g. F("10")) // short_name: used in Mqtt as keys -// full name: used in Web and Console +// full name: used in Web and Console unless empty (nullptr) // uom: unit of measure from DeviceValueUOM -// icon (optional): the HA mdi icon to use, from locale_*.h file void EMSdevice::register_device_value(uint8_t tag, void * value_p, uint8_t type, const __FlashStringHelper * const * options, const __FlashStringHelper * short_name, const __FlashStringHelper * full_name, uint8_t uom) { // init the value depending on it's type if (type == DeviceValueType::TEXT) { @@ -383,8 +382,7 @@ void EMSdevice::register_device_value(uint8_t tag, void * value_p, uint8_t type, } else if ((type == DeviceValueType::ULONG) || (type == DeviceValueType::TIME)) { *(uint32_t *)(value_p) = EMS_VALUE_ULONG_NOTSET; } else { - // enums, uint8_t, bool behave as uint8_t - *(uint8_t *)(value_p) = EMS_VALUE_UINT_NOTSET; + *(uint8_t *)(value_p) = EMS_VALUE_UINT_NOTSET; // enums, uint8_t, bool behave as uint8_t } // count #options diff --git a/src/emsdevice.h b/src/emsdevice.h index 069ecd21b..c814aa0ed 100644 --- a/src/emsdevice.h +++ b/src/emsdevice.h @@ -52,6 +52,7 @@ enum DeviceValueType : uint8_t { }; // Unit Of Measurement mapping - maps to DeviceValueUOM_s in emsdevice.cpp +// sequence is important! // uom - also used with HA MAKE_PSTR(percent, "%") MAKE_PSTR(degrees, "°C") @@ -63,6 +64,7 @@ MAKE_PSTR(hours, "hours") MAKE_PSTR(ua, "uA") MAKE_PSTR(lmin, "l/min") enum DeviceValueUOM : uint8_t { + NONE = 0, DEGREES, PERCENT, LMIN, @@ -71,8 +73,7 @@ enum DeviceValueUOM : uint8_t { HOURS, MINUTES, UA, - BAR, - NONE + BAR }; @@ -212,7 +213,7 @@ class EMSdevice { std::string to_string_short() const; void show_telegram_handlers(uuid::console::Shell & shell); - void show_device_values(uuid::console::Shell & shell); + void show_device_values_debug(uuid::console::Shell & shell); char * show_telegram_handlers(char * result); void show_mqtt_handlers(uuid::console::Shell & shell); @@ -225,7 +226,13 @@ class EMSdevice { bool generate_values_json(JsonObject & json, const uint8_t tag_filter, const bool verbose = false); bool generate_values_json_web(JsonObject & json); - void register_device_value(uint8_t tag, void * value_p, uint8_t type, const __FlashStringHelper * const * options, const __FlashStringHelper * short_name, const __FlashStringHelper * full_name, uint8_t uom); + void register_device_value(uint8_t tag, + void * value_p, + uint8_t type, + const __FlashStringHelper * const * options, + const __FlashStringHelper * short_name, + const __FlashStringHelper * full_name, + uint8_t uom = DeviceValueUOM::NONE); void write_command(const uint16_t type_id, const uint8_t offset, uint8_t * message_data, const uint8_t message_length, const uint16_t validate_typeid); void write_command(const uint16_t type_id, const uint8_t offset, const uint8_t value, const uint16_t validate_typeid); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 9935e4298..0d8fadc9e 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -539,16 +539,16 @@ void Mqtt::ha_status() { Mqtt::publish_ha(topic, doc.as()); // publish the config payload with retain flag // create the sensors - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("Wifi strength"), EMSdevice::DeviceType::SYSTEM, F("rssi"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("Uptime"), EMSdevice::DeviceType::SYSTEM, F("uptime"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("Uptime (sec)"), EMSdevice::DeviceType::SYSTEM, F("uptime_sec"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("Free heap memory"), EMSdevice::DeviceType::SYSTEM, F("freemem"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Failed MQTT publishes"), EMSdevice::DeviceType::SYSTEM, F("mqttfails"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Rx Sent"), EMSdevice::DeviceType::SYSTEM, F("rxsent"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Rx Fails"), EMSdevice::DeviceType::SYSTEM, F("rxfails"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Tx Reads"), EMSdevice::DeviceType::SYSTEM, F("txread"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Tx Writes"), EMSdevice::DeviceType::SYSTEM, F("txwrite"), DeviceValueUOM::NONE); - publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Tx Fails"), EMSdevice::DeviceType::SYSTEM, F("txfails"), DeviceValueUOM::NONE); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("Wifi strength"), EMSdevice::DeviceType::SYSTEM, F("rssi")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("Uptime"), EMSdevice::DeviceType::SYSTEM, F("uptime")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("Uptime (sec)"), EMSdevice::DeviceType::SYSTEM, F("uptime_sec")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("Free heap memory"), EMSdevice::DeviceType::SYSTEM, F("freemem")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Failed MQTT publishes"), EMSdevice::DeviceType::SYSTEM, F("mqttfails")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Rx Sent"), EMSdevice::DeviceType::SYSTEM, F("rxsent")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Rx Fails"), EMSdevice::DeviceType::SYSTEM, F("rxfails")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Tx Reads"), EMSdevice::DeviceType::SYSTEM, F("txread")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Tx Writes"), EMSdevice::DeviceType::SYSTEM, F("txwrite")); + publish_mqtt_ha_sensor(DeviceValueType::INT, DeviceValueTAG::TAG_NONE, F("# Tx Fails"), EMSdevice::DeviceType::SYSTEM, F("txfails")); } // add sub or pub task to the queue. @@ -755,7 +755,7 @@ void Mqtt::publish_mqtt_ha_sensor(uint8_t type, // EMSdevice const __FlashStringHelper * name, const uint8_t device_type, // EMSdevice::DeviceType const __FlashStringHelper * entity, - const uint8_t uom) { + const uint8_t uom) { // DeviceValueUOM (0=NONE) // ignore if name (fullname) is empty if (name == nullptr) { return; diff --git a/src/mqtt.h b/src/mqtt.h index fd2231384..0d503bff4 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -104,7 +104,7 @@ class Mqtt { static void publish_ha(const std::string & topic, const JsonObject & payload); static void publish_ha(const __FlashStringHelper * topic, const JsonObject & payload); - static void publish_mqtt_ha_sensor(uint8_t type, uint8_t tag, const __FlashStringHelper * name, const uint8_t device_type, const __FlashStringHelper * entity, const uint8_t uom); + static void publish_mqtt_ha_sensor(uint8_t type, uint8_t tag, const __FlashStringHelper * name, const uint8_t device_type, const __FlashStringHelper * entity, const uint8_t uom = 0); static void register_command(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_p cb); static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type); @@ -211,7 +211,7 @@ class Mqtt { static AsyncMqttClient * mqttClient_; static uint16_t mqtt_message_id_; - static constexpr uint32_t MQTT_PUBLISH_WAIT = 200; // delay between sending publishes, to account for large payloads + static constexpr uint32_t MQTT_PUBLISH_WAIT = 100; // delay between sending publishes, to account for large payloads static constexpr uint8_t MQTT_PUBLISH_MAX_RETRY = 3; // max retries for giving up on publishing static std::shared_ptr queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, bool retain);