mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-15 09:15:34 +08:00
refs #452, data readers / writers for DB (via web services)
* flags for readers (which reader is reading specific entities etc.) * web data services: bundling multiple readers (webdataservices) and implementing IWebDataReaderProvider * readers now able to read single entities
This commit is contained in:
committed by
Mathew Sutcliffe
parent
33330394a9
commit
07c6370819
@@ -9,24 +9,30 @@
|
||||
|
||||
#include "blackmisc/sequence.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
#include "blackmisc/networkutils.h"
|
||||
#include "icaodatareader.h"
|
||||
|
||||
#include <QRegularExpression>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Aviation;
|
||||
using namespace BlackMisc::Network;
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
CIcaoDataReader::CIcaoDataReader(QObject *owner, const QString &protocol, const QString &server, const QString &baseUrl) :
|
||||
CDatabaseReader(owner, "CIcaoDataReader"),
|
||||
m_urlAircraftIcao(getAircraftIcaoUrl(protocol, server, baseUrl)), m_urlAirlineIcao(getAirlineIcaoUrl(protocol, server, baseUrl))
|
||||
m_urlAircraftIcao(getAircraftIcaoUrl(protocol, server, baseUrl)),
|
||||
m_urlAirlineIcao(getAirlineIcaoUrl(protocol, server, baseUrl)),
|
||||
m_urlCountry(getCountryUrl(protocol, server, baseUrl))
|
||||
{
|
||||
this->m_networkManagerAircraft = new QNetworkAccessManager(this);
|
||||
this->m_networkManagerAirlines = new QNetworkAccessManager(this);
|
||||
this->m_networkManagerCountries = new QNetworkAccessManager(this);
|
||||
|
||||
this->connect(this->m_networkManagerAircraft, &QNetworkAccessManager::finished, this, &CIcaoDataReader::ps_parseAircraftIcaoData);
|
||||
this->connect(this->m_networkManagerAirlines, &QNetworkAccessManager::finished, this, &CIcaoDataReader::ps_parseAirlineIcaoData);
|
||||
this->connect(this->m_networkManagerCountries, &QNetworkAccessManager::finished, this, &CIcaoDataReader::ps_parseCountryData);
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CIcaoDataReader::getAircraftIcaoCodes() const
|
||||
@@ -35,12 +41,99 @@ namespace BlackCore
|
||||
return m_aircraftIcaos;
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CIcaoDataReader::getAircraftIcaoCodeForDesignator(const QString &designator) const
|
||||
{
|
||||
return getAircraftIcaoCodes().findFirstByDesignatorAndRank(designator);
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CIcaoDataReader::getAircraftIcaoCodeForDbKey(int key) const
|
||||
{
|
||||
return getAircraftIcaoCodes().findByKey(key);
|
||||
}
|
||||
|
||||
CAirlineIcaoCodeList CIcaoDataReader::getAirlineIcaoCodes() const
|
||||
{
|
||||
QReadLocker l(&m_lockAirline);
|
||||
return m_airlineIcaos;
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CIcaoDataReader::smartAircraftIcaoSelector(const CAircraftIcaoCode &icao) const
|
||||
{
|
||||
CAircraftIcaoCodeList codes(getAircraftIcaoCodes()); // thread safe copy
|
||||
if (icao.hasValidDbKey())
|
||||
{
|
||||
int k = icao.getDbKey();
|
||||
CAircraftIcaoCode c(codes.findByKey(k));
|
||||
if (c.hasCompleteData()) { return c; }
|
||||
}
|
||||
|
||||
if (icao.hasKnownDesignator())
|
||||
{
|
||||
const QString d(icao.getDesignator());
|
||||
codes = codes.findByDesignator(d);
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
if (codes.isEmpty()) { return icao; }
|
||||
codes.sortByRank();
|
||||
|
||||
// intentionally continue here
|
||||
}
|
||||
|
||||
// further reduce by manufacturer
|
||||
if (icao.hasManufacturer())
|
||||
{
|
||||
const QString m(icao.getManufacturer());
|
||||
codes = codes.findByManufacturer(m);
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
if (codes.isEmpty()) { return icao; }
|
||||
|
||||
// intentionally continue here
|
||||
}
|
||||
|
||||
// lucky punch on description?
|
||||
if (icao.hasModelDescription())
|
||||
{
|
||||
// do not affect codes here, it might return no results
|
||||
const QString d(icao.getModelDescription());
|
||||
CAircraftIcaoCodeList cm(codes.findByDescription(d));
|
||||
if (cm.size() == 1) { return cm.front(); }
|
||||
if (cm.size() > 1 && cm.size() < codes.size()) { return codes.front(); }
|
||||
}
|
||||
return codes.frontOrDefault(); // sorted by rank
|
||||
}
|
||||
|
||||
CCountryList CIcaoDataReader::getCountries() const
|
||||
{
|
||||
QReadLocker l(&m_lockCountry);
|
||||
return m_countries;
|
||||
}
|
||||
|
||||
CCountry CIcaoDataReader::getCountryForIsoCode(const QString &isoCode) const
|
||||
{
|
||||
QReadLocker l(&m_lockCountry);
|
||||
return m_countries.findByIsoCode(isoCode);
|
||||
}
|
||||
|
||||
CCountry CIcaoDataReader::getCountryForName(const QString &name) const
|
||||
{
|
||||
QReadLocker l(&m_lockCountry);
|
||||
return m_countries.findBestMatchByCountryName(name);
|
||||
}
|
||||
|
||||
CAirlineIcaoCode CIcaoDataReader::getAirlineIcaoCodeForDesignator(const QString &designator) const
|
||||
{
|
||||
return getAirlineIcaoCodes().findByVDesignator(designator);
|
||||
}
|
||||
|
||||
CAirlineIcaoCode CIcaoDataReader::getAirlineIcaoCodeForDbKey(int key) const
|
||||
{
|
||||
return getAirlineIcaoCodes().findByKey(key);
|
||||
}
|
||||
|
||||
CAirlineIcaoCode CIcaoDataReader::smartAirlineIcaoSelector(const CAirlineIcaoCode &icao) const
|
||||
{
|
||||
return icao;
|
||||
}
|
||||
|
||||
int CIcaoDataReader::getAircraftIcaoCodesCount() const
|
||||
{
|
||||
QReadLocker l(&m_lockAircraft);
|
||||
@@ -53,27 +146,50 @@ namespace BlackCore
|
||||
return m_airlineIcaos.size();
|
||||
}
|
||||
|
||||
QString CIcaoDataReader::getAircraftIcaoUrl(const QString &protocol, const QString &server, const QString &baseUrl)
|
||||
bool CIcaoDataReader::areAllDataRead() const
|
||||
{
|
||||
return buildUrl(protocol, server, baseUrl, "service/allaircrafticao.php?rows=20000&sord=asc");
|
||||
return getCountriesCount() > 0 && getAirlineIcaoCodesCount() > 0 && getAircraftIcaoCodesCount() > 0;
|
||||
}
|
||||
|
||||
QString CIcaoDataReader::getAirlineIcaoUrl(const QString &protocol, const QString &server, const QString &baseUrl)
|
||||
int CIcaoDataReader::getCountriesCount() const
|
||||
{
|
||||
return buildUrl(protocol, server, baseUrl, "service/allairlineicao.php?rows=20000&sord=asc");
|
||||
QReadLocker l(&m_lockCountry);
|
||||
return m_countries.size();
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_read()
|
||||
void CIcaoDataReader::ps_read(BlackMisc::Network::CDbFlags::Entity entities)
|
||||
{
|
||||
this->threadAssertCheck();
|
||||
Q_ASSERT(this->m_networkManagerAircraft);
|
||||
Q_ASSERT(this->m_networkManagerAirlines);
|
||||
Q_ASSERT(this->m_networkManagerCountries);
|
||||
Q_ASSERT(!m_urlAircraftIcao.isEmpty());
|
||||
Q_ASSERT(!m_urlAirlineIcao.isEmpty());
|
||||
QNetworkRequest requestAircraft(m_urlAircraftIcao);
|
||||
QNetworkRequest requestAirline(m_urlAirlineIcao);
|
||||
this->m_networkManagerAircraft->get(requestAircraft);
|
||||
this->m_networkManagerAirlines->get(requestAirline);
|
||||
Q_ASSERT(!m_urlCountry.isEmpty());
|
||||
|
||||
CDbFlags::Entity entitiesTriggered = CDbFlags::NoEntity;
|
||||
if (entities.testFlag(CDbFlags::AircraftIcaoEntity))
|
||||
{
|
||||
QNetworkRequest requestAircraft(m_urlAircraftIcao);
|
||||
this->m_networkManagerAircraft->get(requestAircraft);
|
||||
entitiesTriggered |= CDbFlags::AircraftIcaoEntity;
|
||||
}
|
||||
|
||||
if (entities.testFlag(CDbFlags::AirlineIcaoEntity))
|
||||
{
|
||||
QNetworkRequest requestAirline(m_urlAirlineIcao);
|
||||
this->m_networkManagerAirlines->get(requestAirline);
|
||||
entitiesTriggered |= CDbFlags::AirlineIcaoEntity;
|
||||
}
|
||||
|
||||
if (entities.testFlag(CDbFlags::CountryEntity))
|
||||
{
|
||||
QNetworkRequest requestCountry(m_urlCountry);
|
||||
this->m_networkManagerCountries->get(requestCountry);
|
||||
entitiesTriggered |= CDbFlags::CountryEntity;
|
||||
}
|
||||
|
||||
emit readData(entitiesTriggered, CDbFlags::StartRead, 0);
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_parseAircraftIcaoData(QNetworkReply *nwReplyPtr)
|
||||
@@ -81,8 +197,12 @@ namespace BlackCore
|
||||
// wrap pointer, make sure any exit cleans up reply
|
||||
// required to use delete later as object is created in a different thread
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QJsonArray array = this->transformReplyIntoJsonArray(nwReply.data());
|
||||
if (array.isEmpty()) { return; }
|
||||
QJsonArray array = this->transformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
emit readData(CDbFlags::AircraftIcaoEntity, CDbFlags::ReadFailed, 0);
|
||||
return;
|
||||
}
|
||||
CAircraftIcaoCodeList codes = CAircraftIcaoCodeList::fromDatabaseJson(array);
|
||||
|
||||
// this part needs to be synchronized
|
||||
@@ -92,15 +212,18 @@ namespace BlackCore
|
||||
this->m_aircraftIcaos = codes;
|
||||
n = codes.size();
|
||||
}
|
||||
emit readAircraftIcaoCodes(n);
|
||||
if (this->getAirlineIcaoCodesCount() > 0) { emit readAll(); }
|
||||
emit readData(CDbFlags::AircraftIcaoEntity, CDbFlags::ReadFinished, n);
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_parseAirlineIcaoData(QNetworkReply *nwReplyPtr)
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QJsonArray array = this->transformReplyIntoJsonArray(nwReply.data());
|
||||
if (array.isEmpty()) { return; }
|
||||
QJsonArray array = this->transformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
emit readData(CDbFlags::AirlineIcaoEntity, CDbFlags::ReadFailed, 0);
|
||||
return;
|
||||
}
|
||||
CAirlineIcaoCodeList codes = CAirlineIcaoCodeList::fromDatabaseJson(array);
|
||||
|
||||
// this part needs to be synchronized
|
||||
@@ -110,7 +233,52 @@ namespace BlackCore
|
||||
this->m_airlineIcaos = codes;
|
||||
n = codes.size();
|
||||
}
|
||||
emit readAirlinesIcaoCodes(n);
|
||||
if (this->getAircraftIcaoCodesCount() > 0) { emit readAll(); }
|
||||
emit readData(CDbFlags::AirlineIcaoEntity, CDbFlags::ReadFinished, n);
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_parseCountryData(QNetworkReply *nwReplyPtr)
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QJsonArray array = this->transformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
emit readData(CDbFlags::CountryEntity, CDbFlags::ReadFailed, 0);
|
||||
return;
|
||||
}
|
||||
CCountryList countries = CCountryList::fromDatabaseJson(array);
|
||||
|
||||
// this part needs to be synchronized
|
||||
int n;
|
||||
{
|
||||
QWriteLocker wl(&this->m_lockCountry);
|
||||
this->m_countries = countries;
|
||||
n = m_countries.size();
|
||||
}
|
||||
emit readData(CDbFlags::CountryEntity, CDbFlags::ReadFinished, n);
|
||||
}
|
||||
|
||||
bool CIcaoDataReader::canConnect(QString &message) const
|
||||
{
|
||||
if (m_urlAircraftIcao.isEmpty() || m_urlAirlineIcao.isEmpty()) { return false; }
|
||||
bool cm = CNetworkUtils::canConnect(m_urlAircraftIcao, message);
|
||||
|
||||
// currently only testing one URL, might be changed in the future
|
||||
return cm;
|
||||
}
|
||||
|
||||
QString CIcaoDataReader::getAircraftIcaoUrl(const QString &protocol, const QString &server, const QString &baseUrl)
|
||||
{
|
||||
return CNetworkUtils::buildUrl(protocol, server, baseUrl, "service/jsonaircrafticao.php");
|
||||
}
|
||||
|
||||
QString CIcaoDataReader::getAirlineIcaoUrl(const QString &protocol, const QString &server, const QString &baseUrl)
|
||||
{
|
||||
return CNetworkUtils::buildUrl(protocol, server, baseUrl, "service/jsonairlineicao.php");
|
||||
}
|
||||
|
||||
QString CIcaoDataReader::getCountryUrl(const QString &protocol, const QString &server, const QString &baseUrl)
|
||||
{
|
||||
return CNetworkUtils::buildUrl(protocol, server, baseUrl, "service/jsoncountry.php");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user