Refactor notification system to use HTTP POST for buzz events, updating configuration and documentation accordingly.
This commit is contained in:
+30
-20
@@ -13,11 +13,19 @@
|
||||
static const char *TAG = "config";
|
||||
#define NVS_NS "buzz"
|
||||
#define CFG_KEY "cfg"
|
||||
#define CFG_MAGIC 0xB0220003u
|
||||
#define CFG_MAGIC 0xB0220004u
|
||||
#define CFG_MAGIC_PRE_EVENT_POST 0xB0220003u
|
||||
#define CFG_MAGIC_PRE_500MS 0xB0220002u
|
||||
#define CFG_MAGIC_PRE_COOLDOWN_MS 0xB0220001u
|
||||
|
||||
typedef struct { uint32_t magic; app_config_t cfg; } cfg_blob_t;
|
||||
typedef struct {
|
||||
uint32_t magic;
|
||||
float threshold, min_rms;
|
||||
uint32_t win_n, win_m, cooldown_ms;
|
||||
float noise_mult, adapt_weight;
|
||||
uint32_t monitor_ms, heartbeat_h;
|
||||
} legacy_cfg_blob_t;
|
||||
|
||||
typedef enum { PT_FLOAT, PT_UINT } ptype_t;
|
||||
typedef struct {
|
||||
@@ -38,7 +46,6 @@ static const param_t s_params[] = {
|
||||
P(noise_mult, PT_FLOAT, 1.0, 100.0, "learn/adapt gate = noise floor x this"),
|
||||
P(adapt_weight, PT_FLOAT, 0.0, 1.0, "weight of new template in adapt"),
|
||||
P(monitor_ms, PT_UINT, 10, 60000, "min interval between monitor lines (ms)"),
|
||||
P(heartbeat_h, PT_UINT, 0, 720, "heartbeat period (h), 0 = off"),
|
||||
};
|
||||
#define NPARAMS (sizeof(s_params) / sizeof(s_params[0]))
|
||||
|
||||
@@ -55,7 +62,6 @@ static void load_defaults(app_config_t *c)
|
||||
c->noise_mult = CONFIG_BUZZ_NOISE_MULT_X10 / 10.0f;
|
||||
c->adapt_weight = CONFIG_BUZZ_ADAPT_WEIGHT_PCT / 100.0f;
|
||||
c->monitor_ms = CONFIG_BUZZ_MONITOR_MS;
|
||||
c->heartbeat_h = CONFIG_BUZZ_HEARTBEAT_HOURS;
|
||||
if (c->win_m > c->win_n) c->win_m = c->win_n;
|
||||
}
|
||||
|
||||
@@ -94,25 +100,29 @@ esp_err_t config_init(void)
|
||||
load_defaults(&s_cfg);
|
||||
nvs_handle_t h;
|
||||
if (nvs_open(NVS_NS, NVS_READONLY, &h) == ESP_OK) {
|
||||
cfg_blob_t blob;
|
||||
union {
|
||||
cfg_blob_t current;
|
||||
legacy_cfg_blob_t legacy;
|
||||
} blob;
|
||||
size_t len = sizeof(blob);
|
||||
if (nvs_get_blob(h, CFG_KEY, &blob, &len) == ESP_OK && len == sizeof(blob)) {
|
||||
if (blob.magic == CFG_MAGIC) {
|
||||
s_cfg = blob.cfg;
|
||||
if (nvs_get_blob(h, CFG_KEY, &blob, &len) == ESP_OK) {
|
||||
if (len == sizeof(cfg_blob_t) && blob.current.magic == CFG_MAGIC) {
|
||||
s_cfg = blob.current.cfg;
|
||||
ESP_LOGI(TAG, "loaded saved config from NVS");
|
||||
} else if (blob.magic == CFG_MAGIC_PRE_COOLDOWN_MS) {
|
||||
s_cfg = blob.cfg;
|
||||
s_cfg.cooldown_ms = CONFIG_BUZZ_COOLDOWN_MS;
|
||||
ESP_LOGI(TAG, "migrating saved config to event-grouping cooldown");
|
||||
err = save_locked();
|
||||
if (err != ESP_OK) {
|
||||
nvs_close(h);
|
||||
return err;
|
||||
}
|
||||
} else if (blob.magic == CFG_MAGIC_PRE_500MS) {
|
||||
s_cfg = blob.cfg;
|
||||
s_cfg.cooldown_ms = CONFIG_BUZZ_COOLDOWN_MS;
|
||||
ESP_LOGI(TAG, "updating saved cooldown for buzz-event grouping");
|
||||
} else if (len == sizeof(legacy_cfg_blob_t) &&
|
||||
(blob.legacy.magic == CFG_MAGIC_PRE_EVENT_POST ||
|
||||
blob.legacy.magic == CFG_MAGIC_PRE_500MS ||
|
||||
blob.legacy.magic == CFG_MAGIC_PRE_COOLDOWN_MS)) {
|
||||
s_cfg.threshold = blob.legacy.threshold;
|
||||
s_cfg.min_rms = blob.legacy.min_rms;
|
||||
s_cfg.win_n = blob.legacy.win_n;
|
||||
s_cfg.win_m = blob.legacy.win_m;
|
||||
s_cfg.cooldown_ms = blob.legacy.magic == CFG_MAGIC_PRE_EVENT_POST
|
||||
? blob.legacy.cooldown_ms : CONFIG_BUZZ_COOLDOWN_MS;
|
||||
s_cfg.noise_mult = blob.legacy.noise_mult;
|
||||
s_cfg.adapt_weight = blob.legacy.adapt_weight;
|
||||
s_cfg.monitor_ms = blob.legacy.monitor_ms;
|
||||
ESP_LOGI(TAG, "migrating saved detector settings; heartbeat setting removed");
|
||||
err = save_locked();
|
||||
if (err != ESP_OK) {
|
||||
nvs_close(h);
|
||||
|
||||
Reference in New Issue
Block a user