QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
CompInfoGeneral.cc
Go to the documentation of this file.
1#include "CompInfoGeneral.h"
3#include "JsonParsing.h"
5
6#include <QtCore/QJsonDocument>
7#include <QtCore/QJsonArray>
8
9QGC_LOGGING_CATEGORY(CompInfoGeneralLog, "ComponentInformation.CompInfoGeneral")
10
11CompInfoGeneral::CompInfoGeneral(uint8_t compId_, Vehicle* vehicle_, QObject* parent)
12 : CompInfo(COMP_METADATA_TYPE_GENERAL, compId_, vehicle_, parent)
13{
14
15}
16
18{
19 const auto& metadataTypeIter = _supportedTypes.constFind(compInfo.type);
20 if (metadataTypeIter == _supportedTypes.constEnd()) {
21 compInfo._uris = {}; // reset
22 } else {
23 compInfo._uris = *metadataTypeIter;
24 }
25}
26
27void CompInfoGeneral::setJson(const QString& metadataJsonFileName)
28{
29 if (metadataJsonFileName.isEmpty()) {
30 return;
31 }
32
33 QString errorString;
34 QJsonDocument jsonDoc;
35
36 if (!JsonParsing::isJsonFile(metadataJsonFileName, jsonDoc, errorString)) {
37 qCWarning(CompInfoGeneralLog) << "Metadata json file open failed: compid:" << compId << errorString;
38 return;
39 }
40 QJsonObject jsonObj = jsonDoc.object();
41
42 QList<JsonParsing::KeyValidateInfo> keyInfoList = {
43 { JsonParsing::jsonVersionKey, QJsonValue::Double, true },
44 { _jsonMetadataTypesKey, QJsonValue::Array, true },
45 };
46 if (!JsonParsing::validateKeys(jsonObj, keyInfoList, errorString)) {
47 qCWarning(CompInfoGeneralLog) << "Metadata json validation failed: compid:" << compId << errorString;
48 return;
49 }
50
51 int version = jsonObj[JsonParsing::jsonVersionKey].toInt();
52 if (version != 1) {
53 qCWarning(CompInfoGeneralLog) << "Metadata json unsupported version" << version;
54 return;
55 }
56
57 QJsonArray rgSupportedTypes = jsonObj[_jsonMetadataTypesKey].toArray();
58 for (QJsonValue typeValue : rgSupportedTypes) {
59 int metadataType = typeValue["type"].toInt(-1);
60 if (metadataType == -1)
61 continue;
62 Uris uris;
63 uris.uriMetaData = typeValue["uri"].toString();
64 uris.crcMetaData = typeValue["fileCrc"].toVariant().toLongLong(); // Note: can't use toInt(), as it returns 0 when exceeding 2^31
65 uris.crcMetaDataValid = typeValue.toObject().contains("fileCrc");
66 uris.uriMetaDataFallback = typeValue["uriFallback"].toString();
67 uris.crcMetaDataFallback = typeValue["fileCrcFallback"].toVariant().toLongLong();
68 uris.crcMetaDataFallbackValid = typeValue.toObject().contains("fileCrcFallback");
69 uris.uriTranslation = typeValue["translationUri"].toString();
70 uris.uriTranslationFallback = typeValue["translationUriFallback"].toString();
71
72 if (uris.uriMetaData.isEmpty() || !uris.crcMetaDataValid) {
73 // The CRC is optional for dynamically updated metadata, and once we want to support that this logic needs
74 // to be updated.
75 qCDebug(CompInfoGeneralLog) << "Metadata missing fields: type:uri:crcValid" << metadataType <<
76 uris.uriMetaData << uris.crcMetaDataValid;
77 continue;
78 }
79
80 _supportedTypes[(COMP_METADATA_TYPE)metadataType] = uris;
81 qCDebug(CompInfoGeneralLog) << "Metadata type : uri : crc" << metadataType << uris.uriMetaData << uris.crcMetaData;
82 }
83}
QString errorString
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void setJson(const QString &metadataJsonFileName) override
void setUris(CompInfo &compInfo) const
Base class for all CompInfo types.
Definition CompInfo.h:15
const COMP_METADATA_TYPE type
Definition CompInfo.h:36
const uint8_t compId
Definition CompInfo.h:38
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.
bool isJsonFile(const QByteArray &bytes, QJsonDocument &jsonDoc, QString &errorString)
Determines whether an in-memory byte buffer contains parseable JSON content.
constexpr const char * jsonVersionKey
Definition JsonParsing.h:12