93 ESP_LOGV(TAG,
"Read from serial: %s", buffer.substr(0, buffer.size() - 2).c_str());
106 const char *cursor = buffer.c_str();
107 char token_buf[TEXT_SENSOR_MAX_LEN] = {0};
110 auto get_token = [&](
char *token_buf) ->
void {
112 while (*cursor ==
' ' || *cursor ==
'\t') {
116 if (*cursor ==
'\0') {
121 const char *start = cursor;
124 while (*cursor !=
'\0' && *cursor !=
' ' && *cursor !=
'\t' && *cursor !=
'\r') {
128 size_t token_len = std::min(
static_cast<size_t>(cursor - start),
static_cast<size_t>(TEXT_SENSOR_MAX_LEN - 1));
129 memcpy(token_buf, start, token_len);
130 token_buf[token_len] = 0;
134 get_token(token_buf);
136 if (
val.has_value() &&
val.value() > 0) {
137 l.bat_num =
val.value();
138 }
else if (strcmp(token_buf,
"Power") == 0) {
140 this->
has_tlow_id_ = buffer.find(
"Tlow.Id") != std::string::npos;
141 ESP_LOGD(TAG,
"header line %s Tlow.Id: %s", this->
has_tlow_id_ ?
"with" :
"without",
142 buffer.substr(0, buffer.size() - 2).c_str());
145 ESP_LOGD(TAG,
"unknown line %s", buffer.substr(0, buffer.size() - 2).c_str());
149 PARSE_INT(
l.volt,
"Volt");
150 PARSE_INT(
l.curr,
"Curr");
151 PARSE_INT(
l.tempr,
"Tempr");
152 PARSE_INT(
l.tlow,
"Tlow");
154 get_token(token_buf);
156 PARSE_INT(
l.thigh,
"Thigh");
158 get_token(token_buf);
160 PARSE_INT(
l.vlow,
"Vlow");
162 get_token(token_buf);
164 PARSE_INT(
l.vhigh,
"Vhigh");
166 get_token(token_buf);
168 PARSE_STR(
l.base_st,
"Base.St");
169 PARSE_STR(
l.volt_st,
"Volt.St");
170 PARSE_STR(
l.curr_st,
"Curr.St");
171 PARSE_STR(
l.temp_st,
"Temp.St");
173 get_token(token_buf);
174 for (
char &i : token_buf) {
181 if (coul_val.has_value()) {
182 l.coulomb = coul_val.value();
184 ESP_LOGD(TAG,
"invalid Coulomb in line %s", buffer.substr(0, buffer.size() - 2).c_str());
188 get_token(token_buf);
189 get_token(token_buf);
190 get_token(token_buf);
191 get_token(token_buf);
192 PARSE_INT(
l.mostempr,
"Mostempr");
194 ESP_LOGD(TAG,
"successful line %s", buffer.substr(0, buffer.size() - 2).c_str());
197 listener->on_line_read(&
l);