uom made optional

This commit is contained in:
proddy
2021-02-27 21:37:50 +01:00
parent e9e143dfa7
commit 3929ac07af
4 changed files with 28 additions and 23 deletions

View File

@@ -59,7 +59,7 @@ const std::string EMSdevice::uom_to_string(uint8_t uom) {
if (uom == DeviceValueUOM::NONE) { if (uom == DeviceValueUOM::NONE) {
return std::string{}; 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) { 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 // 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; size_t total_s = 0;
uint8_t count = 0; uint8_t count = 0;
for (const auto & dv : devicevalues_) { 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 // type: one of DeviceValueType
// options: options for enum or a divider for int (e.g. F("10")) // options: options for enum or a divider for int (e.g. F("10"))
// short_name: used in Mqtt as keys // 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 // 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) { 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 // init the value depending on it's type
if (type == DeviceValueType::TEXT) { 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)) { } else if ((type == DeviceValueType::ULONG) || (type == DeviceValueType::TIME)) {
*(uint32_t *)(value_p) = EMS_VALUE_ULONG_NOTSET; *(uint32_t *)(value_p) = EMS_VALUE_ULONG_NOTSET;
} else { } else {
// enums, uint8_t, bool behave as uint8_t *(uint8_t *)(value_p) = EMS_VALUE_UINT_NOTSET; // enums, uint8_t, bool behave as uint8_t
*(uint8_t *)(value_p) = EMS_VALUE_UINT_NOTSET;
} }
// count #options // count #options

View File

@@ -52,6 +52,7 @@ enum DeviceValueType : uint8_t {
}; };
// Unit Of Measurement mapping - maps to DeviceValueUOM_s in emsdevice.cpp // Unit Of Measurement mapping - maps to DeviceValueUOM_s in emsdevice.cpp
// sequence is important!
// uom - also used with HA // uom - also used with HA
MAKE_PSTR(percent, "%") MAKE_PSTR(percent, "%")
MAKE_PSTR(degrees, "°C") MAKE_PSTR(degrees, "°C")
@@ -63,6 +64,7 @@ MAKE_PSTR(hours, "hours")
MAKE_PSTR(ua, "uA") MAKE_PSTR(ua, "uA")
MAKE_PSTR(lmin, "l/min") MAKE_PSTR(lmin, "l/min")
enum DeviceValueUOM : uint8_t { enum DeviceValueUOM : uint8_t {
NONE = 0,
DEGREES, DEGREES,
PERCENT, PERCENT,
LMIN, LMIN,
@@ -71,8 +73,7 @@ enum DeviceValueUOM : uint8_t {
HOURS, HOURS,
MINUTES, MINUTES,
UA, UA,
BAR, BAR
NONE
}; };
@@ -212,7 +213,7 @@ class EMSdevice {
std::string to_string_short() const; std::string to_string_short() const;
void show_telegram_handlers(uuid::console::Shell & shell); 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); char * show_telegram_handlers(char * result);
void show_mqtt_handlers(uuid::console::Shell & shell); 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(JsonObject & json, const uint8_t tag_filter, const bool verbose = false);
bool generate_values_json_web(JsonObject & json); 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, 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); void write_command(const uint16_t type_id, const uint8_t offset, const uint8_t value, const uint16_t validate_typeid);

View File

@@ -539,16 +539,16 @@ void Mqtt::ha_status() {
Mqtt::publish_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag Mqtt::publish_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag
// create the sensors // 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("Wifi strength"), EMSdevice::DeviceType::SYSTEM, F("rssi"));
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"), EMSdevice::DeviceType::SYSTEM, F("uptime"));
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("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"), DeviceValueUOM::NONE); 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"), DeviceValueUOM::NONE); 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"), DeviceValueUOM::NONE); 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"), DeviceValueUOM::NONE); 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"), DeviceValueUOM::NONE); 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"), DeviceValueUOM::NONE); 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"), DeviceValueUOM::NONE); 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. // 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 __FlashStringHelper * name,
const uint8_t device_type, // EMSdevice::DeviceType const uint8_t device_type, // EMSdevice::DeviceType
const __FlashStringHelper * entity, const __FlashStringHelper * entity,
const uint8_t uom) { const uint8_t uom) { // DeviceValueUOM (0=NONE)
// ignore if name (fullname) is empty // ignore if name (fullname) is empty
if (name == nullptr) { if (name == nullptr) {
return; return;

View File

@@ -104,7 +104,7 @@ class Mqtt {
static void publish_ha(const std::string & topic, const JsonObject & payload); static void publish_ha(const std::string & topic, const JsonObject & payload);
static void publish_ha(const __FlashStringHelper * 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 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); static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type);
@@ -211,7 +211,7 @@ class Mqtt {
static AsyncMqttClient * mqttClient_; static AsyncMqttClient * mqttClient_;
static uint16_t mqtt_message_id_; 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 constexpr uint8_t MQTT_PUBLISH_MAX_RETRY = 3; // max retries for giving up on publishing
static std::shared_ptr<const MqttMessage> queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, bool retain); static std::shared_ptr<const MqttMessage> queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, bool retain);