Skip to content

Commit

Permalink
add device model - emsesp#2073
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Oct 28, 2024
1 parent 6e9bc2d commit eed676f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 86 deletions.
9 changes: 7 additions & 2 deletions src/emsdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1982,9 +1982,14 @@ void EMSdevice::read_command(const uint16_t type_id, const uint8_t offset, const
// returns either default or custom name
std::string EMSdevice::name() {
if (custom_name_.empty()) {
return default_name_;
// return default name prefixed with a model if exists
if (model().empty()) {
return default_name();
}
return model() + "/" + default_name();
}
return custom_name_;

return custom_name();
}

// copy a raw value (i.e. without applying the numeric_operator) to the output buffer.
Expand Down
143 changes: 68 additions & 75 deletions src/emsdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,48 @@ class EMSdevice {
static uint8_t decode_brand(uint8_t value);
static bool export_values(uint8_t device_type, JsonObject output, const int8_t id, const uint8_t output_target);

// non static
// non static functions

const char * device_type_name(); // returns short non-translated device type name
const char * device_type_2_device_name_translated(); // returns translated device type name
bool has_tags(const int8_t tag) const;
bool has_cmd(const char * cmd, const int8_t id) const;
const char * brand_to_char();
std::string to_string();
std::string to_string_short();
std::string name(); // returns either default or custom name of a device (if defined)

bool has_tags(const int8_t tag) const;
bool has_cmd(const char * cmd, const int8_t id) const;
bool is_device_id(uint8_t device_id) const {
return ((device_id & 0x7F) == (device_id_ & 0x7F));
}

inline uint8_t device_id() const {
// Getters
uint8_t device_id() const {
return device_id_;
}

inline uint8_t product_id() const {
uint8_t product_id() const {
return product_id_;
}

void product_id(uint8_t product_id) {
product_id_ = product_id;
uint8_t device_type() const {
return device_type_; // see enum DeviceType below
}

const char * version() const {
return version_;
}

inline bool is_device_id(uint8_t device_id) const {
return ((device_id & 0x7F) == (device_id_ & 0x7F));
uint8_t brand() const {
return brand_;
}

void active(bool active) {
active_ = active;
}

const char * default_name() const {
return default_name_;
}

// flags
Expand All @@ -91,129 +111,105 @@ class EMSdevice {
return flags_;
}

inline uint8_t device_type() const {
return device_type_; // see enum DeviceType below
}

inline void version(const char * version) {
strlcpy(version_, version, sizeof(version_));
}

inline const char * version() const {
return version_;
}

inline void brand(uint8_t brand) {
brand_ = brand;
}

inline uint8_t brand() const {
return brand_;
// set custom device name
void custom_name(std::string const & custom_name) {
custom_name_ = custom_name;
}

inline void active(bool active) {
active_ = active;
std::string custom_name() const {
return custom_name_;
}

// set custom device name
inline void custom_name(std::string const & custom_name) {
custom_name_ = custom_name;
// set device model
void model(std::string const & model) {
model_ = model;
}
std::string name(); // returns either default or custom name if defined

// default name
inline void default_name(const char * default_name) {
default_name_ = default_name;
}
inline const char * default_name() const {
return default_name_;
std::string model() const {
return model_;
}

inline uint8_t unique_id() const {
return unique_id_;
}

inline void unique_id(uint8_t unique_id) {
void unique_id(uint8_t unique_id) {
unique_id_ = unique_id;
}

inline bool has_update() const {
bool has_update() const {
return has_update_;
}

inline void has_update(bool flag) {
void has_update(bool flag) {
has_update_ = flag;
}

inline void has_update(void * value) {
void has_update(void * value) {
has_update_ = true;
publish_value(value);
}

inline void has_update(char * value, const char * newvalue, size_t len) {
void has_update(char * value, const char * newvalue, size_t len) {
if (strcmp(value, newvalue) != 0) {
strlcpy(value, newvalue, len);
has_update_ = true;
publish_value(value);
}
}

inline void has_update(uint8_t & value, uint8_t newvalue) {
void has_update(uint8_t & value, uint8_t newvalue) {
if (value != newvalue) {
value = newvalue;
has_update_ = true;
publish_value((void *)&value);
}
}

inline void has_update(uint16_t & value, uint16_t newvalue) {
void has_update(uint16_t & value, uint16_t newvalue) {
if (value != newvalue) {
value = newvalue;
has_update_ = true;
publish_value((void *)&value);
}
}

inline void has_update(uint32_t & value, uint32_t newvalue) {
void has_update(uint32_t & value, uint32_t newvalue) {
if (value != newvalue) {
value = newvalue;
has_update_ = true;
publish_value((void *)&value);
}
}

inline void has_enumupdate(std::shared_ptr<const Telegram> telegram, uint8_t & value, const uint8_t index, int8_t s = 0) {
void has_enumupdate(std::shared_ptr<const Telegram> telegram, uint8_t & value, const uint8_t index, int8_t s = 0) {
if (telegram->read_enumvalue(value, index, s)) {
has_update_ = true;
publish_value((void *)&value);
}
}

template <typename Value>
inline void has_update(std::shared_ptr<const Telegram> telegram, Value & value, const uint8_t index, uint8_t s = 0) {
void has_update(std::shared_ptr<const Telegram> telegram, Value & value, const uint8_t index, uint8_t s = 0) {
if (telegram->read_value(value, index, s)) {
has_update_ = true;
publish_value((void *)&value);
}
}

template <typename BitValue>
inline void has_bitupdate(std::shared_ptr<const Telegram> telegram, BitValue & value, const uint8_t index, uint8_t b) {
void has_bitupdate(std::shared_ptr<const Telegram> telegram, BitValue & value, const uint8_t index, uint8_t b) {
if (telegram->read_bitvalue(value, index, b)) {
has_update_ = true;
publish_value((void *)&value);
}
}

// modbus
int get_modbus_value(uint8_t tag, const std::string & shortname, std::vector<uint16_t> & result);
int modbus_value_to_json(uint8_t tag, const std::string & shortname, const std::vector<uint8_t> & modbus_data, JsonObject jsonValue);

const char * brand_to_char();
std::string to_string();
std::string to_string_short();

enum Handlers : uint8_t { ALL, RECEIVED, FETCHED, PENDING, IGNORED };

void show_telegram_handlers(uuid::console::Shell & shell) const;
char * show_telegram_handlers(char * result, const size_t len, const uint8_t handlers);
void show_mqtt_handlers(uuid::console::Shell & shell) const;
Expand All @@ -227,10 +223,9 @@ class EMSdevice {
bool handle_telegram(std::shared_ptr<const Telegram> telegram);

std::string get_value_uom(const std::string & shortname) const;

bool get_value_info(JsonObject root, const char * cmd, const int8_t id);
void get_value_json(JsonObject output, DeviceValue & dv);
void get_dv_info(JsonObject json);
bool get_value_info(JsonObject root, const char * cmd, const int8_t id);
void get_value_json(JsonObject output, DeviceValue & dv);
void get_dv_info(JsonObject json);

enum OUTPUT_TARGET : uint8_t { API_VERBOSE, API_SHORTNAMES, MQTT, CONSOLE };
bool generate_values(JsonObject output, const int8_t tag_filter, const bool nested, const uint8_t output_target);
Expand Down Expand Up @@ -314,23 +309,20 @@ class EMSdevice {

void read_command(const uint16_t type_id, uint8_t offset = 0, uint8_t length = 0) const;

bool is_readable(const void * value_p) const;
bool is_readonly(const std::string & cmd, const int8_t id) const;
bool has_command(const void * value_p) const;
void set_minmax(const void * value_p, int16_t min, uint32_t max);
void publish_value(void * value_p) const;
void publish_all_values();

void mqtt_ha_entity_config_create();

bool is_readable(const void * value_p) const;
bool is_readonly(const std::string & cmd, const int8_t id) const;
bool has_command(const void * value_p) const;
void set_minmax(const void * value_p, int16_t min, uint32_t max);
void publish_value(void * value_p) const;
void publish_all_values();
void mqtt_ha_entity_config_create();
const char * telegram_type_name(std::shared_ptr<const Telegram> telegram);

void fetch_values();
void toggle_fetch(uint16_t telegram_id, bool toggle);
bool is_fetch(uint16_t telegram_id) const;
bool is_received(uint16_t telegram_id) const;
bool has_telegram_id(uint16_t id) const;
void ha_config_clear();
void fetch_values();
void toggle_fetch(uint16_t telegram_id, bool toggle);
bool is_fetch(uint16_t telegram_id) const;
bool is_received(uint16_t telegram_id) const;
bool has_telegram_id(uint16_t id) const;
void ha_config_clear();

bool ha_config_done() const {
return ha_config_done_;
Expand Down Expand Up @@ -502,6 +494,7 @@ class EMSdevice {
char version_[6];
const char * default_name_; // the fixed name the EMS model taken from the device library
std::string custom_name_ = ""; // custom name
std::string model_ = ""; // model, taken from the 0x01 telegram. see process_deviceName()
uint8_t flags_ = 0;
uint8_t brand_ = Brand::NO_BRAND;
bool active_ = true;
Expand Down
17 changes: 8 additions & 9 deletions src/emsesp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,14 +972,13 @@ void EMSESP::process_deviceName(std::shared_ptr<const Telegram> telegram) {
uint8_t len = telegram->offset + telegram->message_length - 27;
strlcpy(name, (const char *)&telegram->message_data[27 - telegram->offset], len < 16 ? len : 16);
if (strlen(name)) {
webCustomizationService.read([&](WebCustomization const & settings) {
for (EntityCustomization e : settings.entityCustomizations) {
if ((e.device_id == telegram->src) && e.custom_name.empty()) {
e.custom_name = name;
break;
}
LOG_DEBUG("Model name received for device 0x%02X: %s", telegram->src, name);
for (const auto & emsdevice : emsdevices) {
if (emsdevice->is_device_id(telegram->src)) {
emsdevice->model(name);
break;
}
});
}
}
}

Expand Down Expand Up @@ -1204,8 +1203,8 @@ void EMSESP::show_devices(uuid::console::Shell & shell) {
for (const auto & device_class : EMSFactory::device_handlers()) {
for (const auto & emsdevice : emsdevices) {
if (emsdevice && (emsdevice->device_type() == device_class.first)) {
shell.printf("%s: %s", emsdevice->device_type_name(), emsdevice->to_string().c_str());
shell.println();
// print header, with device type translated
shell.printfln("%s: %s (%d)", emsdevice->device_type_2_device_name_translated(), emsdevice->to_string().c_str(), emsdevice->count_entities());
emsdevice->show_telegram_handlers(shell);

#if defined(EMSESP_DEBUG)
Expand Down

0 comments on commit eed676f

Please sign in to comment.