mirror of
https://github.com/esphome/esphome.git
synced 2026-01-15 14:37:43 -07:00
Compare commits
2 Commits
sml_sprint
...
statsd_sta
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ea5d7abff | ||
|
|
03f3deff41 |
@@ -413,6 +413,7 @@ class TextValidator(LValidator):
|
||||
str_args = [str(x) for x in value[CONF_ARGS]]
|
||||
arg_expr = cg.RawExpression(",".join(str_args))
|
||||
format_str = cpp_string_escape(format_str)
|
||||
# str_sprintf justified: user-defined format, can't optimize without permanent RAM cost
|
||||
sprintf_str = f"str_sprintf({format_str}, {arg_expr}).c_str()"
|
||||
if nanval := value.get(CONF_IF_NAN):
|
||||
nanval = cpp_string_escape(nanval)
|
||||
|
||||
@@ -65,7 +65,10 @@ std::string lv_event_code_name_for(uint8_t event_code) {
|
||||
if (event_code < sizeof(EVENT_NAMES) / sizeof(EVENT_NAMES[0])) {
|
||||
return EVENT_NAMES[event_code];
|
||||
}
|
||||
return str_sprintf("%2d", event_code);
|
||||
// max 4 bytes: "%u" with uint8_t (max 255, 3 digits) + null
|
||||
char buf[4];
|
||||
snprintf(buf, sizeof(buf), "%u", event_code);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static void rounder_cb(lv_disp_drv_t *disp_drv, lv_area_t *area) {
|
||||
|
||||
@@ -104,10 +104,7 @@ std::vector<ObisInfo> SmlFile::get_obis_info() {
|
||||
std::string bytes_repr(const BytesView &buffer) {
|
||||
std::string repr;
|
||||
for (auto const value : buffer) {
|
||||
// max 3: 2 hex digits + null
|
||||
char hex_buf[3];
|
||||
snprintf(hex_buf, sizeof(hex_buf), "%02x", static_cast<unsigned int>(value));
|
||||
repr += hex_buf;
|
||||
repr += str_sprintf("%02x", value & 0xff);
|
||||
}
|
||||
return repr;
|
||||
}
|
||||
@@ -149,11 +146,7 @@ ObisInfo::ObisInfo(const BytesView &server_id, const SmlNode &val_list_entry) :
|
||||
}
|
||||
|
||||
std::string ObisInfo::code_repr() const {
|
||||
// max 20: "255-255:255.255.255" (19 chars) + null
|
||||
char buf[20];
|
||||
snprintf(buf, sizeof(buf), "%d-%d:%d.%d.%d", this->code[0], this->code[1], this->code[2], this->code[3],
|
||||
this->code[4]);
|
||||
return buf;
|
||||
return str_sprintf("%d-%d:%d.%d.%d", this->code[0], this->code[1], this->code[2], this->code[3], this->code[4]);
|
||||
}
|
||||
|
||||
} // namespace sml
|
||||
|
||||
@@ -114,14 +114,23 @@ void StatsdComponent::update() {
|
||||
// This implies you can't explicitly set a gauge to a negative number without first setting it to zero.
|
||||
if (val < 0) {
|
||||
if (this->prefix_) {
|
||||
out.append(str_sprintf("%s.", this->prefix_));
|
||||
out.append(this->prefix_);
|
||||
out.append(".");
|
||||
}
|
||||
out.append(str_sprintf("%s:0|g\n", s.name));
|
||||
out.append(s.name);
|
||||
out.append(":0|g\n");
|
||||
}
|
||||
if (this->prefix_) {
|
||||
out.append(str_sprintf("%s.", this->prefix_));
|
||||
out.append(this->prefix_);
|
||||
out.append(".");
|
||||
}
|
||||
out.append(str_sprintf("%s:%f|g\n", s.name, val));
|
||||
out.append(s.name);
|
||||
// Buffer for ":" + value + "|g\n".
|
||||
// %g uses max 13 chars for value (sign + 6 significant digits + e+xxx)
|
||||
// Total: 1 + 13 + 4 = 18 chars + null, use 24 for safety
|
||||
char val_buf[24];
|
||||
snprintf(val_buf, sizeof(val_buf), ":%g|g\n", val);
|
||||
out.append(val_buf);
|
||||
|
||||
if (out.length() > SEND_THRESHOLD) {
|
||||
this->send_(&out);
|
||||
|
||||
Reference in New Issue
Block a user