mirror of
https://github.com/esphome/esphome.git
synced 2024-11-14 02:58:11 +01:00
pylontech: Fix parsing error with US2000 (#6061)
This commit is contained in:
parent
da56d333dc
commit
33051906bd
2 changed files with 36 additions and 23 deletions
|
@ -19,7 +19,7 @@ PylontechComponent = pylontech_ns.class_(
|
||||||
)
|
)
|
||||||
PylontechBattery = pylontech_ns.class_("PylontechBattery")
|
PylontechBattery = pylontech_ns.class_("PylontechBattery")
|
||||||
|
|
||||||
CV_NUM_BATTERIES = cv.int_range(1, 6)
|
CV_NUM_BATTERIES = cv.int_range(1, 16)
|
||||||
|
|
||||||
PYLONTECH_COMPONENT_SCHEMA = cv.Schema(
|
PYLONTECH_COMPONENT_SCHEMA = cv.Schema(
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "pylontech.h"
|
#include "pylontech.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace pylontech {
|
namespace pylontech {
|
||||||
|
@ -34,26 +35,30 @@ void PylontechComponent::setup() {
|
||||||
void PylontechComponent::update() { this->write_str("pwr\n"); }
|
void PylontechComponent::update() { this->write_str("pwr\n"); }
|
||||||
|
|
||||||
void PylontechComponent::loop() {
|
void PylontechComponent::loop() {
|
||||||
uint8_t data;
|
if (this->available() > 0) {
|
||||||
|
// pylontech sends a lot of data very suddenly
|
||||||
// pylontech sends a lot of data very suddenly
|
// we need to quickly put it all into our own buffer, otherwise the uart's buffer will overflow
|
||||||
// we need to quickly put it all into our own buffer, otherwise the uart's buffer will overflow
|
uint8_t data;
|
||||||
while (this->available() > 0) {
|
int recv = 0;
|
||||||
if (this->read_byte(&data)) {
|
while (this->available() > 0) {
|
||||||
buffer_[buffer_index_write_] += (char) data;
|
if (this->read_byte(&data)) {
|
||||||
if (buffer_[buffer_index_write_].back() == static_cast<char>(ASCII_LF) ||
|
buffer_[buffer_index_write_] += (char) data;
|
||||||
buffer_[buffer_index_write_].length() >= MAX_DATA_LENGTH_BYTES) {
|
recv++;
|
||||||
// complete line received
|
if (buffer_[buffer_index_write_].back() == static_cast<char>(ASCII_LF) ||
|
||||||
buffer_index_write_ = (buffer_index_write_ + 1) % NUM_BUFFERS;
|
buffer_[buffer_index_write_].length() >= MAX_DATA_LENGTH_BYTES) {
|
||||||
|
// complete line received
|
||||||
|
buffer_index_write_ = (buffer_index_write_ + 1) % NUM_BUFFERS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
ESP_LOGV(TAG, "received %d bytes", recv);
|
||||||
|
} else {
|
||||||
// only process one line per call of loop() to not block esphome for too long
|
// only process one line per call of loop() to not block esphome for too long
|
||||||
if (buffer_index_read_ != buffer_index_write_) {
|
if (buffer_index_read_ != buffer_index_write_) {
|
||||||
this->process_line_(buffer_[buffer_index_read_]);
|
this->process_line_(buffer_[buffer_index_read_]);
|
||||||
buffer_[buffer_index_read_].clear();
|
buffer_[buffer_index_read_].clear();
|
||||||
buffer_index_read_ = (buffer_index_read_ + 1) % NUM_BUFFERS;
|
buffer_index_read_ = (buffer_index_read_ + 1) % NUM_BUFFERS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,10 +71,11 @@ void PylontechComponent::process_line_(std::string &buffer) {
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
PylontechListener::LineContents l{};
|
PylontechListener::LineContents l{};
|
||||||
const int parsed = sscanf( // NOLINT
|
char mostempr_s[6];
|
||||||
buffer.c_str(), "%d %d %d %d %d %d %d %d %7s %7s %7s %7s %d%% %*d-%*d-%*d %*d:%*d:%*d %*s %*s %d %*s", // NOLINT
|
const int parsed = sscanf( // NOLINT
|
||||||
&l.bat_num, &l.volt, &l.curr, &l.tempr, &l.tlow, &l.thigh, &l.vlow, &l.vhigh, l.base_st, l.volt_st, // NOLINT
|
buffer.c_str(), "%d %d %d %d %d %d %d %d %7s %7s %7s %7s %d%% %*d-%*d-%*d %*d:%*d:%*d %*s %*s %5s %*s", // NOLINT
|
||||||
l.curr_st, l.temp_st, &l.coulomb, &l.mostempr); // NOLINT
|
&l.bat_num, &l.volt, &l.curr, &l.tempr, &l.tlow, &l.thigh, &l.vlow, &l.vhigh, l.base_st, l.volt_st, // NOLINT
|
||||||
|
l.curr_st, l.temp_st, &l.coulomb, mostempr_s); // NOLINT
|
||||||
|
|
||||||
if (l.bat_num <= 0) {
|
if (l.bat_num <= 0) {
|
||||||
ESP_LOGD(TAG, "invalid bat_num in line %s", buffer.substr(0, buffer.size() - 2).c_str());
|
ESP_LOGD(TAG, "invalid bat_num in line %s", buffer.substr(0, buffer.size() - 2).c_str());
|
||||||
|
@ -79,6 +85,13 @@ void PylontechComponent::process_line_(std::string &buffer) {
|
||||||
ESP_LOGW(TAG, "invalid line: found only %d items in %s", parsed, buffer.substr(0, buffer.size() - 2).c_str());
|
ESP_LOGW(TAG, "invalid line: found only %d items in %s", parsed, buffer.substr(0, buffer.size() - 2).c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
auto mostempr_parsed = parse_number<int>(mostempr_s);
|
||||||
|
if (mostempr_parsed.has_value()) {
|
||||||
|
l.mostempr = mostempr_parsed.value();
|
||||||
|
} else {
|
||||||
|
l.mostempr = -300;
|
||||||
|
ESP_LOGW(TAG, "bat_num %d: received no mostempr", l.bat_num);
|
||||||
|
}
|
||||||
|
|
||||||
for (PylontechListener *listener : this->listeners_) {
|
for (PylontechListener *listener : this->listeners_) {
|
||||||
listener->on_line_read(&l);
|
listener->on_line_read(&l);
|
||||||
|
|
Loading…
Reference in a new issue