Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
TimerService.h
Go to the documentation of this file.
1
37#pragma once
38
39#include <cstdint>
40#include <ctime>
41#include <string>
42#include <unordered_map>
43#include <functional>
44#include <FreeRTOS.h>
45#include <semphr.h>
46#include <timers.h>
47
48#include "events/Event.h"
49#include "time/TimeOfDay.h"
50#include "time/DaysOfWeek.h"
51
52
65
70{
71public:
72
73
75 ~TimerService() = default;
76
77 void withLock(const std::function<void()>& fn);
78
79
83 static TimerService &instance();
84
91 void scheduleAt(time_t unixTime, const Event &event);
92 void scheduleAt(time_t unixTime, const Event &event, const std::string &jobId);
93
100 void scheduleEvery(uint32_t intervalMs, const Event &event);
101 void scheduleEvery(uint32_t intervalMs, const Event &event, const std::string &jobId);
102
110 void scheduleDailyAt(TimeOfDay time, DaysOfWeek days, const Event &event);
111 void scheduleDailyAt(TimeOfDay time, DaysOfWeek days, const Event &event, const std::string &jobId);
112
122 void scheduleDuration(TimeOfDay start, DaysOfWeek days, uint32_t durationMs,
123 const Event &startEvent, const Event &stopEvent);
124 void scheduleDuration(TimeOfDay start, DaysOfWeek days, uint32_t durationMs,
125 const Event &startEvent, const Event &stopEvent, const std::string &baseJobId);
126
132 void checkMissedEvents(time_t now);
133
134 bool cancel(const std::string &jobId);
135
139 void scheduleCallbackAt(time_t unixTime, std::function<void()> callback);
140
141
142private:
143
144 SemaphoreHandle_t lock_;
145 static StaticSemaphore_t lockBuffer_;
146
147 std::unordered_map<std::string, TimerHandle_t> scheduledJobs;
148 // Note: TimerHandles are created and managed by FreeRTOS.
149 // They will automatically clean up after firing if pdFALSE is passed to xTimerDelete.
150 // If pdTRUE is passed, the timer will be deleted immediately after firing.
151 // This is useful for one-shot timers.
152
156 void rescheduleDailyJob(const TimerJob &job);
157};
Defines a bitmask enum for days of the week.
uint8_t DaysOfWeek
Type alias for a set of days (bitmask).
Definition DaysOfWeek.h:31
Defines the Event structure and related utilities for event messaging.
A simple representation of wall-clock time (HH:MM or HH:MM:SS)
Central service for time-driven scheduling of framework events.
void scheduleAt(time_t unixTime, const Event &event)
Schedule a one-time event at an absolute UNIX timestamp.
void rescheduleDailyJob(const TimerJob &job)
Placeholder for persistence/rescheduling in the future.
void scheduleDailyAt(TimeOfDay time, DaysOfWeek days, const Event &event)
Schedule a recurring event based on time-of-day and day mask.
bool cancel(const std::string &jobId)
void scheduleCallbackAt(time_t unixTime, std::function< void()> callback)
Schedule a one-shot callback at a given absolute time.
void withLock(const std::function< void()> &fn)
SemaphoreHandle_t lock_
std::unordered_map< std::string, TimerHandle_t > scheduledJobs
Map of job IDs to TimerHandles.
void scheduleDuration(TimeOfDay start, DaysOfWeek days, uint32_t durationMs, const Event &startEvent, const Event &stopEvent)
Schedule a start event and stop event with a delay between them.
void checkMissedEvents(time_t now)
Detect and fire any missed events after a reboot (TBD).
void scheduleEvery(uint32_t intervalMs, const Event &event)
Schedule a repeating event at fixed intervals.
static StaticSemaphore_t lockBuffer_
static TimerService & instance()
Access the singleton instance.
~TimerService()=default
Represents a framework event, optionally carrying payload data.
Definition Event.h:24
A simple value type representing a time of day (hour, minute, second).
Definition TimeOfDay.h:22
Represents a job scheduled by the TimerService.
Event startEvent
Event to post at start.
TimeOfDay startTime
When the event should start.
Event stopEvent
Optional stop event.
uint32_t durationMs
Optional run duration in ms.
bool recurring
If true, job repeats.
DaysOfWeek repeatDays
Days the job runs (bitmask)