Browse Source

cleanup_task: Add task to automatically delete outdated entries

Helmut Pozimski 1 year ago
parent
commit
11f9ac4c5a
4 changed files with 43 additions and 3 deletions
  1. 1 1
      main/CMakeLists.txt
  2. 28 0
      main/cleanup_task.c
  3. 7 0
      main/cleanup_task.h
  4. 7 2
      main/esp32_alarm_clock_main.c

+ 1 - 1
main/CMakeLists.txt

@@ -1,2 +1,2 @@
-idf_component_register(SRCS "esp32_alarm_clock_main.c" "wifi.c" "storage.c" "api.c" "ds3231.c" "bcd.c" "tm1637.c" "time_sync.c" "time_display.c" "alarm.c" "time_conversion.c" "alarm_task.c"
+idf_component_register(SRCS "esp32_alarm_clock_main.c" "wifi.c" "storage.c" "api.c" "ds3231.c" "bcd.c" "tm1637.c" "time_sync.c" "time_display.c" "alarm.c" "time_conversion.c" "alarm_task.c" "cleanup_task.c"
                     INCLUDE_DIRS "")

+ 28 - 0
main/cleanup_task.c

@@ -0,0 +1,28 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+ 
+#include <esp_log.h>
+#include <freertos/FreeRTOS.h>
+#include <freertos/event_groups.h>
+
+#include "storage.h"
+#include "ds3231.h"
+
+#define SECONDS_PER_DAY 86400
+#define TAG "cleanup-task"
+
+void cleanup_task(void *pvParameters) {
+	char* namespace = (char*) pvParameters;
+	struct tm current_time;
+	
+	while (1) {
+		ESP_LOGI(TAG, "Executing database cleanup");
+		ds3231_read_date_time(&current_time);
+		delete_outdated_entries(&current_time, namespace);
+		vTaskDelay(SECONDS_PER_DAY * 1000 / portTICK_PERIOD_MS);
+	}
+	vTaskDelete( NULL ); 
+}

+ 7 - 0
main/cleanup_task.h

@@ -0,0 +1,7 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+ 
+void cleanup_task(void *pvParameters);

+ 7 - 2
main/esp32_alarm_clock_main.c

@@ -22,6 +22,10 @@
 #include "alarm_task.h"
 #include "api.h"
 #include "storage.h"
+#include "cleanup_task.h"
+
+
+static const char* STORAGE_NAMESPACE = "a1";
 
 static void IRAM_ATTR gpio_interrupt_handler(void *args)
 {
@@ -37,7 +41,7 @@ static void init_peripherals(uint8_t* button_pressed_flag) {
 	wifi_init();
   	wifi_start();
 
-	ESP_ERROR_CHECK(storage_init("a1"));
+	ESP_ERROR_CHECK(storage_init(STORAGE_NAMESPACE));
 	ds3231_init(DS3231_SDA_PIN, DS3231_SCL_PIN);
 	tm1637_init(TM1637_CLK_PIN, TM1637_DIO_PIN, true, 1);
 
@@ -72,6 +76,7 @@ void app_main(void) {
 		await_sntp_sync();
 	}
 	
-	xTaskCreate(display_update_task, "display_update_task", 2048, NULL, 7, NULL);
+	xTaskCreate(display_update_task, "display_update_task", 2048, NULL, 8, NULL);
 	xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &alarm_task_parameters, 7, NULL);
+	xTaskCreate(cleanup_task, "cleanup_task", 2048, STORAGE_NAMESPACE, 8, NULL);
 }