ESPHome  2022.9.1
scheduler.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include <vector>
5 #include <memory>
6 
7 namespace esphome {
8 
9 class Component;
10 
11 class Scheduler {
12  public:
13  void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function<void()> func);
14  bool cancel_timeout(Component *component, const std::string &name);
15  void set_interval(Component *component, const std::string &name, uint32_t interval, std::function<void()> func);
16  bool cancel_interval(Component *component, const std::string &name);
17 
18  void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts,
19  std::function<RetryResult()> func, float backoff_increase_factor = 1.0f);
20  bool cancel_retry(Component *component, const std::string &name);
21 
23 
24  void call();
25 
26  void process_to_add();
27 
28  protected:
29  struct SchedulerItem {
31  std::string name;
33  union {
34  uint32_t interval;
35  uint32_t timeout;
36  };
37  uint32_t last_execution;
38  std::function<void()> callback;
39  bool remove;
41 
42  inline uint32_t next_execution() { return this->last_execution + this->timeout; }
43  inline uint8_t next_execution_major() {
44  uint32_t next_exec = this->next_execution();
45  uint8_t next_exec_major = this->last_execution_major;
46  if (next_exec < this->last_execution)
47  next_exec_major++;
48  return next_exec_major;
49  }
50 
51  static bool cmp(const std::unique_ptr<SchedulerItem> &a, const std::unique_ptr<SchedulerItem> &b);
52  const char *get_type_str() {
53  switch (this->type) {
55  return "interval";
57  return "timeout";
58  default:
59  return "";
60  }
61  }
62  };
63 
64  uint32_t millis_();
65  void cleanup_();
66  void pop_raw_();
67  void push_(std::unique_ptr<SchedulerItem> item);
68  bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type);
69  bool empty_() {
70  this->cleanup_();
71  return this->items_.empty();
72  }
73 
74  std::vector<std::unique_ptr<SchedulerItem>> items_;
75  std::vector<std::unique_ptr<SchedulerItem>> to_add_;
76  uint32_t last_millis_{0};
77  uint8_t millis_major_{0};
78  uint32_t to_remove_{0};
79 };
80 
81 } // namespace esphome
std::function< void()> callback
Definition: scheduler.h:38
uint32_t to_remove_
Definition: scheduler.h:78
const char * name
Definition: stm32flash.h:78
RetryResult
Definition: component.h:64
void push_(std::unique_ptr< SchedulerItem > item)
Definition: scheduler.cpp:262
bool cancel_timeout(Component *component, const std::string &name)
Definition: scheduler.cpp:39
optional< uint32_t > next_schedule_in()
Definition: scheduler.cpp:120
uint8_t millis_major_
Definition: scheduler.h:77
uint32_t millis_()
Definition: scheduler.cpp:281
std::vector< std::unique_ptr< SchedulerItem > > to_add_
Definition: scheduler.h:75
bool cancel_retry(Component *component, const std::string &name)
Definition: scheduler.cpp:116
uint32_t last_millis_
Definition: scheduler.h:76
bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type)
Definition: scheduler.cpp:263
static bool cmp(const std::unique_ptr< SchedulerItem > &a, const std::unique_ptr< SchedulerItem > &b)
Definition: scheduler.cpp:291
bool cancel_interval(Component *component, const std::string &name)
Definition: scheduler.cpp:72
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Definition: scheduler.cpp:16
Definition: a4988.cpp:4
enum esphome::Scheduler::SchedulerItem::Type type
std::vector< std::unique_ptr< SchedulerItem > > items_
Definition: scheduler.h:74
void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult()> func, float backoff_increase_factor=1.0f)
Definition: scheduler.cpp:94
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)
Definition: scheduler.cpp:42