AirMapFlightPlanManager.h 7.53 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/****************************************************************************
 *
 *   (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

#include "LifetimeChecker.h"
#include "AirMapSharedState.h"
#include "AirspaceFlightPlanProvider.h"

#include <QObject>
#include <QTimer>
#include <QList>
#include <QGeoCoordinate>

Gus Grubba's avatar
Gus Grubba committed
21
#include "airmap/flight.h"
22
#include "airmap/flight_plan.h"
23
#include "airmap/ruleset.h"
Gus Grubba's avatar
Gus Grubba committed
24

25 26
class PlanMasterController;

Gus Grubba's avatar
Gus Grubba committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
//-----------------------------------------------------------------------------
class AirMapFlightAuthorization : public AirspaceFlightAuthorization
{
    Q_OBJECT
public:
    AirMapFlightAuthorization               (const airmap::Evaluation::Authorization auth, QObject *parent = nullptr);

    AirspaceFlightAuthorization::AuthorizationStatus
                            status          () override;
    QString                 name            () override { return QString::fromStdString(_auth.authority.name); }
    QString                 id              () override { return QString::fromStdString(_auth.authority.id); }
    QString                 message         () override { return QString::fromStdString(_auth.message); }
private:
    airmap::Evaluation::Authorization   _auth;
};

Gus Grubba's avatar
Gus Grubba committed
43 44 45 46 47
//-----------------------------------------------------------------------------
class AirMapFlightInfo : public AirspaceFlightInfo
{
    Q_OBJECT
public:
Gus Grubba's avatar
Gus Grubba committed
48 49
    AirMapFlightInfo                        (const airmap::Flight& flight, QObject *parent = nullptr);
    QString             flightID            () override { return QString::fromStdString(_flight.id); }
Gus Grubba's avatar
Gus Grubba committed
50 51 52 53
    QString             flightPlanID        () override { return _flight.flight_plan_id ? QString::fromStdString(_flight.flight_plan_id.get()) : QString(); }
    QString             createdTime         () override;
    QString             startTime           () override;
    QString             endTime             () override;
54
    QDateTime           qStartTime          () override;
Gus Grubba's avatar
Gus Grubba committed
55
    QGeoCoordinate      takeOff             () override { return QGeoCoordinate(_flight.latitude, _flight.longitude);}
56 57 58
    QVariantList        boundingBox         () override { return _boundingBox; }
    bool                active              () override;
    void                setEndFlight        (airmap::DateTime end);
Gus Grubba's avatar
Gus Grubba committed
59 60
private:
    airmap::Flight      _flight;
61
    QVariantList        _boundingBox;
Gus Grubba's avatar
Gus Grubba committed
62 63
};

Gus Grubba's avatar
Gus Grubba committed
64 65 66 67 68 69
//-----------------------------------------------------------------------------
/// class to upload a flight
class AirMapFlightPlanManager : public AirspaceFlightPlanProvider, public LifetimeChecker
{
    Q_OBJECT
public:
70 71
    AirMapFlightPlanManager                 (AirMapSharedState& shared, QObject *parent = nullptr);
    ~AirMapFlightPlanManager                ();
Gus Grubba's avatar
Gus Grubba committed
72

73
    PermitStatus        flightPermitStatus  () const override { return _flightPermitStatus; }
74 75
    QDateTime           flightStartTime     () const override;
    QDateTime           flightEndTime       () const override;
76 77
    bool                valid               () override { return _valid; }
    QmlObjectListModel* advisories          () override { return &_advisories; }
Gus Grubba's avatar
Gus Grubba committed
78
    QmlObjectListModel* ruleSets            () override { return &_rulesets; }
79
    QGCGeoBoundingCube* missionArea         () override { return &_flight.bc; }
80 81 82

    AirspaceAdvisoryProvider::AdvisoryColor
                        airspaceColor       () override { return _airspaceColor; }
83

84 85 86 87
    QmlObjectListModel* rulesViolation      () override { return &_rulesViolation; }
    QmlObjectListModel* rulesInfo           () override { return &_rulesInfo; }
    QmlObjectListModel* rulesReview         () override { return &_rulesReview; }
    QmlObjectListModel* rulesFollowing      () override { return &_rulesFollowing; }
88
    QmlObjectListModel* briefFeatures       () override { return &_briefFeatures; }
Gus Grubba's avatar
Gus Grubba committed
89
    QmlObjectListModel* authorizations      () override { return &_authorizations; }
Gus Grubba's avatar
Gus Grubba committed
90
    AirspaceFlightModel*flightList          () override { return &_flightList; }
Gus Grubba's avatar
Gus Grubba committed
91
    bool                loadingFlightList   () override { return _loadingFlightList; }
92
    QString             flightPlanID        () {return QString::fromStdString(_flightPlan.id); }
93
    QString             flightID            () {return _flightId; }
94

95
    void                updateFlightPlan    () override;
Gus Grubba's avatar
Gus Grubba committed
96
    void                submitFlightPlan    () override;
97 98 99
    void                startFlightPlanning (PlanMasterController* planController) override;
    void                setFlightStartTime  (QDateTime start) override;
    void                setFlightEndTime    (QDateTime end) override;
Gus Grubba's avatar
Gus Grubba committed
100
    void                loadFlightList      (QDateTime startTime, QDateTime endTime) override;
101
    void                endFlight           (QString flightID) override;
Gus Grubba's avatar
Gus Grubba committed
102 103

signals:
104
    void            error                   (const QString& what, const QString& airmapdMessage, const QString& airmapdDetails);
Gus Grubba's avatar
Gus Grubba committed
105 106

private slots:
107 108
    void _pollBriefing                      ();
    void _missionChanged                    ();
109
    void _endFlight                         ();
110
    void _uploadFlightPlan                  ();
111
    void _updateFlightPlanOnTimer           ();
Gus Grubba's avatar
Gus Grubba committed
112 113 114
    void _loadFlightList                    ();

private:
115 116
    void _createFlightPlan                  ();
    bool _collectFlightDtata                ();
117 118 119
    void _updateFlightPlan                  (bool interactive = false);
    bool _findBriefFeature                  (const QString& name);
    void _updateRulesAndFeatures            (std::vector<airmap::RuleSet::Id>& rulesets, std::unordered_map<std::string, airmap::RuleSet::Feature::Value>& features, bool updateFeatures = false);
Gus Grubba's avatar
Gus Grubba committed
120 121 122 123 124 125

private:
    enum class State {
        Idle,
        GetPilotID,
        FlightUpload,
126
        FlightUpdate,
127
        FlightEnd,
128
        FlightSubmit,
Gus Grubba's avatar
Gus Grubba committed
129
        FlightPolling,
Gus Grubba's avatar
Gus Grubba committed
130
        LoadFlightList,
Gus Grubba's avatar
Gus Grubba committed
131 132 133
    };

    struct Flight {
134
        QGCGeoBoundingCube bc;
Gus Grubba's avatar
Gus Grubba committed
135 136 137 138
        QList<QGeoCoordinate> coords;
        QGeoCoordinate  takeoffCoord;
        float maxAltitude = 0;
        void reset() {
139
            bc.reset();
Gus Grubba's avatar
Gus Grubba committed
140 141 142 143 144 145 146 147 148
            coords.clear();
            maxAltitude = 0;
        }
    };

    Flight                  _flight;                ///< flight pending to be uploaded
    State                   _state = State::Idle;
    AirMapSharedState&      _shared;
    QTimer                  _pollTimer;             ///< timer to poll for approval check
149
    QString                 _flightId;              ///< Current flight ID, not necessarily accepted yet
Gus Grubba's avatar
Gus Grubba committed
150
    QString                 _pilotID;               ///< Pilot ID in the form "auth0|abc123"
151
    QString                 _flightToEnd;
152
    PlanMasterController*   _planController = nullptr;
153
    bool                    _valid = false;
Gus Grubba's avatar
Gus Grubba committed
154
    bool                    _loadingFlightList = false;
155
    QmlObjectListModel      _advisories;
Gus Grubba's avatar
Gus Grubba committed
156
    QmlObjectListModel      _rulesets;
157 158 159 160
    QmlObjectListModel      _rulesViolation;
    QmlObjectListModel      _rulesInfo;
    QmlObjectListModel      _rulesReview;
    QmlObjectListModel      _rulesFollowing;
161
    QmlObjectListModel      _briefFeatures;
Gus Grubba's avatar
Gus Grubba committed
162
    QmlObjectListModel      _authorizations;
Gus Grubba's avatar
Gus Grubba committed
163
    AirspaceFlightModel     _flightList;
Gus Grubba's avatar
Gus Grubba committed
164 165
    QDateTime               _rangeStart;
    QDateTime               _rangeEnd;
166
    airmap::FlightPlan      _flightPlan;
Gus Grubba's avatar
Gus Grubba committed
167

168
    AirspaceAdvisoryProvider::AdvisoryColor  _airspaceColor;
169
    AirspaceFlightPlanProvider::PermitStatus _flightPermitStatus = AirspaceFlightPlanProvider::PermitNone;
Gus Grubba's avatar
Gus Grubba committed
170 171 172

};