Commit e48aad6b authored by DonLakeFlyer's avatar DonLakeFlyer

Implement GeoFence persistence

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