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
28class StateHistoryRecorder : public QObject
29{
30 Q_OBJECT
31
32public:
43 Q_ENUM(TransitionReason)
44
45
46 struct HistoryEntry {
47 QDateTime timestamp;
48 QString stateName;
50 QString details;
51
52 QJsonObject toJson() const;
53 QString toString() const;
54 };
55
56 explicit StateHistoryRecorder(QGCStateMachine* machine, int maxEntries = 1000);
57 ~StateHistoryRecorder() override = default;
58
60 void setEnabled(bool enabled);
61 bool isEnabled() const { return _enabled; }
62
64 void setMaxEntries(int max);
65 int maxEntries() const { return _maxEntries; }
66
68 void clear();
69
71 int count() const { return _history.size(); }
72
74 QList<HistoryEntry> history() const { return _history; }
75
77 QList<HistoryEntry> lastEntries(int n) const;
78
80 QList<HistoryEntry> entriesForState(const QString& stateName) const;
81
83 QString dumpHistory() const;
84
86 QJsonArray toJson() const;
87
89 void logHistory() const;
90
92 void addEntry(const QString& stateName, TransitionReason reason,
93 const QString& details = QString());
94
95private slots:
96 void _onStateEntered();
97 void _onStateExited();
98
99private:
100 void _addEntry(const HistoryEntry& entry);
101 void _connectToState(QAbstractState* state);
102
103 QGCStateMachine* _machine = nullptr;
104 bool _enabled = false;
105 int _maxEntries = 1000;
106 QList<HistoryEntry> _history;
107 QList<QMetaObject::Connection> _connections;
108};
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.
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.