AirMapFlightPlanManager.h 7.3 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

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

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

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

private:
114 115
    void _createFlightPlan                  ();
    bool _collectFlightDtata                ();
116
    void _updateRulesAndFeatures            (std::vector<airmap::RuleSet::Id>& rulesets, std::unordered_map<std::string, airmap::RuleSet::Feature::Value>& features);
Gus Grubba's avatar
Gus Grubba committed
117 118 119 120 121 122

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

    struct Flight {
131
        QGCGeoBoundingCube bc;
Gus Grubba's avatar
Gus Grubba committed
132 133 134 135
        QList<QGeoCoordinate> coords;
        QGeoCoordinate  takeoffCoord;
        float maxAltitude = 0;
        void reset() {
136
            bc.reset();
Gus Grubba's avatar
Gus Grubba committed
137 138 139 140 141 142 143 144 145
            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
146
    QString                 _flightId;              ///< Current flight ID, not necessarily accepted yet
Gus Grubba's avatar
Gus Grubba committed
147
    QString                 _pilotID;               ///< Pilot ID in the form "auth0|abc123"
148
    QString                 _flightToEnd;
149
    PlanMasterController*   _planController = nullptr;
150
    bool                    _valid = false;
Gus Grubba's avatar
Gus Grubba committed
151
    bool                    _loadingFlightList = false;
152
    QmlObjectListModel      _advisories;
Gus Grubba's avatar
Gus Grubba committed
153
    QmlObjectListModel      _rulesets;
154 155 156 157
    QmlObjectListModel      _rulesViolation;
    QmlObjectListModel      _rulesInfo;
    QmlObjectListModel      _rulesReview;
    QmlObjectListModel      _rulesFollowing;
158
    QmlObjectListModel      _briefFeatures;
Gus Grubba's avatar
Gus Grubba committed
159
    QmlObjectListModel      _authorizations;
Gus Grubba's avatar
Gus Grubba committed
160
    AirspaceFlightModel     _flightList;
Gus Grubba's avatar
Gus Grubba committed
161 162
    QDateTime               _rangeStart;
    QDateTime               _rangeEnd;
163
    airmap::FlightPlan      _flightPlan;
Gus Grubba's avatar
Gus Grubba committed
164

165
    AirspaceAdvisoryProvider::AdvisoryColor  _airspaceColor;
166
    AirspaceFlightPlanProvider::PermitStatus _flightPermitStatus = AirspaceFlightPlanProvider::PermitNone;
Gus Grubba's avatar
Gus Grubba committed
167 168 169

};