mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-24 06:38:29 +02:00
Add callback for raw sml messages (#5668)
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user