mirror of
https://github.com/esphome/esphome.git
synced 2025-10-08 21:03:49 +01:00
preen
This commit is contained in:
@@ -73,14 +73,13 @@ MDNS_STATIC_CONST_CHAR(NETWORK_THREAD, "thread");
|
||||
|
||||
void MDNSComponent::compile_records_() {
|
||||
this->hostname_ = App.get_name();
|
||||
this->services_count_ = 0;
|
||||
|
||||
// IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES
|
||||
// in mdns/__init__.py. If you add a new service here, update both locations.
|
||||
|
||||
#ifdef USE_API
|
||||
if (api::global_api_server != nullptr) {
|
||||
auto &service = this->services_[this->services_count_++];
|
||||
auto &service = this->services_[this->services_.count()++];
|
||||
service.service_type = MDNS_STR(SERVICE_ESPHOMELIB);
|
||||
service.proto = MDNS_STR(SERVICE_TCP);
|
||||
service.port = api::global_api_server->get_port();
|
||||
@@ -159,29 +158,23 @@ void MDNSComponent::compile_records_() {
|
||||
#endif // USE_API
|
||||
|
||||
#ifdef USE_PROMETHEUS
|
||||
auto &prom_service = this->services_[this->services_count_++];
|
||||
auto &prom_service = this->services_[this->services_.count()++];
|
||||
prom_service.service_type = MDNS_STR(SERVICE_PROMETHEUS);
|
||||
prom_service.proto = MDNS_STR(SERVICE_TCP);
|
||||
prom_service.port = USE_WEBSERVER_PORT;
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
auto &web_service = this->services_[this->services_count_++];
|
||||
auto &web_service = this->services_[this->services_.count()++];
|
||||
web_service.service_type = MDNS_STR(SERVICE_HTTP);
|
||||
web_service.proto = MDNS_STR(SERVICE_TCP);
|
||||
web_service.port = USE_WEBSERVER_PORT;
|
||||
#endif
|
||||
|
||||
#ifdef USE_MDNS_EXTRA_SERVICES
|
||||
for (const auto &extra_service : this->services_extra_) {
|
||||
this->services_[this->services_count_++] = extra_service;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(USE_API) && !defined(USE_PROMETHEUS) && !defined(USE_WEBSERVER) && !defined(USE_MDNS_EXTRA_SERVICES)
|
||||
// Publish "http" service if not using native API or any other services
|
||||
// This is just to have *some* mDNS service so that .local resolution works
|
||||
auto &fallback_service = this->services_[this->services_count_++];
|
||||
auto &fallback_service = this->services_[this->services_.count()++];
|
||||
fallback_service.service_type = "_http";
|
||||
fallback_service.proto = "_tcp";
|
||||
fallback_service.port = USE_WEBSERVER_PORT;
|
||||
@@ -189,12 +182,6 @@ void MDNSComponent::compile_records_() {
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_MDNS_EXTRA_SERVICES
|
||||
void MDNSComponent::add_extra_service(MDNSService service) {
|
||||
this->services_[this->services_count_++] = std::move(service);
|
||||
}
|
||||
#endif
|
||||
|
||||
void MDNSComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"mDNS:\n"
|
||||
@@ -202,8 +189,7 @@ void MDNSComponent::dump_config() {
|
||||
this->hostname_.c_str());
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||
ESP_LOGV(TAG, " Services:");
|
||||
for (uint8_t i = 0; i < this->services_count_; i++) {
|
||||
const auto &service = this->services_[i];
|
||||
for (const auto &service : this->services_) {
|
||||
ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(),
|
||||
const_cast<TemplatableValue<uint16_t> &>(service.port).value());
|
||||
for (const auto &record : service.txt_records) {
|
||||
@@ -214,6 +200,8 @@ void MDNSComponent::dump_config() {
|
||||
#endif
|
||||
}
|
||||
|
||||
StaticVector<MDNSService, MDNS_SERVICE_COUNT> MDNSComponent::get_services() { return this->services_; }
|
||||
|
||||
} // namespace mdns
|
||||
} // namespace esphome
|
||||
#endif
|
||||
|
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_MDNS
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mdns {
|
||||
@@ -39,17 +39,15 @@ class MDNSComponent : public Component {
|
||||
float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
|
||||
|
||||
#ifdef USE_MDNS_EXTRA_SERVICES
|
||||
void add_extra_service(MDNSService service);
|
||||
void add_extra_service(MDNSService service) { services_.push_back(std::move(service)); }
|
||||
#endif
|
||||
|
||||
const std::array<MDNSService, MDNS_SERVICE_COUNT> &get_services() const { return services_; }
|
||||
uint8_t get_services_count() const { return services_count_; }
|
||||
StaticVector<MDNSService, MDNS_SERVICE_COUNT> get_services();
|
||||
|
||||
void on_shutdown() override;
|
||||
|
||||
protected:
|
||||
std::array<MDNSService, MDNS_SERVICE_COUNT> services_{};
|
||||
uint8_t services_count_{0};
|
||||
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services_{};
|
||||
std::string hostname_;
|
||||
void compile_records_();
|
||||
};
|
||||
|
@@ -25,8 +25,7 @@ void MDNSComponent::setup() {
|
||||
mdns_hostname_set(this->hostname_.c_str());
|
||||
mdns_instance_name_set(this->hostname_.c_str());
|
||||
|
||||
for (uint8_t i = 0; i < this->services_count_; i++) {
|
||||
const auto &service = this->services_[i];
|
||||
for (const auto &service : this->services_) {
|
||||
std::vector<mdns_txt_item_t> txt_records;
|
||||
for (const auto &record : service.txt_records) {
|
||||
mdns_txt_item_t it{};
|
||||
|
@@ -16,8 +16,7 @@ void MDNSComponent::setup() {
|
||||
|
||||
MDNS.begin(this->hostname_.c_str());
|
||||
|
||||
for (uint8_t i = 0; i < this->services_count_; i++) {
|
||||
const auto &service = this->services_[i];
|
||||
for (const auto &service : this->services_) {
|
||||
// Strip the leading underscore from the proto and service_type. While it is
|
||||
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
||||
// expects the underscore to be there, the ESP8266 implementation always adds
|
||||
|
@@ -16,8 +16,7 @@ void MDNSComponent::setup() {
|
||||
|
||||
MDNS.begin(this->hostname_.c_str());
|
||||
|
||||
for (uint8_t i = 0; i < this->services_count_; i++) {
|
||||
const auto &service = this->services_[i];
|
||||
for (const auto &service : this->services_) {
|
||||
// Strip the leading underscore from the proto and service_type. While it is
|
||||
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
||||
// expects the underscore to be there, the ESP8266 implementation always adds
|
||||
|
@@ -16,8 +16,7 @@ void MDNSComponent::setup() {
|
||||
|
||||
MDNS.begin(this->hostname_.c_str());
|
||||
|
||||
for (uint8_t i = 0; i < this->services_count_; i++) {
|
||||
const auto &service = this->services_[i];
|
||||
for (const auto &service : this->services_) {
|
||||
// Strip the leading underscore from the proto and service_type. While it is
|
||||
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
||||
// expects the underscore to be there, the ESP8266 implementation always adds
|
||||
|
@@ -144,11 +144,9 @@ void OpenThreadSrpComponent::setup() {
|
||||
}
|
||||
|
||||
// Get mdns services and copy their data (strings are copied with strdup below)
|
||||
const auto &mdns_services = this->mdns_->get_services();
|
||||
uint8_t mdns_count = this->mdns_->get_services_count();
|
||||
ESP_LOGD(TAG, "Setting up SRP services. count = %d\n", mdns_count);
|
||||
for (uint8_t i = 0; i < mdns_count; i++) {
|
||||
const auto &service = mdns_services[i];
|
||||
auto mdns_services = this->mdns_->get_services();
|
||||
ESP_LOGD(TAG, "Setting up SRP services. count = %d\n", mdns_services.size());
|
||||
for (const auto &service : mdns_services) {
|
||||
otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
|
||||
if (!entry) {
|
||||
ESP_LOGW(TAG, "Failed to allocate service entry");
|
||||
|
@@ -130,6 +130,9 @@ template<typename T, size_t N> class StaticVector {
|
||||
size_t size() const { return count_; }
|
||||
bool empty() const { return count_ == 0; }
|
||||
|
||||
// Direct access to increment size for efficient initialization
|
||||
size_t &count() { return count_; }
|
||||
|
||||
T &operator[](size_t i) { return data_[i]; }
|
||||
const T &operator[](size_t i) const { return data_[i]; }
|
||||
|
||||
|
Reference in New Issue
Block a user