QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
HealthAndArmingCheckReport.cc
Go to the documentation of this file.
2#include "EventHandler.h"
4#include "LibEvents.h"
5#include "MAVLinkEnums.h"
7
8QGC_LOGGING_CATEGORY(HealthAndArmingCheckReportLog, "MAVLink.LibEvents.HealthAndArmingCheckReport")
9
10HealthAndArmingCheckProblem::HealthAndArmingCheckProblem(const QString& message, const QString& description, const QString& severity, QObject *parent)
11 : QObject(parent)
12 , _message(message)
13 , _description(description)
14 , _severity(severity)
15{
16 qCDebug(HealthAndArmingCheckReportLog) << this;
17}
18
20{
21 if (acknowledged != _acknowledged) {
22 _acknowledged = acknowledged;
24 }
25}
26
27/*===========================================================================*/
28
30 : QObject(parent)
31 , _problemsForCurrentMode(new QmlObjectListModel(this))
32{
33 qCDebug(HealthAndArmingCheckReportLog) << this;
34}
35
37{
38 _problemsForCurrentMode->clearAndDeleteContents();
39}
40
41void HealthAndArmingCheckReport::update(uint8_t compid, const EventHandler &eventHandler, int flightModeGroup)
42{
43 if (compid != MAV_COMP_ID_AUTOPILOT1) {
44 return;
45 }
46
47 // A mode missing from the metadata still gets the report: the mode group results are valid for other modes
48 const bool currentModeKnown = (flightModeGroup >= 0) && (flightModeGroup < MaxModeGroups);
49 if (!currentModeKnown) {
50 flightModeGroup = -1;
51 if (!_supported || (_modeGroupResults.currentModeGroup != -1)) {
52 qCWarning(HealthAndArmingCheckReportLog) << "Flight mode group not set";
53 }
54 }
55 _supported = true;
56
57 const events::HealthAndArmingChecks::Results &results = eventHandler.healthAndArmingChecks()->results();
58
59 QList<Check> checks;
60 for (const auto& check : results.checks()) {
61 QString severity = "";
62 if (events::externalLogLevel(check.log_levels) <= events::Log::Error) {
63 severity = "error";
64 } else if (events::externalLogLevel(check.log_levels) <= events::Log::Warning) {
65 severity = "warning";
66 }
67
68 QString description = QString::fromStdString(check.description);
69 checks.append(Check{
70 .ackKey = (static_cast<quint64>(check.id) << 32) | check.affected_health_component_index,
71 .message = QString::fromStdString(check.message),
72 .description = description.replace("\n", "<br/>"),
73 .severity = severity,
74 // Without a current mode every check is listed, since any of them might apply
75 .inCurrentMode = !currentModeKnown || ((check.affected_mode_groups & (1ull << flightModeGroup)) != 0),
76 });
77 }
78 _rebuild(checks);
79
80 _modeGroupResults.currentModeGroup = flightModeGroup;
81 _modeGroupResults.canArm = 0;
82 _modeGroupResults.canRun = 0;
83 for (int modeGroup = 0; modeGroup < MaxModeGroups; modeGroup++) {
84 const quint64 modeGroupBit = 1ull << modeGroup;
85 if (results.canArm(modeGroup)) {
86 _modeGroupResults.canArm |= modeGroupBit;
87 }
88 if (results.canRun(modeGroup)) {
89 _modeGroupResults.canRun |= modeGroupBit;
90 }
91 }
92 qCDebug(HealthAndArmingCheckReportLog)
93 << "currentModeGroup:" << flightModeGroup << "canArm:" << Qt::hex << _modeGroupResults.canArm
94 << "canRun:" << _modeGroupResults.canRun << Qt::dec << "checks:" << checks.count()
95 << "currentModeProblems:" << _problemsForCurrentMode->count();
96
97 const auto& healthComponents = results.healthComponents().health_components;
98
99 // GPS state
100 const auto gpsStateIter = healthComponents.find("gps");
101 if (gpsStateIter != healthComponents.end()) {
102 const events::HealthAndArmingChecks::HealthComponent& gpsState = gpsStateIter->second;
103 if (gpsState.health.error || gpsState.arming_check.error) {
104 _gpsState = "red";
105 } else if (gpsState.health.warning || gpsState.arming_check.warning) {
106 _gpsState = "yellow";
107 } else {
108 _gpsState = "green";
109 }
110 }
111
112 emit updated();
113}
114
116{
117 if (_currentModeWarningKeys.isEmpty()) {
118 return;
119 }
120
121 _acknowledgedWarningKeys.unite(_currentModeWarningKeys);
122 for (int i = 0; i < _problemsForCurrentMode->count(); i++) {
123 auto* const problem = _problemsForCurrentMode->value<HealthAndArmingCheckProblem*>(i);
124 if (problem && (problem->severity() == QStringLiteral("warning"))) {
125 problem->setAcknowledged(true);
126 }
127 }
128
129 emit updated();
130}
131
132void HealthAndArmingCheckReport::_rebuild(const QList<Check>& checks)
133{
134 // Checks are matched by key rather than text, so a warning whose values change stays acknowledged.
135 // Mode-independent so acknowledgements survive a mode switch.
136 QSet<quint64> reportedWarningKeys;
137 for (const Check& check : checks) {
138 if (check.severity == QStringLiteral("warning")) {
139 reportedWarningKeys.insert(check.ackKey);
140 }
141 }
142 _acknowledgedWarningKeys.intersect(reportedWarningKeys);
143
144 _problemsForCurrentMode->clearAndDeleteContents();
145 _currentModeWarningKeys.clear();
146 for (const Check& check : checks) {
147 if (!check.inCurrentMode) {
148 continue;
149 }
150
151 auto* const problem = new HealthAndArmingCheckProblem(check.message, check.description, check.severity, this);
152 if (check.severity == QStringLiteral("warning")) {
153 _currentModeWarningKeys.insert(check.ackKey);
154 problem->setAcknowledged(_acknowledgedWarningKeys.contains(check.ackKey));
155 }
156 _problemsForCurrentMode->append(problem);
157 }
158}
159
160#ifdef QGC_UNITTEST_BUILD
161void HealthAndArmingCheckReport::setProblemsForTest(const QStringList& severities)
162{
163 _supported = true;
164 QList<Check> checks;
165 for (const QString& severity : severities) {
166 checks.append(Check{
167 .ackKey = static_cast<quint64>(checks.count()),
168 .message = QStringLiteral("Test problem"),
169 .severity = severity,
170 .inCurrentMode = true,
171 });
172 }
173 _rebuild(checks);
174 emit updated();
175}
176#endif
177
178int HealthAndArmingCheckReport::_countProblems(const QString& severity) const
179{
180 int count = 0;
181 for (int i = 0; i < _problemsForCurrentMode->count(); i++) {
182 const auto* const problem = _problemsForCurrentMode->value<const HealthAndArmingCheckProblem*>(i);
183 if (problem && (problem->severity() == severity) && !problem->acknowledged()) {
184 count++;
185 }
186 }
187 return count;
188}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Drives the MAVLink events protocol for a single component.
const events::HealthAndArmingChecks * healthAndArmingChecks() const
HealthAndArmingCheckProblem(const QString &message, const QString &description, const QString &severity, QObject *parent=nullptr)
HealthAndArmingCheckReport(QObject *parent=nullptr)
void update(uint8_t compid, const EventHandler &eventHandler, int flightModeGroup)