4#include "roo_scheduler.h"
24template <
typename StateT>
30 roo_time::Duration inertia = roo_time::Millis(500))
31 : scheduler_(scheduler),
35 deferred_setter_([this]() { deferredSet(); }),
36 deferred_set_pending_(
false),
38 when_switched_(roo_time::Uptime::Now()),
45 return actuator_.getState(result);
53 if (!initialized_)
return false;
54 state = intended_state_;
71 if (initialized_ && intended_state_ == state) {
74 intended_state_ = state;
81 roo_time::Uptime now = roo_time::Uptime::Now();
82 roo_time::Uptime deferred_set_time = when_switched_ + inertia_;
83 if (deferred_set_time <= now) {
88 if (!deferred_set_pending_) {
89 deferred_set_pending_ =
true;
90 scheduler_.scheduleOn(deferred_set_time, deferred_setter_,
91 roo_scheduler::PRIORITY_ELEVATED);
105 roo_time::Duration
intertia()
const {
return inertia_; }
117 if (actuator_.setState(intended_state_)) {
118 when_switched_ = roo_time::Uptime::Now();
123 deferred_set_pending_ =
true;
124 roo_time::Duration delay = backoff_policy_(failure_count_);
125 scheduler_.scheduleAfter(delay, deferred_setter_,
126 roo_scheduler::PRIORITY_ELEVATED);
132 deferred_set_pending_ =
false;
139 roo_scheduler::Scheduler& scheduler_;
142 Switch<State>& actuator_;
145 roo_time::Duration inertia_;
148 std::function<roo_time::Duration(
int retry_count)> backoff_policy_;
151 roo_scheduler::Task deferred_setter_;
154 bool deferred_set_pending_;
160 State intended_state_;
163 roo_time::Uptime when_switched_;
Switch that adds inertia between state changes on top of a raw actuator.
bool getIntendedState(State &state) const
Returns the state that the switch has been requested to take.
virtual void stateChanged() const
Can be overridden to receive state change notifications.
roo_time::Uptime whenSwitched() const
Returns the time of last actual state change.
InertSwitch(roo_scheduler::Scheduler &scheduler, Switch< StateT > &actuator, roo_time::Duration inertia=roo_time::Millis(500))
virtual ~InertSwitch()=default
bool hasPendingChange() const
Returns true if a deferred update is pending.
bool getState(State &result) const override
Returns the actual state the switch is currently at.
bool setState(State state) override
Sets the intended state of the switch.
roo_time::Duration intertia() const
Returns the inertia interval.
An abstraction of a multi-state settable switch.
roo_time::Duration DefaultBackoff(int retry_count)
Default backoff policy for InertSwitch retries.