Skip to content

Commit

Permalink
Don't erase queued samples until after submission
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jcalvinowens committed Mar 12, 2024
1 parent 7f60f18 commit 275a425
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,14 @@ 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;
nvs_entry_info_t info;

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",
Expand All @@ -858,7 +857,6 @@ void app_main(void)
}

nvs_release_iterator(it);
nvs_commit(nvsh);
nvs_close(nvsh);
}
}
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 275a425

Please sign in to comment.