1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-11 15:52:20 +01:00

Add central function scheduler (#609)

* Add central function scheduler

* Avoid unnecessary copies

* Lint

* Prevent more copies, store pointers

* Add never update_interval
This commit is contained in:
Otto Winter
2019-06-07 14:26:40 +02:00
committed by GitHub
parent 7a895adec9
commit b51cbc4207
6 changed files with 273 additions and 128 deletions

View File

@@ -1,5 +1,3 @@
#include <algorithm>
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/core/esphal.h"
@@ -45,51 +43,19 @@ void Component::setup() {}
void Component::loop() {}
void Component::set_interval(const std::string &name, uint32_t interval, std::function<void()> &&f) { // NOLINT
const uint32_t now = millis();
// only put offset in lower half
uint32_t offset = 0;
if (interval != 0)
offset = (random_uint32() % interval) / 2;
ESP_LOGVV(TAG, "set_interval(name='%s', interval=%u, offset=%u)", name.c_str(), interval, offset);
if (!name.empty()) {
this->cancel_interval(name);
}
struct TimeFunction function = {
.name = name,
.type = TimeFunction::INTERVAL,
.interval = interval,
.last_execution = now - interval - offset,
.f = std::move(f),
.remove = false,
};
this->time_functions_.push_back(function);
App.scheduler.set_interval(this, name, interval, std::move(f));
}
bool Component::cancel_interval(const std::string &name) { // NOLINT
return this->cancel_time_function_(name, TimeFunction::INTERVAL);
return App.scheduler.cancel_interval(this, name);
}
void Component::set_timeout(const std::string &name, uint32_t timeout, std::function<void()> &&f) { // NOLINT
const uint32_t now = millis();
ESP_LOGVV(TAG, "set_timeout(name='%s', timeout=%u)", name.c_str(), timeout);
if (!name.empty()) {
this->cancel_timeout(name);
}
struct TimeFunction function = {
.name = name,
.type = TimeFunction::TIMEOUT,
.interval = timeout,
.last_execution = now,
.f = std::move(f),
.remove = false,
};
this->time_functions_.push_back(function);
return App.scheduler.set_timeout(this, name, timeout, std::move(f));
}
bool Component::cancel_timeout(const std::string &name) { // NOLINT
return this->cancel_time_function_(name, TimeFunction::TIMEOUT);
return App.scheduler.cancel_timeout(this, name);
}
void Component::call_loop() {
@@ -97,17 +63,6 @@ void Component::call_loop() {
this->loop();
}
bool Component::cancel_time_function_(const std::string &name, TimeFunction::Type type) {
// NOLINTNEXTLINE
for (auto iter = this->time_functions_.begin(); iter != this->time_functions_.end(); iter++) {
if (!iter->remove && iter->name == name && iter->type == type) {
ESP_LOGVV(TAG, "Removing old time function %s.", iter->name.c_str());
iter->remove = true;
return true;
}
}
return false;
}
void Component::call_setup() {
this->setup_internal_();
this->setup();
@@ -116,34 +71,6 @@ uint32_t Component::get_component_state() const { return this->component_state_;
void Component::loop_internal_() {
this->component_state_ &= ~COMPONENT_STATE_MASK;
this->component_state_ |= COMPONENT_STATE_LOOP;
for (unsigned int i = 0; i < this->time_functions_.size(); i++) { // NOLINT
const uint32_t now = millis();
TimeFunction *tf = &this->time_functions_[i];
if (tf->should_run(now)) {
#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
const char *type =
tf->type == TimeFunction::INTERVAL ? "interval" : (tf->type == TimeFunction::TIMEOUT ? "timeout" : "defer");
ESP_LOGVV(TAG, "Running %s '%s':%u with interval=%u last_execution=%u (now=%u)", type, tf->name.c_str(), i,
tf->interval, tf->last_execution, now);
#endif
tf->f();
// The vector might have reallocated due to new items
tf = &this->time_functions_[i];
if (tf->type == TimeFunction::INTERVAL && tf->interval != 0) {
const uint32_t amount = (now - tf->last_execution) / tf->interval;
tf->last_execution += (amount * tf->interval);
} else if (tf->type == TimeFunction::DEFER || tf->type == TimeFunction::TIMEOUT) {
tf->remove = true;
}
}
}
this->time_functions_.erase(std::remove_if(this->time_functions_.begin(), this->time_functions_.end(),
[](const TimeFunction &tf) -> bool { return tf.remove; }),
this->time_functions_.end());
}
void Component::setup_internal_() {
this->component_state_ &= ~COMPONENT_STATE_MASK;
@@ -155,29 +82,20 @@ void Component::mark_failed() {
this->component_state_ |= COMPONENT_STATE_FAILED;
this->status_set_error();
}
void Component::defer(std::function<void()> &&f) { this->defer("", std::move(f)); } // NOLINT
bool Component::cancel_defer(const std::string &name) { // NOLINT
return this->cancel_time_function_(name, TimeFunction::DEFER);
void Component::defer(std::function<void()> &&f) { // NOLINT
App.scheduler.set_timeout(this, "", 0, std::move(f));
}
bool Component::cancel_defer(const std::string &name) { // NOLINT
return App.scheduler.cancel_timeout(this, name);
}
void Component::defer(const std::string &name, std::function<void()> &&f) { // NOLINT
if (!name.empty()) {
this->cancel_defer(name);
}
struct TimeFunction function = {
.name = name,
.type = TimeFunction::DEFER,
.interval = 0,
.last_execution = 0,
.f = std::move(f),
.remove = false,
};
this->time_functions_.push_back(function);
App.scheduler.set_timeout(this, name, 0, std::move(f));
}
void Component::set_timeout(uint32_t timeout, std::function<void()> &&f) { // NOLINT
this->set_timeout("", timeout, std::move(f));
App.scheduler.set_timeout(this, "", timeout, std::move(f));
}
void Component::set_interval(uint32_t interval, std::function<void()> &&f) { // NOLINT
this->set_interval("", interval, std::move(f));
App.scheduler.set_timeout(this, "", interval, std::move(f));
}
bool Component::is_failed() { return (this->component_state_ & COMPONENT_STATE_MASK) == COMPONENT_STATE_FAILED; }
bool Component::can_proceed() { return true; }
@@ -203,7 +121,9 @@ void Component::status_momentary_error(const std::string &name, uint32_t length)
}
void Component::dump_config() {}
float Component::get_actual_setup_priority() const {
return this->setup_priority_override_.value_or(this->get_setup_priority());
if (isnan(this->setup_priority_override_))
return this->get_setup_priority();
return this->setup_priority_override_;
}
void Component::set_setup_priority(float priority) { this->setup_priority_override_ = priority; }
@@ -240,12 +160,4 @@ void Nameable::calc_object_id_() {
}
uint32_t Nameable::get_object_id_hash() { return this->object_id_hash_; }
bool Component::TimeFunction::should_run(uint32_t now) const {
if (this->remove)
return false;
if (this->type == DEFER)
return true;
return this->interval != 4294967295UL && now - this->last_execution > this->interval;
}
} // namespace esphome