refs #853, JSON exception handling adjustments for airport reader

This commit is contained in:
Klaus Basan
2017-01-05 18:57:18 +01:00
committed by Mathew Sutcliffe
parent ee27ca4d44
commit e956986bf4
3 changed files with 43 additions and 17 deletions

View File

@@ -43,33 +43,57 @@ namespace BlackCore
if (dir.isEmpty() || whatToRead == CEntityFlags::NoEntity) { return false; }
QTimer::singleShot(0, this, [this, dir, whatToRead]()
{
bool s = this->readFromJsonFiles(dir, whatToRead);
Q_UNUSED(s);
CStatusMessageList msgs = this->readFromJsonFiles(dir, whatToRead);
if (msgs.isFailure())
{
CLogMessage::preformatted(msgs);
}
});
return true;
}
bool CAirportDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
CStatusMessageList CAirportDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
{
QDir directory(dir);
if (!directory.exists()) { return false; }
BlackMisc::Network::CEntityFlags::Entity reallyRead = CEntityFlags::NoEntity;
if (whatToRead.testFlag(CEntityFlags::AirportEntity))
const QString fileName = CFileUtils::appendFilePaths(dir, "airports.json");
if (!QFile::exists(fileName))
{
QString airportsJson(CFileUtils::readFileToString(CFileUtils::appendFilePaths(directory.absolutePath(), "airports.json")));
if (!airportsJson.isEmpty())
return CStatusMessage(this).error("File '%1' does not exist") << fileName;
}
whatToRead &= CEntityFlags::AirportEntity; // can handle these entities
if (whatToRead == CEntityFlags::NoEntity)
{
return CStatusMessage(this).info("'%1' No entity for this reader") << CEntityFlags::flagToString(whatToRead);
}
int c = 0;
CEntityFlags::Entity reallyRead = CEntityFlags::NoEntity;
const QString airportsJson(CFileUtils::readFileToString(fileName));
if (!airportsJson.isEmpty())
{
try
{
CAirportList airports;
airports.convertFromJson(airportsJson); //! \todo catch CJsonException or use convertFromJsonNoThrow
const int c = airports.size();
airports.convertFromJson(airportsJson);
c = airports.size();
this->m_airportCache.set(airports);
emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFinished, c);
reallyRead |= CEntityFlags::AirportEntity;
}
catch (const CJsonException &ex)
{
emit dataRead(CEntityFlags::AirportEntity, CEntityFlags::ReadFailed, 0);
return ex.toStatusMessage(this, QString("Reading airports from '%1'").arg(fileName));
}
}
if ((reallyRead & CEntityFlags::AirportEntity) == CEntityFlags::AirportEntity)
{
return CStatusMessage(this).info("Written %1 airports from '%2' to cache") << c << fileName;
}
else
{
return CStatusMessage(this).error("Not able to read airports from '%1'") << fileName;
}
return (reallyRead & CEntityFlags::DistributorLiveryModel) == whatToRead;
}
CEntityFlags::Entity CAirportDataReader::getSupportedEntities() const

View File

@@ -43,9 +43,10 @@ namespace BlackCore
int getAirportsCount() const;
//! Read from static data file
bool readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AirportEntity);
BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AirportEntity);
//! Read from static data file in background
//! \return succesfully started?
bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AirportEntity);
// base class overrides

View File

@@ -1209,9 +1209,10 @@ namespace BlackCore
}
if (s && this->m_airportDataReader)
{
s = inBackground ?
this->m_airportDataReader->readFromJsonFilesInBackground(dir) :
this->m_airportDataReader->readFromJsonFiles(dir);
if (inBackground) { return this->m_airportDataReader->readFromJsonFilesInBackground(dir); }
const CStatusMessageList msgs = this->m_airportDataReader->readFromJsonFiles(dir);
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
return msgs.isSuccess();
}
return s;
}