mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 17:35:34 +08:00
Moved smart selector functions to lists
This commit is contained in:
committed by
Mathew Sutcliffe
parent
5e42aa0b95
commit
955b8bafea
@@ -60,48 +60,10 @@ namespace BlackCore
|
||||
return m_airlineIcaos;
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CIcaoDataReader::smartAircraftIcaoSelector(const CAircraftIcaoCode &icao) const
|
||||
CAircraftIcaoCode CIcaoDataReader::smartAircraftIcaoSelector(const CAircraftIcaoCode &icaoPattern) 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
|
||||
return codes.smartAircraftIcaoSelector(icaoPattern); // sorted by rank
|
||||
}
|
||||
|
||||
CCountryList CIcaoDataReader::getCountries() const
|
||||
@@ -132,9 +94,10 @@ namespace BlackCore
|
||||
return getAirlineIcaoCodes().findByKey(key);
|
||||
}
|
||||
|
||||
CAirlineIcaoCode CIcaoDataReader::smartAirlineIcaoSelector(const CAirlineIcaoCode &icao) const
|
||||
CAirlineIcaoCode CIcaoDataReader::smartAirlineIcaoSelector(const CAirlineIcaoCode &icaoPattern) const
|
||||
{
|
||||
return icao;
|
||||
CAirlineIcaoCodeList codes(this->getAirlineIcaoCodes()); // thread safe copy
|
||||
return codes.smartAirlineIcaoSelector(icaoPattern);
|
||||
}
|
||||
|
||||
int CIcaoDataReader::getAircraftIcaoCodesCount() const
|
||||
@@ -210,6 +173,7 @@ 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);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
@@ -225,11 +189,13 @@ namespace BlackCore
|
||||
this->m_aircraftIcaos = codes;
|
||||
}
|
||||
emit dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::AircraftIcaoEntity) << urlString;
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_parseAirlineIcaoData(QNetworkReply *nwReplyPtr)
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
@@ -245,11 +211,13 @@ namespace BlackCore
|
||||
this->m_airlineIcaos = codes;
|
||||
}
|
||||
emit dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::AirlineIcaoEntity) << urlString;
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_parseCountryData(QNetworkReply *nwReplyPtr)
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
@@ -265,6 +233,7 @@ namespace BlackCore
|
||||
this->m_countries = countries;
|
||||
}
|
||||
emit dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::CountryEntity) << urlString;
|
||||
}
|
||||
|
||||
bool CIcaoDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace BlackCore
|
||||
|
||||
//! Get best match for incomplete aircraft ICAO code
|
||||
//! \threadsafe
|
||||
BlackMisc::Aviation::CAircraftIcaoCode smartAircraftIcaoSelector(const BlackMisc::Aviation::CAircraftIcaoCode &icao) const;
|
||||
BlackMisc::Aviation::CAircraftIcaoCode smartAircraftIcaoSelector(const BlackMisc::Aviation::CAircraftIcaoCode &icaoPattern) const;
|
||||
|
||||
//! Get countries
|
||||
//! \threadsafe
|
||||
@@ -82,7 +82,7 @@ namespace BlackCore
|
||||
|
||||
//! Get best match for incomplete airline ICAO code
|
||||
//! \threadsafe
|
||||
BlackMisc::Aviation::CAirlineIcaoCode smartAirlineIcaoSelector(const BlackMisc::Aviation::CAirlineIcaoCode &icao) const;
|
||||
BlackMisc::Aviation::CAirlineIcaoCode smartAirlineIcaoSelector(const BlackMisc::Aviation::CAirlineIcaoCode &icaoPattern) const;
|
||||
|
||||
//! Get aircraft ICAO information count
|
||||
//! \threadsafe
|
||||
|
||||
@@ -59,34 +59,10 @@ namespace BlackCore
|
||||
return liveries.findByKey(id);
|
||||
}
|
||||
|
||||
CLivery CModelDataReader::smartLiverySelector(const CLivery &livery) const
|
||||
CLivery CModelDataReader::smartLiverySelector(const CLivery &liveryPattern) const
|
||||
{
|
||||
CLiveryList liveries(getLiveries()); // thread safe copy
|
||||
|
||||
// first try on id, that would be perfect
|
||||
if (livery.hasValidDbKey())
|
||||
{
|
||||
int k = livery.getDbKey();
|
||||
CLivery l(liveries.findByKey(k));
|
||||
if (l.hasCompleteData()) { return l; }
|
||||
}
|
||||
|
||||
// by combined code
|
||||
if (livery.hasCombinedCode())
|
||||
{
|
||||
QString cc(livery.getCombinedCode());
|
||||
CLivery l(liveries.findByCombinedCode(cc));
|
||||
if (l.hasCompleteData()) { return l; }
|
||||
}
|
||||
|
||||
if (livery.hasValidAirlineDesignator())
|
||||
{
|
||||
QString icao(livery.getAirlineIcaoCodeDesignator());
|
||||
CLivery l(liveries.findByAirlineIcaoDesignatorStdLivery(icao));
|
||||
if (l.hasCompleteData()) { return l; }
|
||||
}
|
||||
|
||||
return CLivery();
|
||||
return liveries.smartLiverySelector(liveryPattern);
|
||||
}
|
||||
|
||||
CDistributorList CModelDataReader::getDistributors() const
|
||||
@@ -127,19 +103,10 @@ namespace BlackCore
|
||||
return m_distributors.size();
|
||||
}
|
||||
|
||||
CDistributor CModelDataReader::smartDistributorSelector(const CDistributor &distributor) const
|
||||
CDistributor CModelDataReader::smartDistributorSelector(const CDistributor &distributorPattern) const
|
||||
{
|
||||
CDistributorList distributors(getDistributors()); // thread safe copy
|
||||
if (distributor.hasValidDbKey())
|
||||
{
|
||||
QString k(distributor.getDbKey());
|
||||
CDistributor d(distributors.findByKey(k));
|
||||
if (d.hasCompleteData()) { return d; }
|
||||
|
||||
// more lenient search
|
||||
return distributors.findByIdOrAlias(k);
|
||||
}
|
||||
return CDistributor();
|
||||
return distributors.smartDistributorSelector(distributorPattern);
|
||||
}
|
||||
|
||||
int CModelDataReader::getModelsCount() const
|
||||
@@ -196,6 +163,7 @@ 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);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
@@ -210,13 +178,15 @@ namespace BlackCore
|
||||
QWriteLocker wl(&this->m_lockLivery);
|
||||
this->m_liveries = liveries;
|
||||
}
|
||||
// never emit when lcok is held -> deadlock
|
||||
// never emit when lock is held -> deadlock
|
||||
emit dataRead(CEntityFlags::LiveryEntity, CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::LiveryEntity) << urlString;
|
||||
}
|
||||
|
||||
void CModelDataReader::ps_parseDistributorData(QNetworkReply *nwReplyPtr)
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
@@ -232,11 +202,13 @@ namespace BlackCore
|
||||
this->m_distributors = distributors;
|
||||
}
|
||||
emit dataRead(CEntityFlags::DistributorEntity, CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::DistributorEntity) << urlString;
|
||||
}
|
||||
|
||||
void CModelDataReader::ps_parseModelData(QNetworkReply *nwReplyPtr)
|
||||
{
|
||||
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
|
||||
QString urlString(nwReply->url().toString());
|
||||
QJsonArray array = this->setStatusAndTransformReplyIntoDatastoreResponse(nwReply.data());
|
||||
if (array.isEmpty())
|
||||
{
|
||||
@@ -252,6 +224,7 @@ namespace BlackCore
|
||||
this->m_models = models;
|
||||
}
|
||||
emit dataRead(CEntityFlags::ModelEntity, CEntityFlags::ReadFinished, n);
|
||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::ModelEntity) << urlString;
|
||||
}
|
||||
|
||||
bool CModelDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace BlackCore
|
||||
|
||||
//! Best match specified by distributor
|
||||
//! \threadsafe
|
||||
BlackMisc::Simulation::CDistributor smartDistributorSelector(const BlackMisc::Simulation::CDistributor &distributor) const;
|
||||
BlackMisc::Simulation::CDistributor smartDistributorSelector(const BlackMisc::Simulation::CDistributor &distributorPattern) const;
|
||||
|
||||
//! Get models count
|
||||
//! \threadsafe
|
||||
|
||||
@@ -82,5 +82,49 @@ namespace BlackMisc
|
||||
return codes;
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CAircraftIcaoCodeList::smartAircraftIcaoSelector(const CAircraftIcaoCode &icaoPattern) const
|
||||
{
|
||||
if (icaoPattern.hasValidDbKey())
|
||||
{
|
||||
int k = icaoPattern.getDbKey();
|
||||
CAircraftIcaoCode c(this->findByKey(k));
|
||||
if (c.hasCompleteData()) { return c; }
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList codes(*this); // copy and reduce
|
||||
if (icaoPattern.hasKnownDesignator())
|
||||
{
|
||||
const QString d(icaoPattern.getDesignator());
|
||||
codes = codes.findByDesignator(d);
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
if (codes.isEmpty()) { return icaoPattern; }
|
||||
codes.sortByRank();
|
||||
|
||||
// intentionally continue here
|
||||
}
|
||||
|
||||
// further reduce by manufacturer
|
||||
if (icaoPattern.hasManufacturer())
|
||||
{
|
||||
const QString m(icaoPattern.getManufacturer());
|
||||
codes = codes.findByManufacturer(m);
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
if (codes.isEmpty()) { return icaoPattern; }
|
||||
|
||||
// intentionally continue here
|
||||
}
|
||||
|
||||
// lucky punch on description?
|
||||
if (icaoPattern.hasModelDescription())
|
||||
{
|
||||
// do not affect codes here, it might return no results
|
||||
const QString d(icaoPattern.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
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -50,6 +50,9 @@ namespace BlackMisc
|
||||
//! Find by designator, then best match by rank
|
||||
CAircraftIcaoCode findFirstByDesignatorAndRank(const QString &designator);
|
||||
|
||||
//! Best selection by given pattern
|
||||
CAircraftIcaoCode smartAircraftIcaoSelector(const CAircraftIcaoCode &icaoPattern) const;
|
||||
|
||||
//! Sort by rank
|
||||
void sortByRank();
|
||||
|
||||
|
||||
@@ -26,6 +26,19 @@ namespace BlackMisc
|
||||
});
|
||||
}
|
||||
|
||||
CAirlineIcaoCode CAirlineIcaoCodeList::smartAirlineIcaoSelector(const CAirlineIcaoCode &icaoPattern) const
|
||||
{
|
||||
if (icaoPattern.hasValidDbKey())
|
||||
{
|
||||
return this->findByKey(icaoPattern.getDbKey(), icaoPattern);
|
||||
}
|
||||
|
||||
if (!icaoPattern.hasValidDesignator()) { return CAirlineIcaoCode(); }
|
||||
|
||||
//! \todo smart airline selector, further criteria
|
||||
return icaoPattern;
|
||||
}
|
||||
|
||||
CAirlineIcaoCode CAirlineIcaoCodeList::findByVDesignator(const QString &designator)
|
||||
{
|
||||
if (CAirlineIcaoCode::isValidAirlineDesignator(designator)) { return CAirlineIcaoCode(); }
|
||||
|
||||
@@ -43,6 +43,9 @@ namespace BlackMisc
|
||||
//! Not unique because of virtual airlines
|
||||
CAirlineIcaoCodeList findByDesignator(const QString &designator);
|
||||
|
||||
//! Best selection by given pattern
|
||||
CAirlineIcaoCode smartAirlineIcaoSelector(const CAirlineIcaoCode &icaoPattern) const;
|
||||
|
||||
//! Find by v-designator, this should be unique
|
||||
CAirlineIcaoCode findByVDesignator(const QString &designator);
|
||||
|
||||
|
||||
@@ -51,5 +51,32 @@ namespace BlackMisc
|
||||
});
|
||||
}
|
||||
|
||||
CLivery CLiveryList::smartLiverySelector(const CLivery &liveryPattern) const
|
||||
{
|
||||
// first try on id, that would be perfect
|
||||
if (liveryPattern.hasValidDbKey())
|
||||
{
|
||||
int k = liveryPattern.getDbKey();
|
||||
CLivery l(this->findByKey(k));
|
||||
if (l.hasCompleteData()) { return l; }
|
||||
}
|
||||
|
||||
// by combined code
|
||||
if (liveryPattern.hasCombinedCode())
|
||||
{
|
||||
QString cc(liveryPattern.getCombinedCode());
|
||||
CLivery l(this->findByCombinedCode(cc));
|
||||
if (l.hasCompleteData()) { return l; }
|
||||
}
|
||||
|
||||
if (liveryPattern.hasValidAirlineDesignator())
|
||||
{
|
||||
QString icao(liveryPattern.getAirlineIcaoCodeDesignator());
|
||||
CLivery l(this->findByAirlineIcaoDesignatorStdLivery(icao));
|
||||
if (l.hasCompleteData()) { return l; }
|
||||
}
|
||||
return CLivery();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -44,6 +44,10 @@ namespace BlackMisc
|
||||
|
||||
//! Find livery by combined code
|
||||
CLivery findByCombinedCode(const QString &combinedCode) const;
|
||||
|
||||
//! Find
|
||||
CLivery smartLiverySelector(const CLivery &liveryPattern) const;
|
||||
|
||||
};
|
||||
} //namespace
|
||||
} // namespace
|
||||
|
||||
@@ -30,5 +30,19 @@ namespace BlackMisc
|
||||
return CDistributor();
|
||||
}
|
||||
|
||||
CDistributor CDistributorList::smartDistributorSelector(const CDistributor &distributorPattern)
|
||||
{
|
||||
if (distributorPattern.hasValidDbKey())
|
||||
{
|
||||
QString k(distributorPattern.getDbKey());
|
||||
CDistributor d(this->findByKey(k));
|
||||
if (d.hasCompleteData()) { return d; }
|
||||
|
||||
// more lenient search
|
||||
return this->findByIdOrAlias(k);
|
||||
}
|
||||
return CDistributor();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -43,6 +43,8 @@ namespace BlackMisc
|
||||
//! Find by id or alias
|
||||
CDistributor findByIdOrAlias(const QString &name);
|
||||
|
||||
//! Best match by given pattern
|
||||
CDistributor smartDistributorSelector(const CDistributor &distributorPattern);
|
||||
};
|
||||
} //namespace
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user