QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
WaitStateBase.cc
Go to the documentation of this file.
1#include "WaitStateBase.h"
2#include "QGCStateMachine.h"
4
5WaitStateBase::WaitStateBase(const QString& stateName, QState* parent, int timeoutMsecs)
6 : QGCState(stateName, parent)
7 , _timeoutMsecs(timeoutMsecs > 0 ? timeoutMsecs : 0)
8{
9 _timeoutTimer.setSingleShot(true);
10 connect(&_timeoutTimer, &QTimer::timeout, this, &WaitStateBase::_onTimeout);
11 connect(this, &QState::entered, this, &WaitStateBase::_onEntered);
12 connect(this, &QState::exited, this, &WaitStateBase::_onExited);
13}
14
15void WaitStateBase::_onEntered()
16{
17 _completed = false;
20
21 // Check for runtime timeout override
22 int effectiveTimeout = _timeoutMsecs;
23 if (machine()) {
24 int override = machine()->timeoutOverride(objectName());
25 if (override >= 0) {
26 effectiveTimeout = override;
27 qCDebug(QGCStateMachineLog) << stateName() << "using timeout override:" << effectiveTimeout << "ms";
28 }
29 }
30
31 if (effectiveTimeout > 0) {
32 _timeoutTimer.start(effectiveTimeout);
33 }
34}
35
36void WaitStateBase::_onExited()
37{
38 _timeoutTimer.stop();
41}
42
43void WaitStateBase::_onTimeout()
44{
45 if (_completed) {
46 return;
47 }
48
49 qCDebug(QGCStateMachineLog) << "Timeout" << stateName();
50
51 // Record timeout for statistics
52 if (machine()) {
53 machine()->recordTimeout(objectName());
54 }
55
58}
59
61{
62 // Default implementation does nothing - subclasses can override
63}
64
66{
67 // Default implementation does nothing - subclasses can override
68}
69
71{
72 emit timeout();
73 emit timedOut();
74}
75
77{
78 if (_completed) {
79 return;
80 }
81 _completed = true;
82
83 _timeoutTimer.stop();
85
86 emit completed();
87 emit advance();
88}
89
91{
92 if (_completed) {
93 return;
94 }
95 _completed = true;
96
97 _timeoutTimer.stop();
99
100 emit error();
101}
102
104{
105 if (_completed) {
106 return;
107 }
108
109 // _onTimeout() disconnects wait signals. Retry flows that remain in the same
110 // state need an explicit rearm of both wait connections and timeout timer.
113
114 int effectiveTimeout = _timeoutMsecs;
115 if (machine()) {
116 const int override = machine()->timeoutOverride(objectName());
117 if (override >= 0) {
118 effectiveTimeout = override;
119 }
120 }
121
122 if (effectiveTimeout > 0) {
123 _timeoutTimer.start(effectiveTimeout);
124 }
125}
QString stateName() const
QGCStateMachine * machine() const
void completed()
WaitStateBase(const QString &stateName, QState *parent, int timeoutMsecs=0)
virtual void onWaitTimeout()
Called when the timeout expires - default emits timeout(), subclasses can override.
virtual void onWaitEntered()
Called when the state is entered - subclasses should call base implementation.
virtual void connectWaitSignal()=0
Subclasses override to set up their signal connections.
virtual void disconnectWaitSignal()=0
Subclasses override to tear down their signal connections.
virtual void onWaitExited()
Called when the state is exited - subclasses should call base implementation.