QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
ErrorRecoveryBuilder.h
Go to the documentation of this file.
1#pragma once
2
3#include "QGCState.h"
4
5#include <QtCore/QTimer>
6
7#include <functional>
8
10
28{
29public:
30 using Action = std::function<bool()>;
31 using VoidAction = std::function<void()>;
32
40
41 ErrorRecoveryBuilder(QGCStateMachine* machine, const QString& stateName);
42
45
49 ErrorRecoveryBuilder& retry(int maxRetries, int delayMsecs = 1000);
50
53
56
59
61 ErrorRecoveryBuilder& withTimeout(int timeoutMsecs);
62
64 QGCState* build();
65
66private:
67 QGCStateMachine* _machine;
68 QString _stateName;
69 Action _action;
70 Action _fallback;
71 VoidAction _rollback;
72 int _maxRetries = 0;
73 int _retryDelayMsecs = 1000;
74 int _timeoutMsecs = 0;
75 ExhaustedBehavior _exhaustedBehavior = EmitError;
76};
77
81{
82 Q_OBJECT
83 Q_DISABLE_COPY(ErrorRecoveryState)
84
85public:
86 using Action = std::function<bool()>;
87 using VoidAction = std::function<void()>;
88
89 ErrorRecoveryState(const QString& stateName, QState* parent);
90
91 void setAction(Action action) { _action = std::move(action); }
92 void setFallback(Action fallback) { _fallback = std::move(fallback); }
93 void setRollback(VoidAction rollback) { _rollback = std::move(rollback); }
94 void setRetry(int maxRetries, int delayMsecs);
95 void setTimeout(int timeoutMsecs);
97
99 QString successPhase() const { return _successPhase; }
100
101signals:
102 void retrying(int attempt, int maxAttempts);
105 void succeeded();
106 void exhausted();
107
108protected:
109 void onEnter() override;
110
111private slots:
112 void _executeAction();
113 void _onTimeout();
114
115private:
116 void _handleFailure();
117 void _handleExhausted();
118
119 Action _action;
120 Action _fallback;
121 VoidAction _rollback;
122 int _maxRetries = 0;
123 int _retryDelayMsecs = 1000;
124 int _timeoutMsecs = 0;
126
127 int _currentAttempt = 0;
128 bool _triedFallback = false;
129 QString _successPhase;
130 QTimer _retryTimer;
131 QTimer _timeoutTimer;
132};
Fluent builder for creating error recovery states.
ErrorRecoveryBuilder & retry(int maxRetries, int delayMsecs=1000)
ErrorRecoveryBuilder & withFallback(Action fallback)
Add a fallback action to try if primary fails.
ErrorRecoveryBuilder & withAction(Action action)
Set the primary action to execute.
ExhaustedBehavior
What to do when all recovery options are exhausted.
@ EmitAdvance
Continue anyway (skip)
@ LogAndAdvance
Log warning and continue.
@ LogAndError
Log warning and emit error()
@ EmitError
Emit error() signal (default)
std::function< bool()> Action
std::function< void()> VoidAction
ErrorRecoveryBuilder & onExhausted(ExhaustedBehavior behavior)
Configure what happens when all options are exhausted.
ErrorRecoveryBuilder & withRollback(VoidAction rollback)
Add a rollback action to execute on failure.
QGCState * build()
Build and return the configured state.
ErrorRecoveryBuilder & withTimeout(int timeoutMsecs)
Set a timeout for the entire operation.
The state created by ErrorRecoveryBuilder.
void setRollback(VoidAction rollback)
std::function< bool()> Action
void onEnter() override
Override to perform actions on state entry.
void setTimeout(int timeoutMsecs)
void setFallback(Action fallback)
QString successPhase() const
Get the phase that succeeded (if any)
void retrying(int attempt, int maxAttempts)
std::function< void()> VoidAction
void setAction(Action action)
void setExhaustedBehavior(ErrorRecoveryBuilder::ExhaustedBehavior behavior)
void setRetry(int maxRetries, int delayMsecs)
QString stateName() const
QGroundControl specific state machine with enhanced error handling.
Full-featured base class for QGroundControl state machine states.
Definition QGCState.h:23