mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 08:36:52 +08:00
Ref T237, init caches from resource files (means the files delivered with swift)
* initFromLocalResourceFiles reads all local DB data * made 2 functions virtual readFromJsonFiles, readFromJsonFilesInBackground
This commit is contained in:
@@ -48,12 +48,9 @@ namespace BlackCore
|
||||
//! \threadsafe
|
||||
int getAirportsCount() const;
|
||||
|
||||
//! Read from static data file
|
||||
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);
|
||||
// data read from local data
|
||||
virtual BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
virtual bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
|
||||
// base class overrides
|
||||
virtual BlackMisc::Network::CEntityFlags::Entity getSupportedEntities() const override;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "blackcore/application.h"
|
||||
#include "blackmisc/db/datastoreutility.h"
|
||||
#include "blackmisc/network/networkutils.h"
|
||||
#include "blackmisc/directoryutils.h"
|
||||
#include "blackmisc/logcategory.h"
|
||||
#include "blackmisc/logcategorylist.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
@@ -580,7 +581,7 @@ namespace BlackCore
|
||||
bool CDatabaseReader::hasCacheTimestampNewerThan(CEntityFlags::Entity entity, const QDateTime &threshold) const
|
||||
{
|
||||
const QDateTime ts = this->getCacheTimestamp(entity);
|
||||
if (!ts.isValid()) return false;
|
||||
if (!ts.isValid()) { return false; }
|
||||
return ts > threshold;
|
||||
}
|
||||
|
||||
@@ -589,6 +590,29 @@ namespace BlackCore
|
||||
return m_statusMessage;
|
||||
}
|
||||
|
||||
CStatusMessageList CDatabaseReader::initFromLocalResourceFiles()
|
||||
{
|
||||
static const QDateTime threshold = QDateTime::currentDateTimeUtc().addYears(-1);
|
||||
const QSet<CEntityFlags::Entity> eSet = CEntityFlags::asSingleEntities(this->getSupportedEntities());
|
||||
|
||||
CStatusMessageList msgs;
|
||||
CEntityFlags::Entity entities = CEntityFlags::NoEntity;
|
||||
for (CEntityFlags::Entity e : eSet)
|
||||
{
|
||||
if (this->hasCacheTimestampNewerThan(e, threshold))
|
||||
{
|
||||
entities |= e;
|
||||
}
|
||||
{
|
||||
msgs.push_back(CStatusMessage(this).info("Will not init from local file, there are already data for '%1'") << CEntityFlags::flagToString(e));
|
||||
}
|
||||
}
|
||||
|
||||
const CStatusMessageList readMsgs = this->readFromJsonFiles(CDirectoryUtils::staticDbFilesDirectory(), entities);
|
||||
msgs.push_back(readMsgs);
|
||||
return readMsgs;
|
||||
}
|
||||
|
||||
void CDatabaseReader::setReplyStatus(QNetworkReply::NetworkError status, const QString &message)
|
||||
{
|
||||
QWriteLocker wl(&m_statusLock);
|
||||
|
||||
@@ -257,6 +257,16 @@ namespace BlackCore
|
||||
//! Severity used for log messages in case of no URLs
|
||||
void setSeverityNoWorkingUrl(BlackMisc::CStatusMessage::StatusSeverity s) { m_severityNoWorkingUrl = s; }
|
||||
|
||||
//! Init from local resource file
|
||||
//! \remark normally used after installation for a 1st time init
|
||||
BlackMisc::CStatusMessageList initFromLocalResourceFiles();
|
||||
|
||||
//! Data read from local data
|
||||
virtual BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) = 0;
|
||||
|
||||
//! Data read from local data
|
||||
virtual bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) = 0;
|
||||
|
||||
//! Log categories
|
||||
static const BlackMisc::CLogCategoryList &getLogCategories();
|
||||
|
||||
|
||||
@@ -397,7 +397,7 @@ namespace BlackCore
|
||||
|
||||
CStatusMessageList CIcaoDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
|
||||
{
|
||||
QDir directory(dir);
|
||||
const QDir directory(dir);
|
||||
if (!directory.exists())
|
||||
{
|
||||
return CStatusMessage(this).error("Missing directory '%1'") << dir;
|
||||
|
||||
@@ -133,15 +133,13 @@ namespace BlackCore
|
||||
//! \threadsafe
|
||||
bool areAllDataRead() const;
|
||||
|
||||
//! Read from static DB data file
|
||||
BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries);
|
||||
|
||||
//! Read from static DB data file
|
||||
bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::AllIcaoAndCountries);
|
||||
|
||||
//! Write to static DB data file
|
||||
bool writeToJsonFiles(const QString &dir) const;
|
||||
|
||||
// data read from local data
|
||||
virtual BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
virtual bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
|
||||
// cache handling for base class
|
||||
virtual BlackMisc::Network::CEntityFlags::Entity getSupportedEntities() const override;
|
||||
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
|
||||
@@ -217,6 +217,22 @@ namespace BlackCore
|
||||
return CUrl();
|
||||
}
|
||||
|
||||
CStatusMessageList CInfoDataReader::readFromJsonFiles(const QString &dir, CEntityFlags::Entity whatToRead)
|
||||
{
|
||||
Q_UNUSED(dir);
|
||||
Q_UNUSED(whatToRead);
|
||||
Q_ASSERT_X(false, Q_FUNC_INFO, "Not supported");
|
||||
return CStatusMessage(this).error("Not supported");
|
||||
}
|
||||
|
||||
bool CInfoDataReader::readFromJsonFilesInBackground(const QString &dir, CEntityFlags::Entity whatToRead)
|
||||
{
|
||||
Q_UNUSED(dir);
|
||||
Q_UNUSED(whatToRead);
|
||||
Q_ASSERT_X(false, Q_FUNC_INFO, "Not supported");
|
||||
return false;
|
||||
}
|
||||
|
||||
CEntityFlags::Entity CInfoDataReader::getSupportedEntities() const
|
||||
{
|
||||
return this->getEntityForMode();
|
||||
|
||||
@@ -53,6 +53,10 @@ namespace BlackCore
|
||||
//! URL depending on mode, i.e. shared/DB
|
||||
BlackMisc::Network::CUrl getInfoObjectsUrl() const;
|
||||
|
||||
// data read from local data
|
||||
virtual BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
virtual bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
|
||||
// cache handling for base class: no cache handling here in that case
|
||||
virtual BlackMisc::Network::CEntityFlags::Entity getSupportedEntities() const override;
|
||||
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
|
||||
@@ -123,11 +123,9 @@ namespace BlackCore
|
||||
//! \threadsafe
|
||||
bool areAllDataRead() const;
|
||||
|
||||
//! Read from local JSON file
|
||||
BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::DistributorLiveryModel);
|
||||
|
||||
//! Read from static DB data file
|
||||
bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead = BlackMisc::Network::CEntityFlags::DistributorLiveryModel);
|
||||
// data read from local data
|
||||
virtual BlackMisc::CStatusMessageList readFromJsonFiles(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
virtual bool readFromJsonFilesInBackground(const QString &dir, BlackMisc::Network::CEntityFlags::Entity whatToRead) override;
|
||||
|
||||
//! Write to JSON file
|
||||
bool writeToJsonFiles(const QString &dir) const;
|
||||
|
||||
@@ -1406,7 +1406,7 @@ namespace BlackCore
|
||||
|
||||
if (this->getAirportsCount() > 0)
|
||||
{
|
||||
QString json(QJsonDocument(this->getAirports().toJson()).toJson());
|
||||
const QString json(QJsonDocument(this->getAirports().toJson()).toJson());
|
||||
const bool s = CFileUtils::writeStringToFileInBackground(json, CFileUtils::appendFilePaths(directory.absolutePath(), "airports.json"));
|
||||
if (!s) { return false; }
|
||||
}
|
||||
@@ -1423,22 +1423,22 @@ namespace BlackCore
|
||||
bool s = false;
|
||||
if (m_icaoDataReader)
|
||||
{
|
||||
if (inBackground) { return m_icaoDataReader->readFromJsonFilesInBackground(dir); }
|
||||
const CStatusMessageList msgs = m_icaoDataReader->readFromJsonFiles(dir);
|
||||
if (inBackground) { return m_icaoDataReader->readFromJsonFilesInBackground(dir, m_icaoDataReader->getSupportedEntities()); }
|
||||
const CStatusMessageList msgs = m_icaoDataReader->readFromJsonFiles(dir, m_icaoDataReader->getSupportedEntities());
|
||||
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
|
||||
s = msgs.isSuccess();
|
||||
}
|
||||
if (s && m_modelDataReader)
|
||||
{
|
||||
if (inBackground) { return m_modelDataReader->readFromJsonFilesInBackground(dir); }
|
||||
const CStatusMessageList msgs = m_modelDataReader->readFromJsonFiles(dir);
|
||||
if (inBackground) { return m_modelDataReader->readFromJsonFilesInBackground(dir, m_modelDataReader->getSupportedEntities()); }
|
||||
const CStatusMessageList msgs = m_modelDataReader->readFromJsonFiles(dir, m_modelDataReader->getSupportedEntities());
|
||||
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
|
||||
s = msgs.isSuccess();
|
||||
}
|
||||
if (s && m_airportDataReader)
|
||||
{
|
||||
if (inBackground) { return m_airportDataReader->readFromJsonFilesInBackground(dir); }
|
||||
const CStatusMessageList msgs = m_airportDataReader->readFromJsonFiles(dir);
|
||||
if (inBackground) { return m_airportDataReader->readFromJsonFilesInBackground(dir, m_airportDataReader->getSupportedEntities()); }
|
||||
const CStatusMessageList msgs = m_airportDataReader->readFromJsonFiles(dir, m_airportDataReader->getSupportedEntities());
|
||||
if (msgs.isFailure()) { CLogMessage::preformatted(msgs); }
|
||||
s = msgs.isSuccess();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user