Skip to content
Snippets Groups Projects
Commit 1c95fb26 authored by DonLakeFlyer's avatar DonLakeFlyer
Browse files

Support param meta data and gzip json
parent ee4d1384
Branches
No related tags found
No related merge requests found
...@@ -337,5 +337,7 @@ ...@@ -337,5 +337,7 @@
<file alias="APMArduSubMockLink.params">src/comm/APMArduSubMockLink.params</file> <file alias="APMArduSubMockLink.params">src/comm/APMArduSubMockLink.params</file>
<file alias="PX4MockLink.params">src/comm/PX4MockLink.params</file> <file alias="PX4MockLink.params">src/comm/PX4MockLink.params</file>
<file alias="Version.MetaData.json">src/comm/MockLink.Version.MetaData.json</file> <file alias="Version.MetaData.json">src/comm/MockLink.Version.MetaData.json</file>
<file alias="Version.MetaData.json.gz">src/comm/MockLink.Version.MetaData.json.gz</file>
<file alias="Parameter.MetaData.json">src/comm/MockLink.Parameter.MetaData.json</file>
</qresource> </qresource>
</RCC> </RCC>
...@@ -10,14 +10,21 @@ ...@@ -10,14 +10,21 @@
#include "ComponentInformationManager.h" #include "ComponentInformationManager.h"
#include "Vehicle.h" #include "Vehicle.h"
#include "FTPManager.h" #include "FTPManager.h"
#include "QGCZlib.h"
#include "JsonHelper.h"
#include <QStandardPaths> #include <QStandardPaths>
#include <QJsonDocument>
#include <QJsonArray>
QGC_LOGGING_CATEGORY(ComponentInformationManagerLog, "ComponentInformationManagerLog") QGC_LOGGING_CATEGORY(ComponentInformationManagerLog, "ComponentInformationManagerLog")
const char* ComponentInformationManager::_jsonVersionKey = "version";
const char* ComponentInformationManager::_jsonSupportedCompMetadataTypesKey = "supportedCompMetadataTypes";
ComponentInformationManager::StateFn ComponentInformationManager::_rgStates[]= { ComponentInformationManager::StateFn ComponentInformationManager::_rgStates[]= {
ComponentInformationManager::_stateRequestCompInfoVersion, ComponentInformationManager::_stateRequestCompInfoVersion,
//ComponentInformationManager::_stateRequestCompInfoParam, ComponentInformationManager::_stateRequestCompInfoParam,
ComponentInformationManager::_stateRequestAllCompInfoComplete ComponentInformationManager::_stateRequestAllCompInfoComplete
}; };
...@@ -27,6 +34,7 @@ RequestMetaDataTypeStateMachine::StateFn RequestMetaDataTypeStateMachine::_rgSta ...@@ -27,6 +34,7 @@ RequestMetaDataTypeStateMachine::StateFn RequestMetaDataTypeStateMachine::_rgSta
RequestMetaDataTypeStateMachine::_stateRequestCompInfo, RequestMetaDataTypeStateMachine::_stateRequestCompInfo,
RequestMetaDataTypeStateMachine::_stateRequestMetaDataJson, RequestMetaDataTypeStateMachine::_stateRequestMetaDataJson,
RequestMetaDataTypeStateMachine::_stateRequestTranslationJson, RequestMetaDataTypeStateMachine::_stateRequestTranslationJson,
RequestMetaDataTypeStateMachine::_stateRequestComplete,
}; };
int RequestMetaDataTypeStateMachine::_cStates = sizeof(RequestMetaDataTypeStateMachine::_rgStates) / sizeof(RequestMetaDataTypeStateMachine::_rgStates[0]); int RequestMetaDataTypeStateMachine::_cStates = sizeof(RequestMetaDataTypeStateMachine::_rgStates) / sizeof(RequestMetaDataTypeStateMachine::_rgStates[0]);
...@@ -69,7 +77,12 @@ void ComponentInformationManager::_stateRequestCompInfoComplete(void) ...@@ -69,7 +77,12 @@ void ComponentInformationManager::_stateRequestCompInfoComplete(void)
void ComponentInformationManager::_stateRequestCompInfoParam(StateMachine* stateMachine) void ComponentInformationManager::_stateRequestCompInfoParam(StateMachine* stateMachine)
{ {
ComponentInformationManager* compMgr = static_cast<ComponentInformationManager*>(stateMachine); ComponentInformationManager* compMgr = static_cast<ComponentInformationManager*>(stateMachine);
compMgr->_requestTypeStateMachine.request(COMP_METADATA_TYPE_PARAMETER);
if (compMgr->_isCompTypeSupported(COMP_METADATA_TYPE_PARAMETER)) {
compMgr->_requestTypeStateMachine.request(COMP_METADATA_TYPE_PARAMETER);
} else {
}
} }
void ComponentInformationManager::_stateRequestAllCompInfoComplete(StateMachine* stateMachine) void ComponentInformationManager::_stateRequestAllCompInfoComplete(StateMachine* stateMachine)
...@@ -80,6 +93,41 @@ void ComponentInformationManager::_stateRequestAllCompInfoComplete(StateMachine* ...@@ -80,6 +93,41 @@ void ComponentInformationManager::_stateRequestAllCompInfoComplete(StateMachine*
compMgr->_requestAllCompleteFnData = nullptr; compMgr->_requestAllCompleteFnData = nullptr;
} }
void ComponentInformationManager::_compInfoJsonAvailable(const QString& metadataJsonFileName, const QString& translationsJsonFileName)
{
qCDebug(ComponentInformationManagerLog) << "_compInfoJsonAvailable metadata:translation" << metadataJsonFileName << translationsJsonFileName;
if (!metadataJsonFileName.isEmpty()) {
QString errorString;
QJsonDocument jsonDoc;
if (!JsonHelper::isJsonFile(metadataJsonFileName, jsonDoc, errorString)) {
qCWarning(ComponentInformationManagerLog) << "Version json file read failed" << errorString;
return;
}
QJsonObject jsonObj = jsonDoc.object();
if (currentState() == _stateRequestCompInfoVersion) {
QList<JsonHelper::KeyValidateInfo> keyInfoList = {
{ _jsonVersionKey, QJsonValue::Double, true },
{ _jsonSupportedCompMetadataTypesKey, QJsonValue::Array, true },
};
if (!JsonHelper::validateKeys(jsonObj, keyInfoList, errorString)) {
qCWarning(ComponentInformationManagerLog) << "Version json validation failed:" << errorString;
return;
}
for (const QJsonValue& idValue: jsonObj[_jsonSupportedCompMetadataTypesKey].toArray()) {
_supportedMetaDataTypes.append(static_cast<COMP_METADATA_TYPE>(idValue.toInt()));
}
}
}
}
bool ComponentInformationManager::_isCompTypeSupported(COMP_METADATA_TYPE type)
{
return _supportedMetaDataTypes.contains(type);
}
RequestMetaDataTypeStateMachine::RequestMetaDataTypeStateMachine(ComponentInformationManager* compMgr) RequestMetaDataTypeStateMachine::RequestMetaDataTypeStateMachine(ComponentInformationManager* compMgr)
: _compMgr(compMgr) : _compMgr(compMgr)
{ {
...@@ -91,6 +139,8 @@ void RequestMetaDataTypeStateMachine::request(COMP_METADATA_TYPE type) ...@@ -91,6 +139,8 @@ void RequestMetaDataTypeStateMachine::request(COMP_METADATA_TYPE type)
_compInfoAvailable = false; _compInfoAvailable = false;
_type = type; _type = type;
_stateIndex = -1; _stateIndex = -1;
_jsonMetadataFileName.clear();
_jsonTranslationFileName.clear();
start(); start();
} }
...@@ -171,9 +221,47 @@ void RequestMetaDataTypeStateMachine::_stateRequestCompInfo(StateMachine* stateM ...@@ -171,9 +221,47 @@ void RequestMetaDataTypeStateMachine::_stateRequestCompInfo(StateMachine* stateM
} }
} }
void RequestMetaDataTypeStateMachine::_downloadComplete(const QString& file, const QString& errorMsg) QString RequestMetaDataTypeStateMachine::_downloadCompleteJsonWorker(const QString& fileName, const QString& inflatedFileName)
{ {
qCDebug(ComponentInformationManagerLog) << "RequestMetaDataTypeStateMachine::_downloadComplete" << file << errorMsg; QString outputFileName = fileName;
if (fileName.endsWith(".gz", Qt::CaseInsensitive)) {
outputFileName = (QDir(QStandardPaths::writableLocation(QStandardPaths::TempLocation)).absoluteFilePath(inflatedFileName));
if (QGCZlib::inflateGzipFile(fileName, outputFileName)) {
QFile(fileName).remove();
} else {
qCWarning(ComponentInformationManagerLog) << "Inflate of compressed json failed" << inflatedFileName;
outputFileName.clear();
}
} else {
outputFileName = fileName;
}
return outputFileName;
}
void RequestMetaDataTypeStateMachine::_downloadCompleteMetaDataJson(const QString& fileName, const QString& errorMsg)
{
qCDebug(ComponentInformationManagerLog) << "RequestMetaDataTypeStateMachine::_downloadCompleteMetaDataJson fileName:errorMsg" << fileName << errorMsg;
if (errorMsg.isEmpty()) {
_jsonMetadataFileName = _downloadCompleteJsonWorker(fileName, "metadata.json");
}
advance();
}
void RequestMetaDataTypeStateMachine::_downloadCompleteTranslationJson(const QString& fileName, const QString& errorMsg)
{
qCDebug(ComponentInformationManagerLog) << "RequestMetaDataTypeStateMachine::_downloadCompleteTranslationJson fileName:errorMsg" << fileName << errorMsg;
QString jsonTranslationFileName;
if (errorMsg.isEmpty()) {
jsonTranslationFileName = _downloadCompleteJsonWorker(fileName, "translation.json");
}
_compMgr->_compInfoJsonAvailable(_jsonMetadataFileName, jsonTranslationFileName);
advance(); advance();
} }
...@@ -186,9 +274,9 @@ void RequestMetaDataTypeStateMachine::_stateRequestMetaDataJson(StateMachine* st ...@@ -186,9 +274,9 @@ void RequestMetaDataTypeStateMachine::_stateRequestMetaDataJson(StateMachine* st
if (requestMachine->_compInfoAvailable) { if (requestMachine->_compInfoAvailable) {
ComponentInformation_t& compInfo = requestMachine->_compInfo; ComponentInformation_t& compInfo = requestMachine->_compInfo;
qCDebug(ComponentInformationManagerLog) << "Downloading metadata json" << compInfo.translationURI; qCDebug(ComponentInformationManagerLog) << "Downloading metadata json" << compInfo.metadataURI;
if (_uriIsFTP(compInfo.metadataURI)) { if (_uriIsFTP(compInfo.metadataURI)) {
connect(ftpManager, &FTPManager::downloadComplete, requestMachine, &RequestMetaDataTypeStateMachine::_downloadComplete); connect(ftpManager, &FTPManager::downloadComplete, requestMachine, &RequestMetaDataTypeStateMachine::_downloadCompleteMetaDataJson);
ftpManager->download(compInfo.metadataURI, QStandardPaths::writableLocation(QStandardPaths::TempLocation)); ftpManager->download(compInfo.metadataURI, QStandardPaths::writableLocation(QStandardPaths::TempLocation));
} else { } else {
// FIXME: NYI // FIXME: NYI
...@@ -214,7 +302,7 @@ void RequestMetaDataTypeStateMachine::_stateRequestTranslationJson(StateMachine* ...@@ -214,7 +302,7 @@ void RequestMetaDataTypeStateMachine::_stateRequestTranslationJson(StateMachine*
} else { } else {
qCDebug(ComponentInformationManagerLog) << "Downloading translation json" << compInfo.translationURI; qCDebug(ComponentInformationManagerLog) << "Downloading translation json" << compInfo.translationURI;
if (_uriIsFTP(compInfo.translationURI)) { if (_uriIsFTP(compInfo.translationURI)) {
connect(ftpManager, &FTPManager::downloadComplete, requestMachine, &RequestMetaDataTypeStateMachine::_downloadComplete); connect(ftpManager, &FTPManager::downloadComplete, requestMachine, &RequestMetaDataTypeStateMachine::_downloadCompleteTranslationJson);
ftpManager->download(compInfo.metadataURI, QStandardPaths::writableLocation(QStandardPaths::TempLocation)); ftpManager->download(compInfo.metadataURI, QStandardPaths::writableLocation(QStandardPaths::TempLocation));
} else { } else {
// FIXME: NYI // FIXME: NYI
...@@ -227,6 +315,14 @@ void RequestMetaDataTypeStateMachine::_stateRequestTranslationJson(StateMachine* ...@@ -227,6 +315,14 @@ void RequestMetaDataTypeStateMachine::_stateRequestTranslationJson(StateMachine*
} }
} }
void RequestMetaDataTypeStateMachine::_stateRequestComplete(StateMachine* stateMachine)
{
RequestMetaDataTypeStateMachine* requestMachine = static_cast<RequestMetaDataTypeStateMachine*>(stateMachine);
requestMachine->compMgr()->_compInfoJsonAvailable(requestMachine->_jsonMetadataFileName, requestMachine->_jsonTranslationFileName);
requestMachine->advance();
}
bool RequestMetaDataTypeStateMachine::_uriIsFTP(const QString& uri) bool RequestMetaDataTypeStateMachine::_uriIsFTP(const QString& uri)
{ {
return uri.startsWith("mavlinkftp", Qt::CaseInsensitive); return uri.startsWith("mavlinkftp", Qt::CaseInsensitive);
......
...@@ -43,12 +43,15 @@ public: ...@@ -43,12 +43,15 @@ public:
void statesCompleted (void) const final; void statesCompleted (void) const final;
private slots: private slots:
void _downloadComplete(const QString& file, const QString& errorMsg); void _downloadCompleteMetaDataJson (const QString& file, const QString& errorMsg);
void _downloadCompleteTranslationJson(const QString& file, const QString& errorMsg);
QString _downloadCompleteJsonWorker (const QString& jsonFileName, const QString& inflatedFileName);
private: private:
static void _stateRequestCompInfo (StateMachine* stateMachine); static void _stateRequestCompInfo (StateMachine* stateMachine);
static void _stateRequestMetaDataJson (StateMachine* stateMachine); static void _stateRequestMetaDataJson (StateMachine* stateMachine);
static void _stateRequestTranslationJson (StateMachine* stateMachine); static void _stateRequestTranslationJson (StateMachine* stateMachine);
static void _stateRequestComplete (StateMachine* stateMachine);
static bool _uriIsFTP (const QString& uri); static bool _uriIsFTP (const QString& uri);
...@@ -56,6 +59,8 @@ private: ...@@ -56,6 +59,8 @@ private:
COMP_METADATA_TYPE _type = COMP_METADATA_TYPE_VERSION; COMP_METADATA_TYPE _type = COMP_METADATA_TYPE_VERSION;
bool _compInfoAvailable = false; bool _compInfoAvailable = false;
ComponentInformation_t _compInfo; ComponentInformation_t _compInfo;
QString _jsonMetadataFileName;
QString _jsonTranslationFileName;
static StateFn _rgStates[]; static StateFn _rgStates[];
static int _cStates; static int _cStates;
...@@ -78,7 +83,9 @@ public: ...@@ -78,7 +83,9 @@ public:
const StateFn* rgStates (void) const final; const StateFn* rgStates (void) const final;
private: private:
void _stateRequestCompInfoComplete(void); void _stateRequestCompInfoComplete (void);
void _compInfoJsonAvailable (const QString& metadataJsonFileName, const QString& translationsJsonFileName);
bool _isCompTypeSupported (COMP_METADATA_TYPE type);
static void _stateRequestCompInfoVersion (StateMachine* stateMachine); static void _stateRequestCompInfoVersion (StateMachine* stateMachine);
static void _stateRequestCompInfoParam (StateMachine* stateMachine); static void _stateRequestCompInfoParam (StateMachine* stateMachine);
...@@ -97,5 +104,8 @@ private: ...@@ -97,5 +104,8 @@ private:
static StateFn _rgStates[]; static StateFn _rgStates[];
static int _cStates; static int _cStates;
static const char* _jsonVersionKey;
static const char* _jsonSupportedCompMetadataTypesKey;
friend class RequestMetaDataTypeStateMachine; friend class RequestMetaDataTypeStateMachine;
}; };
...@@ -50,3 +50,12 @@ void StateMachine::statesCompleted(void) const ...@@ -50,3 +50,12 @@ void StateMachine::statesCompleted(void) const
{ {
} }
StateMachine::StateFn StateMachine::currentState(void)
{
if (_active) {
return rgStates()[_stateIndex];
} else {
return nullptr;
}
}
...@@ -29,6 +29,8 @@ public: ...@@ -29,6 +29,8 @@ public:
/// Move the state machine to the specified state and call the state function /// Move the state machine to the specified state and call the state function
void move(StateFn stateFn); void move(StateFn stateFn);
StateFn currentState(void);
/// @return The number of states in the rgStates array /// @return The number of states in the rgStates array
virtual int stateCount(void) const = 0; virtual int stateCount(void) const = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment