MissionCommandList.cc 3.25 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
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 26 27 28
const char* MissionCommandList::_versionJsonKey =       "version";
const char* MissionCommandList::_mavCmdInfoJsonKey =    "mavCmdInfo";

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

34
void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename, bool baseCommandList)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
{
    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) {
Don Gagne's avatar
Don Gagne committed
53
        qWarning() << jsonFilename << "Unable to open json document" << jsonParseError.errorString();
54 55 56 57 58 59 60
        return;
    }

    QJsonObject json = doc.object();

    int version = json.value(_versionJsonKey).toInt();
    if (version != 1) {
Don Gagne's avatar
Don Gagne committed
61
        qWarning() << jsonFilename << "Invalid version" << version;
62 63 64 65 66
        return;
    }

    QJsonValue jsonValue = json.value(_mavCmdInfoJsonKey);
    if (!jsonValue.isArray()) {
Don Gagne's avatar
Don Gagne committed
67
        qWarning() << jsonFilename << "mavCmdInfo not array";
68 69 70
        return;
    }

71
    // Iterate over MissionCommandUIInfo objects
72
    QJsonArray jsonArray = jsonValue.toArray();
73
    for(QJsonValue info: jsonArray) {
74
        if (!info.isObject()) {
Don Gagne's avatar
Don Gagne committed
75
            qWarning() << jsonFilename << "mavCmdArray should contain objects";
76 77 78
            return;
        }

79
        MissionCommandUIInfo* uiInfo = new MissionCommandUIInfo(this);
80

81 82 83
        QString errorString;
        if (!uiInfo->loadJsonInfo(info.toObject(), baseCommandList, errorString)) {
            uiInfo->deleteLater();
Don Gagne's avatar
Don Gagne committed
84
            qWarning() << jsonFilename << errorString;
85 86 87
            return;
        }

88 89 90 91
        // Update list of known categories
        QString newCategory = uiInfo->category();
        if (!newCategory.isEmpty() && !_categories.contains(newCategory)) {
            _categories.append(newCategory);
92 93
        }

94
        _infoMap[uiInfo->command()] = uiInfo;
95 96
    }

97
    // Build id list
98
    for (MAV_CMD id: _infoMap.keys()) {
99
        _ids << id;
100 101 102
    }
}

103
MissionCommandUIInfo* MissionCommandList::getUIInfo(MAV_CMD command) const
104
{
105
    if (!_infoMap.contains(command)) {
106
        return nullptr;
107 108
    }

109
    return _infoMap[command];
110
}