10 static const uint16_t SEND_THRESHOLD = 1024;
12 static const char *
const TAG =
"statsD";
20 source.
sin_addr.s_addr = htonl(INADDR_ANY);
21 source.
sin_port = htons(this->port_);
22 this->sock_->bind((
struct sockaddr *) &source,
sizeof(source));
25 this->destination_.
sin_port = htons(this->port_);
26 this->destination_.
sin_addr.s_addr = inet_addr(this->host_);
40 ESP_LOGCONFIG(TAG,
"statsD:");
41 ESP_LOGCONFIG(TAG,
" host: %s", this->host_);
42 ESP_LOGCONFIG(TAG,
" port: %d", this->port_);
44 ESP_LOGCONFIG(TAG,
" prefix: %s", this->prefix_);
47 ESP_LOGCONFIG(TAG,
" metrics:");
49 ESP_LOGCONFIG(TAG,
" - name: %s", s.name);
50 ESP_LOGCONFIG(TAG,
" type: %d", s.type);
62 this->sensors_.push_back(s);
66 #ifdef USE_BINARY_SENSOR 71 s.type = TYPE_BINARY_SENSOR;
72 this->sensors_.push_back(s);
78 out.reserve(SEND_THRESHOLD);
85 if (!s.sensor->has_state()) {
88 val = s.sensor->state;
91 #ifdef USE_BINARY_SENSOR 92 case TYPE_BINARY_SENSOR:
93 if (!s.binary_sensor->has_state()) {
97 if (s.binary_sensor->state) {
103 ESP_LOGE(TAG,
"type not known, name: %s type: %d", s.name, s.type);
121 if (out.length() > SEND_THRESHOLD) {
130 void StatsdComponent::send_(std::string *out) {
136 ip.fromString(this->host_);
138 this->sock_.beginPacket(ip, this->port_);
139 this->sock_.write((
const uint8_t *) out->c_str(), out->length());
140 this->sock_.endPacket();
147 int n_bytes = this->sock_->sendto(out->c_str(), out->length(), 0,
reinterpret_cast<sockaddr *
>(&this->destination_),
148 sizeof(this->destination_));
149 if (n_bytes != out->length()) {
150 ESP_LOGE(TAG,
"Failed to send UDP packed (%d of %d)", n_bytes, out->length());
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
void dump_config() override
struct { const char *name sensors_t
void register_binary_sensor(const char *name, esphome::binary_sensor::BinarySensor *binary_sensor)
std::string str_sprintf(const char *fmt,...)
float get_setup_priority() const override
esphome::binary_sensor::BinarySensor * binary_sensor
Implementation of SPI Controller mode.
Base class for all binary_sensor-type classes.
void register_sensor(const char *name, esphome::sensor::Sensor *sensor)
Base-class for all sensors.
esphome::sensor::Sensor * sensor
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.