From ee4d13846cdcd2c7fa37a76f6c3551fa3cf6fbf5 Mon Sep 17 00:00:00 2001 From: DonLakeFlyer Date: Wed, 1 Jul 2020 15:46:14 -0700 Subject: [PATCH] Add hack to generate json param meta --- .../PX4/PX4ParameterMetaData.cc | 88 +++++++++++++++++++ src/FirmwarePlugin/PX4/PX4ParameterMetaData.h | 6 ++ 2 files changed, 94 insertions(+) diff --git a/src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc b/src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc index 9dc467ae1..161a5a479 100644 --- a/src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc +++ b/src/FirmwarePlugin/PX4/PX4ParameterMetaData.cc @@ -401,7 +401,95 @@ void PX4ParameterMetaData::loadParameterFactMetaDataFile(const QString& metaData } xml.readNext(); } + +#ifdef GENERATE_PARAMETER_JSON + _generateParameterJson(); +#endif +} + +#ifdef GENERATE_PARAMETER_JSON +void _jsonWriteLine(QFile& file, int indent, const QString& line) +{ + while (indent--) { + file.write(" "); + } + file.write(line.toLocal8Bit().constData()); + file.write("\n"); +} + +void PX4ParameterMetaData::_generateParameterJson() +{ + qCDebug(ParameterManagerLog) << "PX4ParameterMetaData::_generateParameterJson"; + + int indentLevel = 0; + QFile jsonFile(QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).absoluteFilePath("parameter.json")); + jsonFile.open(QFile::WriteOnly | QFile::Truncate | QFile::Text); + + _jsonWriteLine(jsonFile, indentLevel++, "{"); + _jsonWriteLine(jsonFile, indentLevel, "\"version\": 1,"); + _jsonWriteLine(jsonFile, indentLevel, "\"uid\": 1,"); + _jsonWriteLine(jsonFile, indentLevel, "\"scope\": \"Firmware\","); + _jsonWriteLine(jsonFile, indentLevel++, "\"parameters\": ["); + + int keyIndex = 0; + for (const QString& paramName: _mapParameterName2FactMetaData.keys()) { + const FactMetaData* metaData = _mapParameterName2FactMetaData[paramName]; + _jsonWriteLine(jsonFile, indentLevel++, "{"); + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"name\": \"%1\",").arg(paramName)); + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"type\": \"%1\",").arg(metaData->typeToString(metaData->type()))); + if (!metaData->group().isEmpty()) { + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"group\": \"%1\",").arg(metaData->group())); + } + if (!metaData->category().isEmpty()) { + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"category\": \"%1\",").arg(metaData->category())); + } + if (!metaData->shortDescription().isEmpty()) { + QString text = metaData->shortDescription(); + text.replace("\"", "\\\""); + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"shortDescription\": \"%1\",").arg(text)); + } + if (!metaData->longDescription().isEmpty()) { + QString text = metaData->longDescription(); + text.replace("\"", "\\\""); + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"longDescription\": \"%1\",").arg(text)); + } + if (!metaData->rawUnits().isEmpty()) { + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"units\": \"%1\",").arg(metaData->rawUnits())); + } + if (metaData->defaultValueAvailable()) { + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"defaultValue\": %1,").arg(metaData->rawDefaultValue().toDouble())); + } + if (!qIsNaN(metaData->rawIncrement())) { + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"increment\": %1,").arg(metaData->rawIncrement())); + } + if (metaData->enumValues().count()) { + _jsonWriteLine(jsonFile, indentLevel++, "\"values\": ["); + for (int i=0; ienumValues().count(); i++) { + _jsonWriteLine(jsonFile, indentLevel++, "{"); + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"value\": %1,").arg(metaData->enumValues()[i].toDouble())); + QString text = metaData->enumStrings()[i]; + text.replace("\"", "\\\""); + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"description\": \"%1\"").arg(text)); + _jsonWriteLine(jsonFile, --indentLevel, QStringLiteral("}%1").arg(i == metaData->enumValues().count() - 1 ? "" : ",")); + } + _jsonWriteLine(jsonFile, --indentLevel, "],"); + } + if (metaData->vehicleRebootRequired()) { + _jsonWriteLine(jsonFile, indentLevel, "\"rebootRequired\": true,"); + } + if (metaData->volatileValue()) { + _jsonWriteLine(jsonFile, indentLevel, "\"volatile\": true,"); + } + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"decimalPlaces\": \"%1\",").arg(metaData->decimalPlaces())); + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"minValue\": %1,").arg(metaData->rawMin().toDouble())); + _jsonWriteLine(jsonFile, indentLevel, QStringLiteral("\"maxValue\": %1").arg(metaData->rawMax().toDouble())); + _jsonWriteLine(jsonFile, --indentLevel, QStringLiteral("}%1").arg(++keyIndex == _mapParameterName2FactMetaData.keys().count() ? "" : ",")); + } + + _jsonWriteLine(jsonFile, --indentLevel, "]"); + _jsonWriteLine(jsonFile, --indentLevel, "}"); } +#endif FactMetaData* PX4ParameterMetaData::getMetaDataForFact(const QString& name, MAV_TYPE vehicleType) { diff --git a/src/FirmwarePlugin/PX4/PX4ParameterMetaData.h b/src/FirmwarePlugin/PX4/PX4ParameterMetaData.h index 68e7bc06a..24cfe48d3 100644 --- a/src/FirmwarePlugin/PX4/PX4ParameterMetaData.h +++ b/src/FirmwarePlugin/PX4/PX4ParameterMetaData.h @@ -25,6 +25,8 @@ Q_DECLARE_LOGGING_CATEGORY(PX4ParameterMetaDataLog) +//#define GENERATE_PARAMETER_JSON + /// Loads and holds parameter fact meta data for PX4 stack class PX4ParameterMetaData : public QObject { @@ -52,6 +54,10 @@ private: QVariant _stringToTypedVariant(const QString& string, FactMetaData::ValueType_t type, bool* convertOk); static void _outputFileWarning(const QString& metaDataFile, const QString& error1, const QString& error2); +#ifdef GENERATE_PARAMETER_JSON + void _generateParameterJson(); +#endif + bool _parameterMetaDataLoaded; ///< true: parameter meta data already loaded QMap _mapParameterName2FactMetaData; ///< Maps from a parameter name to FactMetaData }; -- 2.22.0