mirror of
https://github.com/esphome/esphome.git
synced 2025-02-20 20:08:20 +00:00
[modbus_controller] Remove stream
dependency (#8244)
This commit is contained in:
parent
e9a537784e
commit
be5639faf1
@ -1,8 +1,6 @@
|
|||||||
|
|
||||||
#include "modbus_textsensor.h"
|
#include "modbus_textsensor.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
#include <iomanip>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace modbus_controller {
|
namespace modbus_controller {
|
||||||
@ -12,20 +10,17 @@ static const char *const TAG = "modbus_controller.text_sensor";
|
|||||||
void ModbusTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Modbus Controller Text Sensor", this); }
|
void ModbusTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Modbus Controller Text Sensor", this); }
|
||||||
|
|
||||||
void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) {
|
void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) {
|
||||||
std::ostringstream output;
|
std::string output_str{};
|
||||||
uint8_t items_left = this->response_bytes;
|
uint8_t items_left = this->response_bytes;
|
||||||
uint8_t index = this->offset;
|
uint8_t index = this->offset;
|
||||||
char buffer[5];
|
|
||||||
while ((items_left > 0) && index < data.size()) {
|
while ((items_left > 0) && index < data.size()) {
|
||||||
uint8_t b = data[index];
|
uint8_t b = data[index];
|
||||||
switch (this->encode_) {
|
switch (this->encode_) {
|
||||||
case RawEncoding::HEXBYTES:
|
case RawEncoding::HEXBYTES:
|
||||||
sprintf(buffer, "%02x", b);
|
output_str += str_snprintf("%02x", 2, b);
|
||||||
output << buffer;
|
|
||||||
break;
|
break;
|
||||||
case RawEncoding::COMMA:
|
case RawEncoding::COMMA:
|
||||||
sprintf(buffer, index != this->offset ? ",%d" : "%d", b);
|
output_str += str_sprintf(index != this->offset ? ",%d" : "%d", b);
|
||||||
output << buffer;
|
|
||||||
break;
|
break;
|
||||||
case RawEncoding::ANSI:
|
case RawEncoding::ANSI:
|
||||||
if (b < 0x20)
|
if (b < 0x20)
|
||||||
@ -33,25 +28,24 @@ void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) {
|
|||||||
// FALLTHROUGH
|
// FALLTHROUGH
|
||||||
// Anything else no encoding
|
// Anything else no encoding
|
||||||
default:
|
default:
|
||||||
output << (char) b;
|
output_str += (char) b;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
items_left--;
|
items_left--;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = output.str();
|
|
||||||
// Is there a lambda registered
|
// Is there a lambda registered
|
||||||
// call it with the pre converted value and the raw data array
|
// call it with the pre converted value and the raw data array
|
||||||
if (this->transform_func_.has_value()) {
|
if (this->transform_func_.has_value()) {
|
||||||
// the lambda can parse the response itself
|
// the lambda can parse the response itself
|
||||||
auto val = (*this->transform_func_)(this, result, data);
|
auto val = (*this->transform_func_)(this, output_str, data);
|
||||||
if (val.has_value()) {
|
if (val.has_value()) {
|
||||||
ESP_LOGV(TAG, "Value overwritten by lambda");
|
ESP_LOGV(TAG, "Value overwritten by lambda");
|
||||||
result = val.value();
|
output_str = val.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->publish_state(result);
|
this->publish_state(output_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace modbus_controller
|
} // namespace modbus_controller
|
||||||
|
Loading…
x
Reference in New Issue
Block a user