From 275a425d8643ddf6e0a6ab01678bacaa18e5e76e Mon Sep 17 00:00:00 2001 From: Calvin Owens Date: Fri, 31 Mar 2023 13:00:59 -0700 Subject: [PATCH] Don't erase queued samples until after submission There's no reason not to keep the samples and try again, but we will ultimately toss them if the NVS fills up before a submission succeeds. --- main/main.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/main/main.c b/main/main.c index 91f5821..fab7442 100644 --- a/main/main.c +++ b/main/main.c @@ -835,7 +835,7 @@ void app_main(void) if (have_queued_samples) { nvs_handle_t nvsh; - if (nvs_open("data", NVS_READWRITE, &nvsh) == ESP_OK) { + if (nvs_open("data", NVS_READONLY, &nvsh) == ESP_OK) { ret = nvs_entry_find("nvs", "data", NVS_TYPE_U64, &it); while (ret == ESP_OK) { struct nvsdata64 sample; @@ -843,7 +843,6 @@ void app_main(void) nvs_entry_info(it, &info); nvs_get_u64(nvsh, info.key, &sample.u64); - nvs_erase_key(nvsh, info.key); d = cJSON_CreateObject(); cJSON_AddNumberToObject(d, "epoch", @@ -858,7 +857,6 @@ void app_main(void) } nvs_release_iterator(it); - nvs_commit(nvsh); nvs_close(nvsh); } } @@ -886,6 +884,29 @@ void app_main(void) esp_wifi_stop(); esp_wifi_deinit(); + /* + * If we successfully submitted the queued data, delete it. + */ + + if (have_queued_samples && ret == ESP_OK) { + nvs_handle_t nvsh; + + if (nvs_open("data", NVS_READWRITE, &nvsh) == ESP_OK) { + ret = nvs_entry_find("nvs", "data", NVS_TYPE_U64, &it); + while (ret == ESP_OK) { + nvs_entry_info_t info; + + nvs_entry_info(it, &info); + nvs_erase_key(nvsh, info.key); + ret = nvs_entry_next(&it); + } + + nvs_release_iterator(it); + nvs_commit(nvsh); + nvs_close(nvsh); + } + } + /* * If we have a target wake time, compute how long we should sleep. */