AirMapTrafficMonitor.cc 3.02 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

10
#include "AirMapTrafficMonitor.h"
Gus Grubba's avatar
Gus Grubba committed
11
#include "AirMapManager.h"
12

Gus Grubba's avatar
Gus Grubba committed
13 14
using namespace airmap;

15 16 17 18 19 20 21
//-----------------------------------------------------------------------------
AirMapTrafficMonitor::AirMapTrafficMonitor(AirMapSharedState& shared)
    : _shared(shared)
{
}

//-----------------------------------------------------------------------------
22 23 24 25 26
AirMapTrafficMonitor::~AirMapTrafficMonitor()
{
    stop();
}

27
//-----------------------------------------------------------------------------
28 29 30
void
AirMapTrafficMonitor::startConnection(const QString& flightID)
{
Gus Grubba's avatar
Gus Grubba committed
31 32 33
    if(flightID.isEmpty() || _flightID == flightID) {
        return;
    }
34
    _flightID = flightID;
Gus Grubba's avatar
Gus Grubba committed
35
    qCDebug(AirMapManagerLog) << "Traffic update started for" << flightID;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    std::weak_ptr<LifetimeChecker> isAlive(_instance);
    auto handler = [this, isAlive](const Traffic::Monitor::Result& result) {
        if (!isAlive.lock()) return;
        if (result) {
            _monitor = result.value();
            _subscriber = std::make_shared<Traffic::Monitor::FunctionalSubscriber>(
                    std::bind(&AirMapTrafficMonitor::_update, this, std::placeholders::_1,  std::placeholders::_2));
            _monitor->subscribe(_subscriber);
        } else {
            QString description = QString::fromStdString(result.error().description() ? result.error().description().get() : "");
            emit error("Failed to start Traffic Monitoring",
                    QString::fromStdString(result.error().message()), description);
        }
    };
    Traffic::Monitor::Params params{flightID.toStdString(), _shared.loginToken().toStdString()};
    _shared.client()->traffic().monitor(params, handler);
}

54
//-----------------------------------------------------------------------------
55 56 57 58 59 60 61 62 63
void
AirMapTrafficMonitor::_update(Traffic::Update::Type type, const std::vector<Traffic::Update>& update)
{
    qCDebug(AirMapManagerLog) << "Traffic update with" << update.size() << "elements";
    if (type != Traffic::Update::Type::situational_awareness)
        return; // currently we're only interested in situational awareness
    for (const auto& traffic : update) {
        QString traffic_id = QString::fromStdString(traffic.id);
        QString vehicle_id = QString::fromStdString(traffic.aircraft_id);
Gus Grubba's avatar
Gus Grubba committed
64
        emit trafficUpdate(type == Traffic::Update::Type::alert, traffic_id, vehicle_id,
Gus Grubba's avatar
Gus Grubba committed
65
            QGeoCoordinate(traffic.latitude, traffic.longitude, traffic.altitude), static_cast<float>(traffic.heading));
66 67 68
    }
}

69
//-----------------------------------------------------------------------------
70 71 72 73 74 75 76 77 78 79
void
AirMapTrafficMonitor::stop()
{
    if (_monitor) {
        _monitor->unsubscribe(_subscriber);
        _subscriber.reset();
        _monitor.reset();
    }
}