QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
NTRIPConnectionStats.cc
Go to the documentation of this file.
2
3#include <algorithm>
4
5NTRIPConnectionStats::NTRIPConnectionStats(QObject* parent) : QObject(parent), _rateTimer(this)
6{
7 _rateTimer.setInterval(std::chrono::seconds{1});
8 _rateTimer.callOnTimeout(this, [this]() {
9 const quint64 totalBytes = _rateTracker.totalBytes();
10 if (totalBytes != _prevBytesReceived) {
11 _prevBytesReceived = totalBytes;
13 emit dataRateChanged();
14 } else if (_rateTracker.bytesPerSec() > 0) {
15 emit dataRateChanged();
16 }
17 if (_prevMessagesReceived != _messagesReceived) {
18 _prevMessagesReceived = _messagesReceived;
20 }
21 if (_lastMessageTime.isValid()) {
23 }
24
25 const bool stale = _lastMessageTime.isValid() && _lastMessageTime.elapsed() >= kStaleThreshold.count() &&
26 _messagesReceived > 0;
27 if (stale != _dataStale) {
28 _dataStale = stale;
29 emit dataStaleChanged();
30 }
31 if (_messageCountsDirty) {
32 _messageCountsDirty = false;
34 }
35 });
36}
37
39{
40 _rateTimer.start();
41}
42
44{
45 _rateTimer.stop();
46 if (_rateTracker.bytesPerSec() != 0.0) {
47 _rateTracker.reset();
48 _prevBytesReceived = 0;
49 emit dataRateChanged();
51 }
52}
53
54void NTRIPConnectionStats::recordMessage(int bytes, int messageId)
55{
56 if (bytes <= 0) {
57 return;
58 }
59 _rateTracker.recordBytes(bytes);
60 _messagesReceived++;
61 _lastMessageTime.restart();
62 if (_dataStale) {
63 _dataStale = false;
64 emit dataStaleChanged();
65 }
66 if (_rateTracker.rateUpdated()) {
67 _prevBytesReceived = _rateTracker.totalBytes();
68 emit dataRateChanged();
70 }
71 ++_messageCountsById[messageId];
72 _messageCountsDirty = true;
73}
74
76{
77 _rateTracker.reset();
78 _prevBytesReceived = 0;
79 _messagesReceived = 0;
80 _lastMessageTime.invalidate();
81 _messageCountsById.clear();
82 _messageCountsDirty = false;
83 if (_dataStale) {
84 _dataStale = false;
85 emit dataStaleChanged();
86 }
89 emit dataRateChanged();
92}
93
95{
96 QList<int> ids = _messageCountsById.keys();
97 std::sort(ids.begin(), ids.end());
98
99 QVariantList out;
100 out.reserve(ids.size());
101 for (int id : ids) {
102 out.append(QVariant(QVariantList{id, _messageCountsById.value(id)}));
103 }
104 return out;
105}
void reset()
Reset all counters and restart the elapsed timer.
void recordBytes(qsizetype bytes)
Record incoming/outgoing bytes. Call whenever data passes through.
bool rateUpdated() const
double bytesPerSec() const
quint64 totalBytes() const
Total bytes recorded since construction or last reset.
NTRIPConnectionStats(QObject *parent=nullptr)
void messageCountsByIdChanged()
void messagesReceivedChanged()
QVariantList messageCountsById() const
void recordMessage(int bytes, int messageId=0)