mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-28 20:25:34 +08:00
refs #650 Implement CAirportDataReader
* Add BlackCore::CAirportDataReader class * Add cache traits for airport list * Add corresponding WebReaderFlag * Add CAirport::convertFromDatabaseJson() * Add CApplication::headerFromNetwork() to handle HTTP HEAD method
This commit is contained in:
114
src/blackcore/airportdatareader.cpp
Normal file
114
src/blackcore/airportdatareader.cpp
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
/* Copyright (C) 2015
|
||||||
|
* swift project Community / Contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "airportdatareader.h"
|
||||||
|
#include "blackcore/application.h"
|
||||||
|
#include "blackmisc/logmessage.h"
|
||||||
|
#include "blackmisc/network/networkutils.h"
|
||||||
|
#include <QNetworkReply>
|
||||||
|
|
||||||
|
using namespace BlackMisc;
|
||||||
|
using namespace BlackMisc::Aviation;
|
||||||
|
using namespace BlackMisc::Network;
|
||||||
|
|
||||||
|
namespace BlackCore
|
||||||
|
{
|
||||||
|
CAirportDataReader::CAirportDataReader(QObject* parent) :
|
||||||
|
CThreadedReader(parent, QStringLiteral("CAirportDataReader"))
|
||||||
|
{
|
||||||
|
// void
|
||||||
|
}
|
||||||
|
|
||||||
|
void CAirportDataReader::readInBackgroundThread()
|
||||||
|
{
|
||||||
|
bool s = QMetaObject::invokeMethod(this, "ps_readAirports");
|
||||||
|
Q_ASSERT_X(s, Q_FUNC_INFO, "Cannot invoke ");
|
||||||
|
Q_UNUSED(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
BlackMisc::Aviation::CAirportList CAirportDataReader::getAirports() const
|
||||||
|
{
|
||||||
|
return m_airportCache.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
CUrl CAirportDataReader::getAirportsUrl() const
|
||||||
|
{
|
||||||
|
const CUrl url(sApp->getGlobalSetup().getSwiftAirportUrls().getRandomWorkingUrl());
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CAirportDataReader::ps_parseAirportData(QNetworkReply *nwReply)
|
||||||
|
{
|
||||||
|
QJsonParseError error;
|
||||||
|
QByteArray data = nwReply->readAll();
|
||||||
|
QJsonDocument document = QJsonDocument::fromJson(data, &error);
|
||||||
|
if (error.error != QJsonParseError::NoError)
|
||||||
|
{
|
||||||
|
CLogMessage(this).error("Error parsing airport list from JSON (%1)") << error.errorString();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray array = document.array();
|
||||||
|
if (array.isEmpty())
|
||||||
|
{
|
||||||
|
CLogMessage(this).error("Error parsing airport list from JSON (document is not an array)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CAirportList airports = CAirportList::fromDatabaseJson(array);
|
||||||
|
quint64 timestamp = lastModifiedMsSinceEpoch(nwReply);
|
||||||
|
|
||||||
|
{
|
||||||
|
QWriteLocker wl(&this->m_lock);
|
||||||
|
m_airportCache.set(airports, timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFinished, airports.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
void CAirportDataReader::ps_parseAirportHeader(QNetworkReply *nwReply)
|
||||||
|
{
|
||||||
|
this->threadAssertCheck();
|
||||||
|
m_lastModified = lastModifiedMsSinceEpoch(nwReply);
|
||||||
|
ps_readAirports();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CAirportDataReader::ps_readAirports()
|
||||||
|
{
|
||||||
|
this->threadAssertCheck();
|
||||||
|
Q_ASSERT_X(sApp, Q_FUNC_INFO, "No Application");
|
||||||
|
|
||||||
|
CFailoverUrlList urls(sApp->getGlobalSetup().getSwiftAirportUrls());
|
||||||
|
const CUrl url(urls.obtainNextWorkingUrl(true));
|
||||||
|
if (url.isEmpty()) { return; }
|
||||||
|
|
||||||
|
if (0 == m_lastModified) {
|
||||||
|
sApp->headerFromNetwork(url, { this, &CAirportDataReader::ps_parseAirportHeader });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_airportCache.synchronize();
|
||||||
|
|
||||||
|
if (m_airportCache.get().size() > 0 &&
|
||||||
|
m_airportCache.getAvailableTimestamp().toMSecsSinceEpoch() >= static_cast<qint64>(m_lastModified)) // cache is up-to-date
|
||||||
|
{
|
||||||
|
CLogMessage(this).info("Loaded %1 airports from cache") << m_airportCache.get().size();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sApp->getFromNetwork(url, { this, &CAirportDataReader::ps_parseAirportData });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CAirportDataReader::ps_airportCacheChanged()
|
||||||
|
{
|
||||||
|
// void
|
||||||
|
}
|
||||||
|
|
||||||
|
} // ns
|
||||||
71
src/blackcore/airportdatareader.h
Normal file
71
src/blackcore/airportdatareader.h
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
/* Copyright (C) 2015
|
||||||
|
* swift project Community / Contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
|
||||||
|
#ifndef BLACKCORE_AIRPORTDATAREADER_H
|
||||||
|
#define BLACKCORE_AIRPORTDATAREADER_H
|
||||||
|
|
||||||
|
#include "blackcore/blackcoreexport.h"
|
||||||
|
#include "blackcore/threadedreader.h"
|
||||||
|
#include "blackcore/data/dbcaches.h"
|
||||||
|
#include "blackmisc/aviation/airportlist.h"
|
||||||
|
#include "blackmisc/network/entityflags.h"
|
||||||
|
#include <QNetworkAccessManager>
|
||||||
|
|
||||||
|
namespace BlackCore
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Reader for airport database.
|
||||||
|
*/
|
||||||
|
class BLACKCORE_EXPORT CAirportDataReader : public CThreadedReader
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
signals:
|
||||||
|
//! Emitted when data is parsed
|
||||||
|
void dataRead(BlackMisc::Network::CEntityFlags::Entity entity, BlackMisc::Network::CEntityFlags::ReadState state, int number);
|
||||||
|
|
||||||
|
public:
|
||||||
|
//! Constructor
|
||||||
|
CAirportDataReader(QObject* parent);
|
||||||
|
|
||||||
|
//! Read airports
|
||||||
|
void readInBackgroundThread();
|
||||||
|
|
||||||
|
//! Returns a list of all airports in the database.
|
||||||
|
//! \threadsafe
|
||||||
|
BlackMisc::Aviation::CAirportList getAirports() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
//! URL for airport list
|
||||||
|
BlackMisc::Network::CUrl getAirportsUrl() const;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
//! Parse downloaded JSON file
|
||||||
|
void ps_parseAirportData(QNetworkReply *nwReply);
|
||||||
|
|
||||||
|
//! Read Last-Modified header
|
||||||
|
void ps_parseAirportHeader(QNetworkReply *nwReply);
|
||||||
|
|
||||||
|
//! Read airports
|
||||||
|
void ps_readAirports();
|
||||||
|
|
||||||
|
//! Airport cache changed
|
||||||
|
void ps_airportCacheChanged();
|
||||||
|
|
||||||
|
private:
|
||||||
|
BlackMisc::CData<BlackCore::Data::DbAirportCache> m_airportCache {this, &CAirportDataReader::ps_airportCacheChanged};
|
||||||
|
mutable QReadWriteLock m_lock;
|
||||||
|
quint64 m_lastModified = 0; //!< When was data file updated, obtained from HTTP Last-Modified header, in ms from epoch
|
||||||
|
|
||||||
|
};
|
||||||
|
} // ns
|
||||||
|
|
||||||
|
#endif // guard
|
||||||
@@ -471,6 +471,35 @@ namespace BlackCore
|
|||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QNetworkReply *CApplication::headerFromNetwork(const CUrl &url, const BlackMisc::CSlot<void (QNetworkReply *)> &callback)
|
||||||
|
{
|
||||||
|
if (this->m_shutdown) { return nullptr; }
|
||||||
|
return headerFromNetwork(url.toNetworkRequest(), callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
QNetworkReply *CApplication::headerFromNetwork(const QNetworkRequest &request, const BlackMisc::CSlot<void (QNetworkReply *)> &callback)
|
||||||
|
{
|
||||||
|
if (this->m_shutdown) { return nullptr; }
|
||||||
|
QWriteLocker locker(&m_accessManagerLock);
|
||||||
|
Q_ASSERT_X(QCoreApplication::instance()->thread() == m_accessManager.thread(), Q_FUNC_INFO, "Network manager supposed to be in main thread");
|
||||||
|
if (QThread::currentThread() != this->m_accessManager.thread())
|
||||||
|
{
|
||||||
|
QTimer::singleShot(0, this, [this, request, callback]() { this->headerFromNetwork(request, callback); });
|
||||||
|
return nullptr; // not yet started
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_ASSERT_X(QThread::currentThread() == m_accessManager.thread(), Q_FUNC_INFO, "Network manager thread mismatch");
|
||||||
|
QNetworkRequest r(request); // no QObject
|
||||||
|
CNetworkUtils::ignoreSslVerification(r);
|
||||||
|
CNetworkUtils::setSwiftUserAgent(r);
|
||||||
|
QNetworkReply *reply = this->m_accessManager.head(r);
|
||||||
|
if (callback)
|
||||||
|
{
|
||||||
|
connect(reply, &QNetworkReply::finished, callback.object(), [ = ] { callback(reply); }, Qt::QueuedConnection);
|
||||||
|
}
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
|
||||||
void CApplication::deleteAllCookies()
|
void CApplication::deleteAllCookies()
|
||||||
{
|
{
|
||||||
this->m_cookieManager.deleteAllCookies();
|
this->m_cookieManager.deleteAllCookies();
|
||||||
|
|||||||
@@ -325,6 +325,16 @@ namespace BlackCore
|
|||||||
QNetworkReply *postToNetwork(const QNetworkRequest &request, QHttpMultiPart *multiPart,
|
QNetworkReply *postToNetwork(const QNetworkRequest &request, QHttpMultiPart *multiPart,
|
||||||
const BlackMisc::CSlot<void(QNetworkReply *)> &callback);
|
const BlackMisc::CSlot<void(QNetworkReply *)> &callback);
|
||||||
|
|
||||||
|
//! Request to get network repy using HTTP's HEADER method
|
||||||
|
//! \threadsafe
|
||||||
|
QNetworkReply *headerFromNetwork(const BlackMisc::Network::CUrl &url,
|
||||||
|
const BlackMisc::CSlot<void(QNetworkReply *)> &callback);
|
||||||
|
|
||||||
|
//! Request to get network repy using HTTP's HEADER method
|
||||||
|
//! \threadsafe
|
||||||
|
QNetworkReply *headerFromNetwork(const QNetworkRequest &request,
|
||||||
|
const BlackMisc::CSlot<void(QNetworkReply *)> &callback);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
//! Setup available (cache, web load, ..)
|
//! Setup available (cache, web load, ..)
|
||||||
void setupAvailable(bool success);
|
void setupAvailable(bool success);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
#include "blackmisc/simulation/distributorlist.h"
|
#include "blackmisc/simulation/distributorlist.h"
|
||||||
#include "blackmisc/aviation/airlineicaocodelist.h"
|
#include "blackmisc/aviation/airlineicaocodelist.h"
|
||||||
#include "blackmisc/aviation/aircrafticaocodelist.h"
|
#include "blackmisc/aviation/aircrafticaocodelist.h"
|
||||||
|
#include "blackmisc/aviation/airportlist.h"
|
||||||
#include "blackmisc/aviation/liverylist.h"
|
#include "blackmisc/aviation/liverylist.h"
|
||||||
#include "blackmisc/network/url.h"
|
#include "blackmisc/network/url.h"
|
||||||
#include "blackmisc/countrylist.h"
|
#include "blackmisc/countrylist.h"
|
||||||
@@ -108,6 +109,16 @@ namespace BlackCore
|
|||||||
//! Key in data cache
|
//! Key in data cache
|
||||||
static const char *key() { return "dbmodelreaderurl"; }
|
static const char *key() { return "dbmodelreaderurl"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//! Trait for airport list
|
||||||
|
struct DbAirportCache : public BlackMisc::CDataTrait<BlackMisc::Aviation::CAirportList>
|
||||||
|
{
|
||||||
|
//! Defer loading
|
||||||
|
static constexpr bool isDeferred() { return true; }
|
||||||
|
|
||||||
|
//! Key in data cache
|
||||||
|
static const char *key() { return "dbairportcache"; }
|
||||||
|
};
|
||||||
} // ns
|
} // ns
|
||||||
} // ns
|
} // ns
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
* contained in the LICENSE file.
|
* contained in the LICENSE file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "blackcore/airportdatareader.h"
|
||||||
#include "blackcore/application.h"
|
#include "blackcore/application.h"
|
||||||
#include "blackcore/data/globalsetup.h"
|
#include "blackcore/data/globalsetup.h"
|
||||||
#include "blackcore/db/infodatareader.h"
|
#include "blackcore/db/infodatareader.h"
|
||||||
@@ -207,6 +208,15 @@ namespace BlackCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_airportDataReader)
|
||||||
|
{
|
||||||
|
if (whatToRead.testFlag(CEntityFlags::AirportEntity))
|
||||||
|
{
|
||||||
|
m_airportDataReader->readInBackgroundThread();
|
||||||
|
triggeredRead |= CEntityFlags::AirportEntity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (m_icaoDataReader)
|
if (m_icaoDataReader)
|
||||||
{
|
{
|
||||||
if (whatToRead.testFlag(CEntityFlags::AircraftIcaoEntity) || whatToRead.testFlag(CEntityFlags::AirlineIcaoEntity) || whatToRead.testFlag(CEntityFlags::CountryEntity))
|
if (whatToRead.testFlag(CEntityFlags::AircraftIcaoEntity) || whatToRead.testFlag(CEntityFlags::AirlineIcaoEntity) || whatToRead.testFlag(CEntityFlags::CountryEntity))
|
||||||
@@ -226,6 +236,7 @@ namespace BlackCore
|
|||||||
triggeredRead |= modelEntities;
|
triggeredRead |= modelEntities;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return triggeredRead;
|
return triggeredRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -524,6 +535,7 @@ namespace BlackCore
|
|||||||
if (this->m_vatsimDataFileReader) { this->m_vatsimDataFileReader->gracefulShutdown(); }
|
if (this->m_vatsimDataFileReader) { this->m_vatsimDataFileReader->gracefulShutdown(); }
|
||||||
if (this->m_vatsimStatusReader) { this->m_vatsimStatusReader->gracefulShutdown(); }
|
if (this->m_vatsimStatusReader) { this->m_vatsimStatusReader->gracefulShutdown(); }
|
||||||
if (this->m_modelDataReader) { this->m_modelDataReader->gracefulShutdown(); }
|
if (this->m_modelDataReader) { this->m_modelDataReader->gracefulShutdown(); }
|
||||||
|
if (this->m_airportDataReader) { this->m_airportDataReader->gracefulShutdown(); }
|
||||||
if (this->m_icaoDataReader) { this->m_icaoDataReader->gracefulShutdown(); }
|
if (this->m_icaoDataReader) { this->m_icaoDataReader->gracefulShutdown(); }
|
||||||
if (this->m_infoDataReader) { this->m_infoDataReader->gracefulShutdown(); }
|
if (this->m_infoDataReader) { this->m_infoDataReader->gracefulShutdown(); }
|
||||||
if (this->m_databaseWriter) { this->m_databaseWriter->gracefulShutdown(); }
|
if (this->m_databaseWriter) { this->m_databaseWriter->gracefulShutdown(); }
|
||||||
@@ -637,6 +649,16 @@ namespace BlackCore
|
|||||||
Q_ASSERT_X(c, Q_FUNC_INFO, "connect failed models");
|
Q_ASSERT_X(c, Q_FUNC_INFO, "connect failed models");
|
||||||
this->m_modelDataReader->start(QThread::LowPriority);
|
this->m_modelDataReader->start(QThread::LowPriority);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 6. Airport list reader
|
||||||
|
if (flags.testFlag(CWebReaderFlags::WebReaderFlag::AirportReader))
|
||||||
|
{
|
||||||
|
this->m_airportDataReader = new CAirportDataReader(this);
|
||||||
|
bool c = connect(this->m_airportDataReader, &CAirportDataReader::dataRead, this, &CWebDataServices::ps_readFromAirportDb);
|
||||||
|
Q_ASSERT_X(c, Q_FUNC_INFO, "Airport reader signals");
|
||||||
|
Q_UNUSED(c);
|
||||||
|
this->m_airportDataReader->start(QThread::LowPriority);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CDatabaseReader *CWebDataServices::getDbReader(CEntityFlags::Entity entity) const
|
CDatabaseReader *CWebDataServices::getDbReader(CEntityFlags::Entity entity) const
|
||||||
@@ -704,6 +726,11 @@ namespace BlackCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CWebDataServices::ps_readFromAirportDb(CEntityFlags::Entity entity, CEntityFlags::ReadState state, int number)
|
||||||
|
{
|
||||||
|
CLogMessage(this).info("Read data %1 entries: %2 state: %3") << CEntityFlags::flagToString(entity) << number << CEntityFlags::flagToString(state);
|
||||||
|
}
|
||||||
|
|
||||||
void CWebDataServices::ps_setupChanged()
|
void CWebDataServices::ps_setupChanged()
|
||||||
{
|
{
|
||||||
// void
|
// void
|
||||||
|
|||||||
@@ -76,6 +76,8 @@ namespace BlackCore
|
|||||||
class CInfoDataReader;
|
class CInfoDataReader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CAirportDataReader;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Encapsulates reading data from web sources
|
* Encapsulates reading data from web sources
|
||||||
*/
|
*/
|
||||||
@@ -343,6 +345,9 @@ namespace BlackCore
|
|||||||
//! Read finished from reader
|
//! Read finished from reader
|
||||||
void ps_readFromSwiftDb(BlackMisc::Network::CEntityFlags::Entity entity, BlackMisc::Network::CEntityFlags::ReadState state, int number);
|
void ps_readFromSwiftDb(BlackMisc::Network::CEntityFlags::Entity entity, BlackMisc::Network::CEntityFlags::ReadState state, int number);
|
||||||
|
|
||||||
|
//! Read from airport data reader
|
||||||
|
void ps_readFromAirportDb(BlackMisc::Network::CEntityFlags::Entity entity, BlackMisc::Network::CEntityFlags::ReadState state, int number);
|
||||||
|
|
||||||
//! Setup changed
|
//! Setup changed
|
||||||
void ps_setupChanged();
|
void ps_setupChanged();
|
||||||
|
|
||||||
@@ -371,6 +376,7 @@ namespace BlackCore
|
|||||||
Db::CIcaoDataReader *m_icaoDataReader = nullptr;
|
Db::CIcaoDataReader *m_icaoDataReader = nullptr;
|
||||||
Db::CModelDataReader *m_modelDataReader = nullptr;
|
Db::CModelDataReader *m_modelDataReader = nullptr;
|
||||||
Db::CInfoDataReader *m_infoDataReader = nullptr;
|
Db::CInfoDataReader *m_infoDataReader = nullptr;
|
||||||
|
CAirportDataReader *m_airportDataReader = nullptr;
|
||||||
|
|
||||||
// writing objects directly into DB
|
// writing objects directly into DB
|
||||||
Db::CDatabaseWriter *m_databaseWriter = nullptr;
|
Db::CDatabaseWriter *m_databaseWriter = nullptr;
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ namespace BlackCore
|
|||||||
CEntityFlags::Entity entities = CEntityFlags::NoEntity;
|
CEntityFlags::Entity entities = CEntityFlags::NoEntity;
|
||||||
if (readers.testFlag(IcaoDataReader)) { entities |= CEntityFlags::AllIcaoAndCountries; }
|
if (readers.testFlag(IcaoDataReader)) { entities |= CEntityFlags::AllIcaoAndCountries; }
|
||||||
if (readers.testFlag(ModelReader)) { entities |= CEntityFlags::DistributorLiveryModel; }
|
if (readers.testFlag(ModelReader)) { entities |= CEntityFlags::DistributorLiveryModel; }
|
||||||
|
if (readers.testFlag(AirportReader)) { entities |= CEntityFlags::AirportEntity; }
|
||||||
if (readers.testFlag(InfoDataReader)) { entities |= CEntityFlags::InfoObjectEntity; }
|
if (readers.testFlag(InfoDataReader)) { entities |= CEntityFlags::InfoObjectEntity; }
|
||||||
if (readers.testFlag(VatsimBookingReader)) { entities |= CEntityFlags::BookingEntity; }
|
if (readers.testFlag(VatsimBookingReader)) { entities |= CEntityFlags::BookingEntity; }
|
||||||
if (readers.testFlag(VatsimMetarReader)) { entities |= CEntityFlags::MetarEntity; }
|
if (readers.testFlag(VatsimMetarReader)) { entities |= CEntityFlags::MetarEntity; }
|
||||||
|
|||||||
@@ -36,9 +36,10 @@ namespace BlackCore
|
|||||||
VatsimStatusReader = 1 << 3, //!< reader for VATSIM status file
|
VatsimStatusReader = 1 << 3, //!< reader for VATSIM status file
|
||||||
IcaoDataReader = 1 << 4, //!< reader for ICAO data
|
IcaoDataReader = 1 << 4, //!< reader for ICAO data
|
||||||
ModelReader = 1 << 5, //!< reader for model data such as liveries, models, etc
|
ModelReader = 1 << 5, //!< reader for model data such as liveries, models, etc
|
||||||
InfoDataReader = 1 << 6, //!< DB info data (metdata, how many data, when updated)
|
AirportReader = 1 << 6, //!< reader for airport list
|
||||||
|
InfoDataReader = 1 << 7, //!< DB info data (metdata, how many data, when updated)
|
||||||
AllVatsimReaders = VatsimBookingReader | VatsimDataReader | VatsimMetarReader | VatsimStatusReader, //!< all VATSIM readers
|
AllVatsimReaders = VatsimBookingReader | VatsimDataReader | VatsimMetarReader | VatsimStatusReader, //!< all VATSIM readers
|
||||||
AllSwiftDbReaders = IcaoDataReader | ModelReader | InfoDataReader, //!< all swift data
|
AllSwiftDbReaders = IcaoDataReader | ModelReader | AirportReader | InfoDataReader, //!< all swift data
|
||||||
AllReaders = AllSwiftDbReaders | AllVatsimReaders //!< everything
|
AllReaders = AllSwiftDbReaders | AllVatsimReaders //!< everything
|
||||||
};
|
};
|
||||||
Q_DECLARE_FLAGS(WebReader, WebReaderFlag)
|
Q_DECLARE_FLAGS(WebReader, WebReaderFlag)
|
||||||
|
|||||||
@@ -52,6 +52,26 @@ namespace BlackMisc
|
|||||||
(void)QT_TRANSLATE_NOOP("Aviation", "Airport");
|
(void)QT_TRANSLATE_NOOP("Aviation", "Airport");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CAirport::fromDatabaseJson(const QJsonObject &json)
|
||||||
|
{
|
||||||
|
Q_ASSERT(json.value("icao").isString());
|
||||||
|
setIcao(json.value("icao").toString());
|
||||||
|
|
||||||
|
Q_ASSERT(json.value("country").isString());
|
||||||
|
setCountry(json.value("country").toString());
|
||||||
|
|
||||||
|
Q_ASSERT(json.value("name").isString());
|
||||||
|
setDescriptiveName(json.value("name").toString());
|
||||||
|
|
||||||
|
Q_ASSERT(json.value("altitude").isDouble());
|
||||||
|
setElevation(CLength(json.value("altitude").toInt(), CLengthUnit::ft()));
|
||||||
|
|
||||||
|
Q_ASSERT(json.value("latitude").isDouble());
|
||||||
|
Q_ASSERT(json.value("longitude").isDouble());
|
||||||
|
CCoordinateGeodetic pos(json.value("latitude").toDouble(), json.value("longitude").toDouble(), 0);
|
||||||
|
setPosition(pos);
|
||||||
|
}
|
||||||
|
|
||||||
CVariant CAirport::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
|
CVariant CAirport::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
|
||||||
{
|
{
|
||||||
if (index.isMyself()) { return CVariant::from(*this); }
|
if (index.isMyself()) { return CVariant::from(*this); }
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ namespace BlackMisc
|
|||||||
IndexIcao = BlackMisc::CPropertyIndex::GlobalIndexCAirport,
|
IndexIcao = BlackMisc::CPropertyIndex::GlobalIndexCAirport,
|
||||||
IndexDescriptiveName,
|
IndexDescriptiveName,
|
||||||
IndexPosition,
|
IndexPosition,
|
||||||
IndexElevation
|
IndexCountry,
|
||||||
|
IndexElevation,
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Default constructor.
|
//! Default constructor.
|
||||||
@@ -78,6 +79,12 @@ namespace BlackMisc
|
|||||||
//! Set position
|
//! Set position
|
||||||
void setPosition(const BlackMisc::Geo::CCoordinateGeodetic &position) { this->m_position = position; }
|
void setPosition(const BlackMisc::Geo::CCoordinateGeodetic &position) { this->m_position = position; }
|
||||||
|
|
||||||
|
//! Get the country
|
||||||
|
QString getCountry() const { return m_country; }
|
||||||
|
|
||||||
|
//! Set the country
|
||||||
|
void setCountry(const QString &country) { this->m_country = country; }
|
||||||
|
|
||||||
//! Elevation
|
//! Elevation
|
||||||
//! \sa geodeticHeight
|
//! \sa geodeticHeight
|
||||||
const BlackMisc::PhysicalQuantities::CLength getElevation() const { return this->geodeticHeight(); }
|
const BlackMisc::PhysicalQuantities::CLength getElevation() const { return this->geodeticHeight(); }
|
||||||
@@ -123,16 +130,21 @@ namespace BlackMisc
|
|||||||
//! \copydoc BlackMisc::Mixin::String::toQString
|
//! \copydoc BlackMisc::Mixin::String::toQString
|
||||||
QString convertToQString(bool i18n = false) const;
|
QString convertToQString(bool i18n = false) const;
|
||||||
|
|
||||||
|
//! \copydoc BlackMisc::CValueObject::convertFromJson
|
||||||
|
void fromDatabaseJson(const QJsonObject &json);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CAirportIcaoCode m_icao;
|
CAirportIcaoCode m_icao;
|
||||||
QString m_descriptiveName;
|
QString m_descriptiveName;
|
||||||
BlackMisc::Geo::CCoordinateGeodetic m_position;
|
BlackMisc::Geo::CCoordinateGeodetic m_position;
|
||||||
|
QString m_country;
|
||||||
|
|
||||||
BLACK_METACLASS(
|
BLACK_METACLASS(
|
||||||
CAirport,
|
CAirport,
|
||||||
BLACK_METAMEMBER(icao),
|
BLACK_METAMEMBER(icao),
|
||||||
BLACK_METAMEMBER(descriptiveName),
|
BLACK_METAMEMBER(descriptiveName),
|
||||||
BLACK_METAMEMBER(position),
|
BLACK_METAMEMBER(position),
|
||||||
|
BLACK_METAMEMBER(country),
|
||||||
BLACK_METAMEMBER(relativeDistance),
|
BLACK_METAMEMBER(relativeDistance),
|
||||||
BLACK_METAMEMBER(relativeBearing)
|
BLACK_METAMEMBER(relativeBearing)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -44,5 +44,19 @@ namespace BlackMisc
|
|||||||
return this->findFirstByOrDefault(&CAirport::getIcao, icao, ifNotFound);
|
return this->findFirstByOrDefault(&CAirport::getIcao, icao, ifNotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CAirportList CAirportList::fromDatabaseJson(const QJsonArray &json)
|
||||||
|
{
|
||||||
|
CAirportList airports;
|
||||||
|
for (const QJsonValue& value: json)
|
||||||
|
{
|
||||||
|
QJsonObject object = value.toObject();
|
||||||
|
CAirport airport;
|
||||||
|
airport.fromDatabaseJson(object);
|
||||||
|
airports.push_back(airport);
|
||||||
|
}
|
||||||
|
|
||||||
|
return airports;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -49,6 +49,9 @@ namespace BlackMisc
|
|||||||
|
|
||||||
//! Find first station by callsign, if not return given value / default
|
//! Find first station by callsign, if not return given value / default
|
||||||
CAirport findFirstByIcao(const CAirportIcaoCode &icao, const CAirport &ifNotFound = CAirport()) const;
|
CAirport findFirstByIcao(const CAirportIcaoCode &icao, const CAirport &ifNotFound = CAirport()) const;
|
||||||
|
|
||||||
|
//! Reads the airport list from JSON
|
||||||
|
static CAirportList fromDatabaseJson(const QJsonArray& json);
|
||||||
};
|
};
|
||||||
} //namespace
|
} //namespace
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ namespace BlackMisc
|
|||||||
case NoEntity: return "no data";
|
case NoEntity: return "no data";
|
||||||
case VatsimDataFile: return "VATSIM data file";
|
case VatsimDataFile: return "VATSIM data file";
|
||||||
case VatsimStatusFile: return "VATSIM status file";
|
case VatsimStatusFile: return "VATSIM status file";
|
||||||
|
case AirportEntity: return "Airport";
|
||||||
default:
|
default:
|
||||||
BLACK_VERIFY_X(false, Q_FUNC_INFO, "wrong flags");
|
BLACK_VERIFY_X(false, Q_FUNC_INFO, "wrong flags");
|
||||||
return "wrong flags";
|
return "wrong flags";
|
||||||
@@ -56,6 +57,7 @@ namespace BlackMisc
|
|||||||
if (flag.testFlag(NoEntity)) list << "no data";
|
if (flag.testFlag(NoEntity)) list << "no data";
|
||||||
if (flag.testFlag(VatsimDataFile)) list << "VATSIM data file";
|
if (flag.testFlag(VatsimDataFile)) list << "VATSIM data file";
|
||||||
if (flag.testFlag(VatsimStatusFile)) list << "VATSIM status file";
|
if (flag.testFlag(VatsimStatusFile)) list << "VATSIM status file";
|
||||||
|
if (flag.testFlag(AirportEntity)) list << "Airport";
|
||||||
return list.join(',');
|
return list.join(',');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,12 +44,13 @@ namespace BlackMisc
|
|||||||
MetarEntity = 1 << 8, //!< METAR
|
MetarEntity = 1 << 8, //!< METAR
|
||||||
VatsimDataFile = 1 << 9, //!< the VATSIM data file (multiple data entities)
|
VatsimDataFile = 1 << 9, //!< the VATSIM data file (multiple data entities)
|
||||||
VatsimStatusFile = 1 << 10, //!< the VATSIM status file (URLs for data files etc.)
|
VatsimStatusFile = 1 << 10, //!< the VATSIM status file (URLs for data files etc.)
|
||||||
AllEntities = ((1 << 11) - 1), //!< everything
|
AirportEntity = 1 << 11, //!< airports
|
||||||
|
AllEntities = ((1 << 12) - 1), //!< everything
|
||||||
AllIcaoEntities = AircraftIcaoEntity | AirlineIcaoEntity, //!< all ICAO codes
|
AllIcaoEntities = AircraftIcaoEntity | AirlineIcaoEntity, //!< all ICAO codes
|
||||||
AllIcaoAndCountries = AircraftIcaoEntity | AirlineIcaoEntity | CountryEntity, //!< all ICAO codes and countries
|
AllIcaoAndCountries = AircraftIcaoEntity | AirlineIcaoEntity | CountryEntity, //!< all ICAO codes and countries
|
||||||
DistributorLiveryModel = DistributorEntity | LiveryEntity | ModelEntity, //!< Combinded
|
DistributorLiveryModel = DistributorEntity | LiveryEntity | ModelEntity, //!< Combinded
|
||||||
AllDbEntities = AllIcaoAndCountries | DistributorLiveryModel | InfoObjectEntity, //!< All DB stuff
|
AllDbEntities = AllIcaoAndCountries | DistributorLiveryModel | InfoObjectEntity | AirportEntity, //!< All DB stuff
|
||||||
AllDbEntitiesNoInfoObjects = AllIcaoAndCountries | DistributorLiveryModel //!< All DB entities, no info objects
|
AllDbEntitiesNoInfoObjects = AllIcaoAndCountries | DistributorLiveryModel | AirportEntity //!< All DB entities, no info objects
|
||||||
};
|
};
|
||||||
Q_DECLARE_FLAGS(Entity, EntityFlag)
|
Q_DECLARE_FLAGS(Entity, EntityFlag)
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
#include "testreaders.h"
|
#include "testreaders.h"
|
||||||
#include "blackcore/application.h"
|
#include "blackcore/application.h"
|
||||||
|
#include "blackcore/airportdatareader.h"
|
||||||
#include "blackcore/data/globalsetup.h"
|
#include "blackcore/data/globalsetup.h"
|
||||||
#include "blackcore/db/icaodatareader.h"
|
#include "blackcore/db/icaodatareader.h"
|
||||||
#include "blackcore/db/modeldatareader.h"
|
#include "blackcore/db/modeldatareader.h"
|
||||||
@@ -44,12 +45,14 @@ namespace BlackCoreTest
|
|||||||
{
|
{
|
||||||
CTestReaders::CTestReaders(QObject *parent) :
|
CTestReaders::CTestReaders(QObject *parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
|
m_airportReader(new CAirportDataReader(this)),
|
||||||
m_icaoReader(new CIcaoDataReader(this, CDatabaseReaderConfigList::allDirectDbAccess())),
|
m_icaoReader(new CIcaoDataReader(this, CDatabaseReaderConfigList::allDirectDbAccess())),
|
||||||
m_modelReader(new CModelDataReader(this, CDatabaseReaderConfigList::allDirectDbAccess()))
|
m_modelReader(new CModelDataReader(this, CDatabaseReaderConfigList::allDirectDbAccess()))
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
CTestReaders::~CTestReaders()
|
CTestReaders::~CTestReaders()
|
||||||
{
|
{
|
||||||
|
this->m_airportReader->gracefulShutdown();
|
||||||
this->m_icaoReader->gracefulShutdown();
|
this->m_icaoReader->gracefulShutdown();
|
||||||
this->m_modelReader->gracefulShutdown();
|
this->m_modelReader->gracefulShutdown();
|
||||||
}
|
}
|
||||||
@@ -107,6 +110,34 @@ namespace BlackCoreTest
|
|||||||
CApplication::processEventsFor(2500); // make sure events are processed
|
CApplication::processEventsFor(2500); // make sure events are processed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CTestReaders::readAirportData()
|
||||||
|
{
|
||||||
|
using namespace BlackMisc::Geo;
|
||||||
|
using namespace BlackMisc::PhysicalQuantities;
|
||||||
|
|
||||||
|
const CUrl url(sApp->getGlobalSetup().getSwiftAirportUrls().getRandomWorkingUrl());
|
||||||
|
if (!this->pingServer(url)) { return; }
|
||||||
|
m_airportReader->start();
|
||||||
|
m_airportReader->readInBackgroundThread();
|
||||||
|
|
||||||
|
for (int i = 0; i < 120; ++i)
|
||||||
|
{
|
||||||
|
CApplication::processEventsFor(500);
|
||||||
|
if (this->m_airportReader->getAirports().size() > 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVERIFY2(this->m_airportReader->getAirports().size() > 0, "No airports");
|
||||||
|
auto heathrow = this->m_airportReader->getAirports().findByIcao("EGLL");
|
||||||
|
QVERIFY2(heathrow.size() == 1, "No Heathrow");
|
||||||
|
|
||||||
|
auto airports = m_airportReader->getAirports();
|
||||||
|
airports.sortByRange(CCoordinateGeodetic(CLatitude(51.5085300, CAngleUnit::deg()), CLongitude(-0.1257400, CAngleUnit::deg()), CLength()), true);
|
||||||
|
qDebug() << airports[0].getIcao() << airports[1].getIcao();
|
||||||
|
QVERIFY2(airports[0].getIcao() == CAirportIcaoCode("EGLW"), "Wrong airport data");
|
||||||
|
|
||||||
|
CApplication::processEventsFor(2500); // make sure events are processed
|
||||||
|
}
|
||||||
|
|
||||||
bool CTestReaders::pingServer(const CUrl &url)
|
bool CTestReaders::pingServer(const CUrl &url)
|
||||||
{
|
{
|
||||||
QString m;
|
QString m;
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ namespace BlackCore
|
|||||||
class CIcaoDataReader;
|
class CIcaoDataReader;
|
||||||
class CModelDataReader;
|
class CModelDataReader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CAirportDataReader;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace BlackCoreTest
|
namespace BlackCoreTest
|
||||||
@@ -50,7 +52,11 @@ namespace BlackCoreTest
|
|||||||
//! Read model data
|
//! Read model data
|
||||||
void readModelData();
|
void readModelData();
|
||||||
|
|
||||||
|
//! Read airport data
|
||||||
|
void readAirportData();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
BlackCore::CAirportDataReader *m_airportReader = nullptr;
|
||||||
BlackCore::Db::CIcaoDataReader *m_icaoReader = nullptr;
|
BlackCore::Db::CIcaoDataReader *m_icaoReader = nullptr;
|
||||||
BlackCore::Db::CModelDataReader *m_modelReader = nullptr;
|
BlackCore::Db::CModelDataReader *m_modelReader = nullptr;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user