AirMapFlightPlanManager.h 8.03 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
Gus Grubba's avatar
Gus Grubba committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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

Gus Grubba's avatar
Gus Grubba committed
25
class AirMapRuleFeature;
26 27
class PlanMasterController;

Gus Grubba's avatar
Gus Grubba committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
//-----------------------------------------------------------------------------
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
44 45 46 47 48
//-----------------------------------------------------------------------------
class AirMapFlightInfo : public AirspaceFlightInfo
{
    Q_OBJECT
public:
Gus Grubba's avatar
Gus Grubba committed
49 50
    AirMapFlightInfo                        (const airmap::Flight& flight, QObject *parent = nullptr);
    QString             flightID            () override { return QString::fromStdString(_flight.id); }
Gus Grubba's avatar
Gus Grubba committed
51 52 53 54
    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;
55
    QDateTime           qStartTime          () override;
Gus Grubba's avatar
Gus Grubba committed
56
    QGeoCoordinate      takeOff             () override { return QGeoCoordinate(static_cast<double>(_flight.latitude), static_cast<double>(_flight.longitude));}
57 58 59
    QVariantList        boundingBox         () override { return _boundingBox; }
    bool                active              () override;
    void                setEndFlight        (airmap::DateTime end);
Gus Grubba's avatar
Gus Grubba committed
60 61
private:
    airmap::Flight      _flight;
62
    QVariantList        _boundingBox;
Gus Grubba's avatar
Gus Grubba committed
63 64
};

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

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

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

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

97
    void                updateFlightPlan    () override;
Gus Grubba's avatar
Gus Grubba committed
98
    void                submitFlightPlan    () override;
99 100
    void                startFlightPlanning (PlanMasterController* planController) override;
    void                setFlightStartTime  (QDateTime start) override;
101
    void                setFlightDuration   (int seconds) override;
Gus Grubba's avatar
Gus Grubba committed
102
    void                loadFlightList      (QDateTime startTime, QDateTime endTime) override;
103
    void                setFlightStartsNow  (bool now) override;
104
    void                endFlight           (QString flightID) override;
Gus Grubba's avatar
Gus Grubba committed
105 106

signals:
107
    void            error                   (const QString& what, const QString& airmapdMessage, const QString& airmapdDetails);
Gus Grubba's avatar
Gus Grubba committed
108
    void            flightIDChanged         (QString flightID);
Gus Grubba's avatar
Gus Grubba committed
109 110

private slots:
111 112
    void _pollBriefing                      ();
    void _missionChanged                    ();
113
    void _endFlight                         ();
114
    void _uploadFlightPlan                  ();
115
    void _updateFlightPlanOnTimer           ();
Gus Grubba's avatar
Gus Grubba committed
116 117 118
    void _loadFlightList                    ();

private:
119 120
    void _createFlightPlan                  ();
    bool _collectFlightDtata                ();
121 122
    void _updateFlightPlan                  (bool interactive = false);
    bool _findBriefFeature                  (const QString& name);
Gus Grubba's avatar
Gus Grubba committed
123
    void _updateFlightStartEndTime          (airmap::DateTime& start_time, airmap::DateTime& end_time);
124
    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
125 126 127 128 129 130

private:
    enum class State {
        Idle,
        GetPilotID,
        FlightUpload,
131
        FlightUpdate,
132
        FlightEnd,
133
        FlightSubmit,
Gus Grubba's avatar
Gus Grubba committed
134
        FlightPolling,
Gus Grubba's avatar
Gus Grubba committed
135
        LoadFlightList,
Gus Grubba's avatar
Gus Grubba committed
136 137 138
    };

    struct Flight {
139
        QGCGeoBoundingCube bc;
Gus Grubba's avatar
Gus Grubba committed
140 141 142 143
        QList<QGeoCoordinate> coords;
        QGeoCoordinate  takeoffCoord;
        float maxAltitude = 0;
        void reset() {
144
            bc.reset();
Gus Grubba's avatar
Gus Grubba committed
145 146 147 148 149 150 151 152 153
            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
154
    QString                 _flightId;              ///< Current flight ID, not necessarily accepted yet
155
    QString                 _flightToEnd;
156
    PlanMasterController*   _planController = nullptr;
157
    bool                    _valid = false;
Gus Grubba's avatar
Gus Grubba committed
158
    bool                    _loadingFlightList = false;
159
    bool                    _flightStartsNow = false;
160
    QmlObjectListModel      _advisories;
Gus Grubba's avatar
Gus Grubba committed
161
    QmlObjectListModel      _rulesets;
162 163 164 165
    QmlObjectListModel      _rulesViolation;
    QmlObjectListModel      _rulesInfo;
    QmlObjectListModel      _rulesReview;
    QmlObjectListModel      _rulesFollowing;
166
    QmlObjectListModel      _briefFeatures;
Gus Grubba's avatar
Gus Grubba committed
167
    QmlObjectListModel      _authorizations;
Gus Grubba's avatar
Gus Grubba committed
168
    AirspaceFlightModel     _flightList;
Gus Grubba's avatar
Gus Grubba committed
169 170
    QDateTime               _rangeStart;
    QDateTime               _rangeEnd;
171
    airmap::FlightPlan      _flightPlan;
Gus Grubba's avatar
Gus Grubba committed
172
    QDateTime               _flightStartTime;
173
    int                     _flightDuration = 15 * 60;
Gus Grubba's avatar
Gus Grubba committed
174 175

    QList<AirMapRuleFeature*> _importantFeatures;
Gus Grubba's avatar
Gus Grubba committed
176

177
    AirspaceAdvisoryProvider::AdvisoryColor  _airspaceColor;
178
    AirspaceFlightPlanProvider::PermitStatus _flightPermitStatus = AirspaceFlightPlanProvider::PermitNone;
Gus Grubba's avatar
Gus Grubba committed
179 180 181

};