Refactor: Influx-Handling aus sen5x_app ausgelagert und generisch gemacht

- sen5x_app.c auf reine Sensor-App reduziert (SEN5x Readout, Skalierung, GUI vars, device-tag)
- Influx-Init/Retry/Backoff/HTTP komplett nach net_influx.c verlagert
- SEN5x schreibt nicht mehr direkt Line-Protocol, sondern submit via net_influx_submit_point()
- net_influx als generisches Interface für unterschiedliche Sensor-Samples (variable Anzahl Felder) vorbereitet
- Consumer/Queue für Influx aus sen5x_app entfernt; nur noch Producer-Task im Sensor-Modul
This commit is contained in:
eugen
2026-01-29 13:11:21 +01:00
parent 531256529f
commit ac7d26eb9c
4 changed files with 650 additions and 307 deletions
+1
View File
@@ -3,4 +3,5 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.venv
todo.txt
+499 -98
View File
@@ -2,11 +2,18 @@
#include "esp_http_client.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include "sys_status.h"
#include "storage.h"
#include "net_influx.h"
@@ -26,6 +33,37 @@ typedef struct {
static net_influx_state_t s = {0};
/* ---------------- Worker Runtime State (asynchrones Submit) ---------------- */
typedef struct {
uint16_t queue_len;
uint32_t task_stack;
UBaseType_t task_prio;
uint32_t init_retry_ms;
uint32_t backoff_start_ms;
uint32_t backoff_max_ms;
uint8_t fails_before_reinit;
const char* ca_cert_pem_opt;
int http_timeout_ms;
} net_influx_runtime_cfg_i_t;
static QueueHandle_t s_q = NULL;
static TaskHandle_t s_task = NULL;
static bool s_run = false;
static net_influx_runtime_cfg_i_t s_rt = {
.queue_len = 16,
.task_stack = 6144,
.task_prio = 5,
.init_retry_ms = 1000,
.backoff_start_ms = 2000,
.backoff_max_ms = 300000,
.fails_before_reinit = 5,
.ca_cert_pem_opt = NULL,
.http_timeout_ms = 3000,
};
/* ---------------- Utils ---------------- */
static void trim_inplace(char* p) {
if (!p) return;
@@ -106,6 +144,38 @@ static void mask_token(const char* token, char* out, size_t out_len) {
}
}
static char* dup0(const char* str) {
if (!str) return NULL;
size_t n = strlen(str) + 1;
char* p = (char*)malloc(n);
if (!p) return NULL;
memcpy(p, str, n);
return p;
}
/* Minimal escaping für measurement/key: spaces/commas */
static size_t esc_copy(char* dst, size_t cap, const char* src, bool escape_equals) {
size_t pos = 0;
if (!dst || cap == 0) return 0;
dst[0] = 0;
if (!src) return 0;
for (const char* p = src; *p; ++p) {
char c = *p;
bool esc = (c == ' ' || c == ',' || (escape_equals && c == '='));
if (esc) {
if (pos + 2 >= cap) break;
dst[pos++] = '\\';
dst[pos++] = c;
} else {
if (pos + 1 >= cap) break;
dst[pos++] = c;
}
}
dst[(pos < cap) ? pos : (cap - 1)] = 0;
return pos;
}
/* ---------------- HTTP Event Handler (IDF-kompatibel) ---------------- */
static esp_err_t http_evt(esp_http_client_event_t* evt) {
switch (evt->event_id) {
@@ -125,6 +195,318 @@ static esp_err_t http_evt(esp_http_client_event_t* evt) {
return ESP_OK;
}
/* ---------------- Internal: HTTP send "line protocol" ---------------- */
static esp_err_t net_influx_write_line(const char* line) {
bool ok = false;
if (!line || !*line) {
sys_status_set_influx_ok(false);
return ESP_ERR_INVALID_ARG;
}
if (s.url_base[0] == 0 || s.org[0] == 0 || s.bucket[0] == 0 || s.token[0] == 0) {
sys_status_set_influx_ok(false);
return ESP_ERR_INVALID_STATE;
}
// Base parse → host/port/tls
base_parts_t bp;
if (!parse_base_url(s.url_base, &bp)) {
ESP_LOGE(TAG, "Base-URL ungueltig: \"%s\"", s.url_base);
sys_status_set_influx_ok(false);
return ESP_ERR_INVALID_ARG;
}
if (s.use_tls) bp.tls = true;
int port = (bp.port > 0) ? bp.port : (bp.tls ? 443 : 8086);
// Pfad
char path[192];
snprintf(path, sizeof(path), "/api/v2/write?org=%s&bucket=%s&precision=ms", s.org, s.bucket);
ESP_LOGI(TAG, "HTTP cfg: host=%s, port=%d, path=%s, tls=%d", bp.host, port, path, (int)bp.tls);
ESP_LOGD(TAG, "Line: %s", line);
esp_http_client_config_t cfg = {
.host = bp.host,
.port = port,
.path = path,
.timeout_ms = s.http_timeout_ms,
.cert_pem = bp.tls ? s.ca_cert_pem : NULL,
.method = HTTP_METHOD_POST,
.event_handler = http_evt,
.transport_type = bp.tls ? HTTP_TRANSPORT_OVER_SSL : HTTP_TRANSPORT_OVER_TCP,
.disable_auto_redirect = false,
};
esp_http_client_handle_t h = esp_http_client_init(&cfg);
if (!h) {
ESP_LOGE(TAG, "http_client_init: no mem");
sys_status_set_influx_ok(false);
return ESP_ERR_NO_MEM;
}
// Header
size_t auth_len = strlen(s.token) + 7; // "Token " + '\0'
char* auth = (char*)malloc(auth_len);
if (!auth) {
ESP_LOGE(TAG, "malloc auth header: no mem");
esp_http_client_cleanup(h);
sys_status_set_influx_ok(false);
return ESP_ERR_NO_MEM;
}
snprintf(auth, auth_len, "Token %s", s.token);
esp_http_client_set_header(h, "Authorization", auth);
free(auth);
esp_http_client_set_header(h, "Content-Type", "text/plain; charset=utf-8");
esp_err_t err = esp_http_client_set_post_field(h, line, (int)strlen(line));
if (err != ESP_OK) {
ESP_LOGE(TAG, "set_post_field: %s", esp_err_to_name(err));
esp_http_client_cleanup(h);
sys_status_set_influx_ok(false);
return err;
}
err = esp_http_client_perform(h);
int status = -1;
if (err == ESP_OK) {
status = esp_http_client_get_status_code(h);
int64_t cl = esp_http_client_get_content_length(h);
ESP_LOGI(TAG, "HTTP status=%d, content_length=%lld", status, (long long)cl);
if (status != 204) {
char resp[256] = {0};
int n = esp_http_client_read_response(h, resp, sizeof(resp)-1);
if (n > 0) ESP_LOGW(TAG, "Resp(%d): %.*s", status, n, resp);
else ESP_LOGW(TAG, "Resp(%d): <kein Body>", status);
}
} else {
ESP_LOGE(TAG, "perform: %s", esp_err_to_name(err));
}
esp_http_client_cleanup(h);
ok = (err == ESP_OK && status == 204);
sys_status_set_influx_ok(ok);
return ok ? ESP_OK : ESP_FAIL;
}
/* ---------------- Generic Point/Fields → Line Protocol ---------------- */
static bool field_is_valid(const net_influx_field_t* f) {
if (!f || !f->key || !*f->key) return false;
if (f->type == NET_INFLUX_F_FLOAT) return isfinite(f->v.f);
if (f->type == NET_INFLUX_F_STRING) return (f->v.s != NULL);
return true;
}
static esp_err_t build_line_protocol(const net_influx_point_t* p, char* out, size_t out_cap) {
if (!p || !p->measurement || !p->fields || p->num_fields == 0 || !out || out_cap < 64) {
return ESP_ERR_INVALID_ARG;
}
size_t off = 0;
// measurement escaped (space/comma)
char meas[96];
esc_copy(meas, sizeof(meas), p->measurement, false);
off += snprintf(out + off, out_cap - off, "%s", meas);
if (off >= out_cap) return ESP_FAIL;
// tags: passthrough (caller liefert "k=v,k2=v2")
if (p->tags && *p->tags) {
off += snprintf(out + off, out_cap - off, ",%s", p->tags);
if (off >= out_cap) return ESP_FAIL;
}
off += snprintf(out + off, out_cap - off, " ");
if (off >= out_cap) return ESP_FAIL;
bool first = true;
for (size_t i = 0; i < p->num_fields; ++i) {
const net_influx_field_t* f = &p->fields[i];
if (!field_is_valid(f)) continue;
char key[64];
esc_copy(key, sizeof(key), f->key, false);
if (!first) {
off += snprintf(out + off, out_cap - off, ",");
if (off >= out_cap) return ESP_FAIL;
}
first = false;
switch (f->type) {
case NET_INFLUX_F_FLOAT: {
int prec = (f->precision >= 0 && f->precision <= 9) ? f->precision : 3;
char fmt[24];
snprintf(fmt, sizeof(fmt), "%%s=%%.%df", prec);
off += snprintf(out + off, out_cap - off, fmt, key, f->v.f);
break;
}
case NET_INFLUX_F_INT:
off += snprintf(out + off, out_cap - off, "%s=%lldi", key, (long long)f->v.i);
break;
case NET_INFLUX_F_UINT:
off += snprintf(out + off, out_cap - off, "%s=%llui", key, (unsigned long long)f->v.u);
break;
case NET_INFLUX_F_BOOL:
off += snprintf(out + off, out_cap - off, "%s=%s", key, f->v.b ? "true" : "false");
break;
case NET_INFLUX_F_STRING: {
off += snprintf(out + off, out_cap - off, "%s=\"", key);
if (off >= out_cap) return ESP_FAIL;
const char* s0 = f->v.s ? f->v.s : "";
for (const char* sp = s0; *sp; ++sp) {
if (off + 2 >= out_cap) return ESP_FAIL;
if (*sp == '\"' || *sp == '\\') out[off++] = '\\';
out[off++] = *sp;
}
if (off + 2 >= out_cap) return ESP_FAIL;
out[off++] = '\"';
out[off] = 0;
break;
}
default:
return ESP_ERR_INVALID_ARG;
}
if (off >= out_cap) return ESP_FAIL;
}
if (first) return ESP_ERR_INVALID_STATE; // keine validen Felder
if (p->ts_ms != 0) {
off += snprintf(out + off, out_cap - off, " %llu", (unsigned long long)p->ts_ms);
if (off >= out_cap) return ESP_FAIL;
}
return ESP_OK;
}
/* ---------------- Async Queue Message (deep copy) ---------------- */
typedef struct {
char* measurement;
char* tags; // optional
uint64_t ts_ms;
size_t num_fields;
net_influx_field_t* fields; // deep-copied array; keys and string-values are duplicated
} influx_msg_t;
static void free_msg(influx_msg_t* m) {
if (!m) return;
if (m->measurement) free(m->measurement);
if (m->tags) free(m->tags);
if (m->fields) {
for (size_t i = 0; i < m->num_fields; ++i) {
if (m->fields[i].type == NET_INFLUX_F_STRING && m->fields[i].v.s) {
free((void*)m->fields[i].v.s);
}
if (m->fields[i].key) free((void*)m->fields[i].key);
}
free(m->fields);
}
memset(m, 0, sizeof(*m));
}
/* ---------------- Init/Backoff (im Worker) ---------------- */
static bool influx_init_blocking(void) {
while (s_run) {
if (net_influx_init_from_nvs(s_rt.ca_cert_pem_opt, s_rt.http_timeout_ms) == ESP_OK) {
ESP_LOGI(TAG, "Influx ready (from NVS).");
return true;
}
ESP_LOGW(TAG, "Influx nicht bereit – retry in %u ms", (unsigned)s_rt.init_retry_ms);
vTaskDelay(pdMS_TO_TICKS(s_rt.init_retry_ms));
}
return false;
}
static bool influx_reinit_with_backoff(uint32_t* backoff_ms) {
if (!backoff_ms) return false;
uint32_t wait_ms = (*backoff_ms == 0) ? s_rt.backoff_start_ms : *backoff_ms;
ESP_LOGW(TAG, "Re-Init Influx in %u ms", (unsigned)wait_ms);
vTaskDelay(pdMS_TO_TICKS(wait_ms));
bool ok = (net_influx_init_from_nvs(s_rt.ca_cert_pem_opt, s_rt.http_timeout_ms) == ESP_OK);
if (ok) {
ESP_LOGI(TAG, "Influx Re-Init OK");
*backoff_ms = s_rt.backoff_start_ms;
return true;
}
if (wait_ms < s_rt.backoff_max_ms) {
uint32_t next = wait_ms * 2;
*backoff_ms = (next > s_rt.backoff_max_ms) ? s_rt.backoff_max_ms : next;
}
return false;
}
/* ---------------- Worker Task ---------------- */
static void net_influx_worker(void* arg) {
(void)arg;
ESP_LOGI(TAG, "Influx worker started.");
if (!influx_init_blocking()) {
ESP_LOGE(TAG, "Worker beendet (Influx init fatal).");
vTaskDelete(NULL);
return;
}
int fail_count = 0;
uint32_t backoff_ms = s_rt.backoff_start_ms;
while (s_run) {
influx_msg_t msg = {0};
if (xQueueReceive(s_q, &msg, portMAX_DELAY) != pdTRUE) continue;
net_influx_point_t p = {
.measurement = msg.measurement,
.tags = msg.tags,
.ts_ms = msg.ts_ms,
.fields = msg.fields,
.num_fields = msg.num_fields,
};
char line[512];
esp_err_t b = build_line_protocol(&p, line, sizeof(line));
if (b == ESP_ERR_INVALID_STATE) {
ESP_LOGW(TAG, "Skipping write: no valid fields.");
free_msg(&msg);
continue;
}
if (b != ESP_OK) {
ESP_LOGW(TAG, "Line build failed: %s", esp_err_to_name(b));
free_msg(&msg);
continue;
}
esp_err_t e = net_influx_write_line(line);
if (e == ESP_OK) {
if (fail_count) ESP_LOGI(TAG, "Influx write recovered.");
fail_count = 0;
backoff_ms = s_rt.backoff_start_ms;
} else {
ESP_LOGW(TAG, "Influx write failed: %s", esp_err_to_name(e));
if (++fail_count >= (int)s_rt.fails_before_reinit) {
(void)influx_reinit_with_backoff(&backoff_ms);
fail_count = 0;
}
}
free_msg(&msg);
}
ESP_LOGI(TAG, "Influx worker stopping.");
vTaskDelete(NULL);
}
/* ---------------- Public API ---------------- */
esp_err_t net_influx_init(const net_influx_cfg_t* cfg) {
if (!cfg || !cfg->url_base || !cfg->org || !cfg->bucket || !cfg->token) {
@@ -154,15 +536,15 @@ esp_err_t net_influx_write_fields(const char* measurement,
const char* fields_fmt,
...)
{
// --- Neu: pessimistisch starten ---
// --- pessimistisch starten ---
bool ok = false;
if (!measurement || !fields_fmt) {
sys_status_set_influx_ok(false); // <—
sys_status_set_influx_ok(false);
return ESP_ERR_INVALID_ARG;
}
if (s.url_base[0] == 0 || s.org[0] == 0 || s.bucket[0] == 0 || s.token[0] == 0) {
sys_status_set_influx_ok(false); // <—
sys_status_set_influx_ok(false);
return ESP_ERR_INVALID_STATE;
}
@@ -174,114 +556,33 @@ esp_err_t net_influx_write_fields(const char* measurement,
va_end(ap);
// 2) Line Protocol
char line[400];
char line[512];
int off = 0;
off += snprintf(line + off, sizeof(line) - off, "%s", measurement);
if (off < 0 || off >= (int)sizeof(line)) { sys_status_set_influx_ok(false); return ESP_FAIL; } // <—
// measurement escaping (space/comma)
char meas[96];
esc_copy(meas, sizeof(meas), measurement, false);
off += snprintf(line + off, sizeof(line) - off, "%s", meas);
if (off < 0 || off >= (int)sizeof(line)) { sys_status_set_influx_ok(false); return ESP_FAIL; }
if (tags && *tags) {
off += snprintf(line + off, sizeof(line) - off, ",%s", tags);
if (off < 0 || off >= (int)sizeof(line)) { sys_status_set_influx_ok(false); return ESP_FAIL; } // <—
if (off < 0 || off >= (int)sizeof(line)) { sys_status_set_influx_ok(false); return ESP_FAIL; }
}
off += snprintf(line + off, sizeof(line) - off, " %s", fields);
if (off < 0 || off >= (int)sizeof(line)) { sys_status_set_influx_ok(false); return ESP_FAIL; } // <—
if (off < 0 || off >= (int)sizeof(line)) { sys_status_set_influx_ok(false); return ESP_FAIL; }
if (ts_ms != 0) {
off += snprintf(line + off, sizeof(line) - off, " %llu", (unsigned long long)ts_ms);
if (off < 0 || off >= (int)sizeof(line)) { sys_status_set_influx_ok(false); return ESP_FAIL; } // <—
if (off < 0 || off >= (int)sizeof(line)) { sys_status_set_influx_ok(false); return ESP_FAIL; }
}
// 3) Base parse → host/port/tls
base_parts_t bp;
if (!parse_base_url(s.url_base, &bp)) {
ESP_LOGE(TAG, "Base-URL ungueltig: \"%s\"", s.url_base);
sys_status_set_influx_ok(false); // <—
return ESP_ERR_INVALID_ARG;
}
if (s.use_tls) bp.tls = true;
int port = (bp.port > 0) ? bp.port : (bp.tls ? 443 : 8086);
esp_err_t err = net_influx_write_line(line);
// 4) Pfad (Tipp: URL-encoding wenn org/bucket Sonderzeichen haben könnten)
char path[192];
snprintf(path, sizeof(path), "/api/v2/write?org=%s&bucket=%s&precision=ms", s.org, s.bucket);
ESP_LOGI(TAG, "HTTP cfg: host=%s, port=%d, path=%s, tls=%d", bp.host, port, path, (int)bp.tls);
ESP_LOGD(TAG, "Line: %s", line);
// 5) HTTP-Client
esp_http_client_config_t cfg = {
.host = bp.host,
.port = port,
.path = path,
.timeout_ms = s.http_timeout_ms,
.cert_pem = bp.tls ? s.ca_cert_pem : NULL,
.method = HTTP_METHOD_POST,
.event_handler = http_evt,
.transport_type = bp.tls ? HTTP_TRANSPORT_OVER_SSL : HTTP_TRANSPORT_OVER_TCP,
// Optional: stabiler auf schlechten Links
.disable_auto_redirect = false,
};
esp_http_client_handle_t h = esp_http_client_init(&cfg);
if (!h) {
ESP_LOGE(TAG, "http_client_init: no mem");
sys_status_set_influx_ok(false); // <—
return ESP_ERR_NO_MEM;
}
// Header
size_t auth_len = strlen(s.token) + 7; // "Token " + '\0'
char* auth = (char*)malloc(auth_len);
if (!auth) {
ESP_LOGE(TAG, "malloc auth header: no mem");
esp_http_client_cleanup(h);
sys_status_set_influx_ok(false); // <—
return ESP_ERR_NO_MEM;
}
snprintf(auth, auth_len, "Token %s", s.token);
esp_http_client_set_header(h, "Authorization", auth);
free(auth);
esp_http_client_set_header(h, "Content-Type", "text/plain; charset=utf-8");
// Optional hilfreich:
// esp_http_client_set_header(h, "Accept", "application/json");
// esp_http_client_set_header(h, "Connection", "close");
// Body
esp_err_t err = esp_http_client_set_post_field(h, line, (int)strlen(line));
if (err != ESP_OK) {
ESP_LOGE(TAG, "set_post_field: %s", esp_err_to_name(err));
esp_http_client_cleanup(h);
sys_status_set_influx_ok(false); // <—
return err;
}
// 6) Request
err = esp_http_client_perform(h);
int status = -1;
if (err == ESP_OK) {
status = esp_http_client_get_status_code(h);
int64_t cl = esp_http_client_get_content_length(h);
ESP_LOGI(TAG, "HTTP status=%d, content_length=%lld", status, (long long)cl);
if (status != 204) {
char resp[256] = {0};
int n = esp_http_client_read_response(h, resp, sizeof(resp)-1);
if (n > 0) ESP_LOGW(TAG, "Resp(%d): %.*s", status, n, resp);
else ESP_LOGW(TAG, "Resp(%d): <kein Body>", status);
}
} else {
ESP_LOGE(TAG, "perform: %s", esp_err_to_name(err));
}
esp_http_client_cleanup(h);
// --- Wichtig: Flag NACH allem zentral setzen ---
ok = (err == ESP_OK && status == 204);
ok = (err == ESP_OK);
sys_status_set_influx_ok(ok);
return ok ? ESP_OK : ESP_FAIL;
}
// Aus NVS: URL (service_url), org/bucket/token → net_influx_init()
esp_err_t net_influx_init_from_nvs(const char* ca_cert_pem_opt, int timeout_ms)
{
@@ -289,7 +590,6 @@ esp_err_t net_influx_init_from_nvs(const char* ca_cert_pem_opt, int timeout_ms)
if (!storage_load_service_url(&url)) {
ESP_LOGW(TAG, "keine Service-URL in NVS gefunden (mDNS noch nicht fertig?)");
// Du kannst hier optional einen Default setzen oder ESP_ERR_NOT_FOUND zurückgeben
return ESP_ERR_NOT_FOUND;
}
if (!storage_get_effective_influx(&org, &bucket, &token)) {
@@ -316,3 +616,104 @@ esp_err_t net_influx_init_from_nvs(const char* ca_cert_pem_opt, int timeout_ms)
free(token);
return err;
}
/* ---------------- NEW: Start/Stop/Submit (generic, multi-sensor) ---------------- */
esp_err_t net_influx_start(const net_influx_runtime_cfg_t* rt) {
if (s_task) return ESP_OK;
// runtime cfg übernehmen (mit Defaults)
if (rt) {
s_rt.queue_len = rt->queue_len ? rt->queue_len : s_rt.queue_len;
s_rt.task_stack = rt->task_stack ? rt->task_stack : s_rt.task_stack;
s_rt.task_prio = rt->task_prio ? rt->task_prio : s_rt.task_prio;
s_rt.init_retry_ms = rt->init_retry_ms ? rt->init_retry_ms : s_rt.init_retry_ms;
s_rt.backoff_start_ms = rt->backoff_start_ms ? rt->backoff_start_ms : s_rt.backoff_start_ms;
s_rt.backoff_max_ms = rt->backoff_max_ms ? rt->backoff_max_ms : s_rt.backoff_max_ms;
s_rt.fails_before_reinit = rt->fails_before_reinit ? rt->fails_before_reinit : s_rt.fails_before_reinit;
s_rt.ca_cert_pem_opt = rt->ca_cert_pem_opt;
s_rt.http_timeout_ms = (rt->http_timeout_ms > 0) ? rt->http_timeout_ms : s_rt.http_timeout_ms;
}
s_q = xQueueCreate(s_rt.queue_len, sizeof(influx_msg_t));
if (!s_q) return ESP_ERR_NO_MEM;
s_run = true;
if (xTaskCreate(net_influx_worker, "influx_worker", s_rt.task_stack, NULL, s_rt.task_prio, &s_task) != pdPASS) {
vQueueDelete(s_q);
s_q = NULL;
s_run = false;
return ESP_FAIL;
}
return ESP_OK;
}
void net_influx_stop(void) {
s_run = false;
if (s_task) {
vTaskDelete(s_task);
s_task = NULL;
}
if (s_q) {
influx_msg_t msg;
while (xQueueReceive(s_q, &msg, 0) == pdTRUE) {
free_msg(&msg);
}
vQueueDelete(s_q);
s_q = NULL;
}
}
esp_err_t net_influx_submit_point(const net_influx_point_t* p) {
if (!p || !p->measurement || !*p->measurement || !p->fields || p->num_fields == 0) {
return ESP_ERR_INVALID_ARG;
}
if (!s_q) return ESP_ERR_INVALID_STATE;
influx_msg_t msg = {0};
msg.measurement = dup0(p->measurement);
msg.tags = (p->tags && *p->tags) ? dup0(p->tags) : NULL;
msg.ts_ms = p->ts_ms;
msg.num_fields = p->num_fields;
msg.fields = (net_influx_field_t*)calloc(p->num_fields, sizeof(net_influx_field_t));
if (!msg.measurement || !msg.fields) {
free_msg(&msg);
return ESP_ERR_NO_MEM;
}
for (size_t i = 0; i < p->num_fields; ++i) {
msg.fields[i] = p->fields[i];
// deep copy key
msg.fields[i].key = dup0(p->fields[i].key);
if (!msg.fields[i].key) {
free_msg(&msg);
return ESP_ERR_NO_MEM;
}
// deep copy string value if needed
if (p->fields[i].type == NET_INFLUX_F_STRING && p->fields[i].v.s) {
msg.fields[i].v.s = dup0(p->fields[i].v.s);
if (!msg.fields[i].v.s) {
free_msg(&msg);
return ESP_ERR_NO_MEM;
}
}
}
// Non-blocking enqueue; wenn voll: drop oldest
if (xQueueSend(s_q, &msg, 0) != pdPASS) {
influx_msg_t dump;
if (xQueueReceive(s_q, &dump, 0) == pdTRUE) free_msg(&dump);
if (xQueueSend(s_q, &msg, 0) != pdPASS) {
free_msg(&msg);
return ESP_FAIL;
}
}
return ESP_OK;
}
+65 -10
View File
@@ -1,27 +1,82 @@
// File: include/net_influx.h
// File: include/net_influx.h (oder wo dein Header liegt)
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
typedef enum {
NET_INFLUX_F_FLOAT,
NET_INFLUX_F_INT,
NET_INFLUX_F_UINT,
NET_INFLUX_F_BOOL,
NET_INFLUX_F_STRING,
} net_influx_field_type_t;
typedef struct {
const char* url_base; // "http://host:8086" oder "https://host"
const char* key; // field key
net_influx_field_type_t type;
int precision; // nur für FLOAT (z.B. 1,3). sonst ignoriert
union {
double f;
int64_t i;
uint64_t u;
bool b;
const char* s;
} v;
} net_influx_field_t;
typedef struct {
const char* measurement; // z.B. "air"
const char* tags; // z.B. "device=xyz,location=lab" (optional, kann NULL sein)
uint64_t ts_ms; // 0 => server time
const net_influx_field_t* fields;
size_t num_fields;
} net_influx_point_t;
typedef struct {
// Task/Queue
uint16_t queue_len; // z.B. 16
uint32_t task_stack; // z.B. 6144
UBaseType_t task_prio; // z.B. 5
// Init/Retry/Backoff
uint32_t init_retry_ms; // z.B. 1000
uint32_t backoff_start_ms; // z.B. 2000
uint32_t backoff_max_ms; // z.B. 300000
uint8_t fails_before_reinit; // z.B. 5
// Influx init
const char* ca_cert_pem_opt; // optional
int http_timeout_ms; // z.B. 3000
} net_influx_runtime_cfg_t;
// Bestehende Funktionen bleiben:
typedef struct {
const char* url_base;
const char* org;
const char* bucket;
const char* token;
bool use_tls; // optional; wenn true => TLS erzwingen
const char* ca_cert_pem; // optional, bei https (kann NULL sein wenn Test)
int http_timeout_ms; // z.B. 3000
bool use_tls;
const char* ca_cert_pem;
int http_timeout_ms;
} net_influx_cfg_t;
esp_err_t net_influx_init(const net_influx_cfg_t* cfg);
// Convenience: lädt URL/org/bucket/token direkt aus NVS (storage.c)
esp_err_t net_influx_init_from_nvs(const char* ca_cert_pem_opt, int timeout_ms);
// generisches Write (Line Protocol Felderformatierung via printf)
// NEU:
esp_err_t net_influx_start(const net_influx_runtime_cfg_t* rt);
void net_influx_stop(void);
// Asynchron: kopiert point+felder intern und queued sie.
// Rückgabe:
// - ESP_OK: queued
// - ESP_ERR_NO_MEM / ESP_ERR_INVALID_ARG / ESP_FAIL: sonst
esp_err_t net_influx_submit_point(const net_influx_point_t* p);
// Optional (falls du synchron brauchst, bleibt deine alte API nutzbar):
esp_err_t net_influx_write_fields(const char* measurement,
const char* tags, // "k=v,k2=v2" oder NULL
uint64_t ts_ms, // 0 = Serverzeit
const char* tags,
uint64_t ts_ms,
const char* fields_fmt,
...);
+78 -192
View File
@@ -4,7 +4,7 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/queue.h" // bleibt drin (Header-Kompat / ggf. künftig wieder genutzt)
#include "esp_log.h"
#include "sensirion_i2c_hal.h"
@@ -27,16 +27,19 @@ esp_err_t i2cdev_done(void);
static const char* TAG = "sen5x_app";
/* ====================== Konfiguration ====================== */
#define SEN5X_QUEUE_LEN 16 // Puffergröße für Samples
#define SEN5X_PROD_STACK 4096
#define SEN5X_CONS_STACK 6144
#define SEN5X_PROD_PRIO 6 // Producer leicht höher priorisiert
#define SEN5X_CONS_PRIO 5
#define INFLUX_INIT_RETRY_MS 1000 // Retry-Intervall bis Influx init
#define INFLUX_FAILS_REDISCOVER 5 // Nach X Fehlern: Re-Init/Rediscovery
// Influx worker runtime (läuft in net_influx.c)
#define INFLUX_QUEUE_LEN 16
#define INFLUX_TASK_STACK 6144
#define INFLUX_TASK_PRIO 5
#define INFLUX_INIT_RETRY_MS 1000
#define INFLUX_FAILS_REDISCOVER 5
#define INFLUX_BACKOFF_START_MS 2000
#define INFLUX_BACKOFF_MAX_MS 300000
#define INFLUX_HTTP_TIMEOUT_MS 3000
/* ====================== Typen & State ====================== */
typedef struct {
@@ -47,11 +50,9 @@ typedef struct {
static bool g_inited = false;
static bool g_run = false;
static QueueHandle_t g_queue = NULL;
static TaskHandle_t g_task_prod = NULL;
static TaskHandle_t g_task_cons = NULL;
// Nur vom Consumer benutzt:
// App-Context
static char* g_device_tag = NULL; // z.B. "device=xyz"
/* ====================== Helfer ====================== */
@@ -119,6 +120,26 @@ static int16_t sen5x_read_measured_pm_values_float(float* pm1, float* pm2,
return 0;
}
/* ====================== App-Context: Device Tag ====================== */
static esp_err_t make_device_tag_once(void) {
if (g_device_tag) return ESP_OK;
char* dev = NULL;
if (storage_load_device_name(&dev) && dev && dev[0]) {
size_t need = strlen("device=") + strlen(dev) + 1;
g_device_tag = (char*)malloc(need);
if (!g_device_tag) {
if (dev) free(dev);
return ESP_ERR_NO_MEM;
}
snprintf(g_device_tag, need, "device=%s", dev);
free(dev);
return ESP_OK;
}
g_device_tag = strdup("device=unknown");
return g_device_tag ? ESP_OK : ESP_ERR_NO_MEM;
}
/* ====================== Producer-Task ====================== */
static void sen5x_task_producer(void* arg) {
(void)arg;
@@ -186,121 +207,7 @@ static void sen5x_task_producer(void* arg) {
if (!isfinite(pm1f)) snprintf(buf, sizeof(buf), "nan"); else snprintf(buf, sizeof(buf), "%.1f", (double)pm1f);
set_var_pm1p0(buf);
// Non-blocking enqueue; bei voller Queue: ältestes raus, neues rein
if (xQueueSend(g_queue, &s, 0) != pdPASS) {
sen5x_sample_t dump;
(void)xQueueReceive(g_queue, &dump, 0); // drop oldest
(void)xQueueSend(g_queue, &s, 0);
}
}
}
vTaskDelayUntil(&last_wake, period); // präzise 1 Hz
}
ESP_LOGI(TAG, "Producer stopping.");
vTaskDelete(NULL);
}
/* ====================== Consumer-Task ====================== */
static esp_err_t make_device_tag_once(void) {
if (g_device_tag) return ESP_OK;
char* dev = NULL;
if (storage_load_device_name(&dev) && dev && dev[0]) {
size_t need = strlen("device=") + strlen(dev) + 1;
g_device_tag = (char*)malloc(need);
if (!g_device_tag) {
if (dev) free(dev);
return ESP_ERR_NO_MEM;
}
snprintf(g_device_tag, need, "device=%s", dev);
free(dev);
return ESP_OK;
}
g_device_tag = strdup("device=unknown");
return g_device_tag ? ESP_OK : ESP_ERR_NO_MEM;
}
static bool influx_init_blocking(void) {
// Warte/Retry bis Influx-Konfig aus NVS (oder Discovery via net_influx_* intern) bereit ist
while (g_run) {
if (net_influx_init_from_nvs(NULL, 3000) == ESP_OK) {
if (make_device_tag_once() == ESP_OK) {
ESP_LOGI(TAG, "Influx ready, device tag: %s", g_device_tag);
return true;
} else {
ESP_LOGE(TAG, "Device-Tag allokation fehlgeschlagen");
return false; // fatal → Task kann enden
}
} else {
ESP_LOGW(TAG, "Influx nicht bereit – retry in %d ms", INFLUX_INIT_RETRY_MS);
vTaskDelay(pdMS_TO_TICKS(INFLUX_INIT_RETRY_MS));
}
}
return false;
}
static bool influx_reinit_with_backoff(uint32_t* backoff_ms) {
if (!backoff_ms) return false;
uint32_t wait_ms = *backoff_ms;
if (wait_ms == 0) wait_ms = INFLUX_BACKOFF_START_MS;
ESP_LOGW(TAG, "Re-Init Influx in %u ms", (unsigned)wait_ms);
vTaskDelay(pdMS_TO_TICKS(wait_ms));
bool ok = (net_influx_init_from_nvs(NULL, 3000) == ESP_OK);
if (ok) {
ESP_LOGI(TAG, "Influx Re-Init OK");
*backoff_ms = INFLUX_BACKOFF_START_MS;
return true;
}
// Exponentielles Backoff bis Max
if (wait_ms < INFLUX_BACKOFF_MAX_MS) {
uint32_t next = wait_ms * 2;
*backoff_ms = (next > INFLUX_BACKOFF_MAX_MS) ? INFLUX_BACKOFF_MAX_MS : next;
}
return false;
}
/* Hilfsfunktion: Felder-String ohne NaNs bauen (Influx lehnt NaN/Inf ab) */
static size_t append_field(char* dst, size_t cap, size_t pos, const char* key, double val, int precision) {
if (!isfinite(val)) return pos; // skip NaN/Inf
int n;
if (pos > 0) {
n = snprintf(dst + pos, (cap > pos) ? (cap - pos) : 0, ",");
pos += (n > 0) ? n : 0;
}
char fmt[16];
// z.B. "%.1f" oder "%.3f"
snprintf(fmt, sizeof(fmt), "%%s=%%.%df", precision);
n = snprintf(dst + pos, (cap > pos) ? (cap - pos) : 0, fmt, key, val);
pos += (n > 0) ? n : 0;
return pos;
}
static void sen5x_task_consumer(void* arg) {
(void)arg;
ESP_LOGI(TAG, "Consumer started.");
if (!influx_init_blocking()) {
ESP_LOGE(TAG, "Consumer beendet (Influx init fatal gescheitert).");
vTaskDelete(NULL);
return;
}
int fail_count = 0;
uint32_t backoff_ms = INFLUX_BACKOFF_START_MS;
for (; g_run; ) {
sen5x_sample_t s;
if (xQueueReceive(g_queue, &s, portMAX_DELAY) != pdTRUE) {
continue;
}
// Skaliere (physikalische Einheiten)
/* === Influx submit (generisch, variabel viele Felder) === */
const double pm1 = (s.pm1p0 == 0xFFFF) ? NAN : ((double)s.pm1p0 / 10.0);
const double pm2_5 = (s.pm2p5 == 0xFFFF) ? NAN : ((double)s.pm2p5 / 10.0);
const double pm4 = (s.pm4p0 == 0xFFFF) ? NAN : ((double)s.pm4p0 / 10.0);
@@ -310,48 +217,33 @@ static void sen5x_task_consumer(void* arg) {
const double temp = (double)s.temp_c;
const double rh = (double)s.rh_pct;
// Felder dynamisch ohne NaNs zusammenbauen
char fields[256];
size_t pos = 0;
pos = append_field(fields, sizeof(fields), pos, "pm1", pm1, 1);
pos = append_field(fields, sizeof(fields), pos, "pm2_5", pm2_5, 1);
pos = append_field(fields, sizeof(fields), pos, "pm4", pm4, 1);
pos = append_field(fields, sizeof(fields), pos, "pm10", pm10, 1);
pos = append_field(fields, sizeof(fields), pos, "temp", temp, 3);
pos = append_field(fields, sizeof(fields), pos, "rh", rh, 3);
pos = append_field(fields, sizeof(fields), pos, "voc", voc, 1);
pos = append_field(fields, sizeof(fields), pos, "nox", nox, 1);
net_influx_field_t f[] = {
{ .key="pm1", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=pm1 },
{ .key="pm2_5", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=pm2_5 },
{ .key="pm4", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=pm4 },
{ .key="pm10", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=pm10 },
{ .key="temp", .type=NET_INFLUX_F_FLOAT, .precision=3, .v.f=temp },
{ .key="rh", .type=NET_INFLUX_F_FLOAT, .precision=3, .v.f=rh },
{ .key="voc", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=voc },
{ .key="nox", .type=NET_INFLUX_F_FLOAT, .precision=1, .v.f=nox },
};
if (pos == 0) {
// Nichts valides → überspringen
ESP_LOGW(TAG, "Skipping write: no finite fields.");
continue;
}
fields[(pos < sizeof(fields)) ? pos : sizeof(fields) - 1] = '\0';
net_influx_point_t p = {
.measurement = "air",
.tags = g_device_tag ? g_device_tag : "device=unknown",
.ts_ms = 0, // Serverzeit
.fields = f,
.num_fields = sizeof(f) / sizeof(f[0]),
};
// Ohne Timestamp (0) → Influx vergibt Serverzeit. precision=ms im HTTP-Client ist ok.
esp_err_t e = net_influx_write_fields(
"air",
g_device_tag ? g_device_tag : "device=unknown",
0 /* ts_ms */,
"%s",
fields
);
if (e == ESP_OK) {
if (fail_count) ESP_LOGI(TAG, "Influx write recovered.");
fail_count = 0;
backoff_ms = INFLUX_BACKOFF_START_MS;
} else {
ESP_LOGW(TAG, "Influx write failed: %s", esp_err_to_name(e));
if (++fail_count >= INFLUX_FAILS_REDISCOVER) {
(void)influx_reinit_with_backoff(&backoff_ms);
fail_count = 0;
}
(void)net_influx_submit_point(&p);
}
}
ESP_LOGI(TAG, "Consumer stopping.");
vTaskDelayUntil(&last_wake, period); // präzise 1 Hz
}
ESP_LOGI(TAG, "Producer stopping.");
vTaskDelete(NULL);
}
@@ -388,15 +280,22 @@ esp_err_t sen5x_app_init(const sen5x_i2c_cfg_t* cfg) {
CHECK_SEN_OR_RET(sen5x_start_measurement());
ESP_LOGI(TAG, "Measurement started.");
// 5) Queue + Run-Flag
g_queue = xQueueCreate(SEN5X_QUEUE_LEN, sizeof(sen5x_sample_t));
if (!g_queue) {
ESP_LOGE(TAG, "xQueueCreate failed");
sen5x_stop_measurement();
sensirion_i2c_hal_free();
i2cdev_done();
return ESP_ERR_NO_MEM;
}
// 5) Device Tag (App-Kontext)
CHECK_ESP_OR_RET(make_device_tag_once());
// 6) Influx Worker starten (Influx-spezifische Init/Retry/Backoff läuft intern)
net_influx_runtime_cfg_t rt = {
.queue_len = INFLUX_QUEUE_LEN,
.task_stack = INFLUX_TASK_STACK,
.task_prio = INFLUX_TASK_PRIO,
.init_retry_ms = INFLUX_INIT_RETRY_MS,
.backoff_start_ms = INFLUX_BACKOFF_START_MS,
.backoff_max_ms = INFLUX_BACKOFF_MAX_MS,
.fails_before_reinit = INFLUX_FAILS_REDISCOVER,
.ca_cert_pem_opt = NULL,
.http_timeout_ms = INFLUX_HTTP_TIMEOUT_MS,
};
CHECK_ESP_OR_RET(net_influx_start(&rt));
g_run = true;
g_inited = true;
@@ -408,25 +307,18 @@ void sen5x_app_deinit(void) {
ESP_LOGI(TAG, "Deinitializing SEN5x app...");
// Tasks stoppen
// Producer stoppen
g_run = false;
if (g_task_prod) {
vTaskDelete(g_task_prod);
g_task_prod = NULL;
}
if (g_task_cons) {
vTaskDelete(g_task_cons);
g_task_cons = NULL;
}
// Queue
if (g_queue) {
vQueueDelete(g_queue);
g_queue = NULL;
}
// Influx worker stoppen
net_influx_stop();
// Influx Tag
// Device Tag
if (g_device_tag) {
free(g_device_tag);
g_device_tag = NULL;
@@ -443,7 +335,7 @@ void sen5x_app_deinit(void) {
/**
* Kompatibilität zur bisherigen main.c:
* Diese Task fungiert nun als "Launcher": startet Producer & Consumer und beendet sich dann.
* Diese Task fungiert nun als "Launcher": startet Producer und beendet sich dann.
* In main.c bleibt der Aufruf von xTaskCreate(sen5x_app_task, ...) unverändert.
*/
void sen5x_app_task(void* arg) {
@@ -455,20 +347,14 @@ void sen5x_app_task(void* arg) {
return;
}
// Producer / Consumer starten
if (xTaskCreate(sen5x_task_producer, "sen5x_prod", SEN5X_PROD_STACK, NULL, SEN5X_PROD_PRIO, &g_task_prod) != pdPASS) {
// Producer starten
if (xTaskCreate(sen5x_task_producer, "sen5x_prod",
SEN5X_PROD_STACK, NULL, SEN5X_PROD_PRIO, &g_task_prod) != pdPASS) {
ESP_LOGE(TAG, "Failed to create producer task");
vTaskDelete(NULL);
return;
}
if (xTaskCreate(sen5x_task_consumer, "sen5x_cons", SEN5X_CONS_STACK, NULL, SEN5X_CONS_PRIO, &g_task_cons) != pdPASS) {
ESP_LOGE(TAG, "Failed to create consumer task");
vTaskDelete(g_task_prod);
g_task_prod = NULL;
vTaskDelete(NULL);
return;
}
ESP_LOGI(TAG, "Launcher done (producer+consumer started).");
ESP_LOGI(TAG, "Launcher done (producer started).");
vTaskDelete(NULL); // Launcher beendet sich selbst
}