QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
CircuitBreakerState.h
Go to the documentation of this file.
1#pragma once
2
3#include "QGCState.h"
4
5#include <QtCore/QElapsedTimer>
6
7#include <functional>
8
32{
33 Q_OBJECT
34 Q_DISABLE_COPY(CircuitBreakerState)
35
36public:
37 using Action = std::function<bool()>;
38
39 enum class State {
40 Closed,
41 Open,
43 };
44 Q_ENUM(State)
45
46
51 CircuitBreakerState(const QString& stateName, QState* parent,
52 Action action, int failureThreshold = 5,
53 int resetTimeoutMsecs = 30000);
54
56 State circuitState() const { return _circuitState; }
57
59 int failureCount() const { return _failureCount; }
60
62 int failureThreshold() const { return _failureThreshold; }
63
65 void reset();
66
68 bool isTripped() const { return _circuitState == State::Open; }
69
70signals:
72 void tripped();
73
76
78 void succeeded();
79
81 void failed();
82
83protected:
84 void onEnter() override;
85
86private:
87 Action _action;
88 int _failureThreshold;
89 int _resetTimeoutMsecs;
90
91 State _circuitState = State::Closed;
92 int _failureCount = 0;
93 QElapsedTimer _tripTimer;
94};
State circuitState() const
Get the current circuit state.
std::function< bool()> Action
bool isTripped() const
Check if the circuit is currently tripped (open)
void tripped()
Emitted when the circuit trips (too many failures)
int failureCount() const
Get the current failure count.
void succeeded()
Emitted when action succeeds.
void failed()
Emitted when action fails (or circuit is open)
void reset()
Manually reset the circuit breaker.
void onEnter() override
Override to perform actions on state entry.
int failureThreshold() const
Get the failure threshold.
void circuitReset()
Emitted when the circuit resets (after successful half-open test)
@ Closed
Normal operation.
@ Open
Tripped, failing fast.
@ HalfOpen
Testing if service recovered.
QString stateName() const