QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PowerModulePresetController.cc
Go to the documentation of this file.
2#include "JsonHelper.h"
4
5#include <QtCore/QJsonArray>
6#include <QtCore/QJsonObject>
7
8QGC_LOGGING_CATEGORY(PowerModulePresetControllerLog, "AutoPilotPlugins.PowerModulePresetController")
9
14
16{
17 int version;
18 QString errorString;
19 const QJsonObject root = JsonHelper::openInternalQGCJsonFile(
20 QStringLiteral(":/json/PowerModulePresets.json"),
21 QStringLiteral("PowerModulePresets"), 1, 1, version, errorString);
22 if (root.isEmpty()) {
23 qCCritical(PowerModulePresetControllerLog) << errorString;
24 return {};
25 }
26
27 if (!root.value(QStringLiteral("powerModules")).isArray()) {
28 qCCritical(PowerModulePresetControllerLog) << "Missing or invalid 'powerModules' array";
29 return {};
30 }
31
32 static const QList<JsonHelper::KeyValidateInfo> keyInfo = {
33 { "name", QJsonValue::String, true },
34 { "voltMult", QJsonValue::Double, true },
35 { "ampPerVolt", QJsonValue::Double, true },
36 { "ampOffset", QJsonValue::Double, true },
37 };
38
39 const QJsonArray modules = root.value(QStringLiteral("powerModules")).toArray();
40 QVariantList result;
41 result.reserve(modules.size());
42
43 for (int i = 0; i < modules.size(); ++i) {
44 if (!modules.at(i).isObject()) {
45 qCCritical(PowerModulePresetControllerLog) << "powerModules[" << i << "] is not an object";
46 return {};
47 }
48 const QJsonObject obj = modules.at(i).toObject();
49
50 errorString.clear();
51 if (!JsonHelper::validateKeysStrict(obj, keyInfo, errorString)) {
52 qCCritical(PowerModulePresetControllerLog) << "powerModules[" << i << "]:" << errorString;
53 return {};
54 }
55
56 result.append(obj.toVariantMap());
57 }
58
59 return result;
60}
QString errorString
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Used for handling missing Facts from C++ code.
QJsonObject openInternalQGCJsonFile(const QString &jsonFilename, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString)
returned error string if validation fails
bool validateKeysStrict(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)