mirror of
https://github.com/esphome/esphome.git
synced 2025-02-07 00:11:59 +01:00
fixup: remove one extra allocation
Use a callback to iterate OrbisInfo instead of returning a vector with all OrbisInfo. This way, we save one extra allocation.
This commit is contained in:
parent
6010e1c26a
commit
36ca2cc990
@ -67,31 +67,12 @@ void Sml::add_on_data_callback(std::function<void(std::vector<uint8_t>, bool)> &
|
||||
}
|
||||
|
||||
void Sml::process_sml_file_(const byte_span &sml_data) {
|
||||
SmlFile sml_file = SmlFile(sml_data);
|
||||
std::vector<ObisInfo> obis_info = sml_file.get_obis_info();
|
||||
this->publish_obis_info_(obis_info);
|
||||
|
||||
this->log_obis_info_(obis_info);
|
||||
}
|
||||
|
||||
void Sml::log_obis_info_(const std::vector<ObisInfo> &obis_info_vec) {
|
||||
ESP_LOGD(TAG, "OBIS info:");
|
||||
for (auto const &obis_info : obis_info_vec) {
|
||||
std::string info;
|
||||
info += " (" + bytes_repr(obis_info.server_id) + ") ";
|
||||
info += obis_info.code_repr();
|
||||
info += " [0x" + bytes_repr(obis_info.value) + "]";
|
||||
ESP_LOGD(TAG, "%s", info.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void Sml::publish_obis_info_(const std::vector<ObisInfo> &obis_info_vec) {
|
||||
for (auto const &obis_info : obis_info_vec) {
|
||||
this->publish_value_(obis_info);
|
||||
}
|
||||
SmlFile(sml_data).for_each_obis_info([this](const ObisInfo &obis_info) { this->publish_value_(obis_info); });
|
||||
}
|
||||
|
||||
void Sml::publish_value_(const ObisInfo &obis_info) {
|
||||
ESP_LOGD(TAG, "OBIS (%s) %s [0x%s]", bytes_repr(obis_info.server_id).c_str(), obis_info.code_repr().c_str(),
|
||||
bytes_repr(obis_info.value).c_str());
|
||||
for (auto const &sml_listener : sml_listeners_) {
|
||||
if ((!sml_listener->server_id.empty()) && (bytes_repr(obis_info.server_id) != sml_listener->server_id))
|
||||
continue;
|
||||
|
@ -28,8 +28,6 @@ class Sml : public Component, public uart::UARTDevice {
|
||||
|
||||
protected:
|
||||
void process_sml_file_(const byte_span &sml_data);
|
||||
void log_obis_info_(const std::vector<ObisInfo> &obis_info_vec);
|
||||
void publish_obis_info_(const std::vector<ObisInfo> &obis_info_vec);
|
||||
char check_start_end_bytes_(uint8_t byte);
|
||||
void publish_value_(const ObisInfo &obis_info);
|
||||
|
||||
|
@ -75,8 +75,7 @@ bool SmlFile::setup_node(std::vector<SmlNode> &nodes) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<ObisInfo> SmlFile::get_obis_info() {
|
||||
std::vector<ObisInfo> obis_info;
|
||||
void SmlFile::for_each_obis_info(const std::function<void(const ObisInfo &)> &callback) {
|
||||
for (auto const &message : messages) {
|
||||
auto message_body = message.nodes[3];
|
||||
auto message_type = bytes_to_uint(message_body.nodes[0].value_bytes);
|
||||
@ -88,10 +87,9 @@ std::vector<ObisInfo> SmlFile::get_obis_info() {
|
||||
auto val_list = get_list_response.nodes[4];
|
||||
|
||||
for (auto const &val_list_entry : val_list.nodes) {
|
||||
obis_info.emplace_back(server_id, val_list_entry);
|
||||
callback(ObisInfo(server_id, val_list_entry));
|
||||
}
|
||||
}
|
||||
return obis_info;
|
||||
}
|
||||
|
||||
std::string bytes_repr(const byte_span &buffer) {
|
||||
|
@ -41,7 +41,7 @@ class SmlFile {
|
||||
SmlFile(byte_span const &buffer);
|
||||
bool setup_node(std::vector<SmlNode> &nodes);
|
||||
std::vector<SmlNode> messages;
|
||||
std::vector<ObisInfo> get_obis_info();
|
||||
void for_each_obis_info(const std::function<void(const ObisInfo &)> &callback);
|
||||
|
||||
protected:
|
||||
const byte_span buffer_;
|
||||
|
Loading…
Reference in New Issue
Block a user