mirror of
https://github.com/esphome/esphome.git
synced 2026-10-14 02:02:51 +00:00
[statsd] Use the socket UDP API on ESP8266 and RP2040 (#20557)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import binary_sensor, sensor
|
||||
from esphome.components import binary_sensor, sensor, socket
|
||||
from esphome.components.const import CONF_HOST
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
@@ -35,16 +35,21 @@ CONFIG_BINARY_SENSORS_SCHEMA = cv.Schema(
|
||||
}
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(StatsdComponent),
|
||||
cv.Required(CONF_HOST): cv.string_strict,
|
||||
cv.Optional(CONF_PORT, default=8125): cv.port,
|
||||
cv.Optional(CONF_PREFIX, default=""): cv.string_strict,
|
||||
cv.Optional(CONF_SENSORS): cv.ensure_list(CONFIG_SENSORS_SCHEMA),
|
||||
cv.Optional(CONF_BINARY_SENSORS): cv.ensure_list(CONFIG_BINARY_SENSORS_SCHEMA),
|
||||
}
|
||||
).extend(cv.polling_component_schema("10s"))
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(StatsdComponent),
|
||||
cv.Required(CONF_HOST): cv.string_strict,
|
||||
cv.Optional(CONF_PORT, default=8125): cv.port,
|
||||
cv.Optional(CONF_PREFIX, default=""): cv.string_strict,
|
||||
cv.Optional(CONF_SENSORS): cv.ensure_list(CONFIG_SENSORS_SCHEMA),
|
||||
cv.Optional(CONF_BINARY_SENSORS): cv.ensure_list(
|
||||
CONFIG_BINARY_SENSORS_SCHEMA
|
||||
),
|
||||
}
|
||||
).extend(cv.polling_component_schema("10s")),
|
||||
socket.consume_sockets(1, "statsd", socket.SocketType.UDP),
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
|
||||
@@ -13,28 +13,15 @@ static const uint16_t SEND_THRESHOLD = 1024;
|
||||
ESPHOME_LOG_TAG(TAG, "statsD");
|
||||
|
||||
void StatsdComponent::setup() {
|
||||
#ifndef USE_ESP8266
|
||||
this->sock_ = esphome::socket::socket(AF_INET, SOCK_DGRAM, 0);
|
||||
|
||||
struct sockaddr_in source;
|
||||
source.sin_family = AF_INET;
|
||||
source.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
source.sin_port = htons(this->port_);
|
||||
this->sock_->bind((struct sockaddr *) &source, sizeof(source));
|
||||
this->sock_ = esphome::socket::socket_udp_send(AF_INET, 0);
|
||||
if (!this->sock_) {
|
||||
this->mark_failed(LOG_STR("Could not create socket"));
|
||||
return;
|
||||
}
|
||||
|
||||
this->destination_.sin_family = AF_INET;
|
||||
this->destination_.sin_port = htons(this->port_);
|
||||
this->destination_.sin_addr.s_addr = inet_addr(this->host_);
|
||||
#endif
|
||||
}
|
||||
|
||||
StatsdComponent::~StatsdComponent() {
|
||||
#ifndef USE_ESP8266
|
||||
if (!this->sock_) {
|
||||
return;
|
||||
}
|
||||
this->sock_->close();
|
||||
#endif
|
||||
}
|
||||
|
||||
void StatsdComponent::dump_config() {
|
||||
@@ -141,19 +128,7 @@ void StatsdComponent::update() {
|
||||
}
|
||||
|
||||
void StatsdComponent::send_(std::string *out) {
|
||||
if (out->empty()) {
|
||||
return;
|
||||
}
|
||||
#ifdef USE_ESP8266
|
||||
IPAddress ip;
|
||||
ip.fromString(this->host_);
|
||||
|
||||
this->sock_.beginPacket(ip, this->port_);
|
||||
this->sock_.write((const uint8_t *) out->c_str(), out->length());
|
||||
this->sock_.endPacket();
|
||||
|
||||
#else
|
||||
if (!this->sock_) {
|
||||
if (out->empty() || !this->sock_) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -162,7 +137,6 @@ void StatsdComponent::send_(std::string *out) {
|
||||
if (n_bytes != static_cast<int>(out->length())) {
|
||||
ESP_LOGE(TAG, "Failed to send UDP packed (%d of %d)", n_bytes, out->length());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace esphome::statsd
|
||||
|
||||
@@ -20,17 +20,10 @@
|
||||
#include "esphome/components/logger/logger.h"
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
#include "WiFiUdp.h"
|
||||
#include "IPAddress.h"
|
||||
#endif
|
||||
|
||||
namespace esphome::statsd {
|
||||
|
||||
class StatsdComponent final : public PollingComponent {
|
||||
public:
|
||||
~StatsdComponent();
|
||||
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
void update() override;
|
||||
@@ -71,12 +64,8 @@ class StatsdComponent final : public PollingComponent {
|
||||
|
||||
std::vector<sensors_t> sensors_;
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
WiFiUDP sock_;
|
||||
#else
|
||||
std::unique_ptr<esphome::socket::Socket> sock_;
|
||||
struct sockaddr_in destination_;
|
||||
#endif
|
||||
std::unique_ptr<esphome::socket::UDPSendSocket> sock_;
|
||||
struct sockaddr_in destination_ {};
|
||||
|
||||
void send_(std::string *out);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user