Refactor notification system to use HTTP POST for buzz events, updating configuration and documentation accordingly.
This commit is contained in:
+99
-78
@@ -1,5 +1,5 @@
|
||||
// Wi-Fi station with exponential-backoff reconnect + ntfy.sh HTTPS notifications.
|
||||
// Sending happens in its own task fed by a queue so networking can never block audio.
|
||||
// Wi-Fi station with exponential-backoff reconnect and queued event POSTs.
|
||||
// HTTP requests run in their own task so networking cannot block audio.
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "esp_crt_bundle.h"
|
||||
@@ -13,29 +13,26 @@
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/task.h"
|
||||
#include "config_store.h"
|
||||
#include "notifier.h"
|
||||
|
||||
static const char *TAG = "notify";
|
||||
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define SEND_ATTEMPTS 4
|
||||
#define NET_TASK_STACK 8192
|
||||
#define NET_TASK_PRIO 5
|
||||
#define BACKOFF_MIN_MS 1000
|
||||
#define BACKOFF_MAX_MS 60000
|
||||
#define EVENT_BODY_SIZE 512
|
||||
|
||||
typedef struct {
|
||||
notif_kind_t kind;
|
||||
char msg[96];
|
||||
} notif_msg_t;
|
||||
uint32_t delay_ms;
|
||||
} event_msg_t;
|
||||
|
||||
static QueueHandle_t s_q;
|
||||
static EventGroupHandle_t s_eg;
|
||||
static esp_timer_handle_t s_reconnect_timer;
|
||||
static uint32_t s_backoff_ms = BACKOFF_MIN_MS;
|
||||
static notifier_stats_t s_stats;
|
||||
static bool s_wifi_configured;
|
||||
|
||||
static void reconnect_cb(void *arg)
|
||||
{
|
||||
@@ -53,7 +50,7 @@ static void wifi_event_handler(void *arg, esp_event_base_t base, int32_t id, voi
|
||||
xEventGroupClearBits(s_eg, WIFI_CONNECTED_BIT);
|
||||
s_stats.wifi_connected = false;
|
||||
ESP_LOGW(TAG, "Wi-Fi disconnected, retry in %lu ms", (unsigned long)s_backoff_ms);
|
||||
esp_timer_stop(s_reconnect_timer); // ok if not running
|
||||
esp_timer_stop(s_reconnect_timer);
|
||||
esp_timer_start_once(s_reconnect_timer, (uint64_t)s_backoff_ms * 1000ULL);
|
||||
s_backoff_ms = s_backoff_ms * 2 > BACKOFF_MAX_MS ? BACKOFF_MAX_MS : s_backoff_ms * 2;
|
||||
} else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
|
||||
@@ -66,93 +63,107 @@ static void wifi_event_handler(void *arg, esp_event_base_t base, int32_t id, voi
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t post_ntfy(const char *topic, const char *title, const char *prio, const char *tags, const char *body)
|
||||
static bool make_event_body(uint32_t delay_ms, char *body, size_t body_size)
|
||||
{
|
||||
char url[192];
|
||||
size_t sl = strlen(CONFIG_BUZZ_NTFY_SERVER);
|
||||
while (sl > 0 && CONFIG_BUZZ_NTFY_SERVER[sl - 1] == '/') sl--;
|
||||
snprintf(url, sizeof(url), "%.*s/%s", (int)sl, CONFIG_BUZZ_NTFY_SERVER, topic);
|
||||
int n = snprintf(body, body_size, "{\"delay_ms\":%lu,\"secret\":\"",
|
||||
(unsigned long)delay_ms);
|
||||
if (n < 0 || (size_t)n >= body_size) return false;
|
||||
size_t used = (size_t)n;
|
||||
|
||||
for (const unsigned char *p = (const unsigned char *)CONFIG_BUZZ_EVENT_SECRET; *p; p++) {
|
||||
char escaped[7];
|
||||
const char *text = (const char *)p;
|
||||
size_t len = 1;
|
||||
if (*p == '"' || *p == '\\') {
|
||||
escaped[0] = '\\';
|
||||
escaped[1] = (char)*p;
|
||||
text = escaped;
|
||||
len = 2;
|
||||
} else if (*p < 0x20) {
|
||||
snprintf(escaped, sizeof(escaped), "\\u%04x", *p);
|
||||
text = escaped;
|
||||
len = 6;
|
||||
}
|
||||
if (len >= body_size - used) return false;
|
||||
memcpy(body + used, text, len);
|
||||
used += len;
|
||||
}
|
||||
if (body_size - used < 3) return false;
|
||||
body[used++] = '"';
|
||||
body[used++] = '}';
|
||||
body[used] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
// One queue item produces at most one HTTP request: there is deliberately no retry.
|
||||
static bool post_event(uint32_t delay_ms)
|
||||
{
|
||||
EventBits_t bits = xEventGroupWaitBits(s_eg, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE,
|
||||
pdMS_TO_TICKS(15000));
|
||||
if (!(bits & WIFI_CONNECTED_BIT)) {
|
||||
ESP_LOGE(TAG, "Wi-Fi unavailable; event was not sent");
|
||||
return false;
|
||||
}
|
||||
|
||||
char body[EVENT_BODY_SIZE];
|
||||
if (!make_event_body(delay_ms, body, sizeof(body))) {
|
||||
ESP_LOGE(TAG, "event body exceeds buffer");
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *url = "http://192.168.0.154:3000/event/buzz";
|
||||
esp_http_client_config_t cfg = {
|
||||
.url = url,
|
||||
.method = HTTP_METHOD_POST,
|
||||
.timeout_ms = 10000,
|
||||
.disable_auto_redirect = true,
|
||||
.crt_bundle_attach = esp_crt_bundle_attach,
|
||||
};
|
||||
esp_http_client_handle_t c = esp_http_client_init(&cfg);
|
||||
if (!c) return ESP_ERR_NO_MEM;
|
||||
esp_http_client_set_header(c, "Title", title);
|
||||
esp_http_client_set_header(c, "Priority", prio);
|
||||
esp_http_client_set_header(c, "Tags", tags);
|
||||
esp_http_client_set_post_field(c, body, (int)strlen(body));
|
||||
esp_err_t err = esp_http_client_perform(c);
|
||||
if (err == ESP_OK) {
|
||||
int st = esp_http_client_get_status_code(c);
|
||||
if (st != 200) {
|
||||
ESP_LOGW(TAG, "ntfy HTTP status %d", st);
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "HTTP perform: %s", esp_err_to_name(err));
|
||||
if (!c) {
|
||||
ESP_LOGE(TAG, "could not create HTTP client");
|
||||
return false;
|
||||
}
|
||||
esp_err_t err = esp_http_client_set_header(c, "Content-Type", "application/json");
|
||||
if (err == ESP_OK) err = esp_http_client_set_post_field(c, body, (int)strlen(body));
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "configure event POST: %s", esp_err_to_name(err));
|
||||
esp_http_client_cleanup(c);
|
||||
return false;
|
||||
}
|
||||
esp_http_client_cleanup(c);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Retry a few times with backoff while Wi-Fi is down / the request fails, then drop.
|
||||
static bool deliver(const char *topic, const char *title, const char *prio, const char *tags, const char *body)
|
||||
{
|
||||
uint32_t wait_ms = 2000;
|
||||
for (int attempt = 1; attempt <= SEND_ATTEMPTS; attempt++) {
|
||||
EventBits_t bits = xEventGroupWaitBits(s_eg, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, pdMS_TO_TICKS(15000));
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
if (post_ntfy(topic, title, prio, tags, body) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "sent '%s' to %s (attempt %d)", body, topic, attempt);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Wi-Fi down (attempt %d/%d)", attempt, SEND_ATTEMPTS);
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(wait_ms));
|
||||
wait_ms *= 2;
|
||||
err = esp_http_client_perform(c);
|
||||
int status = err == ESP_OK ? esp_http_client_get_status_code(c) : 0;
|
||||
esp_http_client_cleanup(c);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "event POST failed: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
ESP_LOGE(TAG, "giving up on '%s', dropped", body);
|
||||
return false;
|
||||
if (status < 200 || status >= 300) {
|
||||
ESP_LOGE(TAG, "event endpoint returned HTTP %d", status);
|
||||
return false;
|
||||
}
|
||||
ESP_LOGI(TAG, "sent buzz duration %lu ms (HTTP %d)", (unsigned long)delay_ms, status);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void net_task(void *arg)
|
||||
{
|
||||
int64_t last_hb_us = esp_timer_get_time();
|
||||
notif_msg_t m;
|
||||
event_msg_t msg;
|
||||
for (;;) {
|
||||
if (xQueueReceive(s_q, &m, pdMS_TO_TICKS(30000)) == pdTRUE) {
|
||||
bool ok = deliver(CONFIG_BUZZ_NTFY_TOPIC, "Door buzzer",
|
||||
m.kind == NOTIF_ALERT ? "high" : "default",
|
||||
m.kind == NOTIF_ALERT ? "bell" : "wrench", m.msg);
|
||||
if (ok) s_stats.sent_ok++; else s_stats.sent_failed++;
|
||||
last_hb_us = esp_timer_get_time(); // an alert proves liveness; restart the heartbeat timer
|
||||
}
|
||||
app_config_t cfg;
|
||||
config_get(&cfg);
|
||||
if (cfg.heartbeat_h > 0 &&
|
||||
(esp_timer_get_time() - last_hb_us) >= (int64_t)cfg.heartbeat_h * 3600LL * 1000000LL) {
|
||||
last_hb_us = esp_timer_get_time();
|
||||
if (deliver(CONFIG_BUZZ_NTFY_HB_TOPIC, "Door buzzer heartbeat", "min", "green_heart", "alive")) {
|
||||
s_stats.sent_ok++;
|
||||
} else {
|
||||
s_stats.sent_failed++;
|
||||
}
|
||||
if (xQueueReceive(s_q, &msg, portMAX_DELAY) == pdTRUE) {
|
||||
if (post_event(msg.delay_ms)) s_stats.sent_ok++;
|
||||
else s_stats.sent_failed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool notifier_send(notif_kind_t kind, const char *msg)
|
||||
bool notifier_send(uint32_t delay_ms)
|
||||
{
|
||||
notif_msg_t m = { .kind = kind };
|
||||
snprintf(m.msg, sizeof(m.msg), "%s", msg);
|
||||
if (xQueueSend(s_q, &m, 0) != pdTRUE) {
|
||||
event_msg_t msg = { .delay_ms = delay_ms };
|
||||
if (xQueueSend(s_q, &msg, 0) != pdTRUE) {
|
||||
s_stats.dropped++;
|
||||
ESP_LOGW(TAG, "notification queue full, dropped");
|
||||
ESP_LOGW(TAG, "event queue full, dropped");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -161,17 +172,26 @@ bool notifier_send(notif_kind_t kind, const char *msg)
|
||||
void notifier_get_stats(notifier_stats_t *out)
|
||||
{
|
||||
*out = s_stats;
|
||||
out->wifi_configured = s_wifi_configured;
|
||||
out->wifi_configured = strlen(CONFIG_BUZZ_WIFI_SSID) > 0;
|
||||
out->endpoint_configured = strlen(CONFIG_BUZZ_EVENT_URL) > 0;
|
||||
}
|
||||
|
||||
esp_err_t notifier_init(void)
|
||||
{
|
||||
s_q = xQueueCreate(4, sizeof(notif_msg_t));
|
||||
s_q = xQueueCreate(4, sizeof(event_msg_t));
|
||||
s_eg = xEventGroupCreate();
|
||||
if (!s_q || !s_eg) return ESP_ERR_NO_MEM;
|
||||
|
||||
s_wifi_configured = strlen(CONFIG_BUZZ_WIFI_SSID) > 0;
|
||||
if (s_wifi_configured) {
|
||||
s_stats.endpoint_configured = strlen(CONFIG_BUZZ_EVENT_URL) > 0;
|
||||
if (!s_stats.endpoint_configured) {
|
||||
ESP_LOGW(TAG, "no event endpoint configured; buzz events will be dropped");
|
||||
}
|
||||
if (!strlen(CONFIG_BUZZ_EVENT_SECRET)) {
|
||||
ESP_LOGW(TAG, "event secret is empty; configure it before using the endpoint");
|
||||
}
|
||||
|
||||
s_stats.wifi_configured = strlen(CONFIG_BUZZ_WIFI_SSID) > 0;
|
||||
if (s_stats.wifi_configured) {
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
@@ -193,8 +213,9 @@ esp_err_t notifier_init(void)
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wc));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
} else {
|
||||
ESP_LOGW(TAG, "no Wi-Fi SSID configured (menuconfig): notifications will be dropped");
|
||||
ESP_LOGW(TAG, "no Wi-Fi SSID configured; event requests cannot be sent");
|
||||
}
|
||||
|
||||
BaseType_t ok = xTaskCreatePinnedToCore(net_task, "net", NET_TASK_STACK, NULL, NET_TASK_PRIO, NULL, 0);
|
||||
return ok == pdPASS ? ESP_OK : ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user