Added Scheduler
This commit is contained in:
124
main/scheduler.h
Normal file
124
main/scheduler.h
Normal file
@ -0,0 +1,124 @@
|
||||
#ifndef SCHEDULER_H
|
||||
#define SCHEDULER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
// Maximum number of schedules per pump
|
||||
#define SCHEDULER_MAX_SCHEDULES_PER_PUMP 4
|
||||
#define SCHEDULER_MAX_PUMPS 2
|
||||
|
||||
// Schedule types
|
||||
typedef enum {
|
||||
SCHEDULE_TYPE_DISABLED = 0,
|
||||
SCHEDULE_TYPE_INTERVAL, // Every X minutes
|
||||
SCHEDULE_TYPE_TIME_OF_DAY, // Daily at specific time
|
||||
SCHEDULE_TYPE_DAYS_TIME, // Specific days at specific time
|
||||
SCHEDULE_TYPE_MAX
|
||||
} schedule_type_t;
|
||||
|
||||
// Days of week bitmask (bit 0 = Sunday, bit 6 = Saturday)
|
||||
#define SCHEDULE_DAY_SUNDAY (1 << 0)
|
||||
#define SCHEDULE_DAY_MONDAY (1 << 1)
|
||||
#define SCHEDULE_DAY_TUESDAY (1 << 2)
|
||||
#define SCHEDULE_DAY_WEDNESDAY (1 << 3)
|
||||
#define SCHEDULE_DAY_THURSDAY (1 << 4)
|
||||
#define SCHEDULE_DAY_FRIDAY (1 << 5)
|
||||
#define SCHEDULE_DAY_SATURDAY (1 << 6)
|
||||
#define SCHEDULE_DAY_WEEKDAYS (SCHEDULE_DAY_MONDAY | SCHEDULE_DAY_TUESDAY | \
|
||||
SCHEDULE_DAY_WEDNESDAY | SCHEDULE_DAY_THURSDAY | \
|
||||
SCHEDULE_DAY_FRIDAY)
|
||||
#define SCHEDULE_DAY_WEEKEND (SCHEDULE_DAY_SATURDAY | SCHEDULE_DAY_SUNDAY)
|
||||
#define SCHEDULE_DAY_ALL 0x7F
|
||||
|
||||
// Schedule configuration
|
||||
typedef struct {
|
||||
schedule_type_t type;
|
||||
bool enabled;
|
||||
|
||||
// Timing configuration
|
||||
uint32_t interval_minutes; // For SCHEDULE_TYPE_INTERVAL
|
||||
uint8_t hour; // For TIME_OF_DAY and DAYS_TIME (0-23)
|
||||
uint8_t minute; // For TIME_OF_DAY and DAYS_TIME (0-59)
|
||||
uint8_t days_mask; // For DAYS_TIME (bitmask)
|
||||
|
||||
// Watering configuration
|
||||
uint32_t duration_ms; // How long to water (milliseconds)
|
||||
uint8_t speed_percent; // Pump speed (0-100)
|
||||
|
||||
// Runtime info (not saved to NVS)
|
||||
time_t last_run; // Last execution timestamp
|
||||
time_t next_run; // Next scheduled run
|
||||
} schedule_config_t;
|
||||
|
||||
// Schedule entry with ID
|
||||
typedef struct {
|
||||
uint8_t pump_id; // Which pump (1 or 2)
|
||||
uint8_t schedule_id; // Schedule slot (0-3)
|
||||
schedule_config_t config; // Schedule configuration
|
||||
} schedule_entry_t;
|
||||
|
||||
// Schedule status
|
||||
typedef struct {
|
||||
bool holiday_mode; // Global disable for all schedules
|
||||
bool time_synchronized; // Whether we have valid time
|
||||
time_t last_sync_time; // When time was last synchronized
|
||||
uint32_t active_schedules; // Number of active schedules
|
||||
} scheduler_status_t;
|
||||
|
||||
// Callbacks
|
||||
typedef void (*schedule_trigger_callback_t)(uint8_t pump_id, uint8_t schedule_id,
|
||||
uint32_t duration_ms, uint8_t speed_percent);
|
||||
typedef void (*schedule_status_callback_t)(const char* status_json);
|
||||
|
||||
// Scheduler functions
|
||||
esp_err_t scheduler_init(void);
|
||||
esp_err_t scheduler_deinit(void);
|
||||
|
||||
// Schedule management
|
||||
esp_err_t scheduler_add_schedule(uint8_t pump_id, uint8_t schedule_id,
|
||||
const schedule_config_t *config);
|
||||
esp_err_t scheduler_get_schedule(uint8_t pump_id, uint8_t schedule_id,
|
||||
schedule_config_t *config);
|
||||
esp_err_t scheduler_remove_schedule(uint8_t pump_id, uint8_t schedule_id);
|
||||
esp_err_t scheduler_enable_schedule(uint8_t pump_id, uint8_t schedule_id, bool enable);
|
||||
esp_err_t scheduler_clear_all_schedules(void);
|
||||
|
||||
// Time management
|
||||
esp_err_t scheduler_set_time(time_t current_time);
|
||||
esp_err_t scheduler_sync_time_ntp(void);
|
||||
bool scheduler_is_time_synchronized(void);
|
||||
time_t scheduler_get_current_time(void);
|
||||
|
||||
// Holiday mode
|
||||
esp_err_t scheduler_set_holiday_mode(bool enabled);
|
||||
bool scheduler_get_holiday_mode(void);
|
||||
|
||||
// Status and information
|
||||
esp_err_t scheduler_get_status(scheduler_status_t *status);
|
||||
esp_err_t scheduler_get_next_run_times(time_t *next_runs, size_t max_count);
|
||||
esp_err_t scheduler_get_all_schedules(schedule_entry_t *entries, size_t max_entries,
|
||||
size_t *count);
|
||||
|
||||
// JSON serialization for MQTT
|
||||
esp_err_t scheduler_schedule_to_json(uint8_t pump_id, uint8_t schedule_id,
|
||||
char *buffer, size_t buffer_size);
|
||||
esp_err_t scheduler_json_to_schedule(const char *json, uint8_t pump_id,
|
||||
uint8_t schedule_id);
|
||||
esp_err_t scheduler_status_to_json(char *buffer, size_t buffer_size);
|
||||
|
||||
// Callbacks
|
||||
void scheduler_register_trigger_callback(schedule_trigger_callback_t callback);
|
||||
void scheduler_register_status_callback(schedule_status_callback_t callback);
|
||||
|
||||
// Manual trigger (for testing)
|
||||
esp_err_t scheduler_trigger_schedule(uint8_t pump_id, uint8_t schedule_id);
|
||||
|
||||
// Utility functions
|
||||
const char* scheduler_get_type_string(schedule_type_t type);
|
||||
const char* scheduler_get_days_string(uint8_t days_mask, char *buffer, size_t size);
|
||||
time_t scheduler_calculate_next_run(const schedule_config_t *config, time_t from_time);
|
||||
|
||||
#endif // SCHEDULER_H
|
||||
Reference in New Issue
Block a user