1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- /*
- * 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 "wifi.h"
- #include "ds3231.h"
- #include "time_conversion.h"
- #define TAG "wifi_management_task"
- #define SECONDS_PER_DAY 86400
- static uint32_t calculate_current_second(struct tm* local_time) {
- return local_time->tm_hour * 60 * 60 + local_time->tm_min * 60 + local_time->tm_sec;
- }
- static uint32_t determine_sleep_seconds(struct tm* local_time) {
- uint32_t current_second = calculate_current_second(local_time);
- uint32_t reference_second;
- if (local_time->tm_hour >= 22) {
- reference_second = (SECONDS_PER_DAY - current_second) + 6 * 60 * 60;
- } else {
- reference_second = (22 * 60 * 60) - current_second;
- }
- return reference_second;
- }
- void wifi_management_task(void *pvParameters) {
- struct tm current_time;
- uint8_t* wifi_enabled_flag = (uint8_t*) pvParameters;
-
- while(1) {
- ESP_LOGI(TAG, "wifi_managent_task is executed");
- struct tm* local_time;
- ds3231_read_date_time(¤t_time);
- local_time = convert_to_local(¤t_time);
- ESP_LOGI(TAG, "Current local hour: %d", local_time->tm_hour);
- if (local_time->tm_hour >= 22) {
- ESP_LOGI(TAG, "Stopping wifi");
- wifi_stop();
- } else if (local_time->tm_hour >= 6 && !wifi_enabled_flag) {
- ESP_LOGI(TAG, "Starting wifi");
- wifi_start();
- }
- uint32_t sleep_seconds = determine_sleep_seconds(local_time);
- ESP_LOGI(TAG, "Sleeping for %ld seconds", sleep_seconds);
- vTaskDelay(sleep_seconds * 1000 / portTICK_PERIOD_MS);
- }
- vTaskDelete( NULL );
- }
|