1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/****************************************************************************
*
* (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
*
* QGroundControl is licensed according to the terms in the file
* COPYING.md in the root of the source code directory.
*
****************************************************************************/
#include "MissionCommandList.h"
#include "FactMetaData.h"
#include "Vehicle.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
#include "QGroundControlQmlGlobal.h"
#include "JsonHelper.h"
#include "MissionCommandUIInfo.h"
#include <QStringList>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonArray>
#include <QDebug>
#include <QFile>
const char* MissionCommandList::_versionJsonKey = "version";
const char* MissionCommandList::_mavCmdInfoJsonKey = "mavCmdInfo";
MissionCommandList::MissionCommandList(const QString& jsonFilename, bool baseCommandList, QObject* parent)
: QObject(parent)
{
_loadMavCmdInfoJson(jsonFilename, baseCommandList);
}
void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename, bool baseCommandList)
{
if (jsonFilename.isEmpty()) {
return;
}
qCDebug(MissionCommandsLog) << "Loading" << jsonFilename;
QFile jsonFile(jsonFilename);
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Unable to open file" << jsonFilename << jsonFile.errorString();
return;
}
QByteArray bytes = jsonFile.readAll();
jsonFile.close();
QJsonParseError jsonParseError;
QJsonDocument doc = QJsonDocument::fromJson(bytes, &jsonParseError);
if (jsonParseError.error != QJsonParseError::NoError) {
qWarning() << jsonFilename << "Unable to open json document" << jsonParseError.errorString();
return;
}
QJsonObject json = doc.object();
int version = json.value(_versionJsonKey).toInt();
if (version != 1) {
qWarning() << jsonFilename << "Invalid version" << version;
return;
}
QJsonValue jsonValue = json.value(_mavCmdInfoJsonKey);
if (!jsonValue.isArray()) {
qWarning() << jsonFilename << "mavCmdInfo not array";
return;
}
// Iterate over MissionCommandUIInfo objects
QJsonArray jsonArray = jsonValue.toArray();
foreach(QJsonValue info, jsonArray) {
if (!info.isObject()) {
qWarning() << jsonFilename << "mavCmdArray should contain objects";
return;
}
MissionCommandUIInfo* uiInfo = new MissionCommandUIInfo(this);
QString errorString;
if (!uiInfo->loadJsonInfo(info.toObject(), baseCommandList, errorString)) {
uiInfo->deleteLater();
qWarning() << jsonFilename << errorString;
return;
}
// Update list of known categories
QString newCategory = uiInfo->category();
if (!newCategory.isEmpty() && !_categories.contains(newCategory)) {
_categories.append(newCategory);
}
_infoMap[uiInfo->command()] = uiInfo;
}
// Build id list
foreach (MAV_CMD id, _infoMap.keys()) {
_ids << id;
}
}
MissionCommandUIInfo* MissionCommandList::getUIInfo(MAV_CMD command) const
{
if (!_infoMap.contains(command)) {
return NULL;
}
return _infoMap[command];
}