10 #include <QNetworkReply>
11 #include <QScopedPointer>
12 #include <QScopedPointerDeleteLater>
14 #include <QStringList>
26 using namespace swift::misc::aviation;
27 using namespace swift::misc::network;
28 using namespace swift::misc::simulation;
29 using namespace swift::misc::physical_quantities;
32 namespace swift::core::vatsim
34 CVatsimStatusFileReader::CVatsimStatusFileReader(QObject *owner) :
CThreadedReader(owner,
"CVatsimStatusFileReader")
41 Q_ASSERT_X(
hasStarted(), Q_FUNC_INFO,
"Thread was not started yet!");
42 QPointer<CVatsimStatusFileReader> myself(
this);
44 if (!myself) {
return; }
53 void CVatsimStatusFileReader::read()
58 Q_ASSERT_X(
sApp, Q_FUNC_INFO,
"Missing application");
60 if (url.isEmpty()) {
return; }
61 CLogMessage(
this).
info(u
"Trigger read of VATSIM status file from '%1'") << url.toQString(
true);
65 void CVatsimStatusFileReader::parseVatsimFile(QNetworkReply *nwReplyPtr)
69 QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
76 CLogMessage(
this).
info(u
"Terminated VATSIM status file parsing process");
81 const QString urlString = nwReply->url().toString();
83 if (nwReply->error() == QNetworkReply::NoError)
85 const QString statusFileData = nwReply->readAll();
88 if (statusFileData.isEmpty())
return;
89 auto jsonDoc = QJsonDocument::fromJson(statusFileData.toUtf8());
90 if (jsonDoc.isEmpty()) {
return; }
98 if (
const QJsonArray dataUrls = jsonDoc[
"data"][
"v3"].toArray(); !dataUrls.empty())
100 dataFileUrl = QUrl(dataUrls.at(0).toString());
103 if (
const QJsonArray serverFileUrls = jsonDoc[
"data"][
"servers"].toArray(); !serverFileUrls.empty())
105 serverFileUrl = QUrl(serverFileUrls.at(0).toString());
108 if (
const QJsonArray metarUrls = jsonDoc[
"metar"].toArray(); !metarUrls.empty())
110 metarFileUrl = QUrl(metarUrls.at(0).toString());
115 const bool changed = vs.setUrls(dataFileUrl, serverFileUrl, metarFileUrl);
116 vs.setUtcTimestamp(QDateTime::currentDateTime());
119 else {
CLogMessage(
this).
info(u
"Read VATSIM status file from '%1'") << urlString; }
124 emit this->
dataRead(CEntityFlags::VatsimStatusFile, CEntityFlags::ReadFinished, statusFileData.count());
130 << nwReply->errorString() << urlString;
132 emit this->
dataRead(CEntityFlags::VatsimStatusFile, CEntityFlags::ReadFailed, 0);
SWIFT_CORE_EXPORT swift::core::CApplication * sApp
Single instance of application object.
data::CGlobalSetup getGlobalSetup() const
Global setup.
Support for threaded based reading and parsing tasks such as data files via http, or file system and ...
void threadAssertCheck() const
Make sure everything runs correctly in own thread.
void logNetworkReplyReceived(QNetworkReply *reply)
Network reply received, mark in m_urlReadLog.
QNetworkReply * getFromNetworkAndLog(const swift::misc::network::CUrl &url, const swift::misc::CSlot< void(QNetworkReply *)> &callback)
Get request from network, and log with m_urlReadLog.
bool doWorkCheck() const
Still enabled etc.?
const swift::misc::network::CUrl & getVatsimStatusFileUrl() const
VATSIM status file URL.
VATSIM data (servers, URLs) cached as last known good setup.
swift::misc::network::CUrl getDataFileUrl() const
Data file URL.
swift::misc::network::CUrl getMetarFileUrl() const
METAR URL.
void readInBackgroundThread()
Start reading in own thread.
void dataRead(swift::misc::network::CEntityFlags::Entity entity, swift::misc::network::CEntityFlags::ReadState state, int bytes)
Data have been read.
void statusFileRead(int bytes)
Data have been read.
T get() const
Get a copy of the current value.
CStatusMessage set(const typename Trait::type &value, qint64 timestamp=0)
Write a new value. Must be called from the thread in which the owner lives.
Class for emitting a log message.
static void preformatted(const CStatusMessage &statusMessage)
Sends a verbatim, preformatted message to the log.
Derived & warning(const char16_t(&format)[N])
Set the severity to warning, providing a format string.
Derived & debug()
Set the severity to debug.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
Streamable status message, e.g.
bool isFailure() const
Operation considered unsuccessful.
bool hasStarted() const
True if the worker has started.
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Core data traits (aka cached values) and classes.
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...