AirMapFlightManager.h 3.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/

#pragma once

Gus Grubba's avatar
Gus Grubba committed
12
#include "LifetimeChecker.h"
Gus Grubba's avatar
Gus Grubba committed
13
#include "AirMapSharedState.h"
Gus Grubba's avatar
Gus Grubba committed
14
#include "AirspaceFlightPlanProvider.h"
Gus Grubba's avatar
Gus Grubba committed
15

16 17
#include <QObject>
#include <QTimer>
Gus Grubba's avatar
Gus Grubba committed
18
#include <QList>
Gus Grubba's avatar
Gus Grubba committed
19 20 21
#include <QGeoCoordinate>

class MissionItem;
22

23 24 25 26 27 28 29 30 31 32 33 34


/*
 * TODO: This is here for reference. It will have to modifed quite a bit before it
 * can be used. This was the original file that combined both the creation of a
 * flight plan and immediate submission of a flight. The flight plan is now handled
 * by its own class. This now needs to be made into a proper "Flight Manager".
*/




35 36 37 38 39 40 41 42 43 44 45
//-----------------------------------------------------------------------------
/// class to upload a flight
class AirMapFlightManager : public QObject, public LifetimeChecker
{
    Q_OBJECT
public:
    AirMapFlightManager(AirMapSharedState& shared);

    /// Send flight path to AirMap
    void createFlight(const QList<MissionItem*>& missionItems);

Gus Grubba's avatar
Gus Grubba committed
46
    AirspaceFlightPlanProvider::PermitStatus flightPermitStatus() const { return _flightPermitStatus; }
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    const QString& flightID() const { return _currentFlightId; }

public slots:
    void endFlight();

signals:
    void error(const QString& what, const QString& airmapdMessage, const QString& airmapdDetails);
    void flightPermitStatusChanged();

private slots:
    void _pollBriefing();

private:

    /**
     * upload flight stored in _flight
     */
    void _uploadFlight();

    /**
     * query the active flights and end the first one (because only a single flight can be active at a time).
     */
    void _endFirstFlight();

    /**
     * implementation of endFlight()
     */
    void _endFlight(const QString& flightID);

    /**
     * check if the briefing response is valid and call _submitPendingFlightPlan() if it is.
     */
    void _checkForValidBriefing();

    void _submitPendingFlightPlan();

    enum class State {
        Idle,
        GetPilotID,
        FlightUpload,
        FlightBrief,
        FlightSubmit,
        FlightPolling, // poll & check for approval
        FlightEnd,
        EndFirstFlight, // get a list of open flights & end the first one (because there can only be 1 active at a time)
    };
    struct Flight {
        QList<QGeoCoordinate> coords;
        QGeoCoordinate takeoffCoord;
        float maxAltitude = 0;

        void reset() {
            coords.clear();
            maxAltitude = 0;
        }
    };
    Flight                              _flight; ///< flight pending to be uploaded

    State                               _state = State::Idle;
    AirMapSharedState&                  _shared;
    QString                             _currentFlightId; ///< Flight ID, empty if there is none
    QString                             _pendingFlightId; ///< current flight ID, not necessarily accepted yet (once accepted, it's equal to _currentFlightId)
    QString                             _pendingFlightPlan; ///< current flight plan, waiting to be submitted
111
    AirspaceFlightPlanProvider::PermitStatus _flightPermitStatus = AirspaceFlightPlanProvider::PermitNone;
112 113 114 115 116
    QString                             _pilotID; ///< Pilot ID in the form "auth0|abc123"
    bool                                _noFlightCreatedYet = true;
    QTimer                              _pollTimer; ///< timer to poll for approval check
};