Use received door state for 2nd door toggle in sec+1.0

This commit is contained in:
Marius Muja 2024-01-18 10:23:45 -08:00
parent 9a4cfe3c56
commit 02319d49a5
7 changed files with 33 additions and 17 deletions

View File

@ -13,15 +13,17 @@ namespace ratgdo {
class OnceCallbacks<void(Ts...)> {
public:
template <typename Callback>
void then(Callback&& callback) { this->callbacks_.push_back(std::forward<Callback>(callback)); }
void operator()(Callback&& callback) { this->callbacks_.push_back(std::forward<Callback>(callback)); }
void operator()(Ts... args)
void trigger(Ts... args)
{
for (auto& cb : this->callbacks_)
cb(args...);
this->callbacks_.clear();
}
protected:
std::vector<std::function<void(Ts...)>> callbacks_;
};

View File

@ -187,7 +187,7 @@ namespace ratgdo {
}
this->door_state = door_state;
this->door_state_received(door_state);
this->on_door_state_.trigger(door_state);
}
void RATGDOComponent::received(const LearnState learn_state)
@ -442,7 +442,7 @@ namespace ratgdo {
if (*this->door_state == DoorState::OPENING) {
// have to stop door first, otherwise close command is ignored
this->door_action(DoorAction::STOP);
this->door_state_received.then([=](DoorState s) {
this->on_door_state_([=](DoorState s) {
if (s == DoorState::STOPPED) {
this->door_action(DoorAction::CLOSE);
} else {
@ -487,7 +487,7 @@ namespace ratgdo {
ESP_LOGW(TAG, "It's not recommended to use ensure_door_action with non-idempotent commands such as DOOR_TOGGLE");
}
auto prev_door_state = *this->door_state;
this->door_state_received.then([=](DoorState s) {
this->on_door_state_([=](DoorState s) {
if ((action == DoorAction::STOP) && (s != DoorState::STOPPED) && !(prev_door_state == DoorState::OPENING && s == DoorState::OPEN) && !(prev_door_state == DoorState::CLOSING && s == DoorState::CLOSED)) {
return;
}
@ -512,7 +512,7 @@ namespace ratgdo {
{
if (*this->door_state == DoorState::OPENING || *this->door_state == DoorState::CLOSING) {
this->door_action(DoorAction::STOP);
this->door_state_received.then([=](DoorState s) {
this->on_door_state_([=](DoorState s) {
if (s == DoorState::STOPPED) {
this->door_move_to_position(position);
}

View File

@ -84,7 +84,7 @@ namespace ratgdo {
observable<MotionState> motion_state { MotionState::UNKNOWN };
observable<LearnState> learn_state { LearnState::UNKNOWN };
OnceCallbacks<void(DoorState)> door_state_received;
OnceCallbacks<void(DoorState)> on_door_state_;
observable<bool> sync_failed { false };

View File

@ -154,8 +154,10 @@ namespace secplus1 {
} else if (this->door_state == DoorState::STOPPED) {
this->toggle_door(); // this starts closing door
// this changes direction of door
this->scheduler_->set_timeout(this->ratgdo_, "", double_toggle_delay, [=] {
this->toggle_door();
this->on_door_state_([=](DoorState s) {
if (s==DoorState::CLOSING) {
this->toggle_door();
}
});
}
} else if (action == DoorAction::CLOSE) {
@ -164,8 +166,10 @@ namespace secplus1 {
} else if (this->door_state == DoorState::OPENING) {
this->toggle_door(); // this switches to stopped
// another toggle needed to close
this->scheduler_->set_timeout(this->ratgdo_, "", double_toggle_delay, [=] {
this->toggle_door();
this->on_door_state_([=](DoorState s) {
if (s==DoorState::STOPPED) {
this->toggle_door();
}
});
} else if (this->door_state == DoorState::STOPPED) {
this->toggle_door();
@ -175,9 +179,12 @@ namespace secplus1 {
this->toggle_door();
} else if (this->door_state == DoorState::CLOSING) {
this->toggle_door(); // this switches to opening
// another toggle needed to stop
this->scheduler_->set_timeout(this->ratgdo_, "", double_toggle_delay, [=] {
this->toggle_door();
this->on_door_state_([=](DoorState s) {
if (s==DoorState::OPENING) {
this->toggle_door();
}
});
}
}
@ -196,7 +203,7 @@ namespace secplus1 {
void Secplus1::toggle_door()
{
this->enqueue_transmit(CommandType::TOGGLE_DOOR_PRESS);
this->enqueue_transmit(CommandType::QUERY_DOOR_STATUS);
// this->enqueue_transmit(CommandType::QUERY_DOOR_STATUS);
if (this->door_state == DoorState::STOPPED || this->door_state == DoorState::OPEN || this->door_state == DoorState::CLOSED) {
this->door_moving_ = true;
}
@ -319,9 +326,14 @@ namespace secplus1 {
door_state = DoorState::UNKNOWN;
}
if (this->maybe_door_state != door_state) {
this->on_door_state_.trigger(door_state);
}
if (!this->is_0x37_panel_ && door_state != this->maybe_door_state) {
this->maybe_door_state = door_state;
} else {
this->maybe_door_state = door_state;
this->door_state = door_state;
if (this->door_state == DoorState::STOPPED || this->door_state == DoorState::OPEN || this->door_state == DoorState::CLOSED) {
this->door_moving_ = false;

View File

@ -115,6 +115,8 @@ namespace secplus1 {
LockState maybe_lock_state { LockState::UNKNOWN };
DoorState maybe_door_state { DoorState::UNKNOWN };
OnceCallbacks<void(DoorState)> on_door_state_;
bool door_moving_ { false };
bool wall_panel_starting_ { false };

View File

@ -471,7 +471,7 @@ namespace secplus2 {
void Secplus2::send_command(Command command, IncrementRollingCode increment, std::function<void()>&& on_sent)
{
this->command_sent_.then(on_sent);
this->on_command_sent_(on_sent);
this->send_command(command, increment);
}
@ -521,7 +521,7 @@ namespace secplus2 {
this->transmit_pending_ = false;
this->transmit_pending_start_ = 0;
this->command_sent_();
this->on_command_sent_.trigger();
return true;
}

View File

@ -128,7 +128,7 @@ namespace secplus2 {
bool transmit_pending_ { false };
uint32_t transmit_pending_start_ { 0 };
WirePacket tx_packet_;
OnceCallbacks<void()> command_sent_;
OnceCallbacks<void()> on_command_sent_;
ProtocolTraits traits_;