AirspaceManagement.cc 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/****************************************************************************
 *
 *   (c) 2017 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.
 *
 ****************************************************************************/


#include "AirspaceManagement.h"
#include <Vehicle.h>

QGC_LOGGING_CATEGORY(AirspaceManagementLog, "AirspaceManagementLog")

AirspaceManager::AirspaceManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
{
    _roiUpdateTimer.setInterval(2000);
    _roiUpdateTimer.setSingleShot(true);
    connect(&_roiUpdateTimer, &QTimer::timeout, this, &AirspaceManager::_updateToROI);
    qmlRegisterUncreatableType<AirspaceAuthorization>("QGroundControl", 1, 0, "AirspaceAuthorization", "Reference only");
}

AirspaceManager::~AirspaceManager()
{
    if (_restrictionsProvider) {
        delete _restrictionsProvider;
    }
Gus Grubba's avatar
Gus Grubba committed
30 31 32
    if(_rulesetsProvider) {
        delete _rulesetsProvider;
    }
33 34 35 36 37 38 39
    _polygonRestrictions.clearAndDeleteContents();
    _circleRestrictions.clearAndDeleteContents();
}

void AirspaceManager::setToolbox(QGCToolbox* toolbox)
{
    QGCTool::setToolbox(toolbox);
40
    // We should not call virtual methods in the constructor, so we instantiate the restriction provider here
41 42
    _restrictionsProvider = instantiateRestrictionProvider();
    if (_restrictionsProvider) {
43
        connect(_restrictionsProvider, &AirspaceRestrictionProvider::requestDone, this,   &AirspaceManager::_restrictionsUpdated);
44
    }
Gus Grubba's avatar
Gus Grubba committed
45 46
    _rulesetsProvider = instantiateRulesetsProvider();
    if (_rulesetsProvider) {
47
        connect(_rulesetsProvider, &AirspaceRulesetsProvider::requestDone, this,  &AirspaceManager::_rulessetsUpdated);
Gus Grubba's avatar
Gus Grubba committed
48
    }
49
    _weatherProvider = instatiateAirspaceWeatherInfoProvider();
50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

void AirspaceManager::setROI(const QGeoCoordinate& center, double radiusMeters)
{
    _roiCenter = center;
    _roiRadius = radiusMeters;
    _roiUpdateTimer.start();
}

void AirspaceManager::_updateToROI()
{
    if (_restrictionsProvider) {
        _restrictionsProvider->setROI(_roiCenter, _roiRadius);
    }
Gus Grubba's avatar
Gus Grubba committed
64 65 66
    if(_rulesetsProvider) {
        _rulesetsProvider->setROI(_roiCenter);
    }
67 68 69
    if(_weatherProvider) {
        _weatherProvider->setROI(_roiCenter);
    }
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
}

void AirspaceManager::_restrictionsUpdated(bool success)
{
    _polygonRestrictions.clearAndDeleteContents();
    _circleRestrictions.clearAndDeleteContents();
    if (success) {
        for (const auto& circle : _restrictionsProvider->circles()) {
            _circleRestrictions.append(circle);
        }
        for (const auto& polygon : _restrictionsProvider->polygons()) {
            _polygonRestrictions.append(polygon);
        }
    } else {
        // TODO: show error?
    }
}

88 89 90 91
void AirspaceManager::_rulessetsUpdated(bool)
{

}
92

93
AirspaceVehicleManager::AirspaceVehicleManager(const Vehicle& vehicle)
94 95
    : _vehicle(vehicle)
{
96 97
    connect(&_vehicle, &Vehicle::armedChanged, this, &AirspaceVehicleManager::_vehicleArmedChanged);
    connect(&_vehicle, &Vehicle::mavlinkMessageReceived, this, &AirspaceVehicleManager::vehicleMavlinkMessageReceived);
98 99
}

100
void AirspaceVehicleManager::_vehicleArmedChanged(bool armed)
101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
    if (armed) {
        startTelemetryStream();
        _vehicleWasInMissionMode = _vehicle.flightMode() == _vehicle.missionFlightMode();
    } else {
        stopTelemetryStream();
        // end the flight if we were in mission mode during arming or disarming
        // TODO: needs to be improved. for instance if we do RTL and then want to continue the mission...
        if (_vehicleWasInMissionMode || _vehicle.flightMode() == _vehicle.missionFlightMode()) {
            endFlight();
        }
    }
}