5#if defined(USE_NETWORK) && !defined(USE_ZEPHYR)
11static const uint16_t SEND_THRESHOLD = 1024;
18 this->
mark_failed(LOG_STR(
"Could not create socket"));
22 this->destination_.sin_family = AF_INET;
23 this->destination_.sin_port = htons(this->port_);
24 this->destination_.sin_addr.s_addr = inet_addr(this->host_);
32 this->host_, this->port_);
34 ESP_LOGCONFIG(TAG,
" prefix: %s", this->prefix_);
37 ESP_LOGCONFIG(TAG,
" metrics:");
38 for (sensors_t s : this->sensors_) {
54 this->sensors_.push_back(s);
58#ifdef USE_BINARY_SENSOR
62 s.binary_sensor = binary_sensor;
63 s.type = TYPE_BINARY_SENSOR;
64 this->sensors_.push_back(s);
70 out.reserve(SEND_THRESHOLD);
72 for (sensors_t s : this->sensors_) {
77 if (!s.sensor->has_state()) {
80 val = s.sensor->state;
83#ifdef USE_BINARY_SENSOR
84 case TYPE_BINARY_SENSOR:
85 if (!s.binary_sensor->has_state()) {
89 if (s.binary_sensor->state) {
95 ESP_LOGE(TAG,
"type not known, name: %s type: %d", s.name, s.type);
104 out.append(this->prefix_);
108 out.append(
":0|g\n");
111 out.append(this->prefix_);
118 buf_append_printf(val_buf,
sizeof(val_buf), 0,
":%f|g\n",
val);
121 if (
out.length() > SEND_THRESHOLD) {
130void StatsdComponent::send_(std::string *
out) {
131 if (
out->empty() || !this->sock_) {
135 int n_bytes = this->sock_->sendto(
out->c_str(),
out->length(), 0,
reinterpret_cast<sockaddr *
>(&this->destination_),
136 sizeof(this->destination_));
137 if (n_bytes !=
static_cast<int>(
out->length())) {
138 ESP_LOGE(TAG,
"Failed to send UDP packed (%d of %d)", n_bytes,
out->length());
void mark_failed()
Mark this component as failed.
Base class for all binary_sensor-type classes.
Base-class for all sensors.
void register_sensor(const char *name, esphome::sensor::Sensor *sensor)
void register_binary_sensor(const char *name, esphome::binary_sensor::BinarySensor *binary_sensor)
float get_setup_priority() const override
void dump_config() override
std::vector< uint8_t > & out
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
std::unique_ptr< UDPSendSocket > socket_udp_send(int domain, int protocol)
Create a send-only UDP socket (socket_udp_send), a UDP socket with receive support (socket_udp),...
ESPHOME_LOG_TAG(TAG, "statsD")