refs #23, anticipating aircraft ICAO code from VATSIM data file

* helper methods
* renamed methods
This commit is contained in:
Klaus Basan
2014-05-31 01:31:16 +02:00
parent c7108a51f8
commit 9c5726dc13
9 changed files with 93 additions and 21 deletions

View File

@@ -26,6 +26,14 @@ namespace BlackCore
{ {
// update // update
CIndexVariantMap vm(CAircraft::IndexIcao, icaoData.toQVariant()); CIndexVariantMap vm(CAircraft::IndexIcao, icaoData.toQVariant());
if (!icaoData.hasAircraftDesignator())
{
// empty so far, try to fetch from data file
qDebug() << "Empty ICAO info for " << callsign << icaoData;
CAircraftIcao icaoDataDataFile = this->m_vatsimDataFileReader->getIcaoInfo(callsign);
if (!icaoDataDataFile.hasAircraftDesignator()) return; // give up!
vm = CIndexVariantMap(CAircraft::IndexIcao, icaoData.toQVariant());
}
this->m_aircraftsInRange.applyIf(BlackMisc::Predicates::MemberEqual<CAircraft>(&CAircraft::getCallsign, callsign), vm); this->m_aircraftsInRange.applyIf(BlackMisc::Predicates::MemberEqual<CAircraft>(&CAircraft::getCallsign, callsign), vm);
emit this->changedAircraftsInRange(); emit this->changedAircraftsInRange();
} }

View File

@@ -70,6 +70,7 @@ namespace BlackCore
foreach(CAircraft aircraft, this->m_aircraftsInRange) foreach(CAircraft aircraft, this->m_aircraftsInRange)
{ {
this->m_network->sendFrequencyQuery(aircraft.getCallsign()); this->m_network->sendFrequencyQuery(aircraft.getCallsign());
this->m_network->sendIcaoCodesQuery(aircraft.getCallsign());
} }
} }

View File

@@ -4,6 +4,8 @@
#include "blackmisc/nwserver.h" #include "blackmisc/nwserver.h"
#include "vatsimdatafilereader.h" #include "vatsimdatafilereader.h"
#include <QRegularExpression>
using namespace BlackMisc; using namespace BlackMisc;
using namespace BlackMisc::Aviation; using namespace BlackMisc::Aviation;
using namespace BlackMisc::Network; using namespace BlackMisc::Network;
@@ -62,6 +64,12 @@ namespace BlackCore
return this->getPilotsForCallsigns(callsigns); return this->getPilotsForCallsigns(callsigns);
} }
CAircraftIcao CVatsimDataFileReader::getIcaoInfo(const CCallsign &callsign)
{
CAircraft aircraft = this->m_aircrafts.findFirstByCallsign(callsign);
return aircraft.getIcaoInfo();
}
CUserList CVatsimDataFileReader::getControllersForCallsign(const CCallsign &callsign) CUserList CVatsimDataFileReader::getControllersForCallsign(const CCallsign &callsign)
{ {
CCallsignList callsigns; CCallsignList callsigns;
@@ -176,6 +184,25 @@ namespace BlackCore
CAircraftSituation situation(position, altitude); CAircraftSituation situation(position, altitude);
situation.setGroundspeed(CSpeed(groundspeed, CSpeedUnit::kts())); situation.setGroundspeed(CSpeed(groundspeed, CSpeedUnit::kts()));
CAircraft aircraft(user.getCallsign().getStringAsSet(), user, situation); CAircraft aircraft(user.getCallsign().getStringAsSet(), user, situation);
QString icaoCode = clientPartsMap["planned_aircraft"];
if (!icaoCode.isEmpty())
{
// http://uk.flightaware.com/about/faq_aircraft_flight_plan_suffix.rvt
// we expect something like H/B772/F B773 B773/F
static const QRegularExpression reg("/.");
icaoCode = icaoCode.replace(reg, "").trimmed().toUpper();
if (CAircraftIcao::isValidDesignator(icaoCode))
{
aircraft.setIcaoInfo(CAircraftIcao(icaoCode));
}
else
{
const QString w = QString("Illegal ICAO code in VATSIM data file: %1").arg(icaoCode);
qWarning(w.toLatin1());
}
}
this->m_aircrafts.push_back(aircraft); this->m_aircrafts.push_back(aircraft);
} }
else if (clientType.startsWith('a')) else if (clientType.startsWith('a'))

View File

@@ -71,6 +71,9 @@ namespace BlackCore
//! Users for callsign //! Users for callsign
BlackMisc::Network::CUserList getPilotsForCallsign(const BlackMisc::Aviation::CCallsign &callsign); BlackMisc::Network::CUserList getPilotsForCallsign(const BlackMisc::Aviation::CCallsign &callsign);
//! ICAO info for callsign
BlackMisc::Aviation::CAircraftIcao getIcaoInfo(const BlackMisc::Aviation::CCallsign &callsign);
private slots: private slots:
//! Data have been read //! Data have been read
void loadFinished(QNetworkReply *nwReply); void loadFinished(QNetworkReply *nwReply);

View File

@@ -87,6 +87,9 @@ namespace BlackMisc
//! Has valid id? //! Has valid id?
bool hasValidId() const { return this->m_pilot.hasValidId(); } bool hasValidId() const { return this->m_pilot.hasValidId(); }
//! Valid designators?
bool hasValidAircraftAndAirlineDesignator() const { return this->m_icao.hasAircraftAndAirlineDesignator(); }
/*! /*!
* Calculcate distance to plane, set it, and also return it * Calculcate distance to plane, set it, and also return it
* \param position calculated from this postion to my own aircraft * \param position calculated from this postion to my own aircraft

View File

@@ -1,6 +1,8 @@
#include "avaircrafticao.h" #include "avaircrafticao.h"
#include "blackmisc/blackmiscfreefunctions.h" #include "blackmisc/blackmiscfreefunctions.h"
#include <tuple> #include <tuple>
#include <QRegularExpression>
namespace BlackMisc namespace BlackMisc
{ {
@@ -12,7 +14,7 @@ namespace BlackMisc
QString CAircraftIcao::convertToQString(bool /** i18n **/) const QString CAircraftIcao::convertToQString(bool /** i18n **/) const
{ {
QString s(this->m_aircraftDesignator); QString s(this->m_aircraftDesignator);
s.append(" ").append(this->m_aircraftCombinedType); if (this->hasAircraftCombinedType()) s.append(" ").append(this->m_aircraftCombinedType);
if (this->hasAirlineDesignator()) s.append(" ").append(this->m_airlineDesignator); if (this->hasAirlineDesignator()) s.append(" ").append(this->m_airlineDesignator);
if (this->hasLivery()) s.append(" ").append(this->m_livery); if (this->hasLivery()) s.append(" ").append(this->m_livery);
if (this->hasAircraftColor()) s.append(" ").append(this->m_aircraftColor); if (this->hasAircraftColor()) s.append(" ").append(this->m_aircraftColor);
@@ -62,7 +64,7 @@ namespace BlackMisc
} }
/* /*
* As string? * As string
*/ */
QString CAircraftIcao::asString() const QString CAircraftIcao::asString() const
{ {
@@ -163,6 +165,16 @@ namespace BlackMisc
} }
} }
/*
* Valid designator?
*/
bool CAircraftIcao::isValidDesignator(const QString &designator)
{
static QRegularExpression regexp("^[A-Z]+[A-Z0-9]*$");
if (designator.length() < 2 || designator.length() > 5) return false;
return (regexp.match(designator).hasMatch());
}
/* /*
* Register metadata * Register metadata
*/ */

View File

@@ -25,6 +25,9 @@ namespace BlackMisc
//! Default constructor. //! Default constructor.
CAircraftIcao() {} CAircraftIcao() {}
//! Default constructor.
explicit CAircraftIcao(const QString &icao) : m_aircraftDesignator(icao.trimmed().toUpper()) {}
/*! /*!
* Constructor. * Constructor.
* \param icao "B737" * \param icao "B737"
@@ -62,7 +65,7 @@ namespace BlackMisc
bool hasAirlineDesignator() const { return !this->m_airlineDesignator.isEmpty(); } bool hasAirlineDesignator() const { return !this->m_airlineDesignator.isEmpty(); }
//! Airline and Aircraft designator? //! Airline and Aircraft designator?
bool hasAircraftAndAirlineDsignator() const { return this->hasAirlineDesignator() && this->hasAircraftDesignator(); } bool hasAircraftAndAirlineDesignator() const { return this->hasAirlineDesignator() && this->hasAircraftDesignator(); }
//! Get livery //! Get livery
const QString &getLivery() const { return this->m_livery; } const QString &getLivery() const { return this->m_livery; }
@@ -149,6 +152,9 @@ namespace BlackMisc
//! \copydoc CValueObject::setPropertyByIndex(variant, index) //! \copydoc CValueObject::setPropertyByIndex(variant, index)
virtual void setPropertyByIndex(const QVariant &variant, int index) override; virtual void setPropertyByIndex(const QVariant &variant, int index) override;
//! Valid designator?
static bool isValidDesignator(const QString &designator);
protected: protected:
//! \copydoc CValueObject::convertToQString //! \copydoc CValueObject::convertToQString
virtual QString convertToQString(bool i18n = false) const override; virtual QString convertToQString(bool i18n = false) const override;

View File

@@ -47,6 +47,16 @@ namespace BlackMisc
return this->findBy(&CAircraft::getCallsign, callsign); return this->findBy(&CAircraft::getCallsign, callsign);
} }
/*
* Find by callsign
*/
CAircraft CAircraftList::findFirstByCallsign(const CCallsign &callsign, const CAircraft &ifNotFound) const
{
CAircraftList aircrafts = this->findByCallsign(callsign);
if (aircrafts.isEmpty()) return ifNotFound;
return aircrafts.front();
}
/* /*
* All pilots * All pilots
*/ */
@@ -75,27 +85,26 @@ namespace BlackMisc
/* /*
* Merge with aircraft * Merge with aircraft
*/ */
int CAircraftList::updateFromVatsimDataFileAircraft(CAircraft &aircraftToBeUpdated) const bool CAircraftList::updateFromVatsimDataFileAircraft(CAircraft &aircraftToBeUpdated) const
{ {
if (this->isEmpty()) return 0; if (this->isEmpty()) return false;
if (aircraftToBeUpdated.hasValidRealName() && aircraftToBeUpdated.hasValidId()) return 0; if (aircraftToBeUpdated.hasValidRealName() && aircraftToBeUpdated.hasValidId() && aircraftToBeUpdated.hasValidAircraftAndAirlineDesignator()) return false;
int c = 0; CAircraft currentDataFileAircraft = this->findFirstByCallsign(aircraftToBeUpdated.getCallsign());
for (auto i = this->begin(); i != this->end(); ++i) if (currentDataFileAircraft.getCallsign().isEmpty()) return false;
{
CAircraft currentDataFileAircraft = *i;
if (currentDataFileAircraft.getCallsign() != aircraftToBeUpdated.getCallsign()) continue;
CUser user = aircraftToBeUpdated.getPilot(); CUser user = aircraftToBeUpdated.getPilot();
if (!aircraftToBeUpdated.hasValidRealName()) user.setRealName(currentDataFileAircraft.getPilotRealname()); if (!aircraftToBeUpdated.hasValidRealName()) user.setRealName(currentDataFileAircraft.getPilotRealname());
if (!aircraftToBeUpdated.hasValidId()) user.setId(currentDataFileAircraft.getPilotId()); if (!aircraftToBeUpdated.hasValidId()) user.setId(currentDataFileAircraft.getPilotId());
aircraftToBeUpdated.setPilot(user); aircraftToBeUpdated.setPilot(user);
c++;
}
// normally 1 expected, as I should find CAircraftIcao icao = aircraftToBeUpdated.getIcaoInfo();
// only one online station for this booking CAircraftIcao dataFileIcao = currentDataFileAircraft.getIcaoInfo();
return c; if (!icao.hasAircraftDesignator()) icao.setAircraftDesignator(dataFileIcao.getAircraftDesignator());
if (!icao.hasAirlineDesignator()) icao.setAirlineDesignator(dataFileIcao.getAirlineDesignator());
if (!icao.hasAircraftCombinedType()) icao.setAircraftCombinedType(dataFileIcao.getAircraftCombinedType());
aircraftToBeUpdated.setIcaoInfo(icao);
return true;
} }
} // namespace } // namespace
} // namespace } // namespace

View File

@@ -43,6 +43,9 @@ namespace BlackMisc
//! Find 0..n stations by callsign //! Find 0..n stations by callsign
CAircraftList findByCallsign(const CCallsign &callsign) const; CAircraftList findByCallsign(const CCallsign &callsign) const;
//! Find the first aircraft by callsign, if none return given one
CAircraft findFirstByCallsign(const CCallsign &callsign, const CAircraft &ifNotFound = CAircraft()) const;
//! All pilots (with valid data) //! All pilots (with valid data)
BlackMisc::Network::CUserList getPilots() const; BlackMisc::Network::CUserList getPilots() const;
@@ -59,7 +62,7 @@ namespace BlackMisc
//! \brief Update aircraft with data from VATSIM data file //! \brief Update aircraft with data from VATSIM data file
//! \remarks The list used needs to contain the VATSIM data file objects //! \remarks The list used needs to contain the VATSIM data file objects
int updateFromVatsimDataFileAircraft(CAircraft &aircraftToBeUpdated) const; bool updateFromVatsimDataFileAircraft(CAircraft &aircraftToBeUpdated) const;
}; };
} //namespace } //namespace