Model reader refactorings

This commit is contained in:
Klaus Basan
2018-07-26 22:32:10 +02:00
parent 1fd7ad6891
commit efbd24e10e
6 changed files with 60 additions and 41 deletions

View File

@@ -15,6 +15,7 @@
#include <QNetworkReply>
#include <QFileInfo>
#include <QPointer>
using namespace BlackMisc;
using namespace BlackMisc::Aviation;
@@ -38,12 +39,12 @@ namespace BlackCore
CAirport CAirportDataReader::getAirportForIcaoDesignator(const QString &designator) const
{
return getAirports().findFirstByIcao(CAirportIcaoCode(designator));
return this->getAirports().findFirstByIcao(CAirportIcaoCode(designator));
}
CAirport CAirportDataReader::getAirportForNameOrLocation(const QString &nameOrLocation) const
{
return getAirports().findFirstByNameOrLocation(nameOrLocation);
return this->getAirports().findFirstByNameOrLocation(nameOrLocation);
}
int CAirportDataReader::getAirportsCount() const
@@ -54,8 +55,11 @@ namespace BlackCore
bool CAirportDataReader::readFromJsonFilesInBackground(const QString &dir, CEntityFlags::Entity whatToRead, bool overrideNewerOnly)
{
if (dir.isEmpty() || whatToRead == CEntityFlags::NoEntity) { return false; }
QPointer<CAirportDataReader> myself(this);
QTimer::singleShot(0, this, [ = ]()
{
if (!myself) { return; }
const CStatusMessageList msgs = this->readFromJsonFiles(dir, whatToRead, overrideNewerOnly);
if (msgs.isFailure())
{
@@ -179,7 +183,7 @@ namespace BlackCore
return this->getBaseUrl(mode).withAppendedPath(fileNameForMode(CEntityFlags::AirportEntity, mode));
}
void CAirportDataReader::ps_parseAirportData(QNetworkReply *nwReplyPtr)
void CAirportDataReader::parseAirportData(QNetworkReply *nwReplyPtr)
{
// wrap pointer, make sure any exit cleans up reply
// required to use delete later as object is created in a different thread
@@ -190,7 +194,7 @@ namespace BlackCore
if (res.hasErrorMessage())
{
CLogMessage::preformatted(res.lastWarningOrAbove());
emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFailed, 0);
emit this->dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFailed, 0);
return;
}
@@ -247,7 +251,7 @@ namespace BlackCore
if (!url.isEmpty())
{
url.appendQuery(queryLatestTimestamp(newerThan));
this->getFromNetworkAndLog(url, { this, &CAirportDataReader::ps_parseAirportData });
this->getFromNetworkAndLog(url, { this, &CAirportDataReader::parseAirportData });
emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::StartRead, 0);
}
else

View File

@@ -68,9 +68,6 @@ namespace BlackCore
virtual BlackMisc::Network::CUrl getDbServiceBaseUrl() const override;
private slots:
//! Parse downloaded JSON file
void ps_parseAirportData(QNetworkReply *nwReplyPtr);
//! Read / re-read data file
void ps_read(BlackMisc::Network::CEntityFlags::Entity entity = BlackMisc::Network::CEntityFlags::DistributorLiveryModel,
BlackMisc::Db::CDbFlags::DataRetrievalModeFlag mode = BlackMisc::Db::CDbFlags::DbReading, const QDateTime &newerThan = QDateTime());
@@ -81,6 +78,9 @@ namespace BlackCore
//! Reader URL (we read from where?) used to detect changes of location
BlackMisc::CData<BlackCore::Data::TDbModelReaderBaseUrl> m_readerUrlCache {this, &CAirportDataReader::baseUrlCacheChanged };
//! Parse downloaded JSON file
void parseAirportData(QNetworkReply *nwReplyPtr);
//! Airport cache changed
void airportCacheChanged();

View File

@@ -178,7 +178,7 @@ namespace BlackCore
if (!url.isEmpty())
{
url.appendQuery(queryLatestTimestamp(newerThan));
this->getFromNetworkAndLog(url, { this, &CIcaoDataReader::ps_parseAircraftIcaoData });
this->getFromNetworkAndLog(url, { this, &CIcaoDataReader::parseAircraftIcaoData });
entitiesTriggered |= CEntityFlags::AircraftIcaoEntity;
}
else
@@ -193,7 +193,7 @@ namespace BlackCore
if (!url.isEmpty())
{
url.appendQuery(queryLatestTimestamp(newerThan));
this->getFromNetworkAndLog(url, { this, &CIcaoDataReader::ps_parseAirlineIcaoData });
this->getFromNetworkAndLog(url, { this, &CIcaoDataReader::parseAirlineIcaoData });
entitiesTriggered |= CEntityFlags::AirlineIcaoEntity;
}
else
@@ -208,7 +208,7 @@ namespace BlackCore
if (!url.isEmpty())
{
url.appendQuery(queryLatestTimestamp(newerThan));
this->getFromNetworkAndLog(url, { this, &CIcaoDataReader::ps_parseCountryData });
this->getFromNetworkAndLog(url, { this, &CIcaoDataReader::parseCountryData });
entitiesTriggered |= CEntityFlags::CountryEntity;
}
else
@@ -254,7 +254,7 @@ namespace BlackCore
}
}
void CIcaoDataReader::ps_parseAircraftIcaoData(QNetworkReply *nwReplyPtr)
void CIcaoDataReader::parseAircraftIcaoData(QNetworkReply *nwReplyPtr)
{
// wrap pointer, make sure any exit cleans up reply
// required to use delete later as object is created in a different thread
@@ -307,7 +307,7 @@ namespace BlackCore
this->emitAndLogDataRead(CEntityFlags::AircraftIcaoEntity, n, res);
}
void CIcaoDataReader::ps_parseAirlineIcaoData(QNetworkReply *nwReplyPtr)
void CIcaoDataReader::parseAirlineIcaoData(QNetworkReply *nwReplyPtr)
{
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
if (!this->doWorkCheck()) { return; }
@@ -358,7 +358,7 @@ namespace BlackCore
this->emitAndLogDataRead(CEntityFlags::AirlineIcaoEntity, n, res);
}
void CIcaoDataReader::ps_parseCountryData(QNetworkReply *nwReplyPtr)
void CIcaoDataReader::parseCountryData(QNetworkReply *nwReplyPtr)
{
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
const CDatabaseReader::JsonDatastoreResponse res = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());

View File

@@ -154,15 +154,6 @@ namespace BlackCore
virtual BlackMisc::Network::CUrl getDbServiceBaseUrl() const override;
private slots:
//! Aircraft have been read
void ps_parseAircraftIcaoData(QNetworkReply *nwReply);
//! Airlines have been read
void ps_parseAirlineIcaoData(QNetworkReply *nwReply);
//! Countries have been read
void ps_parseCountryData(QNetworkReply *nwReply);
//! Read / re-read data
void ps_read(BlackMisc::Network::CEntityFlags::Entity entities,
BlackMisc::Db::CDbFlags::DataRetrievalModeFlag mode, const QDateTime &newerThan);
@@ -175,9 +166,25 @@ namespace BlackCore
//! Reader URL (we read from where?) used to detect changes of location
BlackMisc::CData<BlackCore::Data::TDbIcaoReaderBaseUrl> m_readerUrlCache {this, &CIcaoDataReader::baseUrlCacheChanged };
//! Aircraft have been read
void parseAircraftIcaoData(QNetworkReply *nwReply);
//! Airlines have been read
void parseAirlineIcaoData(QNetworkReply *nwReply);
//! Countries have been read
void parseCountryData(QNetworkReply *nwReply);
//! Cache has changed elsewhere
void aircraftIcaoCacheChanged();
//! Cache has changed elsewhere
void airlineIcaoCacheChanged();
//! Cache has changed elsewhere
void countryCacheChanged();
//! Cache has changed elsewhere
void baseUrlCacheChanged();
//! Update reader URL

View File

@@ -196,7 +196,7 @@ namespace BlackCore
if (!url.isEmpty())
{
url.appendQuery(queryLatestTimestamp(newerThan));
this->getFromNetworkAndLog(url, { this, &CModelDataReader::ps_parseLiveryData});
this->getFromNetworkAndLog(url, { this, &CModelDataReader::parseLiveryData});
triggeredRead |= CEntityFlags::LiveryEntity;
}
else
@@ -211,7 +211,7 @@ namespace BlackCore
if (!url.isEmpty())
{
url.appendQuery(queryLatestTimestamp(newerThan));
this->getFromNetworkAndLog(url, { this, &CModelDataReader::ps_parseDistributorData});
this->getFromNetworkAndLog(url, { this, &CModelDataReader::parseDistributorData});
triggeredRead |= CEntityFlags::DistributorEntity;
}
else
@@ -226,7 +226,7 @@ namespace BlackCore
if (!url.isEmpty())
{
url.appendQuery(queryLatestTimestamp(newerThan));
this->getFromNetworkAndLog(url, { this, &CModelDataReader::ps_parseModelData});
this->getFromNetworkAndLog(url, { this, &CModelDataReader::parseModelData});
triggeredRead |= CEntityFlags::ModelEntity;
}
else
@@ -272,7 +272,7 @@ namespace BlackCore
}
}
void CModelDataReader::ps_parseLiveryData(QNetworkReply *nwReplyPtr)
void CModelDataReader::parseLiveryData(QNetworkReply *nwReplyPtr)
{
// wrap pointer, make sure any exit cleans up reply
// required to use delete later as object is created in a different thread
@@ -315,7 +315,7 @@ namespace BlackCore
this->emitAndLogDataRead(CEntityFlags::LiveryEntity, n, res);
}
void CModelDataReader::ps_parseDistributorData(QNetworkReply *nwReplyPtr)
void CModelDataReader::parseDistributorData(QNetworkReply *nwReplyPtr)
{
// wrap pointer, make sure any exit cleans up reply
// required to use delete later as object is created in a different thread
@@ -358,7 +358,7 @@ namespace BlackCore
this->emitAndLogDataRead(CEntityFlags::DistributorEntity, n, res);
}
void CModelDataReader::ps_parseModelData(QNetworkReply *nwReplyPtr)
void CModelDataReader::parseModelData(QNetworkReply *nwReplyPtr)
{
// wrap pointer, make sure any exit cleans up reply
// required to use delete later as object is created in a different thread

View File

@@ -155,14 +155,6 @@ namespace BlackCore
virtual BlackMisc::Network::CUrl getDbServiceBaseUrl() const override;
private slots:
//! Liveries have been read
void ps_parseLiveryData(QNetworkReply *nwReply);
//! Distributors have been read
void ps_parseDistributorData(QNetworkReply *nwReply);
//! Models have been read
void ps_parseModelData(QNetworkReply *nwReply);
//! Read / re-read data file
void ps_read(BlackMisc::Network::CEntityFlags::Entity entities = BlackMisc::Network::CEntityFlags::DistributorLiveryModel,
@@ -170,16 +162,32 @@ namespace BlackCore
const QDateTime &newerThan = QDateTime());
private:
BlackMisc::CData<BlackCore::Data::TDbLiveryCache> m_liveryCache {this, &CModelDataReader::liveryCacheChanged };
BlackMisc::CData<BlackCore::Data::TDbModelCache> m_modelCache {this, &CModelDataReader::modelCacheChanged };
BlackMisc::CData<BlackCore::Data::TDbDistributorCache> m_distributorCache {this, &CModelDataReader::distributorCacheChanged };
BlackMisc::CData<BlackCore::Data::TDbLiveryCache> m_liveryCache { this, &CModelDataReader::liveryCacheChanged };
BlackMisc::CData<BlackCore::Data::TDbModelCache> m_modelCache { this, &CModelDataReader::modelCacheChanged };
BlackMisc::CData<BlackCore::Data::TDbDistributorCache> m_distributorCache { this, &CModelDataReader::distributorCacheChanged };
//! Reader URL (we read from where?) used to detect changes of location
BlackMisc::CData<BlackCore::Data::TDbModelReaderBaseUrl> m_readerUrlCache {this, &CModelDataReader::baseUrlCacheChanged };
BlackMisc::CData<BlackCore::Data::TDbModelReaderBaseUrl> m_readerUrlCache { this, &CModelDataReader::baseUrlCacheChanged };
//! Liveries have been read
void parseLiveryData(QNetworkReply *nwReply);
//! Distributors have been read
void parseDistributorData(QNetworkReply *nwReply);
//! Models have been read
void parseModelData(QNetworkReply *nwReply);
//! Livery cache changed elsewhere
void liveryCacheChanged();
//! Model cache changed elsewhere
void modelCacheChanged();
//! Distributor cache changed elsewhere
void distributorCacheChanged();
//! Base URL cache changed
void baseUrlCacheChanged();
//! Update reader URL