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

10
#include "AirMapFlightManager.h"
Gus Grubba's avatar
Gus Grubba committed
11
#include "AirMapManager.h"
12 13
#include "AirMapRulesetsManager.h"
#include "QGCApplication.h"
Gus Grubba's avatar
Gus Grubba committed
14

Gus Grubba's avatar
Gus Grubba committed
15 16 17 18 19 20 21 22 23
#include "QGCMAVLink.h"

#include "airmap/pilots.h"
#include "airmap/flights.h"
#include "airmap/date_time.h"
#include "airmap/flight_plans.h"
#include "airmap/geometry.h"

using namespace airmap;
24

25
//-----------------------------------------------------------------------------
26 27 28 29 30 31
AirMapFlightManager::AirMapFlightManager(AirMapSharedState& shared)
    : _shared(shared)
{

}

32 33 34
//-----------------------------------------------------------------------------
void
AirMapFlightManager::findFlight(const QGCGeoBoundingCube& bc)
35
{
36
    _state = State::FetchFlights;
Gus Grubba's avatar
Gus Grubba committed
37
    _searchArea = bc;
38
    std::weak_ptr<LifetimeChecker> isAlive(_instance);
Gus Grubba's avatar
Gus Grubba committed
39
    _shared.doRequestWithLogin([this, isAlive](const QString& login_token) {
40
        if (!isAlive.lock()) return;
41
        if (_state != State::FetchFlights) return;
Gus Grubba's avatar
Gus Grubba committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        QList<QGeoCoordinate> coords = _searchArea.polygon2D();
        Geometry::LineString lineString;
        for (const auto& qcoord : coords) {
            Geometry::Coordinate coord;
            coord.latitude  = qcoord.latitude();
            coord.longitude = qcoord.longitude();
            lineString.coordinates.push_back(coord);
        }
        _flightID.clear();
        Flights::Search::Parameters params;
        params.authorization = login_token.toStdString();
        params.geometry      = Geometry(lineString);
        _shared.client()->flights().search(params, [this, isAlive](const Flights::Search::Result& result) {
            if (!isAlive.lock()) return;
            if (_state != State::FetchFlights) return;
            if (result && result.value().flights.size() > 0) {
                const Flights::Search::Response& response = result.value();
                qCDebug(AirMapManagerLog) << "Find flights response";
                for (const auto& flight : response.flights) {
                    QString fid = QString::fromStdString(flight.id);
                    qCDebug(AirMapManagerLog) << "Checking flight:" << fid;
                    if(flight.geometry.type() == Geometry::Type::line_string) {
                        const Geometry::LineString& lineString = flight.geometry.details_for_line_string();
                        QList<QGeoCoordinate> rcoords;
                        for (const auto& vertex : lineString.coordinates) {
                            rcoords.append(QGeoCoordinate(vertex.latitude, vertex.longitude));
                        }
                        if(_searchArea == rcoords) {
                            qCDebug(AirMapManagerLog) << "Found match:" << fid;
                            _flightID = fid;
                            _state = State::Idle;
                            emit flightIDChanged();
                            return;
                        }
76
                    }
77 78
                }
            }
Gus Grubba's avatar
Gus Grubba committed
79 80 81 82
            qCDebug(AirMapManagerLog) << "No flights found";
            emit flightIDChanged();
        });
        _state = State::Idle;
83 84 85
    });
}

86 87 88
//-----------------------------------------------------------------------------
void
AirMapFlightManager::endFlight(const QString& flightID)
89 90 91 92 93
{
    if (_state != State::Idle) {
        qCWarning(AirMapManagerLog) << "AirMapFlightManager::endFlight: State not idle";
        return;
    }
94
    qCDebug(AirMapManagerLog) << "Ending flight" << flightID;
95 96 97 98 99 100 101 102 103
    _state = State::FlightEnd;
    Flights::EndFlight::Parameters params;
    params.authorization = _shared.loginToken().toStdString();
    params.id = flightID.toStdString();
    std::weak_ptr<LifetimeChecker> isAlive(_instance);
    _shared.client()->flights().end_flight(params, [this, isAlive](const Flights::EndFlight::Result& result) {
        if (!isAlive.lock()) return;
        if (_state != State::FlightEnd) return;
        _state = State::Idle;
104
        if (!result) {
105 106 107 108 109 110 111 112
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
            emit error("Failed to end Flight",
                    QString::fromStdString(result.error().message()), description);
        }
    });
}