Refactor retry handling (#702)
* add NukiRetryHandler * apply retry wrapper to opener wrapper * apply retry wrapper to all opener communication * apply retry wrapper to all lock communication * remove commented out code * replace millis() with espMillis() in wrapper classes
This commit is contained in:
52
src/util/NukiRetryHandler.cpp
Normal file
52
src/util/NukiRetryHandler.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "NukiRetryHandler.h"
|
||||
#include "Logger.h"
|
||||
|
||||
NukiRetryHandler::NukiRetryHandler(std::string reference, Gpio* gpio, std::vector<uint8_t> pinsCommError, int nrOfRetries, int retryDelay)
|
||||
: _reference(reference),
|
||||
_gpio(gpio),
|
||||
_pinsCommError(pinsCommError),
|
||||
_nrOfRetries(nrOfRetries),
|
||||
_retryDelay(retryDelay)
|
||||
{
|
||||
}
|
||||
|
||||
const Nuki::CmdResult NukiRetryHandler::retryComm(std::function<Nuki::CmdResult()> func)
|
||||
{
|
||||
Nuki::CmdResult cmdResult = Nuki::CmdResult::Error;
|
||||
|
||||
int retryCount = 0;
|
||||
|
||||
while(retryCount < _nrOfRetries + 1 && cmdResult != Nuki::CmdResult::Success)
|
||||
{
|
||||
cmdResult = func();
|
||||
|
||||
if (cmdResult != Nuki::CmdResult::Success)
|
||||
{
|
||||
setCommErrorPins(HIGH);
|
||||
++retryCount;
|
||||
|
||||
Log->print(_reference.c_str());
|
||||
Log->print(": Last command failed, retrying after ");
|
||||
Log->print(_retryDelay);
|
||||
Log->print(" milliseconds. Retry ");
|
||||
Log->print(retryCount);
|
||||
Log->print(" of ");
|
||||
Log->println(_nrOfRetries);
|
||||
|
||||
vTaskDelay(_retryDelay / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
setCommErrorPins(LOW);
|
||||
|
||||
return cmdResult;
|
||||
}
|
||||
|
||||
void NukiRetryHandler::setCommErrorPins(const uint8_t& value)
|
||||
{
|
||||
for (uint8_t pin : _pinsCommError)
|
||||
{
|
||||
_gpio->setPinOutput(pin, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
22
src/util/NukiRetryHandler.h
Normal file
22
src/util/NukiRetryHandler.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include "NukiDataTypes.h"
|
||||
#include "NukiPublisher.h"
|
||||
|
||||
class NukiRetryHandler
|
||||
{
|
||||
public:
|
||||
NukiRetryHandler(std::string reference, Gpio* gpio, std::vector<uint8_t> pinsCommError, int nrOfRetries, int retryDelay);
|
||||
|
||||
const Nuki::CmdResult retryComm(std::function<Nuki::CmdResult ()> func);
|
||||
|
||||
|
||||
private:
|
||||
void setCommErrorPins(const uint8_t& value);
|
||||
|
||||
std::string _reference;
|
||||
Gpio* _gpio = nullptr;
|
||||
int _nrOfRetries = 0;
|
||||
int _retryDelay = 0;
|
||||
std::vector<uint8_t> _pinsCommError;
|
||||
};
|
||||
Reference in New Issue
Block a user