ESPHome  2024.10.2
scheduler.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <memory>
5 
7 #include "esphome/core/helpers.h"
8 
9 namespace esphome {
10 
11 class Component;
12 
13 class 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:
31  struct SchedulerItem {
33  std::string name;
35  union {
36  uint32_t interval;
37  uint32_t timeout;
38  };
39  uint32_t last_execution;
40  std::function<void()> callback;
41  bool remove;
43 
44  inline uint32_t next_execution() { return this->last_execution + this->timeout; }
45  inline uint8_t next_execution_major() {
46  uint32_t next_exec = this->next_execution();
47  uint8_t next_exec_major = this->last_execution_major;
48  if (next_exec < this->last_execution)
49  next_exec_major++;
50  return next_exec_major;
51  }
52 
53  static bool cmp(const std::unique_ptr<SchedulerItem> &a, const std::unique_ptr<SchedulerItem> &b);
54  const char *get_type_str() {
55  switch (this->type) {
57  return "interval";
59  return "timeout";
60  default:
61  return "";
62  }
63  }
64  };
65 
66  uint32_t millis_();
67  void cleanup_();
68  void pop_raw_();
69  void push_(std::unique_ptr<SchedulerItem> item);
70  bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type);
71  bool empty_() {
72  this->cleanup_();
73  return this->items_.empty();
74  }
75 
77  std::vector<std::unique_ptr<SchedulerItem>> items_;
78  std::vector<std::unique_ptr<SchedulerItem>> to_add_;
79  uint32_t last_millis_{0};
80  uint8_t millis_major_{0};
81  uint32_t to_remove_{0};
82 };
83 
84 } // namespace esphome
std::function< void()> callback
Definition: scheduler.h:40
uint32_t to_remove_
Definition: scheduler.h:81
const char * name
Definition: stm32flash.h:78
RetryResult
Definition: component.h:66
void push_(std::unique_ptr< SchedulerItem > item)
Definition: scheduler.cpp:301
bool cancel_timeout(Component *component, const std::string &name)
Definition: scheduler.cpp:45
optional< uint32_t > next_schedule_in()
Definition: scheduler.cpp:137
uint8_t millis_major_
Definition: scheduler.h:80
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)
Definition: scheduler.cpp:102
uint32_t millis_()
Definition: scheduler.cpp:325
std::vector< std::unique_ptr< SchedulerItem > > to_add_
Definition: scheduler.h:78
bool cancel_retry(Component *component, const std::string &name)
Definition: scheduler.cpp:133
uint32_t last_millis_
Definition: scheduler.h:79
bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type)
Definition: scheduler.cpp:305
static bool cmp(const std::unique_ptr< SchedulerItem > &a, const std::unique_ptr< SchedulerItem > &b)
Definition: scheduler.cpp:335
bool cancel_interval(Component *component, const std::string &name)
Definition: scheduler.cpp:78
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
Definition: scheduler.cpp:22
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
enum esphome::Scheduler::SchedulerItem::Type type
std::vector< std::unique_ptr< SchedulerItem > > items_
Definition: scheduler.h:77
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)
Definition: scheduler.cpp:48
Mutex implementation, with API based on the unavailable std::mutex.
Definition: helpers.h:544