Commit e48aad6b authored by DonLakeFlyer's avatar DonLakeFlyer

Implement GeoFence persistence

parent 6e2c8109
...@@ -34,6 +34,9 @@ QGC_LOGGING_CATEGORY(GeoFenceControllerLog, "GeoFenceControllerLog") ...@@ -34,6 +34,9 @@ QGC_LOGGING_CATEGORY(GeoFenceControllerLog, "GeoFenceControllerLog")
const char* GeoFenceController::_jsonFileTypeValue = "GeoFence"; const char* GeoFenceController::_jsonFileTypeValue = "GeoFence";
const char* GeoFenceController::_jsonBreachReturnKey = "breachReturn"; const char* GeoFenceController::_jsonBreachReturnKey = "breachReturn";
const char* GeoFenceController::_jsonPolygonsKey = "polygons";
const char* GeoFenceController::_jsonCirclesKey = "circles";
const char* GeoFenceController::_px4ParamCircularFence = "GF_MAX_HOR_DIST"; const char* GeoFenceController::_px4ParamCircularFence = "GF_MAX_HOR_DIST";
GeoFenceController::GeoFenceController(PlanMasterController* masterController, QObject* parent) GeoFenceController::GeoFenceController(PlanMasterController* masterController, QObject* parent)
...@@ -116,46 +119,77 @@ void GeoFenceController::managerVehicleChanged(Vehicle* managerVehicle) ...@@ -116,46 +119,77 @@ void GeoFenceController::managerVehicleChanged(Vehicle* managerVehicle)
bool GeoFenceController::load(const QJsonObject& json, QString& errorString) bool GeoFenceController::load(const QJsonObject& json, QString& errorString)
{ {
Q_UNUSED(json); errorString.clear();
Q_UNUSED(errorString);
#if 0 QList<JsonHelper::KeyValidateInfo> keyInfoList = {
QString errorStr; { JsonHelper::jsonVersionKey, QJsonValue::Double, true },
QString errorMessage = tr("GeoFence: %1"); { _jsonCirclesKey, QJsonValue::Array, true },
{ _jsonPolygonsKey, QJsonValue::Array, true },
};
if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) {
return false;
}
if (json.contains(_jsonBreachReturnKey) && if (json[JsonHelper::jsonVersionKey].toInt() != _jsonCurrentVersion) {
!JsonHelper::loadGeoCoordinate(json[_jsonBreachReturnKey], false /* altitudeRequired */, _breachReturnPoint, errorStr)) { errorString = tr("GeoFence supports version %1").arg(_jsonCurrentVersion);
errorString = errorMessage.arg(errorStr);
return false; return false;
} }
if (!_mapPolygon.loadFromJson(json, true, errorStr)) { QJsonArray jsonPolygonArray = json[_jsonPolygonsKey].toArray();
errorString = errorMessage.arg(errorStr); foreach (const QJsonValue& jsonPolygonValue, jsonPolygonArray) {
if (jsonPolygonValue.type() != QJsonValue::Object) {
errorString = tr("GeoFence polygon not stored as object");
return false;
}
QGCFencePolygon* fencePolygon = new QGCFencePolygon(false /* inclusion */, this /* parent */);
if (!fencePolygon->loadFromJson(jsonPolygonValue.toObject(), true /* required */, errorString)) {
return false;
}
_polygons.append(fencePolygon);
}
QJsonArray jsonCircleArray = json[_jsonCirclesKey].toArray();
foreach (const QJsonValue& jsonCircleValue, jsonCircleArray) {
if (jsonCircleValue.type() != QJsonValue::Object) {
errorString = tr("GeoFence circle not stored as object");
return false; return false;
} }
_mapPolygon.setDirty(false);
setDirty(false);
QGCFenceCircle* fenceCircle = new QGCFenceCircle(this /* parent */);
if (!fenceCircle->loadFromJson(jsonCircleValue.toObject(), errorString)) {
return false;
}
_circles.append(fenceCircle);
}
setDirty(false);
_signalAll(); _signalAll();
#endif
return true; return true;
} }
void GeoFenceController::save(QJsonObject& json) void GeoFenceController::save(QJsonObject& json)
{ {
Q_UNUSED(json); json[JsonHelper::jsonVersionKey] = _jsonCurrentVersion;
#if 0
json[JsonHelper::jsonVersionKey] = 1;
if (_breachReturnPoint.isValid()) { QJsonArray jsonPolygonArray;
QJsonValue jsonBreachReturn; for (int i=0; i<_polygons.count(); i++) {
JsonHelper::saveGeoCoordinate(_breachReturnPoint, false /* writeAltitude */, jsonBreachReturn); QJsonObject jsonPolygon;
json[_jsonBreachReturnKey] = jsonBreachReturn; QGCFencePolygon* fencePolygon = _polygons.value<QGCFencePolygon*>(i);
fencePolygon->saveToJson(jsonPolygon);
jsonPolygonArray.append(jsonPolygon);
} }
json[_jsonPolygonsKey] = jsonPolygonArray;
_mapPolygon.saveToJson(json); QJsonArray jsonCircleArray;
#endif for (int i=0; i<_circles.count(); i++) {
QJsonObject jsonCircle;
QGCFenceCircle* fenceCircle = _circles.value<QGCFenceCircle*>(i);
fenceCircle->saveToJson(jsonCircle);
jsonCircleArray.append(jsonCircle);
}
json[_jsonCirclesKey] = jsonCircleArray;
} }
void GeoFenceController::removeAll(void) void GeoFenceController::removeAll(void)
......
...@@ -111,9 +111,14 @@ private: ...@@ -111,9 +111,14 @@ private:
bool _itemsRequested; bool _itemsRequested;
Fact* _px4ParamCircularFenceFact; Fact* _px4ParamCircularFenceFact;
static const char* _px4ParamCircularFence;
static const int _jsonCurrentVersion = 1;
static const char* _jsonFileTypeValue; static const char* _jsonFileTypeValue;
static const char* _jsonBreachReturnKey; static const char* _jsonBreachReturnKey;
static const char* _px4ParamCircularFence; static const char* _jsonPolygonsKey;
static const char* _jsonCirclesKey;
}; };
#endif #endif
...@@ -54,26 +54,32 @@ void QGCFenceCircle::_setDirty(void) ...@@ -54,26 +54,32 @@ void QGCFenceCircle::_setDirty(void)
void QGCFenceCircle::saveToJson(QJsonObject& json) void QGCFenceCircle::saveToJson(QJsonObject& json)
{ {
QGCMapCircle::saveToJson(json); json[JsonHelper::jsonVersionKey] = _jsonCurrentVersion;
json[_jsonInclusionKey] = _inclusion; json[_jsonInclusionKey] = _inclusion;
QGCMapCircle::saveToJson(json);
} }
bool QGCFenceCircle::loadFromJson(const QJsonObject& json, QString& errorString) bool QGCFenceCircle::loadFromJson(const QJsonObject& json, QString& errorString)
{ {
if (!QGCMapCircle::loadFromJson(json, errorString)) {
return false;
}
errorString.clear(); errorString.clear();
QList<JsonHelper::KeyValidateInfo> keyInfoList = { QList<JsonHelper::KeyValidateInfo> keyInfoList = {
{ JsonHelper::jsonVersionKey, QJsonValue::Double, true },
{ _jsonInclusionKey, QJsonValue::Bool, true }, { _jsonInclusionKey, QJsonValue::Bool, true },
}; };
if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) { if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) {
return false; return false;
} }
if (json[JsonHelper::jsonVersionKey].toInt() != _jsonCurrentVersion) {
errorString = tr("GeoFence Circle only supports version %1").arg(_jsonCurrentVersion);
return false;
}
if (!QGCMapCircle::loadFromJson(json, errorString)) {
return false;
}
setInclusion(json[_jsonInclusionKey].toBool()); setInclusion(json[_jsonInclusionKey].toBool());
return true; return true;
......
...@@ -51,5 +51,7 @@ private: ...@@ -51,5 +51,7 @@ private:
bool _inclusion; bool _inclusion;
static const int _jsonCurrentVersion = 1;
static const char* _jsonInclusionKey; static const char* _jsonInclusionKey;
}; };
...@@ -47,26 +47,32 @@ void QGCFencePolygon::_setDirty(void) ...@@ -47,26 +47,32 @@ void QGCFencePolygon::_setDirty(void)
void QGCFencePolygon::saveToJson(QJsonObject& json) void QGCFencePolygon::saveToJson(QJsonObject& json)
{ {
QGCMapPolygon::saveToJson(json); json[JsonHelper::jsonVersionKey] = _jsonCurrentVersion;
json[_jsonInclusionKey] = _inclusion; json[_jsonInclusionKey] = _inclusion;
QGCMapPolygon::saveToJson(json);
} }
bool QGCFencePolygon::loadFromJson(const QJsonObject& json, bool required, QString& errorString) bool QGCFencePolygon::loadFromJson(const QJsonObject& json, bool required, QString& errorString)
{ {
if (!QGCMapPolygon::loadFromJson(json, required, errorString)) {
return false;
}
errorString.clear(); errorString.clear();
QList<JsonHelper::KeyValidateInfo> keyInfoList = { QList<JsonHelper::KeyValidateInfo> keyInfoList = {
{ JsonHelper::jsonVersionKey, QJsonValue::Double, true },
{ _jsonInclusionKey, QJsonValue::Bool, true }, { _jsonInclusionKey, QJsonValue::Bool, true },
}; };
if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) { if (!JsonHelper::validateKeys(json, keyInfoList, errorString)) {
return false; return false;
} }
if (json[JsonHelper::jsonVersionKey].toInt() != _jsonCurrentVersion) {
errorString = tr("GeoFence Polygon only supports version %1").arg(_jsonCurrentVersion);
return false;
}
if (!QGCMapPolygon::loadFromJson(json, required, errorString)) {
return false;
}
setInclusion(json[_jsonInclusionKey].toBool()); setInclusion(json[_jsonInclusionKey].toBool());
return true; return true;
......
...@@ -51,5 +51,7 @@ private: ...@@ -51,5 +51,7 @@ private:
bool _inclusion; bool _inclusion;
static const int _jsonCurrentVersion = 1;
static const char* _jsonInclusionKey; static const char* _jsonInclusionKey;
}; };
...@@ -111,11 +111,11 @@ bool QGCMapCircle::loadFromJson(const QJsonObject& json, QString& errorString) ...@@ -111,11 +111,11 @@ bool QGCMapCircle::loadFromJson(const QJsonObject& json, QString& errorString)
} }
QGeoCoordinate center; QGeoCoordinate center;
if (!JsonHelper::loadGeoCoordinate(json[_jsonCenterKey], false /* altitudeRequired */, center, errorString)) { if (!JsonHelper::loadGeoCoordinate(circleObject[_jsonCenterKey], false /* altitudeRequired */, center, errorString)) {
return false; return false;
} }
setCenter(center); setCenter(center);
_radius.setRawValue(json[_jsonRadiusKey].toDouble()); _radius.setRawValue(circleObject[_jsonRadiusKey].toDouble());
return true; return true;
} }
......
...@@ -28,8 +28,8 @@ Item { ...@@ -28,8 +28,8 @@ Item {
property bool planView: false ///< true: visuals showing in plan view property bool planView: false ///< true: visuals showing in plan view
property var homePosition property var homePosition
property var _breachReturnPointComponent //property var _breachReturnPointComponent
property var _mouseAreaComponent //property var _mouseAreaComponent
property var _paramCircleFenceComponent property var _paramCircleFenceComponent
property var _polygons: myGeoFenceController.polygons property var _polygons: myGeoFenceController.polygons
property var _circles: myGeoFenceController.circles property var _circles: myGeoFenceController.circles
...@@ -75,17 +75,17 @@ Item { ...@@ -75,17 +75,17 @@ Item {
} }
Component.onCompleted: { Component.onCompleted: {
_breachReturnPointComponent = breachReturnPointComponent.createObject(map) //_breachReturnPointComponent = breachReturnPointComponent.createObject(map)
map.addMapItem(_breachReturnPointComponent) //map.addMapItem(_breachReturnPointComponent)
//_mouseAreaComponent = mouseAreaComponent.createObject(map)
_paramCircleFenceComponent = paramCircleFenceComponent.createObject(map) _paramCircleFenceComponent = paramCircleFenceComponent.createObject(map)
map.addMapItem(_paramCircleFenceComponent) map.addMapItem(_paramCircleFenceComponent)
_mouseAreaComponent = mouseAreaComponent.createObject(map)
} }
Component.onDestruction: { Component.onDestruction: {
_breachReturnPointComponent.destroy() //_breachReturnPointComponent.destroy()
//_mouseAreaComponent.destroy()
_paramCircleFenceComponent.destroy() _paramCircleFenceComponent.destroy()
_mouseAreaComponent.destroy()
} }
// Mouse area to capture breach return point coordinate // Mouse area to capture breach return point coordinate
......
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