ESPHome 2025.5.0
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <memory>
5
8
9namespace esphome {
10
11class Component;
12
13class Scheduler {
14 public:
15 void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function<void()> func);
16 bool cancel_timeout(Component *component, const std::string &name);
17 void set_interval(Component *component, const std::string &name, uint32_t interval, std::function<void()> func);
18 bool cancel_interval(Component *component, const std::string &name);
19
20 void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
21 std::function<RetryResult(uint8_t)> func, float backoff_increase_factor = 1.0f);
22 bool cancel_retry(Component *component, const std::string &name);
23
25
26 void call();
27
28 void process_to_add();
29
30 protected:
33 std::string name;
35 uint32_t interval;
37 std::function<void()> callback;
38 bool remove;
39
40 static bool cmp(const std::unique_ptr<SchedulerItem> &a, const std::unique_ptr<SchedulerItem> &b);
41 const char *get_type_str() {
42 switch (this->type) {
44 return "interval";
46 return "timeout";
47 default:
48 return "";
49 }
50 }
51 const char *get_source() {
52 return this->component != nullptr ? this->component->get_component_source() : "unknown";
53 }
54 };
55
56 uint64_t millis_();
57 void cleanup_();
58 void pop_raw_();
59 void push_(std::unique_ptr<SchedulerItem> item);
60 bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type);
61 bool empty_() {
62 this->cleanup_();
63 return this->items_.empty();
64 }
65
67 std::vector<std::unique_ptr<SchedulerItem>> items_;
68 std::vector<std::unique_ptr<SchedulerItem>> to_add_;
69 uint32_t last_millis_{0};
70 uint16_t millis_major_{0};
71 uint32_t to_remove_{0};
72};
73
74} // namespace esphome
const char * get_component_source() const
Get the integration where this component was declared as a string.
Mutex implementation, with API based on the unavailable std::mutex.
Definition helpers.h:561
uint32_t to_remove_
Definition scheduler.h:71
bool cancel_retry(Component *component, const std::string &name)
uint32_t last_millis_
Definition scheduler.h:69
bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type)
void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> func, float backoff_increase_factor=1.0f)
bool cancel_timeout(Component *component, const std::string &name)
Definition scheduler.cpp:47
void push_(std::unique_ptr< SchedulerItem > item)
std::vector< std::unique_ptr< SchedulerItem > > to_add_
Definition scheduler.h:68
bool cancel_interval(Component *component, const std::string &name)
Definition scheduler.cpp:79
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Definition scheduler.cpp:25
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)
Definition scheduler.cpp:50
uint16_t millis_major_
Definition scheduler.h:70
optional< uint32_t > next_schedule_in()
std::vector< std::unique_ptr< SchedulerItem > > items_
Definition scheduler.h:67
uint8_t type
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
enum esphome::Scheduler::SchedulerItem::Type type
std::function< void()> callback
Definition scheduler.h:37
static bool cmp(const std::unique_ptr< SchedulerItem > &a, const std::unique_ptr< SchedulerItem > &b)