92 ESP_LOGV(TAG,
"Read from serial: %s", buffer.substr(0, buffer.size() - 2).c_str());
105 const char *cursor = buffer.c_str();
106 char token_buf[TEXT_SENSOR_MAX_LEN] = {0};
109 auto get_token = [&](
char *token_buf) ->
void {
111 while (*cursor ==
' ' || *cursor ==
'\t') {
115 if (*cursor ==
'\0') {
120 const char *start = cursor;
123 while (*cursor !=
'\0' && *cursor !=
' ' && *cursor !=
'\t' && *cursor !=
'\r') {
127 size_t token_len = std::min(
static_cast<size_t>(cursor - start),
static_cast<size_t>(TEXT_SENSOR_MAX_LEN - 1));
128 memcpy(token_buf, start, token_len);
129 token_buf[token_len] = 0;
133 get_token(token_buf);
135 if (
val.has_value() &&
val.value() > 0) {
136 l.bat_num =
val.value();
137 }
else if (strcmp(token_buf,
"Power") == 0) {
139 this->
has_tlow_id_ = buffer.find(
"Tlow.Id") != std::string::npos;
140 ESP_LOGD(TAG,
"header line %s Tlow.Id: %s", this->
has_tlow_id_ ?
"with" :
"without",
141 buffer.substr(0, buffer.size() - 2).c_str());
144 ESP_LOGD(TAG,
"unknown line %s", buffer.substr(0, buffer.size() - 2).c_str());
148 PARSE_INT(
l.volt,
"Volt");
149 PARSE_INT(
l.curr,
"Curr");
150 PARSE_INT(
l.tempr,
"Tempr");
151 PARSE_INT(
l.tlow,
"Tlow");
153 get_token(token_buf);
155 PARSE_INT(
l.thigh,
"Thigh");
157 get_token(token_buf);
159 PARSE_INT(
l.vlow,
"Vlow");
161 get_token(token_buf);
163 PARSE_INT(
l.vhigh,
"Vhigh");
165 get_token(token_buf);
167 PARSE_STR(
l.base_st,
"Base.St");
168 PARSE_STR(
l.volt_st,
"Volt.St");
169 PARSE_STR(
l.curr_st,
"Curr.St");
170 PARSE_STR(
l.temp_st,
"Temp.St");
172 get_token(token_buf);
173 for (
char &i : token_buf) {
180 if (coul_val.has_value()) {
181 l.coulomb = coul_val.value();
183 ESP_LOGD(TAG,
"invalid Coulomb in line %s", buffer.substr(0, buffer.size() - 2).c_str());
187 get_token(token_buf);
188 get_token(token_buf);
189 get_token(token_buf);
190 get_token(token_buf);
191 PARSE_INT(
l.mostempr,
"Mostempr");
193 ESP_LOGD(TAG,
"successful line %s", buffer.substr(0, buffer.size() - 2).c_str());
196 listener->on_line_read(&
l);