11 static const uint16_t SEND_THRESHOLD = 1024;
13 static 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_);
41 ESP_LOGCONFIG(TAG,
"statsD:");
42 ESP_LOGCONFIG(TAG,
" host: %s", this->host_);
43 ESP_LOGCONFIG(TAG,
" port: %d", this->port_);
45 ESP_LOGCONFIG(TAG,
" prefix: %s", this->prefix_);
48 ESP_LOGCONFIG(TAG,
" metrics:");
50 ESP_LOGCONFIG(TAG,
" - name: %s", s.name);
51 ESP_LOGCONFIG(TAG,
" type: %d", s.type);
63 this->sensors_.push_back(s);
67 #ifdef USE_BINARY_SENSOR 72 s.type = TYPE_BINARY_SENSOR;
73 this->sensors_.push_back(s);
79 out.reserve(SEND_THRESHOLD);
86 if (!s.sensor->has_state()) {
89 val = s.sensor->state;
92 #ifdef USE_BINARY_SENSOR 93 case TYPE_BINARY_SENSOR:
94 if (!s.binary_sensor->has_state()) {
98 if (s.binary_sensor->state) {
104 ESP_LOGE(TAG,
"type not known, name: %s type: %d", s.name, s.type);
122 if (out.length() > SEND_THRESHOLD) {
131 void StatsdComponent::send_(std::string *out) {
137 ip.fromString(this->host_);
139 this->sock_.beginPacket(ip, this->port_);
140 this->sock_.write((
const uint8_t *) out->c_str(), out->length());
141 this->sock_.endPacket();
148 int n_bytes = this->sock_->sendto(out->c_str(), out->length(), 0,
reinterpret_cast<sockaddr *
>(&this->destination_),
149 sizeof(this->destination_));
150 if (n_bytes != out->length()) {
151 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.