refs #296 implemented airportsInRange on XBus side

This commit is contained in:
Mathew Sutcliffe
2014-07-14 23:31:16 +01:00
parent db6c6a6331
commit 527fa8a492
8 changed files with 343 additions and 1 deletions

View File

@@ -7,6 +7,7 @@
#include "plugin.h"
#include "utils.h"
#include "traffic.h"
#include "service.h"
#include <XPLM/XPLMPlanes.h>
#if ! defined(XPLM210)
@@ -32,9 +33,13 @@ PLUGIN_API void XPluginStop()
PLUGIN_API int XPluginEnable()
{
qRegisterMetaType<QDoubleList>();
qDBusRegisterMetaType<QDoubleList>();
QXPlaneMessageHandler::install();
g_qApp = QSharedApplication::sharedInstance();
QXPlaneEventLoop::exec();
g_plugin = new XBus::CPlugin;
return 1;
}
@@ -49,12 +54,19 @@ PLUGIN_API void XPluginReceiveMessage(XPLMPluginID from, long msg, void *param)
{
if (from == XPLM_PLUGIN_XPLANE)
{
if (msg == XPLM_MSG_PLANE_LOADED || msg == XPLM_MSG_LIVERY_LOADED)
switch (msg)
{
case XPLM_MSG_PLANE_LOADED:
case XPLM_MSG_LIVERY_LOADED:
if (reinterpret_cast<intptr_t>(param) == XPLM_USER_AIRCRAFT)
{
g_plugin->onAircraftModelChanged();
}
break;
case XPLM_MSG_AIRPORT_LOADED:
g_plugin->onAircraftRepositioned();
break;
}
}
}

View File

@@ -40,4 +40,12 @@ namespace XBus
}
}
void CPlugin::onAircraftRepositioned()
{
if (m_service)
{
m_service->updateAirportsInRange();
}
}
}

View File

@@ -42,6 +42,9 @@ namespace XBus
//! Called by XPluginReceiveMessage when the model is changed
void onAircraftModelChanged();
//! Called by XPluginReceiveMessage when the aircraft is positioned at an airport
void onAircraftRepositioned();
private:
BlackCore::CDBusServer *m_server = nullptr;
CService *m_service = nullptr;

View File

@@ -6,12 +6,18 @@
#include "service.h"
#include <XPLM/XPLMPlanes.h>
#include <XPLM/XPLMUtilities.h>
#include <QDebug>
#include <QTimer>
namespace XBus
{
CService::CService(QObject *parent) : QObject(parent)
{
m_airportUpdater = new QTimer(this);
m_airportUpdater->start(60000);
connect(m_airportUpdater, &QTimer::timeout, this, &CService::updateAirportsInRange);
updateAirportsInRange();
}
void CService::onAircraftModelChanged()
@@ -68,4 +74,60 @@ namespace XBus
return path;
}
void CService::readAirportsDatabase()
{
auto first = XPLMFindFirstNavAidOfType(xplm_Nav_Airport);
auto last = XPLMFindLastNavAidOfType(xplm_Nav_Airport);
if (first != XPLM_NAV_NOT_FOUND)
{
for (auto i = first; i <= last; ++i)
{
float lat, lon;
char icao[32];
XPLMGetNavAidInfo(i, nullptr, &lat, &lon, nullptr, nullptr, nullptr, icao, nullptr, nullptr);
if (icao[0] != 0)
{
using namespace BlackMisc::Math;
m_airports.insert(CMath::deg2rad(lat), CMath::deg2rad(lon), i);
}
}
}
int total = 0, count = 0, max = 0;
for (auto key : m_airports.keys())
{
qDebug() << "<><><><>" << QString("%1").arg(key, 6, 16, QChar('0')) << m_airports.count(key);
total += m_airports.count(key);
count++;
if (m_airports.count(key) > max) { max = m_airports.count(key); }
}
qDebug() << "<><><><> total" << total;
qDebug() << "<><><><> max" << max;
qDebug() << "<><><><> mean" << (total / count);
}
void CService::updateAirportsInRange()
{
if (m_airports.isEmpty())
{
readAirportsDatabase();
}
using namespace BlackMisc::Math;
QStringList icaos, names;
QDoubleList lats, lons, alts;
for (auto navref : m_airports.inAdjacentTiles(CMath::deg2rad(getLatitude()), CMath::deg2rad(getLongitude())))
{
float lat, lon, alt;
char icao[32], name[256];
XPLMGetNavAidInfo(navref, nullptr, &lat, &lon, &alt, nullptr, nullptr, icao, name, nullptr);
icaos.push_back(icao);
names.push_back(name);
lats.push_back(lat);
lons.push_back(lon);
alts.push_back(alt);
}
qDebug() << alts;
emit airportsInRangeUpdated(icaos, names, lats, lons, alts);
}
}

View File

@@ -8,14 +8,25 @@
//! \file
#define NOMINMAX
#include "datarefs.h"
#include "blackmisc/geodesicgrid.h"
#include <XPLM/XPLMNavigation.h>
#include <QStringList>
#include <QObject>
#include <QList>
class QTimer;
//! \cond PRIVATE
#define XBUS_SERVICE_INTERFACENAME "net.vatsim.xbus.service"
#define XBUS_SERVICE_OBJECTPATH "/xbus"
//! \endcond
//! Typedef needed to use QList<double> as a DBus argument
typedef QList<double> QDoubleList;
Q_DECLARE_METATYPE(QDoubleList);
namespace XBus
{
@@ -52,7 +63,13 @@ namespace XBus
//! Emitted when the model or livery changes.
void aircraftModelChanged(const QString &path, const QString &filename, const QString &livery, const QString &icao);
//! Airports in range updated.
void airportsInRangeUpdated(const QStringList &icaoCodes, const QStringList &names, const QDoubleList &lats, const QDoubleList &lons, const QDoubleList &alts);
public slots:
//! Called by newly connected client to cause airportsInRangeUpdated to be emitted.
void updateAirportsInRange();
//! Get full path to current aircraft model
QString getAircraftModelPath() const;
@@ -153,6 +170,10 @@ namespace XBus
void setTransponderMode(int mode) { m_xpdrMode.set(mode); }
private:
BlackMisc::Geo::CGeodesicGrid<128, XPLMNavRef> m_airports;
QTimer *m_airportUpdater = nullptr;
void readAirportsDatabase();
StringDataRef<xplane::data::sim::aircraft::view::acf_livery_path> m_liveryPath;
StringDataRef<xplane::data::sim::aircraft::view::acf_ICAO> m_icao;
DataRef<xplane::data::sim::flightmodel::position::latitude> m_latitude;