refactor: entkopple mDNS-Discovery, Status-Flags und WiFi-Manager

- net_mdns: Autostart + Discovery-Retry stabilisiert (Forward-Declarations, Task-Start bei GOT_IP)
- sys_status: EventGroup zentralisiert; Statusbits nicht mehr aus net_mdns gesetzt
- main: Integration von net_mdns Autostart/Discovery und UI-Status-Update vorbereitet
- wifi_mgr: mDNS-Discovery-Logik entfernt/vereinheitlicht, damit keine doppelte Discovery läuft
- net_influx: Anpassungen an neue Discovery/Storage-Semantik
This commit is contained in:
2026-01-22 13:45:15 +01:00
parent ca8ec3c993
commit 2352075c58
8 changed files with 493 additions and 154 deletions
+54 -1
View File
@@ -45,6 +45,10 @@
#include "dns_captive.h"
#include "http_portal.h"
#include "net_mdns.h"
#include "esp_timer.h"
// --- Callback-Implementierungen für das Portal ---
static bool app_load_sta_creds(char **ssid, char **pass)
@@ -150,6 +154,26 @@ static lv_indev_t *lvgl_touch_indev = NULL;
/* --- Button Handle global halten, falls benötigt --- */
static button_handle_t g_btn = NULL;
static void app_update_mdns_influx_status(void)
{
char *url = NULL;
// Ich kenne deine exakte storage-API nicht – bitte eine vorhandene Funktion nutzen:
// Beispiele: storage_load_service_url(&url) oder storage_get_service_url(&url)
// -> Nimm die, die bei dir existiert.
bool ok = storage_load_service_url(&url); // <- ggf. Namen anpassen
if (ok && url && url[0]) {
sys_status_set_mdns_ok(true);
sys_status_set_influx_ok(true);
} else {
sys_status_set_mdns_ok(false);
sys_status_set_influx_ok(false);
}
if (url) free(url);
}
/* Vorwärtsdeklarationen */
void lv_fs_fatfs_init(void);
static esp_err_t app_lvgl_init(void);
@@ -223,6 +247,9 @@ static esp_err_t app_lvgl_init(void)
static void ui_tick_cb(lv_timer_t *t) {
(void)t;
app_update_mdns_influx_status(); // <<< NEU
if (lvgl_port_lock(0)) {
ui_update_status_icons();
tick_screen(0);
@@ -335,6 +362,7 @@ static void button_init(void)
/* ==================== main ==================== */
void app_main(void)
{
/*
esp_log_level_set("storage", ESP_LOG_NONE);
esp_log_level_set("*", ESP_LOG_NONE);
@@ -343,7 +371,7 @@ void app_main(void)
sys_status_set_wifi_ok(false);
sys_status_set_influx_ok(false);
sys_status_set_mdns_ok(false);
*/
// NVS zuerst
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
@@ -393,6 +421,31 @@ void app_main(void)
// WiFi AP+STA starten
wifi_mgr_start();
// --- mDNS: Autostart (startet bei STA_GOT_IP automatisch) ---
ESP_ERROR_CHECK(net_mdns_autostart_init(NULL, NULL));
// --- mDNS: Service-Discovery mit Retry (ersetzt mdns_discover_task in wifi_mgr) ---
net_mdns_discover_cfg_t mdns_cfg = {
.service = "_influxdb2",
.proto = "_tcp",
.enforce_port = 8086,
.fallback_service = "_http",
.fallback_proto = "_tcp",
.fallback_port = 8086,
.txt_key = NULL,
.txt_value = NULL,
.query_timeout_ms = 2000,
.retry_interval_ms = 60000,
.max_results = 5,
.instance_prefix = NULL,
.require_instance_prefix = false
};
ESP_ERROR_CHECK(net_mdns_autodiscover_start(&mdns_cfg));
// AP-Netif an DNS weitergeben
dns_captive_set_ap_netif(wifi_mgr_get_ap_netif());
// Captive-DNS starten
+2 -2
View File
@@ -1,6 +1,4 @@
// File: src/net_influx.c
#include "storage.h"
#include "net_influx.h"
#include "esp_http_client.h"
#include "esp_err.h"
#include "esp_log.h"
@@ -10,6 +8,8 @@
#include <ctype.h>
#include <stdlib.h>
#include "sys_status.h"
#include "storage.h"
#include "net_influx.h"
static const char* TAG = "net_influx";
-30
View File
@@ -232,8 +232,6 @@ static void wifi_event_handler(void* arg, esp_event_base_t base, int32_t id, voi
ESP_LOGI(TAG, "STA got IP: " IPSTR, IP2STR(&e->ip_info.ip));
xEventGroupSetBits(wifi_events, EVT_STA_GOTIP);
xTaskCreate(mdns_discover_task, "mdns_discover", 3072, NULL, 4, NULL);
if (ap_off_timer) {
if (esp_timer_is_active(ap_off_timer)) esp_timer_stop(ap_off_timer);
ESP_ERROR_CHECK(esp_timer_start_once(ap_off_timer, 10 * 1000 * 1000)); // 10 s
@@ -353,31 +351,3 @@ static void ap_set_dhcp_dns_to_self(void) {
&ip.ip, sizeof(ip.ip));
esp_netif_dhcps_start(netif_ap);
}
// Task für mDNS-Discover ---
static void mdns_discover_task(void *arg) {
// Start/ensure mDNS läuft
(void)net_mdns_autostart_init(NULL, NULL);
const TickType_t kDelay = pdMS_TO_TICKS(60000); // 60 s
for (;;) {
// Erst offizieller Service, dann Fallback auf HTTP:8086
esp_err_t err = net_mdns_autodiscover_and_store("_myapp", "_tcp", -1,
NULL, NULL, 2000);
if (err != ESP_OK) {
err = net_mdns_autodiscover_and_store("_http", "_tcp", 8086,
NULL, NULL, 2000);
}
if (err == ESP_OK) {
sys_status_set_mdns_ok(true);
ESP_LOGI(TAG, "mDNS: Service gefunden und gespeichert. Beende Re-Try-Task.");
vTaskDelete(NULL); // fertig, keine weiteren Versuche
} else {
sys_status_set_mdns_ok(false);
ESP_LOGW(TAG, "mDNS: nichts gefunden – neuer Versuch in 60 s");
vTaskDelay(kDelay);
}
}
}