AirMapManager.cc 5.42 KB
Newer Older
1 2
/****************************************************************************
 *
3
 *   (c) 2017 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#include "AirMapManager.h"
11 12 13 14 15 16 17 18
#include "AirmapWeatherInformation.h"
#include "AirMapRestrictionManager.h"
#include "AirMapRulesetsManager.h"
#include "AirMapSettings.h"
#include "AirMapSharedState.h"
#include "AirMapTelemetry.h"
#include "AirMapTrafficMonitor.h"

19 20 21 22
#include "QmlObjectListModel.h"
#include "JsonHelper.h"
#include "SettingsManager.h"
#include "AppSettings.h"
23
#include "QGCQGeoCoordinate.h"
24
#include "QGCApplication.h"
25

26 27
#include <airmap/authenticator.h>
#include <airmap/airspaces.h>
28
#include <airmap/evaluation.h>
29 30 31 32
#include <airmap/flight_plans.h>
#include <airmap/flights.h>
#include <airmap/pilots.h>
#include <airmap/telemetry.h>
Gus Grubba's avatar
Gus Grubba committed
33
#include <airmap/rulesets.h>
34

35
using namespace airmap;
36

37
QGC_LOGGING_CATEGORY(AirMapManagerLog, "AirMapManagerLog")
38

39 40 41
AirMapManager::AirMapManager(QGCApplication* app, QGCToolbox* toolbox)
    : AirspaceManager(app, toolbox)
{
42
    _logger = std::make_shared<qt::Logger>();
43
    qt::register_types(); // TODO: still needed?s
44 45 46 47 48 49 50 51 52 53 54 55
    _logger->logging_category().setEnabled(QtDebugMsg, true);
    _logger->logging_category().setEnabled(QtInfoMsg, true);
    _logger->logging_category().setEnabled(QtWarningMsg, true);
    _dispatchingLogger = std::make_shared<qt::DispatchingLogger>(_logger);
    connect(&_shared, &AirMapSharedState::error, this, &AirMapManager::_error);
}

AirMapManager::~AirMapManager()
{
    if (_shared.client()) {
        delete _shared.client();
    }
56 57
}

58 59
void
AirMapManager::setToolbox(QGCToolbox* toolbox)
60
{
61
    AirspaceManager::setToolbox(toolbox);
62
    AirMapSettings* ap = toolbox->settingsManager()->airMapSettings();
63 64 65 66
    connect(ap->apiKey(),    &Fact::rawValueChanged, this, &AirMapManager::_settingsChanged);
    connect(ap->clientID(),  &Fact::rawValueChanged, this, &AirMapManager::_settingsChanged);
    connect(ap->userName(),  &Fact::rawValueChanged, this, &AirMapManager::_settingsChanged);
    connect(ap->password(),  &Fact::rawValueChanged, this, &AirMapManager::_settingsChanged);
67 68 69
    _settingsChanged();
}

70 71
void
AirMapManager::_error(const QString& what, const QString& airmapdMessage, const QString& airmapdDetails)
72
{
73 74
    qCDebug(AirMapManagerLog) << "Error: "<<what<<", msg: "<<airmapdMessage<<", details: "<<airmapdDetails;
    qgcApp()->showMessage(QString("AirMap Error: %1. %2").arg(what).arg(airmapdMessage));
75 76
}

77 78
void
AirMapManager::_settingsChanged()
79 80 81
{
    qCDebug(AirMapManagerLog) << "AirMap settings changed";
    AirMapSettings* ap = _toolbox->settingsManager()->airMapSettings();
82 83 84 85 86 87 88 89 90 91 92
    AirMapSharedState::Settings settings;
    settings.apiKey = ap->apiKey()->rawValueString();
    bool apiKeyChanged = settings.apiKey != _shared.settings().apiKey;
    settings.clientID = ap->clientID()->rawValueString();
    settings.userName = ap->userName()->rawValueString();
    settings.password = ap->password()->rawValueString();
    _shared.setSettings(settings);
    // need to re-create the client if the API key changed
    if (_shared.client() && apiKeyChanged) {
        delete _shared.client();
        _shared.setClient(nullptr);
93
    }
94 95 96 97 98 99 100 101 102 103 104
    if (!_shared.client() && settings.apiKey != "") {
        qCDebug(AirMapManagerLog) << "Creating AirMap client";
        auto credentials    = Credentials{};
        credentials.api_key = _shared.settings().apiKey.toStdString();
        auto configuration  = Client::default_staging_configuration(credentials);
        qt::Client::create(configuration, _dispatchingLogger, this, [this, ap](const qt::Client::CreateResult& result) {
            if (result) {
                qCDebug(AirMapManagerLog) << "Successfully created airmap::qt::Client instance";
                _shared.setClient(result.value());
            } else {
                qWarning("Failed to create airmap::qt::Client instance");
105 106 107
                QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
                _error("Failed to create airmap::qt::Client instance",
                        QString::fromStdString(result.error().message()), description);
108 109
            }
        });
110 111 112
    }
}

113 114
AirspaceVehicleManager*
AirMapManager::instantiateVehicle(const Vehicle& vehicle)
115
{
116 117
    AirMapVehicleManager* manager = new AirMapVehicleManager(_shared, vehicle, *_toolbox);
    connect(manager, &AirMapVehicleManager::error, this, &AirMapManager::_error);
118
    return manager;
119 120
}

121 122
AirspaceRestrictionProvider*
AirMapManager::instantiateRestrictionProvider()
123
{
124 125
    AirMapRestrictionManager* restrictionManager = new AirMapRestrictionManager(_shared);
    connect(restrictionManager, &AirMapRestrictionManager::error, this, &AirMapManager::_error);
126
    return restrictionManager;
127 128
}

129 130
AirspaceRulesetsProvider*
AirMapManager::instantiateRulesetsProvider()
Gus Grubba's avatar
Gus Grubba committed
131 132 133 134 135 136
{
    AirMapRulesetsManager* rulesetsManager = new AirMapRulesetsManager(_shared);
    connect(rulesetsManager, &AirMapRulesetsManager::error, this, &AirMapManager::_error);
    return rulesetsManager;
}

137 138
AirspaceWeatherInfoProvider*
AirMapManager::instatiateAirspaceWeatherInfoProvider()
Gus Grubba's avatar
Gus Grubba committed
139
{
140 141 142
    AirMapWeatherInformation* weatherInfo = new AirMapWeatherInformation(_shared);
    connect(weatherInfo, &AirMapWeatherInformation::error, this, &AirMapManager::_error);
    return weatherInfo;
Gus Grubba's avatar
Gus Grubba committed
143
}