mirror of
https://github.com/esphome/esphome.git
synced 2024-11-12 10:16:02 +01:00
Add callback for raw sml messages (#5668)
This commit is contained in:
parent
4ca9aefc43
commit
3ee85d7516
@ -1,9 +1,10 @@
|
||||
import re
|
||||
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import uart
|
||||
from esphome.const import CONF_ID
|
||||
from esphome.const import CONF_ID, CONF_TRIGGER_ID
|
||||
|
||||
CODEOWNERS = ["@alengwenus"]
|
||||
|
||||
@ -16,10 +17,26 @@ MULTI_CONF = True
|
||||
CONF_SML_ID = "sml_id"
|
||||
CONF_OBIS_CODE = "obis_code"
|
||||
CONF_SERVER_ID = "server_id"
|
||||
CONF_ON_DATA = "on_data"
|
||||
|
||||
sml_ns = cg.esphome_ns.namespace("sml")
|
||||
|
||||
DataTrigger = sml_ns.class_(
|
||||
"DataTrigger",
|
||||
automation.Trigger.template(
|
||||
cg.std_vector.template(cg.uint8).operator("ref"), cg.bool_
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(Sml),
|
||||
cv.Optional(CONF_ON_DATA): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(DataTrigger),
|
||||
}
|
||||
),
|
||||
}
|
||||
).extend(uart.UART_DEVICE_SCHEMA)
|
||||
|
||||
@ -28,6 +45,19 @@ async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await uart.register_uart_device(var, config)
|
||||
for conf in config.get(CONF_ON_DATA, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger,
|
||||
[
|
||||
(
|
||||
cg.std_vector.template(cg.uint8).operator("ref").operator("const"),
|
||||
"bytes",
|
||||
),
|
||||
(cg.bool_, "valid"),
|
||||
],
|
||||
conf,
|
||||
)
|
||||
|
||||
|
||||
def obis_code(value):
|
||||
|
19
esphome/components/sml/automation.h
Normal file
19
esphome/components/sml/automation.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "sml.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace sml {
|
||||
|
||||
class DataTrigger : public Trigger<const std::vector<uint8_t> &, bool> {
|
||||
public:
|
||||
explicit DataTrigger(Sml *sml) {
|
||||
sml->add_on_data_callback([this](const std::vector<uint8_t> &data, bool valid) { this->trigger(data, valid); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sml
|
||||
} // namespace esphome
|
@ -18,8 +18,10 @@ enum SmlType : uint8_t {
|
||||
enum SmlMessageType : uint16_t { SML_PUBLIC_OPEN_RES = 0x0101, SML_GET_LIST_RES = 0x701 };
|
||||
|
||||
// masks with two-bit mapping 0x1b -> 0b01; 0x01 -> 0b10; 0x1a -> 0b11
|
||||
const uint16_t START_MASK = 0x55aa; // 0x1b 1b 1b 1b 1b 01 01 01 01
|
||||
const uint16_t START_MASK = 0x55aa; // 0x1b 1b 1b 1b 01 01 01 01
|
||||
const uint16_t END_MASK = 0x0157; // 0x1b 1b 1b 1b 1a
|
||||
|
||||
const std::vector<uint8_t> START_SEQ = {0x1b, 0x1b, 0x1b, 0x1b, 0x01, 0x01, 0x01, 0x01};
|
||||
|
||||
} // namespace sml
|
||||
} // namespace esphome
|
||||
|
@ -35,16 +35,24 @@ void Sml::loop() {
|
||||
case START_BYTES_DETECTED: {
|
||||
this->record_ = true;
|
||||
this->sml_data_.clear();
|
||||
// add start sequence (for callbacks)
|
||||
this->sml_data_.insert(this->sml_data_.begin(), START_SEQ.begin(), START_SEQ.end());
|
||||
break;
|
||||
};
|
||||
case END_BYTES_DETECTED: {
|
||||
if (this->record_) {
|
||||
this->record_ = false;
|
||||
|
||||
if (!check_sml_data(this->sml_data_))
|
||||
bool valid = check_sml_data(this->sml_data_);
|
||||
|
||||
// call callbacks
|
||||
this->data_callbacks_.call(this->sml_data_, valid);
|
||||
|
||||
if (!valid)
|
||||
break;
|
||||
|
||||
// remove footer bytes
|
||||
// remove start/end sequence
|
||||
this->sml_data_.erase(this->sml_data_.begin(), this->sml_data_.begin() + START_SEQ.size());
|
||||
this->sml_data_.resize(this->sml_data_.size() - 8);
|
||||
this->process_sml_file_(this->sml_data_);
|
||||
}
|
||||
@ -54,6 +62,10 @@ void Sml::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
void Sml::add_on_data_callback(std::function<void(std::vector<uint8_t>, bool)> &&callback) {
|
||||
this->data_callbacks_.add(std::move(callback));
|
||||
}
|
||||
|
||||
void Sml::process_sml_file_(const bytes &sml_data) {
|
||||
SmlFile sml_file = SmlFile(sml_data);
|
||||
std::vector<ObisInfo> obis_info = sml_file.get_obis_info();
|
||||
@ -100,14 +112,14 @@ bool check_sml_data(const bytes &buffer) {
|
||||
}
|
||||
|
||||
uint16_t crc_received = (buffer.at(buffer.size() - 2) << 8) | buffer.at(buffer.size() - 1);
|
||||
uint16_t crc_calculated = crc16(buffer.data(), buffer.size() - 2, 0x6e23, 0x8408, true, true);
|
||||
uint16_t crc_calculated = crc16(buffer.data() + 8, buffer.size() - 10, 0x6e23, 0x8408, true, true);
|
||||
crc_calculated = (crc_calculated >> 8) | (crc_calculated << 8);
|
||||
if (crc_received == crc_calculated) {
|
||||
ESP_LOGV(TAG, "Checksum verification successful with CRC16/X25.");
|
||||
return true;
|
||||
}
|
||||
|
||||
crc_calculated = crc16(buffer.data(), buffer.size() - 2, 0xed50, 0x8408);
|
||||
crc_calculated = crc16(buffer.data() + 8, buffer.size() - 10, 0xed50, 0x8408);
|
||||
if (crc_received == crc_calculated) {
|
||||
ESP_LOGV(TAG, "Checksum verification successful with CRC16/KERMIT.");
|
||||
return true;
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "sml_parser.h"
|
||||
|
||||
@ -23,6 +24,7 @@ class Sml : public Component, public uart::UARTDevice {
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
std::vector<SmlListener *> sml_listeners_{};
|
||||
void add_on_data_callback(std::function<void(std::vector<uint8_t>, bool)> &&callback);
|
||||
|
||||
protected:
|
||||
void process_sml_file_(const bytes &sml_data);
|
||||
@ -35,6 +37,8 @@ class Sml : public Component, public uart::UARTDevice {
|
||||
bool record_ = false;
|
||||
uint16_t incoming_mask_ = 0;
|
||||
bytes sml_data_;
|
||||
|
||||
CallbackManager<void(const std::vector<uint8_t> &, bool)> data_callbacks_{};
|
||||
};
|
||||
|
||||
bool check_sml_data(const bytes &buffer);
|
||||
|
Loading…
Reference in New Issue
Block a user