MissionCommandList.cc 2.83 KB
Newer Older
1 2
/****************************************************************************
 *
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10 11 12 13 14

#include "MissionCommandList.h"
#include "FactMetaData.h"
#include "Vehicle.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
15
#include "JsonHelper.h"
16
#include "MissionCommandUIInfo.h"
17 18 19 20 21 22 23 24

#include <QStringList>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonArray>
#include <QDebug>
#include <QFile>

25
const char* MissionCommandList::qgcFileType =           "MavCmdInfo";
26 27 28 29
const char* MissionCommandList::_versionJsonKey =       "version";
const char* MissionCommandList::_mavCmdInfoJsonKey =    "mavCmdInfo";

MissionCommandList::MissionCommandList(const QString& jsonFilename, bool baseCommandList, QObject* parent)
30 31
    : QObject(parent)
{
32
    _loadMavCmdInfoJson(jsonFilename, baseCommandList);
33 34
}

35
void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename, bool baseCommandList)
36 37 38 39 40 41 42
{
    if (jsonFilename.isEmpty()) {
        return;
    }

    qCDebug(MissionCommandsLog) << "Loading" << jsonFilename;

43 44 45 46 47
    QString errorString;
    int version;
    QJsonObject jsonObject = JsonHelper::openInternalQGCJsonFile(jsonFilename, qgcFileType, 1, 1, version, errorString);
    if (!errorString.isEmpty()) {
        qWarning() << "Internal Error: " << errorString;
48 49 50
        return;
    }

51
    QJsonValue jsonValue = jsonObject.value(_mavCmdInfoJsonKey);
52
    if (!jsonValue.isArray()) {
Don Gagne's avatar
Don Gagne committed
53
        qWarning() << jsonFilename << "mavCmdInfo not array";
54 55 56
        return;
    }

57
    // Iterate over MissionCommandUIInfo objects
58
    QJsonArray jsonArray = jsonValue.toArray();
59
    for(QJsonValue info: jsonArray) {
60
        if (!info.isObject()) {
Don Gagne's avatar
Don Gagne committed
61
            qWarning() << jsonFilename << "mavCmdArray should contain objects";
62 63 64
            return;
        }

65
        MissionCommandUIInfo* uiInfo = new MissionCommandUIInfo(this);
66

67 68 69
        QString errorString;
        if (!uiInfo->loadJsonInfo(info.toObject(), baseCommandList, errorString)) {
            uiInfo->deleteLater();
Don Gagne's avatar
Don Gagne committed
70
            qWarning() << jsonFilename << errorString;
71 72 73
            return;
        }

74 75 76 77
        // Update list of known categories
        QString newCategory = uiInfo->category();
        if (!newCategory.isEmpty() && !_categories.contains(newCategory)) {
            _categories.append(newCategory);
78 79
        }

80
        _infoMap[uiInfo->command()] = uiInfo;
81 82
    }

83
    // Build id list
84
    for (MAV_CMD id: _infoMap.keys()) {
85
        _ids << id;
86 87 88
    }
}

89
MissionCommandUIInfo* MissionCommandList::getUIInfo(MAV_CMD command) const
90
{
91
    if (!_infoMap.contains(command)) {
92
        return nullptr;
93 94
    }

95
    return _infoMap[command];
96
}