QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
RetryState.cc
Go to the documentation of this file.
1#include "RetryState.h"
3
4RetryState::RetryState(const QString& stateName, QState* parent,
5 Action action, int maxRetries, int retryDelayMsecs,
6 ExhaustedBehavior exhaustedBehavior)
7 : QGCState(stateName, parent)
8 , _action(std::move(action))
9 , _maxRetries(maxRetries)
10 , _retryDelayMsecs(retryDelayMsecs)
11 , _exhaustedBehavior(exhaustedBehavior)
12{
13 _retryTimer.setSingleShot(true);
14 connect(&_retryTimer, &QTimer::timeout, this, &RetryState::_executeAction);
15}
16
18{
19 _currentAttempt = 0;
20 _wasSkipped = false;
21 _executeAction();
22}
23
24void RetryState::_executeAction()
25{
26 _currentAttempt++;
27
28 qCDebug(QGCStateMachineLog) << stateName() << "attempt" << _currentAttempt
29 << "of" << totalAttempts();
30
31 bool success = false;
32 if (_action) {
33 success = _action();
34 }
35
36 if (success) {
37 qCDebug(QGCStateMachineLog) << stateName() << "succeeded on attempt" << _currentAttempt;
38 emit succeeded();
39 emit advance();
40 } else if (_currentAttempt <= _maxRetries) {
41 qCDebug(QGCStateMachineLog) << stateName() << "failed, retrying in"
42 << _retryDelayMsecs << "ms";
43 emit retrying(_currentAttempt + 1, totalAttempts());
44 _retryTimer.start(_retryDelayMsecs);
45 } else {
46 qCDebug(QGCStateMachineLog) << stateName() << "all retries exhausted";
47 emit exhausted();
48
49 if (_exhaustedBehavior == EmitAdvance) {
50 _wasSkipped = true;
51 emit skipped();
52 emit advance();
53 } else {
54 emit error();
55 }
56 }
57}
QString stateName() const
void succeeded()
Emitted when the action eventually succeeds.
int totalAttempts() const
Definition RetryState.h:35
std::function< bool()> Action
Definition RetryState.h:19
void skipped()
Emitted when retries are exhausted and behavior is EmitAdvance.
void onEnter() override
Override to perform actions on state entry.
Definition RetryState.cc:17
void exhausted()
Emitted when all retries are exhausted (both behaviors).
RetryState(const QString &stateName, QState *parent, Action action, int maxRetries=0, int retryDelayMsecs=1000, ExhaustedBehavior exhaustedBehavior=EmitError)
Definition RetryState.cc:4
@ EmitAdvance
Emit advance() after retries exhausted (skip/continue).
Definition RetryState.h:23
void retrying(int attempt, int maxAttempts)
Emitted before each retry attempt.