MissionCommandUIInfo.cc 17.4 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 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "MissionCommandUIInfo.h"
#include "JsonHelper.h"
#include "FactMetaData.h"
#include "QGCLoggingCategory.h"

const char* MissionCommandUIInfo::_categoryJsonKey              = "category";
const char* MissionCommandUIInfo::_decimalPlacesJsonKey         = "decimalPlaces";
const char* MissionCommandUIInfo::_defaultJsonKey               = "default";
const char* MissionCommandUIInfo::_descriptionJsonKey           = "description";
const char* MissionCommandUIInfo::_enumStringsJsonKey           = "enumStrings";
const char* MissionCommandUIInfo::_enumValuesJsonKey            = "enumValues";
21
const char* MissionCommandUIInfo::_nanUnchangedJsonKey          = "nanUnchanged";
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
const char* MissionCommandUIInfo::_friendlyEditJsonKey          = "friendlyEdit";
const char* MissionCommandUIInfo::_friendlyNameJsonKey          = "friendlyName";
const char* MissionCommandUIInfo::_idJsonKey                    = "id";
const char* MissionCommandUIInfo::_labelJsonKey                 = "label";
const char* MissionCommandUIInfo::_mavCmdInfoJsonKey            = "mavCmdInfo";
const char* MissionCommandUIInfo::_param1JsonKey                = "param1";
const char* MissionCommandUIInfo::_param2JsonKey                = "param2";
const char* MissionCommandUIInfo::_param3JsonKey                = "param3";
const char* MissionCommandUIInfo::_param4JsonKey                = "param4";
const char* MissionCommandUIInfo::_param5JsonKey                = "param5";
const char* MissionCommandUIInfo::_param6JsonKey                = "param6";
const char* MissionCommandUIInfo::_param7JsonKey                = "param7";
const char* MissionCommandUIInfo::_paramJsonKeyFormat           = "param%1";
const char* MissionCommandUIInfo::_paramRemoveJsonKey           = "paramRemove";
const char* MissionCommandUIInfo::_rawNameJsonKey               = "rawName";
const char* MissionCommandUIInfo::_standaloneCoordinateJsonKey  = "standaloneCoordinate";
const char* MissionCommandUIInfo::_specifiesCoordinateJsonKey   = "specifiesCoordinate";
39
const char* MissionCommandUIInfo::_specifiesAltitudeOnlyJsonKey = "specifiesAltitudeOnly";
40
const char* MissionCommandUIInfo::_isLandCommandJsonKey         = "isLandCommand";
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
const char* MissionCommandUIInfo::_unitsJsonKey                 = "units";
const char* MissionCommandUIInfo::_commentJsonKey               = "comment";
const char* MissionCommandUIInfo::_advancedCategory             = "Advanced";

MissionCmdParamInfo::MissionCmdParamInfo(QObject* parent)
    : QObject(parent)
{

}

MissionCmdParamInfo::MissionCmdParamInfo(const MissionCmdParamInfo& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
}

const MissionCmdParamInfo& MissionCmdParamInfo::operator=(const MissionCmdParamInfo& other)
{
    _decimalPlaces =    other._decimalPlaces;
    _defaultValue =     other._defaultValue;
    _enumStrings =      other._enumStrings;
    _enumValues =       other._enumValues;
    _label =            other._label;
    _param =            other._param;
    _units =            other._units;
66
    _nanUnchanged =     other._nanUnchanged;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    return *this;
}

MissionCommandUIInfo::MissionCommandUIInfo(QObject* parent)
    : QObject(parent)
{

}

MissionCommandUIInfo::MissionCommandUIInfo(const MissionCommandUIInfo& other, QObject* parent)
    : QObject(parent)
{
    *this = other;
}

const MissionCommandUIInfo& MissionCommandUIInfo::operator=(const MissionCommandUIInfo& other)
{
    _command =          other._command;
    _infoMap =          other._infoMap;
    _paramRemoveList =  other._paramRemoveList;

89
    for (int index: other._paramInfoMap.keys()) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 143 144 145 146 147 148 149
        _paramInfoMap[index] = new MissionCmdParamInfo(*other._paramInfoMap[index], this);
    }

    return *this;
}

QString MissionCommandUIInfo::category(void) const
{
    if (_infoMap.contains(_categoryJsonKey)) {
        return _infoMap[_categoryJsonKey].toString();
    } else {
        return _advancedCategory;
    }
}

QString MissionCommandUIInfo::description(void) const
{
    if (_infoMap.contains(_descriptionJsonKey)) {
        return _infoMap[_descriptionJsonKey].toString();
    } else {
        return QString();
    }
}

bool MissionCommandUIInfo::friendlyEdit(void) const
{
    if (_infoMap.contains(_friendlyEditJsonKey)) {
        return _infoMap[_friendlyEditJsonKey].toBool();
    } else {
        return false;
    }
}

QString MissionCommandUIInfo::friendlyName(void) const
{
    if (_infoMap.contains(_friendlyNameJsonKey)) {
        return _infoMap[_friendlyNameJsonKey].toString();
    } else {
        return QString();
    }
}

QString MissionCommandUIInfo::rawName(void) const
{
    if (_infoMap.contains(_rawNameJsonKey)) {
        return _infoMap[_rawNameJsonKey].toString();
    } else {
        return QString();
    }
}

bool MissionCommandUIInfo::isStandaloneCoordinate(void) const
{
    if (_infoMap.contains(_standaloneCoordinateJsonKey)) {
        return _infoMap[_standaloneCoordinateJsonKey].toBool();
    } else {
        return false;
    }
}

150
bool MissionCommandUIInfo::specifiesCoordinate(void) const
151 152 153 154 155 156 157 158
{
    if (_infoMap.contains(_specifiesCoordinateJsonKey)) {
        return _infoMap[_specifiesCoordinateJsonKey].toBool();
    } else {
        return false;
    }
}

159 160 161 162 163 164 165 166 167
bool MissionCommandUIInfo::specifiesAltitudeOnly(void) const
{
    if (_infoMap.contains(_specifiesAltitudeOnlyJsonKey)) {
        return _infoMap[_specifiesAltitudeOnlyJsonKey].toBool();
    } else {
        return false;
    }
}

168 169 170 171 172 173 174 175 176
bool MissionCommandUIInfo::isLandCommand(void) const
{
    if (_infoMap.contains(_isLandCommandJsonKey)) {
        return _infoMap[_isLandCommandJsonKey].toBool();
    } else {
        return false;
    }
}

177 178 179
void MissionCommandUIInfo::_overrideInfo(MissionCommandUIInfo* uiInfo)
{
    // Override info values
180
    for (const QString& valueKey: uiInfo->_infoMap.keys()) {
181 182 183 184
        _setInfoValue(valueKey, uiInfo->_infoMap[valueKey]);
    }

    // Add to the remove params list
185
    for (int removeIndex: uiInfo->_paramRemoveList) {
186 187 188 189 190 191
        if (!_paramRemoveList.contains(removeIndex)) {
            _paramRemoveList.append(removeIndex);
        }
    }

    // Override param info
192
    for (const int paramIndex: uiInfo->_paramInfoMap.keys()) {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        _paramRemoveList.removeOne(paramIndex);
        // MissionCmdParamInfo objects are owned by MissionCommandTree are are in existence for the entire run so
        // we can just use the same pointer reference.
        _paramInfoMap[paramIndex] = uiInfo->_paramInfoMap[paramIndex];
    }
}

QString MissionCommandUIInfo::_loadErrorString(const QString& errorString) const
{
    return QString("%1 %2").arg(_infoValue(_rawNameJsonKey).toString()).arg(errorString);
}

bool MissionCommandUIInfo::loadJsonInfo(const QJsonObject& jsonObject, bool requireFullObject, QString& errorString)
{
    QString internalError;

    QStringList allKeys;
    allKeys << _idJsonKey << _rawNameJsonKey << _friendlyNameJsonKey << _descriptionJsonKey << _standaloneCoordinateJsonKey << _specifiesCoordinateJsonKey
            <<_friendlyEditJsonKey << _param1JsonKey << _param2JsonKey << _param3JsonKey << _param4JsonKey << _param5JsonKey << _param6JsonKey << _param7JsonKey
212
            << _paramRemoveJsonKey << _categoryJsonKey << _specifiesAltitudeOnlyJsonKey << _isLandCommandJsonKey;
213 214

    // Look for unknown keys in top level object
215
    for (const QString& key: jsonObject.keys()) {
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        if (!allKeys.contains(key) && key != _commentJsonKey) {
            errorString = _loadErrorString(QString("Unknown key: %1").arg(key));
            return false;
        }
    }

    // Make sure we have the required keys
    QStringList requiredKeys;
    requiredKeys << _idJsonKey;
    if (requireFullObject) {
        requiredKeys << _rawNameJsonKey;
    }
    if (!JsonHelper::validateRequiredKeys(jsonObject, requiredKeys, internalError)) {
        errorString = _loadErrorString(internalError);
        return false;
    }

    // Only the full object should specify rawName, friendlyName
    if (!requireFullObject && (jsonObject.contains(_rawNameJsonKey) || jsonObject.contains(_friendlyNameJsonKey))) {
        errorString = _loadErrorString(QStringLiteral("Only the full object should specify rawName or friendlyName"));
        return false;
    }

    // Validate key types

    QList<QJsonValue::Type> types;
    types << QJsonValue::Double << QJsonValue::String << QJsonValue::String<< QJsonValue::String << QJsonValue::Bool << QJsonValue::Bool << QJsonValue::Bool
          << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object << QJsonValue::Object
244
          << QJsonValue::String << QJsonValue::String << QJsonValue::Bool << QJsonValue::Bool;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    if (!JsonHelper::validateKeyTypes(jsonObject, allKeys, types, internalError)) {
        errorString = _loadErrorString(internalError);
        return false;
    }

    // Read in top level values

    _command = (MAV_CMD)jsonObject.value(_idJsonKey).toInt();

    if (jsonObject.contains(_categoryJsonKey)) {
        _infoMap[_categoryJsonKey] = jsonObject.value(_categoryJsonKey).toVariant();
    }
    if (jsonObject.contains(_rawNameJsonKey)) {
        _infoMap[_rawNameJsonKey] = jsonObject.value(_rawNameJsonKey).toVariant();
    }
    if (jsonObject.contains(_friendlyNameJsonKey)) {
        _infoMap[_friendlyNameJsonKey] = jsonObject.value(_friendlyNameJsonKey).toVariant();
    }
    if (jsonObject.contains(_descriptionJsonKey)) {
        _infoMap[_descriptionJsonKey] = jsonObject.value(_descriptionJsonKey).toVariant();
    }
    if (jsonObject.contains(_standaloneCoordinateJsonKey)) {
        _infoMap[_standaloneCoordinateJsonKey] = jsonObject.value(_standaloneCoordinateJsonKey).toVariant();
    }
    if (jsonObject.contains(_specifiesCoordinateJsonKey)) {
        _infoMap[_specifiesCoordinateJsonKey] = jsonObject.value(_specifiesCoordinateJsonKey).toVariant();
    }
272 273 274
    if (jsonObject.contains(_specifiesAltitudeOnlyJsonKey)) {
        _infoMap[_specifiesAltitudeOnlyJsonKey] = jsonObject.value(_specifiesAltitudeOnlyJsonKey).toBool();
    }
275 276 277
    if (jsonObject.contains(_isLandCommandJsonKey)) {
        _infoMap[_isLandCommandJsonKey] = jsonObject.value(_isLandCommandJsonKey).toBool();
    }
278 279 280 281 282
    if (jsonObject.contains(_friendlyEditJsonKey)) {
        _infoMap[_friendlyEditJsonKey] = jsonObject.value(_friendlyEditJsonKey).toVariant();
    }
    if (jsonObject.contains(_paramRemoveJsonKey)) {
        QStringList indexList = jsonObject.value(_paramRemoveJsonKey).toString().split(QStringLiteral(","));
283
        for (const QString& indexString: indexList) {
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            _paramRemoveList.append(indexString.toInt());
        }
    }

    if (requireFullObject) {
        // Since this is the base of the hierarchy it must contain valid defaults for all values.
        if (!_infoAvailable(_categoryJsonKey)) {
            _setInfoValue(_categoryJsonKey, _advancedCategory);
        }
        if (!_infoAvailable(_friendlyNameJsonKey)) {
            _setInfoValue(_friendlyNameJsonKey, _infoValue(_rawNameJsonKey));
        }
        if (!_infoAvailable(_descriptionJsonKey)) {
            _setInfoValue(_descriptionJsonKey, QStringLiteral(""));
        }
        if (!_infoAvailable(_standaloneCoordinateJsonKey)) {
            _setInfoValue(_standaloneCoordinateJsonKey, false);
        }
        if (!_infoAvailable(_specifiesCoordinateJsonKey)) {
            _setInfoValue(_specifiesCoordinateJsonKey, false);
        }
305 306 307
        if (!_infoAvailable(_isLandCommandJsonKey)) {
            _setInfoValue(_isLandCommandJsonKey, false);
        }
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        if (!_infoAvailable(_friendlyEditJsonKey)) {
            _setInfoValue(_friendlyEditJsonKey, false);
        }
    }

    if (requireFullObject) {
        if (_infoAvailable(_friendlyEditJsonKey) && _infoValue(_friendlyEditJsonKey).toBool()) {
            if (!_infoAvailable(_descriptionJsonKey)) {
                errorString = _loadErrorString(QStringLiteral("Missing description for friendly edit"));
                return false;
            }
            if (_infoValue(_rawNameJsonKey).toString() == _infoValue(_friendlyNameJsonKey).toString()) {
                errorString = _loadErrorString(QStringLiteral("Missing friendlyName for friendly edit"));
                return false;
            }
        }
    }

    QString debugOutput;
327
    for (const QString& infoKey: _infoMap.keys()) {
328 329 330 331 332 333 334 335 336 337 338 339 340
        debugOutput.append(QString("MavCmdInfo %1: %2 ").arg(infoKey).arg(_infoMap[infoKey].toString()));
    }
    qCDebug(MissionCommandsLog) << debugOutput;

    // 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();

            QStringList allParamKeys;
341
            allParamKeys << _defaultJsonKey << _decimalPlacesJsonKey << _enumStringsJsonKey << _enumValuesJsonKey << _labelJsonKey << _unitsJsonKey << _nanUnchangedJsonKey;
342 343

            // Look for unknown keys in param object
344
            for (const QString& key: paramObject.keys()) {
345 346 347 348 349 350 351 352
                if (!allParamKeys.contains(key) && key != _commentJsonKey) {
                    errorString = _loadErrorString(QString("Unknown param key: %1").arg(key));
                    return false;
                }
            }

            // Validate key types
            QList<QJsonValue::Type> types;
353
            types << QJsonValue::Null <<  QJsonValue::Double << QJsonValue::String << QJsonValue::String << QJsonValue::String << QJsonValue::String << QJsonValue::Bool;
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
            if (!JsonHelper::validateKeyTypes(jsonObject, allParamKeys, types, internalError)) {
                errorString = _loadErrorString(internalError);
                return false;
            }

            _setInfoValue(_friendlyEditJsonKey, true); // Assume friendly edit if we have params

            if (!paramObject.contains(_labelJsonKey)) {
                internalError = QString("param object missing label key: %1").arg(paramKey);
                errorString = _loadErrorString(internalError);
                return false;
            }

            MissionCmdParamInfo* paramInfo = new MissionCmdParamInfo(this);

            paramInfo->_label =         paramObject.value(_labelJsonKey).toString();
DonLakeFlyer's avatar
DonLakeFlyer committed
370
            paramInfo->_decimalPlaces = paramObject.value(_decimalPlacesJsonKey).toInt(FactMetaData::kUnknownDecimalPlaces);
371 372 373
            paramInfo->_enumStrings =   paramObject.value(_enumStringsJsonKey).toString().split(",", QString::SkipEmptyParts);
            paramInfo->_param =         i;
            paramInfo->_units =         paramObject.value(_unitsJsonKey).toString();
374 375 376
            paramInfo->_nanUnchanged =  paramObject.value(_nanUnchangedJsonKey).toBool(false);

            if (paramObject.contains(_defaultJsonKey)) {
377 378 379 380 381 382 383 384 385
                if (paramInfo->_nanUnchanged) {
                    paramInfo->_defaultValue = JsonHelper::possibleNaNJsonValue(paramObject[_defaultJsonKey]);
                } else {
                    if (paramObject[_defaultJsonKey].type() == QJsonValue::Null) {
                        errorString = QString("Param %1 default value was null/NaN but NaN is not allowed");
                        return false;
                    }
                    paramInfo->_defaultValue = paramObject.value(_defaultJsonKey).toDouble(0.0);
                }
386
            } else {
Don Gagne's avatar
Don Gagne committed
387
                paramInfo->_defaultValue = paramInfo->_nanUnchanged ? std::numeric_limits<double>::quiet_NaN() : 0;
388
            }
389 390

            QStringList enumValues = paramObject.value(_enumValuesJsonKey).toString().split(",", QString::SkipEmptyParts);
391
            for (const QString &enumValue: enumValues) {
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
                bool    convertOk;
                double  value = enumValue.toDouble(&convertOk);

                if (!convertOk) {
                    internalError = QString("Bad enumValue: %1").arg(enumValue);
                    errorString = _loadErrorString(internalError);
                    return false;
                }

                paramInfo->_enumValues << QVariant(value);
            }
            if (paramInfo->_enumValues.count() != paramInfo->_enumStrings.count()) {
                internalError = QString("enum strings/values count mismatch: %1, %2").arg(paramInfo->_enumStrings.count()).arg(paramInfo->_enumValues.count());
                errorString = _loadErrorString(internalError);
                return false;
            }

            qCDebug(MissionCommandsLog) << "Param"
                                        << paramInfo->_label
                                        << paramInfo->_defaultValue
                                        << paramInfo->_decimalPlaces
                                        << paramInfo->_param
                                        << paramInfo->_units
                                        << paramInfo->_enumStrings
416 417
                                        << paramInfo->_enumValues
                                        << paramInfo->_nanUnchanged;
418 419 420 421 422 423 424 425

            _paramInfoMap[i] = paramInfo;
        }
    }

    return true;
}

426
const MissionCmdParamInfo* MissionCommandUIInfo::getParamInfo(int index, bool& showUI) const
427
{
428
    const MissionCmdParamInfo* paramInfo = nullptr;
429 430 431

    if (_paramInfoMap.contains(index)) {
        paramInfo = _paramInfoMap[index];
432
    }
433

434
    showUI = (paramInfo != nullptr) && !_paramRemoveList.contains(index);
435 436

    return paramInfo;
437
}