SML runtime optimizations (#8571)

This commit is contained in:
Marius Greuel
2025-04-16 07:58:14 +12:00
committed by GitHub
parent bc56d319b5
commit 7e133171e0
4 changed files with 67 additions and 37 deletions
+16 -18
View File
@@ -5,17 +5,17 @@
namespace esphome {
namespace sml {
SmlFile::SmlFile(bytes buffer) : buffer_(std::move(buffer)) {
SmlFile::SmlFile(const BytesView &buffer) : buffer_(buffer) {
// extract messages
this->pos_ = 0;
while (this->pos_ < this->buffer_.size()) {
if (this->buffer_[this->pos_] == 0x00)
break; // EndOfSmlMsg
SmlNode message = SmlNode();
SmlNode message;
if (!this->setup_node(&message))
break;
this->messages.emplace_back(message);
this->messages.emplace_back(std::move(message));
}
}
@@ -62,22 +62,20 @@ bool SmlFile::setup_node(SmlNode *node) {
return false;
node->type = type;
node->nodes.clear();
node->value_bytes.clear();
if (type == SML_LIST) {
node->nodes.reserve(length);
for (size_t i = 0; i != length; i++) {
SmlNode child_node = SmlNode();
SmlNode child_node;
if (!this->setup_node(&child_node))
return false;
node->nodes.emplace_back(child_node);
node->nodes.emplace_back(std::move(child_node));
}
} else {
// Value starts at the current position
// Value ends "length" bytes later,
// (since the TL field is counted but already subtracted from length)
node->value_bytes = bytes(this->buffer_.begin() + this->pos_, this->buffer_.begin() + this->pos_ + length);
node->value_bytes = buffer_.subview(this->pos_, length);
// Increment the pointer past all consumed bytes
this->pos_ += length;
}
@@ -87,14 +85,14 @@ bool SmlFile::setup_node(SmlNode *node) {
std::vector<ObisInfo> SmlFile::get_obis_info() {
std::vector<ObisInfo> obis_info;
for (auto const &message : messages) {
SmlNode message_body = message.nodes[3];
const auto &message_body = message.nodes[3];
uint16_t message_type = bytes_to_uint(message_body.nodes[0].value_bytes);
if (message_type != SML_GET_LIST_RES)
continue;
SmlNode get_list_response = message_body.nodes[1];
bytes server_id = get_list_response.nodes[1].value_bytes;
SmlNode val_list = get_list_response.nodes[4];
const auto &get_list_response = message_body.nodes[1];
const auto &server_id = get_list_response.nodes[1].value_bytes;
const 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);
@@ -103,7 +101,7 @@ std::vector<ObisInfo> SmlFile::get_obis_info() {
return obis_info;
}
std::string bytes_repr(const bytes &buffer) {
std::string bytes_repr(const BytesView &buffer) {
std::string repr;
for (auto const value : buffer) {
repr += str_sprintf("%02x", value & 0xff);
@@ -111,7 +109,7 @@ std::string bytes_repr(const bytes &buffer) {
return repr;
}
uint64_t bytes_to_uint(const bytes &buffer) {
uint64_t bytes_to_uint(const BytesView &buffer) {
uint64_t val = 0;
for (auto const value : buffer) {
val = (val << 8) + value;
@@ -119,7 +117,7 @@ uint64_t bytes_to_uint(const bytes &buffer) {
return val;
}
int64_t bytes_to_int(const bytes &buffer) {
int64_t bytes_to_int(const BytesView &buffer) {
uint64_t tmp = bytes_to_uint(buffer);
int64_t val;
@@ -135,14 +133,14 @@ int64_t bytes_to_int(const bytes &buffer) {
return val;
}
std::string bytes_to_string(const bytes &buffer) { return std::string(buffer.begin(), buffer.end()); }
std::string bytes_to_string(const BytesView &buffer) { return std::string(buffer.begin(), buffer.end()); }
ObisInfo::ObisInfo(bytes server_id, SmlNode val_list_entry) : server_id(std::move(server_id)) {
ObisInfo::ObisInfo(const BytesView &server_id, const SmlNode &val_list_entry) : server_id(server_id) {
this->code = val_list_entry.nodes[0].value_bytes;
this->status = val_list_entry.nodes[1].value_bytes;
this->unit = bytes_to_uint(val_list_entry.nodes[3].value_bytes);
this->scaler = bytes_to_int(val_list_entry.nodes[4].value_bytes);
SmlNode value_node = val_list_entry.nodes[5];
const auto &value_node = val_list_entry.nodes[5];
this->value = value_node.value_bytes;
this->value_type = value_node.type;
}