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"
6
7#include <QtCore/QJsonDocument>
8#include <QtCore/QJsonArray>
9
10QGC_LOGGING_CATEGORY(CompInfoGeneralLog, "ComponentInformation.CompInfoGeneral")
11
12CompInfoGeneral::CompInfoGeneral(uint8_t compId_, Vehicle* vehicle_, QObject* parent)
13 : CompInfo(COMP_METADATA_TYPE_GENERAL, compId_, vehicle_, parent)
14{
15
16}
17
19{
20 const auto& metadataTypeIter = _supportedTypes.constFind(compInfo.type);
21 if (metadataTypeIter == _supportedTypes.constEnd()) {
22 compInfo._uris = {}; // reset
23 } else {
24 compInfo._uris = *metadataTypeIter;
25 }
26}
27
28void CompInfoGeneral::setJson(const QString& metadataJsonFileName)
29{
30 if (metadataJsonFileName.isEmpty()) {
31 return;
32 }
33
34 QString errorString;
35 QJsonDocument jsonDoc;
36
37 if (!JsonParsing::isJsonFile(metadataJsonFileName, jsonDoc, errorString)) {
38 qCWarning(CompInfoGeneralLog) << "Metadata json file open failed: compid:" << compId << errorString;
39 return;
40 }
41
42 QString schemaError;
43 if (!JsonSchemaValidator::validate(jsonDoc, QStringLiteral(":/json/component_metadata/general.schema.json"), schemaError)) {
44 qCWarning(CompInfoGeneralLog) << "Metadata json schema validation failed: compid:" << compId << schemaError;
45 }
46
47 QJsonObject jsonObj = jsonDoc.object();
48
49 QList<JsonParsing::KeyValidateInfo> keyInfoList = {
50 { JsonParsing::jsonVersionKey, QJsonValue::Double, true },
51 { _jsonMetadataTypesKey, QJsonValue::Array, true },
52 };
53 if (!JsonParsing::validateKeys(jsonObj, keyInfoList, errorString)) {
54 qCWarning(CompInfoGeneralLog) << "Metadata json validation failed: compid:" << compId << errorString;
55 return;
56 }
57
58 int version = jsonObj[JsonParsing::jsonVersionKey].toInt();
59 if (version != 1) {
60 qCWarning(CompInfoGeneralLog) << "Metadata json unsupported version" << version;
61 return;
62 }
63
64 QJsonArray rgSupportedTypes = jsonObj[_jsonMetadataTypesKey].toArray();
65 for (QJsonValue typeValue : rgSupportedTypes) {
66 int metadataType = typeValue["type"].toInt(-1);
67 if (metadataType == -1)
68 continue;
69 Uris uris;
70 uris.uriMetaData = typeValue["uri"].toString();
71 uris.crcMetaData = typeValue["fileCrc"].toVariant().toLongLong(); // Note: can't use toInt(), as it returns 0 when exceeding 2^31
72 uris.crcMetaDataValid = typeValue.toObject().contains("fileCrc");
73 uris.uriMetaDataFallback = typeValue["uriFallback"].toString();
74 uris.crcMetaDataFallback = typeValue["fileCrcFallback"].toVariant().toLongLong();
75 uris.crcMetaDataFallbackValid = typeValue.toObject().contains("fileCrcFallback");
76 uris.uriTranslation = typeValue["translationUri"].toString();
77 uris.uriTranslationFallback = typeValue["translationUriFallback"].toString();
78
79 if (uris.uriMetaData.isEmpty() || !uris.crcMetaDataValid) {
80 // The CRC is optional for dynamically updated metadata, and once we want to support that this logic needs
81 // to be updated.
82 qCDebug(CompInfoGeneralLog) << "Metadata missing fields: type:uri:crcValid" << metadataType <<
83 uris.uriMetaData << uris.crcMetaDataValid;
84 continue;
85 }
86
87 _supportedTypes[(COMP_METADATA_TYPE)metadataType] = uris;
88 qCDebug(CompInfoGeneralLog) << "Metadata type : uri : crc" << metadataType << uris.uriMetaData << uris.crcMetaData;
89 }
90}
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
bool validate(const QJsonDocument &doc, const QString &schemaResourcePath, QString &errorString)