6#include <QtCore/QStringList>
7#include <QtCore/QTimeZone>
12#include <ulog_cpp/subscription.hpp>
21QString _px4NavStateName(
int state)
25 case 0:
return QStringLiteral(
"Manual");
26 case 1:
return QStringLiteral(
"Altitude");
27 case 2:
return QStringLiteral(
"Position");
28 case 3:
return QStringLiteral(
"Mission");
29 case 4:
return QStringLiteral(
"Hold");
30 case 5:
return QStringLiteral(
"Return");
31 case 6:
return QStringLiteral(
"Position Slow");
32 case 8:
return QStringLiteral(
"Altitude Cruise");
33 case 10:
return QStringLiteral(
"Acro");
34 case 12:
return QStringLiteral(
"Descend");
35 case 13:
return QStringLiteral(
"Termination");
36 case 14:
return QStringLiteral(
"Offboard");
37 case 15:
return QStringLiteral(
"Stabilized");
38 case 17:
return QStringLiteral(
"Takeoff");
39 case 18:
return QStringLiteral(
"Land");
40 case 19:
return QStringLiteral(
"Follow Target");
41 case 20:
return QStringLiteral(
"Precision Land");
42 case 21:
return QStringLiteral(
"Orbit");
43 case 22:
return QStringLiteral(
"VTOL Takeoff");
44 default:
return QStringLiteral(
"Mode %1").arg(state);
48bool _isNumericBasicType(ulog_cpp::Field::BasicType type)
50 using BT = ulog_cpp::Field::BasicType;
78 const QString errorMessage = QString::fromStdString(msg);
79 if (!is_recoverable) {
80 _hadFatalError =
true;
81 if (_result.errorMessage.isEmpty()) {
82 _result.errorMessage = errorMessage;
85 qCWarning(ULogFullHandlerLog) <<
"ULog parse error:" << errorMessage;
90 _logStartSecs =
static_cast<double>(header.header().timestamp) / 1e6;
95 _formats[message_format.name()] = std::make_shared<ulog_cpp::MessageFormat>(message_format);
100 const auto it = _formats.find(add_logged_message.messageName());
101 if (it != _formats.cend()) {
102 _subscriptions[add_logged_message.msgId()] = {
104 add_logged_message.multiId(),
105 add_logged_message.messageName()
112 _headerComplete =
true;
113 for (
auto &[name, fmt] : _formats) {
114 fmt->resolveDefinition(_formats);
120 if (!_headerComplete) {
124 const auto it = _subscriptions.find(
data.msgId());
125 if (it == _subscriptions.cend()) {
129 const SubscriptionInfo &sub = it->second;
135 const ulog_cpp::TypedDataView view(
data, *sub.format);
138 double timestampSecs = -1.0;
139 if (sub.format->fieldMap().count(
"timestamp") > 0) {
140 const uint64_t tsUs = view.at(
"timestamp").as<uint64_t>();
141 timestampSecs =
static_cast<double>(tsUs) / 1e6;
143 if (timestampSecs < _logStartSecs) {
144 timestampSecs = _logStartSecs;
147 _lastTimestampSecs = timestampSecs;
152#ifndef QGC_NO_LOG_START_TIME
153 if (_result.startTime.isNull() && timestampSecs >= 0.0) {
154 const auto& fieldMap = sub.format->fieldMap();
155 uint64_t utcUsec = 0;
156 uint64_t utcTimestampUs = 0;
157 if ((sub.topicName ==
"sensor_gnss" || sub.topicName ==
"sensor_gps" ||
158 sub.topicName ==
"vehicle_gps_position") &&
159 fieldMap.count(
"time_utc_usec") > 0) {
160 utcUsec = view.at(
"time_utc_usec").as<uint64_t>();
161 utcTimestampUs = view.at(
"timestamp").as<uint64_t>();
162 }
else if (sub.topicName ==
"vehicle_gnss" && fieldMap.count(
"receiver") > 0) {
165 const auto& receiver = fieldMap.at(
"receiver");
166 if (receiver->type().type == ulog_cpp::Field::BasicType::NESTED && receiver->definitionResolved() &&
167 receiver->nestedFormat()->fieldMap().count(
"time_utc_usec") > 0 &&
168 receiver->nestedFormat()->fieldMap().count(
"timestamp") > 0) {
169 utcUsec = view.at(receiver)[
"time_utc_usec"].as<uint64_t>();
170 utcTimestampUs = view.at(receiver)[
"timestamp"].as<uint64_t>();
174 if (utcUsec >= utcTimestampUs) {
175 const qint64 startMs =
static_cast<qint64
>((utcUsec - utcTimestampUs) / 1000);
176 _result.startTime = QDateTime::fromMSecsSinceEpoch(startMs, QTimeZone::utc());
183 const QString prefix = (sub.multiId > 0)
184 ? QStringLiteral(
"%1[%2].").arg(QString::fromStdString(sub.topicName)).arg(sub.multiId)
185 : QString::fromStdString(sub.topicName) + QLatin1Char(
'.');
187 for (
const auto &field : sub.format->fields()) {
189 if (field->name().rfind(
"_padding", 0) == 0) {
192 if (field->name() ==
"timestamp") {
195 if (!field->definitionResolved()) {
199 const QString fieldName = prefix + QString::fromStdString(field->name());
200 _collectFieldSamples(fieldName, *field, view.at(field), timestampSecs);
203 _result.sampleCount++;
205 if (timestampSecs >= 0.0) {
206 if (_result.minTimestamp < 0.0 || timestampSecs < _result.minTimestamp) {
207 _result.minTimestamp = timestampSecs;
209 _result.maxTimestamp = std::max(_result.maxTimestamp, timestampSecs);
211 }
catch (
const std::exception &e) {
212 qCWarning(ULogFullHandlerLog) <<
"Failed to decode data message:" << e.what();
216void ULogFullHandler::_collectFieldSamples(
const QString& fieldName,
const ulog_cpp::Field& field,
217 const ulog_cpp::Value& value,
double timestampSecs)
219 if (field.arrayLength() < 0) {
220 _collectElementSamples(fieldName, field, value, timestampSecs);
225 if (field.type().type == ulog_cpp::Field::BasicType::CHAR) {
226 _fieldSet.insert(fieldName);
230 for (
int i = 0; i < field.arrayLength(); i++) {
231 _collectElementSamples(QStringLiteral(
"%1[%2]").arg(fieldName).arg(i), field, value[
static_cast<size_t>(i)],
236void ULogFullHandler::_collectElementSamples(
const QString& fieldName,
const ulog_cpp::Field& field,
237 const ulog_cpp::Value& value,
double timestampSecs)
239 if (field.type().type == ulog_cpp::Field::BasicType::NESTED) {
240 for (
const auto& nestedField : field.nestedFormat()->fields()) {
241 if (nestedField->name().rfind(
"_padding", 0) == 0) {
244 if (!nestedField->definitionResolved()) {
247 const QString nestedFieldName = fieldName + QLatin1Char(
'.') + QString::fromStdString(nestedField->name());
248 _collectFieldSamples(nestedFieldName, *nestedField, value[*nestedField], timestampSecs);
253 _fieldSet.insert(fieldName);
255 if (!_isNumericBasicType(field.type().type) || (timestampSecs < 0.0)) {
259 _result.fieldSamples[fieldName].append(QPointF(timestampSecs, value.as<
double>()));
260 _plottableFieldSet.insert(fieldName);
265 const double timestampSecs =
static_cast<double>(
logging.timestamp()) / 1e6;
266 const QString text = QString::fromStdString(
logging.message());
268 if (text.isEmpty()) {
273 msgRow[QStringLiteral(
"time")] = timestampSecs;
274 msgRow[QStringLiteral(
"text")] = text;
275 _result.messages.append(msgRow);
277 using Level = ulog_cpp::Logging::Level;
278 if (
logging.logLevel() <= Level::Warning) {
279 const QString eventType = (
logging.logLevel() <= Level::Error)
280 ? QStringLiteral(
"error")
281 : QStringLiteral(
"warning");
283 if (timestampSecs >= 0.0) {
284 QVariantMap eventRow;
285 eventRow[QStringLiteral(
"time")] = timestampSecs;
286 eventRow[QStringLiteral(
"type")] = eventType;
287 eventRow[QStringLiteral(
"description")] = text;
288 _result.events.append(eventRow);
295 const QString name = QString::fromStdString(param.field().name());
296 if (name.isEmpty()) {
301 bool isFloat =
false;
305 value = param.value().as<
double>();
306 isFloat = (param.field().type().type == ulog_cpp::Field::BasicType::FLOAT);
307 }
catch (
const std::exception &) {
312 row[QStringLiteral(
"name")] = name;
313 row[QStringLiteral(
"value")] = value;
314 row[QStringLiteral(
"isFloat")] = isFloat;
317 _result.parameters.append(row);
322 const QString name = QString::fromStdString(param_default.field().name());
323 if (name.isEmpty()) {
327 const double defaultVal = param_default.value().as<
double>();
330 using DT = ulog_cpp::ulog_parameter_default_type_t;
331 if ((
static_cast<uint8_t
>(param_default.defaultType()) &
332 static_cast<uint8_t
>(DT::system)) != 0) {
333 _paramDefaults[name] = defaultVal;
335 }
catch (
const std::exception &) {
342 if (_lastTimestampSecs < 0.0) {
346 const double start = _lastTimestampSecs;
347 const double end = start +
static_cast<double>(
dropout.durationMs()) / 1000.0;
349 row[QStringLiteral(
"start")] = start;
350 row[QStringLiteral(
"end")] = end;
351 _result.dropouts.append(row);
356 if (_staleSampleCount > 0) {
357 qCDebug(ULogFullHandlerLog) <<
"Moved" << _staleSampleCount <<
"samples older than log start to"
358 << _logStartSecs <<
"s";
363 const auto vehicleTypeIt = _result.fieldSamples.constFind(QStringLiteral(
"vehicle_status.vehicle_type"));
364 if (vehicleTypeIt != _result.fieldSamples.cend() && !vehicleTypeIt->isEmpty()) {
365 const int vtype =
static_cast<int>(vehicleTypeIt->first().y());
367 case 1: _result.detectedVehicleType = QStringLiteral(
"Multirotor/Helicopter");
break;
368 case 2: _result.detectedVehicleType = QStringLiteral(
"Fixed Wing");
break;
369 case 3: _result.detectedVehicleType = QStringLiteral(
"Rover");
break;
370 case 4: _result.detectedVehicleType = QStringLiteral(
"Airship");
break;
377 const auto navStateIt = _result.fieldSamples.constFind(QStringLiteral(
"vehicle_status.nav_state"));
378 if (navStateIt != _result.fieldSamples.cend()) {
379 const QVector<QPointF> &samples = navStateIt.value();
380 int lastNavState = -1;
381 double segmentStart = -1.0;
384 for (
const QPointF &pt : samples) {
385 const int navState =
static_cast<int>(pt.y());
386 if (navState != lastNavState) {
388 if (lastNavState >= 0 && segmentStart >= 0.0) {
390 seg[QStringLiteral(
"mode")] = segmentMode;
391 seg[QStringLiteral(
"start")] = segmentStart;
392 seg[QStringLiteral(
"end")] = pt.x();
393 _result.modeSegments.append(seg);
395 lastNavState = navState;
396 segmentStart = pt.x();
397 segmentMode = _px4NavStateName(navState);
402 if (lastNavState >= 0 && segmentStart >= 0.0 && _result.maxTimestamp >= segmentStart) {
404 seg[QStringLiteral(
"mode")] = segmentMode;
405 seg[QStringLiteral(
"start")] = segmentStart;
406 seg[QStringLiteral(
"end")] = _result.maxTimestamp;
407 _result.modeSegments.append(seg);
412 _result.availableFields = _fieldSet.values();
413 std::sort(_result.availableFields.begin(), _result.availableFields.end());
414 _result.plottableFields = _plottableFieldSet.values();
415 std::sort(_result.plottableFields.begin(), _result.plottableFields.end());
419 for (
int i = 0; i < _result.parameters.size(); i++) {
420 QVariantMap row = _result.parameters[i].toMap();
421 const QString name = row.value(QStringLiteral(
"name")).toString();
422 const auto it = _paramDefaults.constFind(name);
423 if (it != _paramDefaults.constEnd()) {
424 const double defaultVal = it.value();
425 const QVariant valueVariant = row.value(QStringLiteral(
"value"));
426 row[QStringLiteral(
"hasDefault")] =
true;
427 row[QStringLiteral(
"defaultValue")] = defaultVal;
431 bool isDefault =
false;
432 if (valueVariant.isValid() && valueVariant.canConvert<
double>()) {
433 const double currentVal = valueVariant.toDouble();
436 isDefault = (currentVal == defaultVal)
437 || (!qFuzzyIsNull(defaultVal) && qFuzzyCompare(currentVal, defaultVal));
439 row[QStringLiteral(
"isDefault")] = isDefault;
441 row[QStringLiteral(
"hasDefault")] =
false;
442 row[QStringLiteral(
"defaultValue")] = QVariant();
443 row[QStringLiteral(
"isDefault")] =
false;
445 _result.parameters[i] = row;
std::function< void(float)> ProgressCallback
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void parameter(const ulog_cpp::Parameter ¶meter) override
void fileHeader(const ulog_cpp::FileHeader &header) override
ULogFullHandler(LogParseResult &result, const ProgressCallback &progressCallback=nullptr)
void addLoggedMessage(const ulog_cpp::AddLoggedMessage &add_logged_message) override
void error(const std::string &msg, bool is_recoverable) override
void logging(const ulog_cpp::Logging &logging) override
void messageFormat(const ulog_cpp::MessageFormat &message_format) override
void parameterDefault(const ulog_cpp::ParameterDefault ¶meter_default) override
void data(const ulog_cpp::Data &data) override
void dropout(const ulog_cpp::Dropout &dropout) override
void headerComplete() override