MissionCommandList.cc 11.2 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/
9 10 11 12 13 14 15 16


#include "MissionCommandList.h"
#include "FactMetaData.h"
#include "Vehicle.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
#include "QGroundControlQmlGlobal.h"
Don Gagne's avatar
Don Gagne committed
17
#include "JsonHelper.h"
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

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

const QString MissionCommandList::_categoryJsonKey             (QStringLiteral("category"));
const QString MissionCommandList::_decimalPlacesJsonKey        (QStringLiteral("decimalPlaces"));
const QString MissionCommandList::_defaultJsonKey              (QStringLiteral("default"));
const QString MissionCommandList::_descriptionJsonKey          (QStringLiteral("description"));
const QString MissionCommandList::_enumStringsJsonKey          (QStringLiteral("enumStrings"));
const QString MissionCommandList::_enumValuesJsonKey           (QStringLiteral("enumValues"));
const QString MissionCommandList::_friendlyEditJsonKey         (QStringLiteral("friendlyEdit"));
const QString MissionCommandList::_friendlyNameJsonKey         (QStringLiteral("friendlyName"));
const QString MissionCommandList::_idJsonKey                   (QStringLiteral("id"));
const QString MissionCommandList::_labelJsonKey                (QStringLiteral("label"));
const QString MissionCommandList::_mavCmdInfoJsonKey           (QStringLiteral("mavCmdInfo"));
const QString MissionCommandList::_param1JsonKey               (QStringLiteral("param1"));
const QString MissionCommandList::_param2JsonKey               (QStringLiteral("param2"));
const QString MissionCommandList::_param3JsonKey               (QStringLiteral("param3"));
const QString MissionCommandList::_param4JsonKey               (QStringLiteral("param4"));
const QString MissionCommandList::_paramJsonKeyFormat          (QStringLiteral("param%1"));
const QString MissionCommandList::_rawNameJsonKey              (QStringLiteral("rawName"));
const QString MissionCommandList::_standaloneCoordinateJsonKey (QStringLiteral("standaloneCoordinate"));
const QString MissionCommandList::_specifiesCoordinateJsonKey  (QStringLiteral("specifiesCoordinate"));
const QString MissionCommandList::_unitsJsonKey                (QStringLiteral("units"));
const QString MissionCommandList::_versionJsonKey              (QStringLiteral("version"));

MissionCommandList::MissionCommandList(const QString& jsonFilename, QObject* parent)
    : QObject(parent)
{
    _loadMavCmdInfoJson(jsonFilename);
}

void MissionCommandList::_loadMavCmdInfoJson(const QString& jsonFilename)
{
    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
73
        qWarning() << jsonFilename << "Unable to open json document" << jsonParseError.errorString();
74 75 76 77 78 79 80
        return;
    }

    QJsonObject json = doc.object();

    int version = json.value(_versionJsonKey).toInt();
    if (version != 1) {
Don Gagne's avatar
Don Gagne committed
81
        qWarning() << jsonFilename << "Invalid version" << version;
82 83 84 85 86
        return;
    }

    QJsonValue jsonValue = json.value(_mavCmdInfoJsonKey);
    if (!jsonValue.isArray()) {
Don Gagne's avatar
Don Gagne committed
87
        qWarning() << jsonFilename << "mavCmdInfo not array";
88 89 90 91 92 93
        return;
    }

    QJsonArray jsonArray = jsonValue.toArray();
    foreach(QJsonValue info, jsonArray) {
        if (!info.isObject()) {
Don Gagne's avatar
Don Gagne committed
94
            qWarning() << jsonFilename << "mavCmdArray should contain objects";
95 96 97 98 99
            return;
        }
        QJsonObject jsonObject = info.toObject();

        // Make sure we have the required keys
Don Gagne's avatar
Don Gagne committed
100
        QString errorString;
101 102
        QStringList requiredKeys;
        requiredKeys << _idJsonKey << _rawNameJsonKey;
Don Gagne's avatar
Don Gagne committed
103
        if (!JsonHelper::validateRequiredKeys(jsonObject, requiredKeys, errorString)) {
Don Gagne's avatar
Don Gagne committed
104
            qWarning() << jsonFilename << errorString;
Don Gagne's avatar
Don Gagne committed
105
            return;
106 107 108 109 110 111 112 113 114 115
        }

        // Validate key types

        QStringList             keys;
        QList<QJsonValue::Type> types;
        keys << _idJsonKey << _rawNameJsonKey << _friendlyNameJsonKey << _descriptionJsonKey << _standaloneCoordinateJsonKey << _specifiesCoordinateJsonKey <<_friendlyEditJsonKey
             << _param1JsonKey << _param2JsonKey << _param3JsonKey << _param4JsonKey << _categoryJsonKey;
        types << QJsonValue::Double << QJsonValue::String << QJsonValue::String<< QJsonValue::String << QJsonValue::Bool << QJsonValue::Bool << QJsonValue::Bool
              << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object << QJsonValue::String;
Don Gagne's avatar
Don Gagne committed
116
        if (!JsonHelper::validateKeyTypes(jsonObject, keys, types, errorString)) {
Don Gagne's avatar
Don Gagne committed
117
            qWarning() << jsonFilename << errorString;
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            return;
        }

        MavCmdInfo* mavCmdInfo = new MavCmdInfo(this);

        mavCmdInfo->_command = (MAV_CMD)      jsonObject.value(_idJsonKey).toInt();
        mavCmdInfo->_category =               jsonObject.value(_categoryJsonKey).toString("Advanced");
        mavCmdInfo->_rawName =                jsonObject.value(_rawNameJsonKey).toString();
        mavCmdInfo->_friendlyName =           jsonObject.value(_friendlyNameJsonKey).toString(QString());
        mavCmdInfo->_description =            jsonObject.value(_descriptionJsonKey).toString(QString());
        mavCmdInfo->_standaloneCoordinate =   jsonObject.value(_standaloneCoordinateJsonKey).toBool(false);
        mavCmdInfo->_specifiesCoordinate =    jsonObject.value(_specifiesCoordinateJsonKey).toBool(false);
        mavCmdInfo->_friendlyEdit =           jsonObject.value(_friendlyEditJsonKey).toBool(false);

        qCDebug(MissionCommandsLog) << "Command"
                                    << mavCmdInfo->_command
                                    << mavCmdInfo->_category
                                    << mavCmdInfo->_rawName
                                    << mavCmdInfo->_friendlyName
                                    << mavCmdInfo->_description
                                    << mavCmdInfo->_standaloneCoordinate
                                    << mavCmdInfo->_specifiesCoordinate
                                    << mavCmdInfo->_friendlyEdit;

        if (_mavCmdInfoMap.contains((MAV_CMD)mavCmdInfo->command())) {
Don Gagne's avatar
Don Gagne committed
143
            qWarning() << jsonFilename << "Duplicate command" << mavCmdInfo->command();
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            return;
        }

        _mavCmdInfoMap[mavCmdInfo->_command] = mavCmdInfo;

        // Read params

        for (int i=1; i<=7; i++) {
            QString paramKey = QString(_paramJsonKeyFormat).arg(i);

            if (jsonObject.contains(paramKey)) {
                QJsonObject paramObject = jsonObject.value(paramKey).toObject();

                // Validate key types
                QStringList             keys;
                QList<QJsonValue::Type> types;
                keys << _defaultJsonKey << _decimalPlacesJsonKey << _enumStringsJsonKey << _enumValuesJsonKey << _labelJsonKey << _unitsJsonKey;
                types << QJsonValue::Double <<  QJsonValue::Double << QJsonValue::String << QJsonValue::String << QJsonValue::String << QJsonValue::String;
Don Gagne's avatar
Don Gagne committed
162
                if (!JsonHelper::validateKeyTypes(jsonObject, keys, types, errorString)) {
Don Gagne's avatar
Don Gagne committed
163
                    qWarning() << jsonFilename << errorString;
164 165 166 167 168 169
                    return;
                }

                mavCmdInfo->_friendlyEdit = true; // Assume friendly edit if we have params

                if (!paramObject.contains(_labelJsonKey)) {
Don Gagne's avatar
Don Gagne committed
170
                    qWarning() << jsonFilename << "param object missing label key" << mavCmdInfo->rawName() << paramKey;
171 172 173 174 175 176 177
                    return;
                }

                MavCmdParamInfo* paramInfo = new MavCmdParamInfo(this);

                paramInfo->_label =         paramObject.value(_labelJsonKey).toString();
                paramInfo->_defaultValue =  paramObject.value(_defaultJsonKey).toDouble(0.0);
178
                paramInfo->_decimalPlaces = paramObject.value(_decimalPlacesJsonKey).toInt(FactMetaData::unknownDecimalPlaces);
179 180 181 182 183 184 185 186 187 188
                paramInfo->_enumStrings =   paramObject.value(_enumStringsJsonKey).toString().split(",", QString::SkipEmptyParts);
                paramInfo->_param =         i;
                paramInfo->_units =         paramObject.value(_unitsJsonKey).toString();

                QStringList enumValues = paramObject.value(_enumValuesJsonKey).toString().split(",", QString::SkipEmptyParts);
                foreach (const QString &enumValue, enumValues) {
                    bool    convertOk;
                    double  value = enumValue.toDouble(&convertOk);

                    if (!convertOk) {
Don Gagne's avatar
Don Gagne committed
189
                        qWarning() << jsonFilename << "Bad enumValue" << enumValue;
190 191 192 193 194 195
                        return;
                    }

                    paramInfo->_enumValues << QVariant(value);
                }
                if (paramInfo->_enumValues.count() != paramInfo->_enumStrings.count()) {
Don Gagne's avatar
Don Gagne committed
196
                    qWarning() << jsonFilename << "enum strings/values count mismatch" << paramInfo->_enumStrings.count() << paramInfo->_enumValues.count();
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                    return;
                }

                qCDebug(MissionCommandsLog) << "Param"
                                            << paramInfo->_label
                                            << paramInfo->_defaultValue
                                            << paramInfo->_decimalPlaces
                                            << paramInfo->_param
                                            << paramInfo->_units
                                            << paramInfo->_enumStrings
                                            << paramInfo->_enumValues;

                mavCmdInfo->_paramInfoMap[i] = paramInfo;
            }
        }

        if (mavCmdInfo->friendlyEdit()) {
            if (mavCmdInfo->description().isEmpty()) {
Don Gagne's avatar
Don Gagne committed
215
                qWarning() << jsonFilename << "Missing description" << mavCmdInfo->rawName();
216 217 218
                return;
            }
            if (mavCmdInfo->rawName() ==  mavCmdInfo->friendlyName()) {
Don Gagne's avatar
Don Gagne committed
219
                qWarning() << jsonFilename << "Missing friendly name" << mavCmdInfo->rawName() << mavCmdInfo->friendlyName();
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
                return;
            }
        }
    }
}

bool MissionCommandList::contains(MAV_CMD command) const
{
    return _mavCmdInfoMap.contains(command);
}

MavCmdInfo* MissionCommandList::getMavCmdInfo(MAV_CMD command) const
{
    if (!contains(command)) {
        return NULL;
    }

    return _mavCmdInfoMap[command];
}

QList<MAV_CMD> MissionCommandList::commandsIds(void) const
{
    QList<MAV_CMD> list;

    foreach (const MavCmdInfo* mavCmdInfo, _mavCmdInfoMap) {
        list << (MAV_CMD)mavCmdInfo->command();
    }

    return list;
}