From 02319d49a54245840f5f1a79c664eca8064a01b4 Mon Sep 17 00:00:00 2001 From: Marius Muja Date: Thu, 18 Jan 2024 10:23:45 -0800 Subject: [PATCH] Use received door state for 2nd door toggle in sec+1.0 --- components/ratgdo/callbacks.h | 6 ++++-- components/ratgdo/ratgdo.cpp | 8 ++++---- components/ratgdo/ratgdo.h | 2 +- components/ratgdo/secplus1.cpp | 26 +++++++++++++++++++------- components/ratgdo/secplus1.h | 2 ++ components/ratgdo/secplus2.cpp | 4 ++-- components/ratgdo/secplus2.h | 2 +- 7 files changed, 33 insertions(+), 17 deletions(-) diff --git a/components/ratgdo/callbacks.h b/components/ratgdo/callbacks.h index 9fdce25..627b50d 100644 --- a/components/ratgdo/callbacks.h +++ b/components/ratgdo/callbacks.h @@ -13,15 +13,17 @@ namespace ratgdo { class OnceCallbacks { public: template - void then(Callback&& callback) { this->callbacks_.push_back(std::forward(callback)); } + void operator()(Callback&& callback) { this->callbacks_.push_back(std::forward(callback)); } - void operator()(Ts... args) + void trigger(Ts... args) { for (auto& cb : this->callbacks_) cb(args...); this->callbacks_.clear(); } + + protected: std::vector> callbacks_; }; diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index 9f80a1d..764b5f0 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -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); } diff --git a/components/ratgdo/ratgdo.h b/components/ratgdo/ratgdo.h index 7afb818..1e50a86 100644 --- a/components/ratgdo/ratgdo.h +++ b/components/ratgdo/ratgdo.h @@ -84,7 +84,7 @@ namespace ratgdo { observable motion_state { MotionState::UNKNOWN }; observable learn_state { LearnState::UNKNOWN }; - OnceCallbacks door_state_received; + OnceCallbacks on_door_state_; observable sync_failed { false }; diff --git a/components/ratgdo/secplus1.cpp b/components/ratgdo/secplus1.cpp index f89d451..9f43b01 100644 --- a/components/ratgdo/secplus1.cpp +++ b/components/ratgdo/secplus1.cpp @@ -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; diff --git a/components/ratgdo/secplus1.h b/components/ratgdo/secplus1.h index de6292d..f21a91c 100644 --- a/components/ratgdo/secplus1.h +++ b/components/ratgdo/secplus1.h @@ -115,6 +115,8 @@ namespace secplus1 { LockState maybe_lock_state { LockState::UNKNOWN }; DoorState maybe_door_state { DoorState::UNKNOWN }; + OnceCallbacks on_door_state_; + bool door_moving_ { false }; bool wall_panel_starting_ { false }; diff --git a/components/ratgdo/secplus2.cpp b/components/ratgdo/secplus2.cpp index 62a03b9..c95a1c8 100644 --- a/components/ratgdo/secplus2.cpp +++ b/components/ratgdo/secplus2.cpp @@ -471,7 +471,7 @@ namespace secplus2 { void Secplus2::send_command(Command command, IncrementRollingCode increment, std::function&& 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; } diff --git a/components/ratgdo/secplus2.h b/components/ratgdo/secplus2.h index a715341..c9d0c65 100644 --- a/components/ratgdo/secplus2.h +++ b/components/ratgdo/secplus2.h @@ -128,7 +128,7 @@ namespace secplus2 { bool transmit_pending_ { false }; uint32_t transmit_pending_start_ { 0 }; WirePacket tx_packet_; - OnceCallbacks command_sent_; + OnceCallbacks on_command_sent_; ProtocolTraits traits_;