mirror of
https://github.com/esphome/esphome.git
synced 2026-05-30 23:54:04 +08:00
[ethernet_info] Eliminate heap allocations in DNS text sensor (#12756)
This commit is contained in:
@@ -3,8 +3,7 @@
|
|||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome::ethernet_info {
|
||||||
namespace ethernet_info {
|
|
||||||
|
|
||||||
static const char *const TAG = "ethernet_info";
|
static const char *const TAG = "ethernet_info";
|
||||||
|
|
||||||
@@ -12,7 +11,6 @@ void IPAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo IP
|
|||||||
void DNSAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo DNS Address", this); }
|
void DNSAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo DNS Address", this); }
|
||||||
void MACAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo MAC Address", this); }
|
void MACAddressEthernetInfo::dump_config() { LOG_TEXT_SENSOR("", "EthernetInfo MAC Address", this); }
|
||||||
|
|
||||||
} // namespace ethernet_info
|
} // namespace esphome::ethernet_info
|
||||||
} // namespace esphome
|
|
||||||
|
|
||||||
#endif // USE_ESP32
|
#endif // USE_ESP32
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome::ethernet_info {
|
||||||
namespace ethernet_info {
|
|
||||||
|
|
||||||
class IPAddressEthernetInfo : public PollingComponent, public text_sensor::TextSensor {
|
class IPAddressEthernetInfo : public PollingComponent, public text_sensor::TextSensor {
|
||||||
public:
|
public:
|
||||||
@@ -40,21 +39,27 @@ class IPAddressEthernetInfo : public PollingComponent, public text_sensor::TextS
|
|||||||
class DNSAddressEthernetInfo : public PollingComponent, public text_sensor::TextSensor {
|
class DNSAddressEthernetInfo : public PollingComponent, public text_sensor::TextSensor {
|
||||||
public:
|
public:
|
||||||
void update() override {
|
void update() override {
|
||||||
auto dns_one = ethernet::global_eth_component->get_dns_address(0);
|
auto dns1 = ethernet::global_eth_component->get_dns_address(0);
|
||||||
auto dns_two = ethernet::global_eth_component->get_dns_address(1);
|
auto dns2 = ethernet::global_eth_component->get_dns_address(1);
|
||||||
|
|
||||||
std::string dns_results = dns_one.str() + " " + dns_two.str();
|
if (dns1 != this->last_dns1_ || dns2 != this->last_dns2_) {
|
||||||
|
this->last_dns1_ = dns1;
|
||||||
if (dns_results != this->last_results_) {
|
this->last_dns2_ = dns2;
|
||||||
this->last_results_ = dns_results;
|
// IP_ADDRESS_BUFFER_SIZE (40) = max IP (39) + null; space reuses first null's slot
|
||||||
this->publish_state(dns_results);
|
char buf[network::IP_ADDRESS_BUFFER_SIZE * 2];
|
||||||
|
dns1.str_to(buf);
|
||||||
|
size_t len1 = strlen(buf);
|
||||||
|
buf[len1] = ' ';
|
||||||
|
dns2.str_to(buf + len1 + 1);
|
||||||
|
this->publish_state(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
float get_setup_priority() const override { return setup_priority::ETHERNET; }
|
float get_setup_priority() const override { return setup_priority::ETHERNET; }
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string last_results_;
|
network::IPAddress last_dns1_;
|
||||||
|
network::IPAddress last_dns2_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class MACAddressEthernetInfo : public Component, public text_sensor::TextSensor {
|
class MACAddressEthernetInfo : public Component, public text_sensor::TextSensor {
|
||||||
@@ -64,7 +69,6 @@ class MACAddressEthernetInfo : public Component, public text_sensor::TextSensor
|
|||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ethernet_info
|
} // namespace esphome::ethernet_info
|
||||||
} // namespace esphome
|
|
||||||
|
|
||||||
#endif // USE_ESP32
|
#endif // USE_ESP32
|
||||||
|
|||||||
Reference in New Issue
Block a user