Commit 78115454 authored by Don Gagne's avatar Don Gagne Committed by GitHub

Merge pull request #5339 from DonLakeFlyer/V3MissionItemFormat

New V3 format for mission items
parents 988ffbee 4a36f9d5
......@@ -162,12 +162,8 @@ void MissionItem::save(QJsonObject& json) const
json[_jsonAutoContinueKey] = autoContinue();
json[_jsonDoJumpIdKey] = _sequenceNumber;
QJsonArray rgParams = { param1(), param2(), param3(), param4() };
QJsonArray rgParams = { param1(), param2(), param3(), param4(), param5(), param6(), param7() };
json[_jsonParamsKey] = rgParams;
QJsonValue coordinateValue;
JsonHelper::saveGeoCoordinate(QGeoCoordinate(param5(), param6(), param7()), true /* writeAltitude */, coordinateValue);
json[_jsonCoordinateKey] = coordinateValue;
}
bool MissionItem::load(QTextStream &loadStream)
......@@ -229,10 +225,46 @@ bool MissionItem::_convertJsonV1ToV2(const QJsonObject& json, QJsonObject& v2Jso
return true;
}
bool MissionItem::_convertJsonV2ToV3(QJsonObject& json, QString& errorString)
{
// V2 format: param 5/6/7 stored in GeoCoordinate
// V3 format: param 5/6/7 stored in params array
if (!json.contains(_jsonCoordinateKey)) {
// Already V3 format
return true;
}
QList<JsonHelper::KeyValidateInfo> keyInfoList = {
{ _jsonCoordinateKey, QJsonValue::Array, true },
};
if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) {
return false;
}
QGeoCoordinate coordinate;
if (!JsonHelper::loadGeoCoordinate(json[_jsonCoordinateKey], true /* altitudeRequired */, coordinate, errorString)) {
return false;
}
QJsonArray rgParam = json[_jsonParamsKey].toArray();
rgParam.append(coordinate.latitude());
rgParam.append(coordinate.longitude());
rgParam.append(coordinate.altitude());
json[_jsonParamsKey] = rgParam;
json.remove(_jsonCoordinateKey);
return true;
}
bool MissionItem::load(const QJsonObject& json, int sequenceNumber, QString& errorString)
{
QJsonObject v2Json;
if (!_convertJsonV1ToV2(json, v2Json, errorString)) {
QJsonObject convertedJson;
if (!_convertJsonV1ToV2(json, convertedJson, errorString)) {
return false;
}
if (!_convertJsonV2ToV3(convertedJson, errorString)) {
return false;
}
......@@ -242,21 +274,20 @@ bool MissionItem::load(const QJsonObject& json, int sequenceNumber, QString& err
{ _jsonCommandKey, QJsonValue::Double, true },
{ _jsonParamsKey, QJsonValue::Array, true },
{ _jsonAutoContinueKey, QJsonValue::Bool, true },
{ _jsonCoordinateKey, QJsonValue::Array, true },
{ _jsonDoJumpIdKey, QJsonValue::Double, false },
};
if (!JsonHelper::validateKeys(v2Json, keyInfoList, errorString)) {
if (!JsonHelper::validateKeys(convertedJson, keyInfoList, errorString)) {
return false;
}
if (v2Json[VisualMissionItem::jsonTypeKey] != VisualMissionItem::jsonTypeSimpleItemValue) {
errorString = tr("Type found: %1 must be: %2").arg(v2Json[VisualMissionItem::jsonTypeKey].toString()).arg(VisualMissionItem::jsonTypeSimpleItemValue);
if (convertedJson[VisualMissionItem::jsonTypeKey] != VisualMissionItem::jsonTypeSimpleItemValue) {
errorString = tr("Type found: %1 must be: %2").arg(convertedJson[VisualMissionItem::jsonTypeKey].toString()).arg(VisualMissionItem::jsonTypeSimpleItemValue);
return false;
}
QJsonArray rgParams = v2Json[_jsonParamsKey].toArray();
if (rgParams.count() != 4) {
errorString = tr("%1 key must contains 4 values").arg(_jsonParamsKey);
QJsonArray rgParams = convertedJson[_jsonParamsKey].toArray();
if (rgParams.count() != 7) {
errorString = tr("%1 key must contains 7 values").arg(_jsonParamsKey);
return false;
}
......@@ -268,29 +299,24 @@ bool MissionItem::load(const QJsonObject& json, int sequenceNumber, QString& err
}
// Make sure to set these first since they can signal other changes
setFrame((MAV_FRAME)v2Json[_jsonFrameKey].toInt());
setCommand((MAV_CMD)v2Json[_jsonCommandKey].toInt());
QGeoCoordinate coordinate;
if (!JsonHelper::loadGeoCoordinate(v2Json[_jsonCoordinateKey], true /* altitudeRequired */, coordinate, errorString)) {
return false;
}
setParam5(coordinate.latitude());
setParam6(coordinate.longitude());
setParam7(coordinate.altitude());
setFrame((MAV_FRAME)convertedJson[_jsonFrameKey].toInt());
setCommand((MAV_CMD)convertedJson[_jsonCommandKey].toInt());
_doJumpId = -1;
if (v2Json.contains(_jsonDoJumpIdKey)) {
_doJumpId = v2Json[_jsonDoJumpIdKey].toInt();
if (convertedJson.contains(_jsonDoJumpIdKey)) {
_doJumpId = convertedJson[_jsonDoJumpIdKey].toInt();
}
setIsCurrentItem(false);
setSequenceNumber(sequenceNumber);
setAutoContinue(v2Json[_jsonAutoContinueKey].toBool());
setAutoContinue(convertedJson[_jsonAutoContinueKey].toBool());
setParam1(JsonHelper::possibleNaNJsonValue(rgParams[0]));
setParam2(JsonHelper::possibleNaNJsonValue(rgParams[1]));
setParam3(JsonHelper::possibleNaNJsonValue(rgParams[2]));
setParam4(JsonHelper::possibleNaNJsonValue(rgParams[3]));
setParam5(JsonHelper::possibleNaNJsonValue(rgParams[4]));
setParam6(JsonHelper::possibleNaNJsonValue(rgParams[5]));
setParam7(JsonHelper::possibleNaNJsonValue(rgParams[6]));
return true;
}
......
......@@ -114,6 +114,7 @@ private slots:
private:
bool _convertJsonV1ToV2(const QJsonObject& json, QJsonObject& v2Json, QString& errorString);
bool _convertJsonV2ToV3(QJsonObject& json, QString& errorString);
int _sequenceNumber;
int _doJumpId;
......@@ -134,10 +135,12 @@ private:
static const char* _jsonFrameKey;
static const char* _jsonCommandKey;
static const char* _jsonAutoContinueKey;
static const char* _jsonCoordinateKey;
static const char* _jsonParamsKey;
static const char* _jsonDoJumpIdKey;
// Deprecated V2 format keys
static const char* _jsonCoordinateKey;
// Deprecated V1 format keys
static const char* _jsonParam1Key;
static const char* _jsonParam2Key;
......
This diff is collapsed.
......@@ -35,11 +35,16 @@ private slots:
void _testSimpleLoadFromStream(void);
void _testLoadFromJsonV1(void);
void _testLoadFromJsonV2(void);
void _testLoadFromJsonV3(void);
void _testLoadFromJsonV3NaN(void);
void _testSimpleLoadFromJson(void);
void _testSaveToJson(void);
private:
void _checkExpectedMissionItem(const MissionItem& missionItem);
void _checkExpectedMissionItem(const MissionItem& missionItem, bool allNaNs = false);
QJsonObject _createV1Json(void);
QJsonObject _createV2Json(void);
QJsonObject _createV3Json(bool allNaNs = false);
int _seq = 10;
Vehicle* _offlineVehicle;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment