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
27{
28public:
29 using Action = std::function<bool()>;
30 using VoidAction = std::function<void()>;
31
39
40 ErrorRecoveryBuilder(QGCStateMachine* machine, const QString& stateName);
41
44
48 ErrorRecoveryBuilder& retry(int maxRetries, int delayMsecs = 1000);
49
52
55
58
60 ErrorRecoveryBuilder& withTimeout(int timeoutMsecs);
61
63 QGCState* build();
64
65private:
66 QGCStateMachine* _machine;
67 QString _stateName;
68 Action _action;
69 Action _fallback;
70 VoidAction _rollback;
71 int _maxRetries = 0;
72 int _retryDelayMsecs = 1000;
73 int _timeoutMsecs = 0;
74 ExhaustedBehavior _exhaustedBehavior = EmitError;
75};
76
79{
80 Q_OBJECT
81 Q_DISABLE_COPY(ErrorRecoveryState)
82
83public:
84 using Action = std::function<bool()>;
85 using VoidAction = std::function<void()>;
86
87 ErrorRecoveryState(const QString& stateName, QState* parent);
88
89 void setAction(Action action) { _action = std::move(action); }
90 void setFallback(Action fallback) { _fallback = std::move(fallback); }
91 void setRollback(VoidAction rollback) { _rollback = std::move(rollback); }
92 void setRetry(int maxRetries, int delayMsecs);
93 void setTimeout(int timeoutMsecs);
95
97 QString successPhase() const { return _successPhase; }
98
99signals:
100 void retrying(int attempt, int maxAttempts);
103 void succeeded();
104 void exhausted();
105
106protected:
107 void onEnter() override;
108
109private slots:
110 void _executeAction();
111 void _onTimeout();
112
113private:
114 void _handleFailure();
115 void _handleExhausted();
116
117 Action _action;
118 Action _fallback;
119 VoidAction _rollback;
120 int _maxRetries = 0;
121 int _retryDelayMsecs = 1000;
122 int _timeoutMsecs = 0;
124
125 int _currentAttempt = 0;
126 bool _triedFallback = false;
127 QString _successPhase;
128 QTimer _retryTimer;
129 QTimer _timeoutTimer;
130};
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.