mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 06:33:51 +00:00
Add retry handler (#2721)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
@@ -15,6 +15,10 @@ class Scheduler {
|
||||
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function<void()> &&func);
|
||||
bool cancel_interval(Component *component, const std::string &name);
|
||||
|
||||
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);
|
||||
bool cancel_retry(Component *component, const std::string &name);
|
||||
|
||||
optional<uint32_t> next_schedule_in();
|
||||
|
||||
void call();
|
||||
@@ -25,13 +29,20 @@ class Scheduler {
|
||||
struct SchedulerItem {
|
||||
Component *component;
|
||||
std::string name;
|
||||
enum Type { TIMEOUT, INTERVAL } type;
|
||||
enum Type { TIMEOUT, INTERVAL, RETRY } type;
|
||||
union {
|
||||
uint32_t interval;
|
||||
uint32_t timeout;
|
||||
};
|
||||
uint32_t last_execution;
|
||||
std::function<void()> f;
|
||||
// Ideally this should be a union or std::variant
|
||||
// but unions don't work with object like std::function
|
||||
// union CallBack_{
|
||||
std::function<void()> void_callback;
|
||||
std::function<RetryResult()> retry_callback;
|
||||
// };
|
||||
uint8_t retry_countdown{3};
|
||||
float backoff_multiplier{1.0f};
|
||||
bool remove;
|
||||
uint8_t last_execution_major;
|
||||
|
||||
@@ -45,6 +56,18 @@ class Scheduler {
|
||||
}
|
||||
|
||||
static bool cmp(const std::unique_ptr<SchedulerItem> &a, const std::unique_ptr<SchedulerItem> &b);
|
||||
const char *get_type_str() {
|
||||
switch (this->type) {
|
||||
case SchedulerItem::INTERVAL:
|
||||
return "interval";
|
||||
case SchedulerItem::RETRY:
|
||||
return "retry";
|
||||
case SchedulerItem::TIMEOUT:
|
||||
return "timeout";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
uint32_t millis_();
|
||||
|
||||
Reference in New Issue
Block a user