Files
pilotclient/src/blackcore/vatsimmetarreader.cpp
Klaus Basan 8827b68b3b refs #452 signals in reader classes. adjusted METAR reader
* renamed to connectDataReadSignal
* removed unused individual signals, e.g. vatsimMetarsRead -> replace by data read signal + entity flag
* added data in METAR class (kept there, not in airspace monitor) -> reader aware
2015-11-19 21:00:15 +00:00

134 lines
4.4 KiB
C++

/* Copyright (C) 2015
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "vatsimmetarreader.h"
#include "blackmisc/network/entityflags.h"
#include "blackmisc/sequence.h"
#include "blackmisc/logmessage.h"
#include <QTextStream>
#include <QFile>
#include <QReadLocker>
#include <QWriteLocker>
using namespace BlackMisc;
using namespace BlackMisc::Network;
using namespace BlackMisc::Weather;
namespace BlackCore
{
CVatsimMetarReader::CVatsimMetarReader(QObject *owner, const QString &url) :
CThreadedReader(owner, "CVatsimMetarReader"),
m_metarUrl(url)
{
this->m_networkManager = new QNetworkAccessManager(this);
this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CVatsimMetarReader::ps_decodeMetars);
this->connect(this->m_updateTimer, &QTimer::timeout, this, &CVatsimMetarReader::ps_readMetars);
}
void CVatsimMetarReader::readInBackgroundThread()
{
bool s = QMetaObject::invokeMethod(this, "ps_readMetar");
Q_ASSERT_X(s, Q_FUNC_INFO, "Cannot invoke");
Q_UNUSED(s);
}
CMetarSet CVatsimMetarReader::getMetars() const
{
QReadLocker l(&m_lock);
return m_metars;
}
CMetar CVatsimMetarReader::getMetarForAirport(const Aviation::CAirportIcaoCode &icao) const
{
QReadLocker l(&m_lock);
return m_metars.getMetarForAirport(icao);
}
int CVatsimMetarReader::getMetarsCount() const
{
QReadLocker l(&m_lock);
return m_metars.size();
}
void CVatsimMetarReader::ps_readMetars()
{
this->threadAssertCheck();
QUrl url(this->m_metarUrl);
if (url.isEmpty()) return;
Q_ASSERT(this->m_networkManager);
QNetworkRequest request(url);
this->m_networkManager->get(request);
}
void CVatsimMetarReader::ps_decodeMetars(QNetworkReply *nwReplyPtr)
{
// wrap pointer, make sure any exit cleans up reply
// required to use delete later as object is created in a different thread
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> nwReply(nwReplyPtr);
this->threadAssertCheck();
// Worker thread, make sure to write no members here!
if (this->isFinished())
{
CLogMessage(this).debug() << Q_FUNC_INFO;
CLogMessage(this).info("terminated METAR decoding process"); // for users
return; // stop, terminate straight away, ending thread
}
if (nwReply->error() == QNetworkReply::NoError)
{
QString metarData = nwReply->readAll();
nwReply->close(); // close asap
CMetarSet metars;
QString invalidMetars;
int invalidLineCount = 0;
QTextStream lineReader(&metarData);
while (!lineReader.atEnd())
{
QString line = lineReader.readLine();
CMetar metar = m_metarDecoder.decode(line);
if (metar != CMetar())
{
metars.push_back(metar);
}
else
{
invalidMetars += line;
invalidMetars += "\n";
invalidLineCount++;
}
}
{
QWriteLocker l(&m_lock);
m_metars = metars;
}
// I could use those for logging, etc.
Q_UNUSED(invalidMetars);
if (invalidLineCount > 0)
{
CLogMessage(this).warning("Reading METARs failed for %1 entries") << invalidLineCount;
}
emit metarsRead(metars);
emit dataRead(CEntityFlags::MetarEntity, CEntityFlags::ReadFinished, metars.size());
}
else
{
// network error
CLogMessage(this).warning("Reading METARs failed %1 %2") << nwReply->errorString() << nwReply->url().toString();
nwReply->abort();
emit dataRead(CEntityFlags::MetarEntity, CEntityFlags::ReadFailed, 0);
}
} // method
} // namespace