mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-28 11:45:40 +08:00
refs #853, JSON exception handling adjustments for ICAO reader
This commit is contained in:
committed by
Mathew Sutcliffe
parent
b0bef3264c
commit
e3197ce375
@@ -302,57 +302,107 @@ namespace BlackCore
|
|||||||
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::CountryEntity) << urlString;
|
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::CountryEntity) << urlString;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CIcaoDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
|
CStatusMessageList CIcaoDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
|
||||||
{
|
{
|
||||||
QDir directory(dir);
|
QDir directory(dir);
|
||||||
if (!directory.exists()) { return false; }
|
if (!directory.exists())
|
||||||
|
{
|
||||||
|
return CStatusMessage(this).error("Missing directory '%1'") << dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hint: Do not emit while locked -> deadlock
|
||||||
|
CStatusMessageList msgs;
|
||||||
|
whatToRead &= CEntityFlags::AllIcaoAndCountries;
|
||||||
CEntityFlags::Entity reallyRead = CEntityFlags::NoEntity;
|
CEntityFlags::Entity reallyRead = CEntityFlags::NoEntity;
|
||||||
if (whatToRead.testFlag(CEntityFlags::CountryEntity))
|
if (whatToRead.testFlag(CEntityFlags::CountryEntity))
|
||||||
{
|
{
|
||||||
QString countriesJson(CFileUtils::readFileToString(CFileUtils::appendFilePaths(directory.absolutePath(), "countries.json")));
|
const QString fileName = CFileUtils::appendFilePaths(directory.absolutePath(), "countries.json");
|
||||||
if (!countriesJson.isEmpty())
|
const QString countriesJson(CFileUtils::readFileToString(fileName));
|
||||||
|
if (countriesJson.isEmpty())
|
||||||
{
|
{
|
||||||
CCountryList countries;
|
msgs.push_back(CStatusMessage(this).error("Failed to read from file/empty file '%1'") << fileName);
|
||||||
countries.convertFromJson(Json::jsonObjectFromString(countriesJson)); //! \todo catch CJsonException or use convertFromJsonNoThrow
|
}
|
||||||
const int c = countries.size();
|
else
|
||||||
this->m_countryCache.set(countries);
|
{
|
||||||
|
try
|
||||||
// Do not emit while locked -> deadlock
|
{
|
||||||
reallyRead |= CEntityFlags::CountryEntity;
|
CCountryList countries;
|
||||||
emit dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFinished, c);
|
countries.convertFromJson(Json::jsonObjectFromString(countriesJson));
|
||||||
|
const int c = countries.size();
|
||||||
|
this->m_countryCache.set(countries);
|
||||||
|
reallyRead |= CEntityFlags::CountryEntity;
|
||||||
|
emit dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFinished, c);
|
||||||
|
}
|
||||||
|
catch (const CJsonException &ex)
|
||||||
|
{
|
||||||
|
emit dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFailed, 0);
|
||||||
|
msgs.push_back(ex.toStatusMessage(this, QString("Reading countries from '%1'").arg(fileName)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (whatToRead.testFlag(CEntityFlags::AircraftIcaoEntity))
|
if (whatToRead.testFlag(CEntityFlags::AircraftIcaoEntity))
|
||||||
{
|
{
|
||||||
QString aircraftJson(CFileUtils::readFileToString(CFileUtils::appendFilePaths(directory.absolutePath(), "aircrafticao.json")));
|
const QString fileName = CFileUtils::appendFilePaths(directory.absolutePath(), "aircrafticao.json");
|
||||||
if (!aircraftJson.isEmpty())
|
const QString aircraftJson(fileName);
|
||||||
|
if (aircraftJson.isEmpty())
|
||||||
{
|
{
|
||||||
CAircraftIcaoCodeList aircraftIcaos;
|
msgs.push_back(CStatusMessage(this).error("Failed to read from file/empty file '%1'") << fileName);
|
||||||
aircraftIcaos.convertFromJson(Json::jsonObjectFromString(aircraftJson)); //! \todo catch CJsonException or use convertFromJsonNoThrow
|
}
|
||||||
const int c = aircraftIcaos.size();
|
else
|
||||||
this->m_aircraftIcaoCache.set(aircraftIcaos);
|
{
|
||||||
|
try
|
||||||
reallyRead |= CEntityFlags::AircraftIcaoEntity;
|
{
|
||||||
emit dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFinished, c);
|
CAircraftIcaoCodeList aircraftIcaos;
|
||||||
|
aircraftIcaos.convertFromJson(Json::jsonObjectFromString(aircraftJson));
|
||||||
|
const int c = aircraftIcaos.size();
|
||||||
|
this->m_aircraftIcaoCache.set(aircraftIcaos);
|
||||||
|
reallyRead |= CEntityFlags::AircraftIcaoEntity;
|
||||||
|
emit dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFinished, c);
|
||||||
|
}
|
||||||
|
catch (const CJsonException &ex)
|
||||||
|
{
|
||||||
|
emit dataRead(CEntityFlags::AircraftIcaoEntity, CEntityFlags::ReadFailed, 0);
|
||||||
|
msgs.push_back(ex.toStatusMessage(this, QString("Reading aircraft ICAOs from '%1'").arg(fileName)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (whatToRead.testFlag(CEntityFlags::AirlineIcaoEntity))
|
if (whatToRead.testFlag(CEntityFlags::AirlineIcaoEntity))
|
||||||
{
|
{
|
||||||
QString airlineJson(CFileUtils::readFileToString(CFileUtils::appendFilePaths(directory.absolutePath(), "airlineicao.json")));
|
const QString fileName = CFileUtils::appendFilePaths(directory.absolutePath(), "airlineicao.json");
|
||||||
if (!airlineJson.isEmpty())
|
const QString airlineJson(fileName);
|
||||||
|
if (airlineJson.isEmpty())
|
||||||
{
|
{
|
||||||
CAirlineIcaoCodeList airlineIcaos;
|
msgs.push_back(CStatusMessage(this).error("Failed to read from file/empty file '%1'") << fileName);
|
||||||
airlineIcaos.convertFromJson(Json::jsonObjectFromString(airlineJson)); //! \todo catch CJsonException or use convertFromJsonNoThrow
|
}
|
||||||
const int c = airlineIcaos.size();
|
else
|
||||||
this->m_airlineIcaoCache.set(airlineIcaos);
|
{
|
||||||
reallyRead |= CEntityFlags::AirlineIcaoEntity;
|
try
|
||||||
emit dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFinished, c);
|
{
|
||||||
|
CAirlineIcaoCodeList airlineIcaos;
|
||||||
|
airlineIcaos.convertFromJson(Json::jsonObjectFromString(airlineJson));
|
||||||
|
const int c = airlineIcaos.size();
|
||||||
|
this->m_airlineIcaoCache.set(airlineIcaos);
|
||||||
|
reallyRead |= CEntityFlags::AirlineIcaoEntity;
|
||||||
|
emit dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFinished, c);
|
||||||
|
}
|
||||||
|
catch (const CJsonException &ex)
|
||||||
|
{
|
||||||
|
emit dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFailed, 0);
|
||||||
|
msgs.push_back(ex.toStatusMessage(this, QString("Reading airline ICAOs from '%1'").arg(fileName)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (whatToRead & CEntityFlags::AllIcaoAndCountries) == reallyRead;
|
|
||||||
|
if (msgs.isSuccess() && (reallyRead & CEntityFlags::DistributorLiveryModel) == whatToRead)
|
||||||
|
{
|
||||||
|
return CStatusMessage(this).info("Updated caches for '%1' from '%2'") << CEntityFlags::flagToString(reallyRead) << dir;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return msgs;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CIcaoDataReader::readFromJsonFilesInBackground(const QString &dir, CEntityFlags::Entity whatToRead)
|
bool CIcaoDataReader::readFromJsonFilesInBackground(const QString &dir, CEntityFlags::Entity whatToRead)
|
||||||
@@ -360,8 +410,11 @@ namespace BlackCore
|
|||||||
if (dir.isEmpty() || whatToRead == CEntityFlags::NoEntity) { return false; }
|
if (dir.isEmpty() || whatToRead == CEntityFlags::NoEntity) { return false; }
|
||||||
QTimer::singleShot(0, this, [this, dir, whatToRead]()
|
QTimer::singleShot(0, this, [this, dir, whatToRead]()
|
||||||
{
|
{
|
||||||
bool s = this->readFromJsonFiles(dir, whatToRead);
|
const CStatusMessageList msgs = this->readFromJsonFiles(dir, whatToRead);
|
||||||
Q_UNUSED(s);
|
if (msgs.isFailure())
|
||||||
|
{
|
||||||
|
CLogMessage::preformatted(msgs);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ namespace BlackCore
|
|||||||
bool areAllDataRead() const;
|
bool areAllDataRead() const;
|
||||||
|
|
||||||
//! Read from static DB data file
|
//! Read from static DB data file
|
||||||
bool readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries);
|
BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries);
|
||||||
|
|
||||||
//! Read from static DB data file
|
//! Read from static DB data file
|
||||||
bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries);
|
bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries);
|
||||||
|
|||||||
@@ -1197,23 +1197,24 @@ namespace BlackCore
|
|||||||
bool s = false;
|
bool s = false;
|
||||||
if (this->m_icaoDataReader)
|
if (this->m_icaoDataReader)
|
||||||
{
|
{
|
||||||
s = inBackground ?
|
if (inBackground) { return this->m_icaoDataReader->readFromJsonFilesInBackground(dir); }
|
||||||
this->m_icaoDataReader->readFromJsonFilesInBackground(dir) :
|
const CStatusMessageList msgs = this->m_icaoDataReader->readFromJsonFiles(dir);
|
||||||
this->m_icaoDataReader->readFromJsonFiles(dir);
|
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
|
||||||
|
s = msgs.isSuccess();
|
||||||
}
|
}
|
||||||
if (s && this->m_modelDataReader)
|
if (s && this->m_modelDataReader)
|
||||||
{
|
{
|
||||||
if(inBackground) { return this->m_modelDataReader->readFromJsonFilesInBackground(dir); }
|
if (inBackground) { return this->m_modelDataReader->readFromJsonFilesInBackground(dir); }
|
||||||
const CStatusMessageList msgs = this->m_modelDataReader->readFromJsonFiles(dir);
|
const CStatusMessageList msgs = this->m_modelDataReader->readFromJsonFiles(dir);
|
||||||
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
|
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
|
||||||
return msgs.isSuccess();
|
s = msgs.isSuccess();
|
||||||
}
|
}
|
||||||
if (s && this->m_airportDataReader)
|
if (s && this->m_airportDataReader)
|
||||||
{
|
{
|
||||||
if (inBackground) { return this->m_airportDataReader->readFromJsonFilesInBackground(dir); }
|
if (inBackground) { return this->m_airportDataReader->readFromJsonFilesInBackground(dir); }
|
||||||
const CStatusMessageList msgs = this->m_airportDataReader->readFromJsonFiles(dir);
|
const CStatusMessageList msgs = this->m_airportDataReader->readFromJsonFiles(dir);
|
||||||
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
|
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
|
||||||
return msgs.isSuccess();
|
s = msgs.isSuccess();
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user