Skip to content

Commit

Permalink
Refactor sync function to be more DRY and easier to extend (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: J. Nick Koston <[email protected]>
  • Loading branch information
mariusmuja and bdraco authored Sep 18, 2023
1 parent e285003 commit d2ae60e
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions components/ratgdo/ratgdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,30 +436,34 @@ namespace ratgdo {

void RATGDOComponent::sync()
{
// increment rolling code counter by some amount in case we crashed without writing to flash the latest value
this->increment_rolling_code_counter(MAX_CODES_WITHOUT_FLASH_WRITE);
auto sync_step = [=]() {
if (*this->door_state == DoorState::UNKNOWN) {
this->send_command(Command::GET_STATUS);
return RetryResult::RETRY;
}
if (*this->openings == 0) {
this->send_command(Command::GET_OPENINGS);
return RetryResult::RETRY;
}
return RetryResult::DONE;
};

const uint8_t MAX_ATTEMPTS = 10;
set_retry(
500, 10, [=](uint8_t r) {
if (*this->door_state != DoorState::UNKNOWN) { // have status
if (*this->openings != 0) { // have openings
return RetryResult::DONE;
} else {
if (r == 0) { // failed to sync probably rolling counter is wrong, notify
ESP_LOGD(TAG, "Triggering sync failed actions.");
this->sync_failed = true;
};
this->send_command(Command::GET_OPENINGS);
return RetryResult::RETRY;
500, MAX_ATTEMPTS, [=](uint8_t r) {
auto result = sync_step();
if (result == RetryResult::RETRY) {
if (r == MAX_ATTEMPTS-2 && *this->door_state == DoorState::UNKNOWN) { // made a few attempts and no progress (door state is the first sync request)
// increment rolling code counter by some amount in case we crashed without writing to flash the latest value
this->increment_rolling_code_counter(MAX_CODES_WITHOUT_FLASH_WRITE);
}
} else {
if (r == 0) { // failed to sync probably rolling counter is wrong, notify
if (r == 0) {
// this was last attempt, notify of sync failure
ESP_LOGD(TAG, "Triggering sync failed actions.");
this->sync_failed = true;
};
this->send_command(Command::GET_STATUS);
return RetryResult::RETRY;
}
}
return result;
},
1.5f);
}
Expand Down

0 comments on commit d2ae60e

Please sign in to comment.