11static const uint16_t SEND_THRESHOLD = 1024;
13static const char *
const TAG =
"statsD";
21 source.
sin_addr.s_addr = htonl(INADDR_ANY);
22 source.
sin_port = htons(this->port_);
23 this->sock_->bind((
struct sockaddr *) &source,
sizeof(source));
26 this->destination_.
sin_port = htons(this->port_);
27 this->destination_.
sin_addr.s_addr = inet_addr(this->host_);
45 this->host_, this->port_);
47 ESP_LOGCONFIG(TAG,
" prefix: %s", this->prefix_);
50 ESP_LOGCONFIG(TAG,
" metrics:");
67 this->sensors_.push_back(s);
71#ifdef USE_BINARY_SENSOR
75 s.binary_sensor = binary_sensor;
76 s.type = TYPE_BINARY_SENSOR;
77 this->sensors_.push_back(s);
83 out.reserve(SEND_THRESHOLD);
90 if (!s.sensor->has_state()) {
93 val = s.sensor->state;
96#ifdef USE_BINARY_SENSOR
97 case TYPE_BINARY_SENSOR:
98 if (!s.binary_sensor->has_state()) {
102 if (s.binary_sensor->state) {
108 ESP_LOGE(TAG,
"type not known, name: %s type: %d", s.name, s.type);
126 if (out.length() > SEND_THRESHOLD) {
135void StatsdComponent::send_(std::string *out) {
141 ip.fromString(this->host_);
143 this->sock_.beginPacket(ip, this->port_);
144 this->sock_.write((
const uint8_t *) out->c_str(), out->length());
145 this->sock_.endPacket();
152 int n_bytes = this->sock_->sendto(out->c_str(), out->length(), 0,
reinterpret_cast<sockaddr *
>(&this->destination_),
153 sizeof(this->destination_));
154 if (n_bytes != out->length()) {
155 ESP_LOGE(TAG,
"Failed to send UDP packed (%d of %d)", n_bytes, out->length());
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
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.
struct { const char *name; sensor_type_t type; union { #ifdef USE_SENSOR esphome::sensor::Sensor *sensor;#endif #ifdef USE_BINARY_SENSOR esphome::binary_sensor::BinarySensor *binary_sensor;#endif };} sensors_t
Providing packet encoding functions for exchanging data with a remote host.
std::string str_sprintf(const char *fmt,...)