AirMapAdvisoryManager.cc 4.09 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 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.
 *
 ****************************************************************************/

Gus Grubba's avatar
Gus Grubba committed
10
#include "AirMapAdvisoryManager.h"
11
#include "AirspaceRestriction.h"
Gus Grubba's avatar
Gus Grubba committed
12 13
#include "AirMapManager.h"

14 15 16 17
#include <QTimer>

#include "airmap/airspaces.h"

Gus Grubba's avatar
Gus Grubba committed
18 19 20 21
#define ADVISORY_UPDATE_DISTANCE    500     //-- 500m threshold for updates

using namespace airmap;

Gus Grubba's avatar
Gus Grubba committed
22 23 24 25 26 27 28 29
//-----------------------------------------------------------------------------
AirMapAdvisory::AirMapAdvisory(QObject* parent)
    : AirspaceAdvisory(parent)
    , _radius(0.0)
{
}

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
30
AirMapAdvisoryManager::AirMapAdvisoryManager(AirMapSharedState& shared, QObject *parent)
Gus Grubba's avatar
Gus Grubba committed
31 32
    : AirspaceAdvisoryProvider(parent)
    , _valid(false)
33
    , _lastRadius(0.0)
Gus Grubba's avatar
Gus Grubba committed
34 35 36 37
    , _shared(shared)
{
}

Gus Grubba's avatar
Gus Grubba committed
38
//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
39
void
Gus Grubba's avatar
Gus Grubba committed
40
AirMapAdvisoryManager::setROI(const QGeoCoordinate& center, double radiusMeters)
Gus Grubba's avatar
Gus Grubba committed
41 42
{
    //-- If first time or we've moved more than ADVISORY_UPDATE_DISTANCE, ask for updates.
43 44
    if(!_lastRoiCenter.isValid() || _lastRoiCenter.distanceTo(center) > ADVISORY_UPDATE_DISTANCE || _lastRadius != radiusMeters) {
        _lastRadius    = radiusMeters;
Gus Grubba's avatar
Gus Grubba committed
45
        _lastRoiCenter = center;
46
        _requestAdvisories();
Gus Grubba's avatar
Gus Grubba committed
47 48 49
    }
}

Gus Grubba's avatar
Gus Grubba committed
50 51 52 53 54 55 56
//-----------------------------------------------------------------------------
static bool
adv_sort(QObject* a, QObject* b)
{
    AirMapAdvisory* aa = qobject_cast<AirMapAdvisory*>(a);
    AirMapAdvisory* bb = qobject_cast<AirMapAdvisory*>(b);
    if(!aa || !bb) return false;
Gus Grubba's avatar
Gus Grubba committed
57
    return (int)aa->color() > (int)bb->color();
Gus Grubba's avatar
Gus Grubba committed
58 59 60
}

//-----------------------------------------------------------------------------
Gus Grubba's avatar
Gus Grubba committed
61
void
62
AirMapAdvisoryManager::_requestAdvisories()
Gus Grubba's avatar
Gus Grubba committed
63 64 65 66 67 68 69 70 71
{
    qCDebug(AirMapManagerLog) << "Advisories Request";
    if (!_shared.client()) {
        qCDebug(AirMapManagerLog) << "No AirMap client instance. Not updating Advisories";
        _valid = false;
        emit advisoryChanged();
        return;
    }
    _valid = false;
Gus Grubba's avatar
Gus Grubba committed
72
    _airspaces.clearAndDeleteContents();
Gus Grubba's avatar
Gus Grubba committed
73
    Status::GetStatus::Parameters params;
74 75 76
    params.longitude = _lastRoiCenter.longitude();
    params.latitude  = _lastRoiCenter.latitude();
    params.types     = Airspace::Type::all;
Gus Grubba's avatar
Gus Grubba committed
77
    params.weather   = false;
78 79 80 81
    params.buffer    = _lastRadius;
    std::weak_ptr<LifetimeChecker> isAlive(_instance);
    _shared.client()->status().get_status_by_point(params, [this, isAlive](const Status::GetStatus::Result& result) {
        if (!isAlive.lock()) return;
Gus Grubba's avatar
Gus Grubba committed
82
        if (result) {
Gus Grubba's avatar
Gus Grubba committed
83 84 85 86 87 88 89 90 91 92 93
            qCDebug(AirMapManagerLog) << "Successful advisory search. Items:" << result.value().advisories.size();
            _airspaceColor = (AirspaceAdvisoryProvider::AdvisoryColor)(int)result.value().advisory_color;
            const std::vector<Status::Advisory> advisories = result.value().advisories;
            for (const auto& advisory : advisories) {
                AirMapAdvisory* pAdvisory = new AirMapAdvisory(this);
                pAdvisory->_id          = QString::fromStdString(advisory.airspace.id());
                pAdvisory->_name        = QString::fromStdString(advisory.airspace.name());
                pAdvisory->_type        = (AirspaceAdvisory::AdvisoryType)(int)advisory.airspace.type();
                pAdvisory->_color       = (AirspaceAdvisoryProvider::AdvisoryColor)(int)advisory.color;
                _airspaces.append(pAdvisory);
                qCDebug(AirMapManagerLog) << "Adding advisory" << pAdvisory->name();
Gus Grubba's avatar
Gus Grubba committed
94
            }
Gus Grubba's avatar
Gus Grubba committed
95
            //-- Sort in order of color (priority)
96
            _airspaces.beginReset();
Gus Grubba's avatar
Gus Grubba committed
97
            std::sort(_airspaces.objectList()->begin(), _airspaces.objectList()->end(), adv_sort);
98
            _airspaces.endReset();
Gus Grubba's avatar
Gus Grubba committed
99
            _valid = true;
Gus Grubba's avatar
Gus Grubba committed
100 101 102 103 104 105
        } else {
            qCDebug(AirMapManagerLog) << "Advisories Request Failed";
        }
        emit advisoryChanged();
    });
}