swift
Loading...
Searching...
No Matches
vatsimmetarreader.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QByteArray>
7#include <QMetaObject>
8#include <QNetworkReply>
9#include <QReadLocker>
10#include <QScopedPointer>
11#include <QScopedPointerDeleteLater>
12#include <QString>
13#include <QTextStream>
14#include <QTimer>
15#include <QUrl>
16#include <QWriteLocker>
17#include <QtGlobal>
18
19#include "core/application.h"
20#include "misc/logmessage.h"
23#include "misc/network/url.h"
24
25using namespace swift::misc;
26using namespace swift::misc::aviation;
27using namespace swift::misc::network;
28using namespace swift::misc::weather;
29using namespace swift::core::data;
30
31namespace swift::core::vatsim
32{
34 {
35 this->reloadSettings();
36 }
37
39 {
41 return m_metars;
42 }
43
45 {
47 return m_metars.getMetarForAirport(icao);
48 }
49
50 void CVatsimMetarReader::doWorkImpl() { this->read(); }
51
52 void CVatsimMetarReader::read()
53 {
54 this->threadAssertCheck();
55 if (!this->doWorkCheck())
56 {
57 return;
58 }
59
60 const CUrl url(sApp->getVatsimMetarUrl());
61 if (url.isEmpty())
62 {
63 return;
64 }
65 Q_ASSERT_X(sApp, Q_FUNC_INFO, "No Application");
66 this->getFromNetworkAndLog(url.withAppendedQuery("id=all"), { this, &CVatsimMetarReader::decodeMetars });
67 }
68
69 void CVatsimMetarReader::decodeMetars(QNetworkReply *nwReplyPtr)
70 {
71 // wrap pointer, make sure any exit cleans up reply
72 // required to use delete later as object is created in a different thread
74
75 // Worker thread, make sure to write thread safe!
76 this->threadAssertCheck();
77
78 if (!this->doWorkCheck())
79 {
80 CLogMessage(this).info(u"Terminated METAR decoding process"); // for users
81 return; // stop, terminate straight away, ending thread
82 }
83
84 this->logNetworkReplyReceived(nwReplyPtr);
85 const QUrl url = nwReply->url();
86 const QString metarUrl = url.toString();
87
88 if (nwReply->error() == QNetworkReply::NoError)
89 {
90 QString metarData = nwReply->readAll();
91 nwReply->close(); // close asap
92
93 if (metarData.isEmpty()) // Quick check by hash
94 {
95 CLogMessage(this).warning(u"No METAR data from '%1', skipped") << metarUrl;
96 return;
97 }
98 if (!this->didContentChange(metarData)) // Quick check by hash
99 {
100 CLogMessage(this).info(u"METAR file from '%1' has same content, skipped") << metarUrl;
101 return;
102 }
103
104 CMetarList metars;
105 int invalidLines = 0;
106 QTextStream lineReader(&metarData);
107 while (!lineReader.atEnd())
108 {
109 if (!this->doWorkCheck())
110 {
111 return;
112 }
113 const QString line = lineReader.readLine();
114 // some check for obvious errors
115 if (line.contains("<html"))
116 {
117 continue;
118 }
119 const CMetar metar = m_metarDecoder.decode(line);
120 if (metar != CMetar())
121 {
122 metars.push_back(metar);
123 }
124 else
125 {
126 invalidLines++;
127 }
128 }
129
130 CLogMessage(this).info(u"METARs: %1 Metars (invalid %2) from '%3'")
131 << metars.size() << invalidLines << metarUrl;
132 {
133 QWriteLocker l(&m_lock);
134 m_metars = metars;
135 }
136
137 emit metarsRead(metars);
139 }
140 else
141 {
142 // network error
143 CLogMessage(this).warning(u"Reading METARs failed '%1' for '%2'") << nwReply->errorString() << metarUrl;
144 nwReply->abort();
146 }
147 } // method
148
149 void CVatsimMetarReader::reloadSettings()
150 {
151 const CReaderSettings s = m_settings.get();
152 this->setInitialAndPeriodicTime(std::chrono::milliseconds(s.getInitialTime().toMs()),
153 std::chrono::milliseconds(s.getPeriodicTime().toMs()));
154 }
155} // namespace swift::core::vatsim
void threadAssertCheck() const
Make sure everything runs correctly in own thread.
void logNetworkReplyReceived(QNetworkReply *reply)
Network reply received, mark in m_urlReadLog.
QReadWriteLock m_lock
lock which can be used from the derived classes
bool didContentChange(const QString &content, int startPosition=-1)
Stores new content hash and returns if content changed (based on hash value.
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.?
CThreadedReaderPeriodic(QObject *owner, const QString &name)
Constructor.
void setInitialAndPeriodicTime(std::chrono::milliseconds initialTime, std::chrono::milliseconds periodicTime)
Set initial and periodic times Changes only apply after the next time the timer restarts.
void metarsRead(const swift::misc::weather::CMetarList &metars)
METARs have been read and converted to swift::misc::weather::CMetarList.
void dataRead(swift::misc::network::CEntityFlags::Entity entity, swift::misc::network::CEntityFlags::ReadState state, int number, const QUrl &url)
Data have been read.
virtual swift::misc::weather::CMetarList getMetars() const
Get METARs.
CVatsimMetarReader(QObject *owner)
Constructor.
virtual swift::misc::weather::CMetar getMetarForAirport(const swift::misc::aviation::CAirportIcaoCode &icao) const
Get METAR for airport.
void doWorkImpl()
This method does the actual work in the derived class.
const QObject * owner() const
Owner of the worker.
Definition worker.h:352
Class for emitting a log message.
Definition logmessage.h:27
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
size_type size() const
Returns number of elements in the sequence.
Definition sequence.h:273
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
Value object encapsulating information of airport ICAO data.
Value object encapsulating information of a location, kind of simplified CValueObject compliant versi...
Definition url.h:27
Value object encapsulating information about METAR FIXME: runway visibilities FIXME: runway wind shea...
Definition metar.h:38
Core data traits (aka cached values) and classes.
Free functions in swift::misc.
QObject(QObject *parent)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString toString(QUrl::FormattingOptions options) const const