QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
StateHistoryRecorder.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QDateTime>
4#include <QtCore/QJsonArray>
5#include <QtCore/QJsonObject>
6#include <QtCore/QList>
7#include <QtCore/QObject>
8#include <QtCore/QString>
9
10class QAbstractState;
11class QGCStateMachine;
12
29class StateHistoryRecorder : public QObject
30{
31 Q_OBJECT
32
33public:
44 Q_ENUM(TransitionReason)
45
46
47 struct HistoryEntry {
48 QDateTime timestamp;
49 QString stateName;
51 QString details;
52
53 QJsonObject toJson() const;
54 QString toString() const;
55 };
56
57 explicit StateHistoryRecorder(QGCStateMachine* machine, int maxEntries = 1000);
58 ~StateHistoryRecorder() override = default;
59
61 void setEnabled(bool enabled);
62 bool isEnabled() const { return _enabled; }
63
65 void setMaxEntries(int max);
66 int maxEntries() const { return _maxEntries; }
67
69 void clear();
70
72 int count() const { return _history.size(); }
73
75 QList<HistoryEntry> history() const { return _history; }
76
78 QList<HistoryEntry> lastEntries(int n) const;
79
81 QList<HistoryEntry> entriesForState(const QString& stateName) const;
82
84 QString dumpHistory() const;
85
87 QJsonArray toJson() const;
88
90 void logHistory() const;
91
93 void addEntry(const QString& stateName, TransitionReason reason,
94 const QString& details = QString());
95
96private slots:
97 void _onStateEntered();
98 void _onStateExited();
99
100private:
101 void _addEntry(const HistoryEntry& entry);
102 void _connectToState(QAbstractState* state);
103
104 QGCStateMachine* _machine = nullptr;
105 bool _enabled = false;
106 int _maxEntries = 1000;
107 QList<HistoryEntry> _history;
108 QList<QMetaObject::Connection> _connections;
109};
QGroundControl specific state machine with enhanced error handling.
void setMaxEntries(int max)
Set the maximum number of entries to keep (circular buffer).
QString dumpHistory() const
Get a human-readable dump of the history.
QList< HistoryEntry > history() const
Get all history entries (oldest first).
void addEntry(const QString &stateName, TransitionReason reason, const QString &details=QString())
Manually add an entry (for custom transition types).
int count() const
Get the number of recorded entries.
QList< HistoryEntry > entriesForState(const QString &stateName) const
Get entries for a specific state.
QJsonArray toJson() const
Export history as JSON array.
StateHistoryRecorder(QGCStateMachine *machine, int maxEntries=1000)
void clear()
Clear all recorded history.
void setEnabled(bool enabled)
Enable or disable recording.
~StateHistoryRecorder() override=default
QList< HistoryEntry > lastEntries(int n) const
Get the last N entries.
TransitionReason
Reason for a state transition.
@ Timeout
Transition due to timeout.
@ Error
Transition due to error.
@ Signal
Transition triggered by signal.
@ Event
Transition triggered by event.
@ Entered
State was entered.
@ Exited
State was exited.
void logHistory() const
Log history to debug output.
A recorded state transition entry.