mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-31 12:55:33 +08:00
refs #23, anticipating aircraft ICAO code from VATSIM data file
* helper methods * renamed methods
This commit is contained in:
@@ -26,6 +26,14 @@ namespace BlackCore
|
||||
{
|
||||
// update
|
||||
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);
|
||||
emit this->changedAircraftsInRange();
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ namespace BlackCore
|
||||
foreach(CAircraft aircraft, this->m_aircraftsInRange)
|
||||
{
|
||||
this->m_network->sendFrequencyQuery(aircraft.getCallsign());
|
||||
this->m_network->sendIcaoCodesQuery(aircraft.getCallsign());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include "blackmisc/nwserver.h"
|
||||
#include "vatsimdatafilereader.h"
|
||||
|
||||
#include <QRegularExpression>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Aviation;
|
||||
using namespace BlackMisc::Network;
|
||||
@@ -62,6 +64,12 @@ namespace BlackCore
|
||||
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)
|
||||
{
|
||||
CCallsignList callsigns;
|
||||
@@ -176,6 +184,25 @@ namespace BlackCore
|
||||
CAircraftSituation situation(position, altitude);
|
||||
situation.setGroundspeed(CSpeed(groundspeed, CSpeedUnit::kts()));
|
||||
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);
|
||||
}
|
||||
else if (clientType.startsWith('a'))
|
||||
|
||||
@@ -71,6 +71,9 @@ namespace BlackCore
|
||||
//! Users for 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:
|
||||
//! Data have been read
|
||||
void loadFinished(QNetworkReply *nwReply);
|
||||
|
||||
@@ -87,6 +87,9 @@ namespace BlackMisc
|
||||
//! Has valid id?
|
||||
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
|
||||
* \param position calculated from this postion to my own aircraft
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "avaircrafticao.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
|
||||
#include <tuple>
|
||||
#include <QRegularExpression>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -12,7 +14,7 @@ namespace BlackMisc
|
||||
QString CAircraftIcao::convertToQString(bool /** i18n **/) const
|
||||
{
|
||||
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->hasLivery()) s.append(" ").append(this->m_livery);
|
||||
if (this->hasAircraftColor()) s.append(" ").append(this->m_aircraftColor);
|
||||
@@ -62,7 +64,7 @@ namespace BlackMisc
|
||||
}
|
||||
|
||||
/*
|
||||
* As string?
|
||||
* As string
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
||||
@@ -25,6 +25,9 @@ namespace BlackMisc
|
||||
//! Default constructor.
|
||||
CAircraftIcao() {}
|
||||
|
||||
//! Default constructor.
|
||||
explicit CAircraftIcao(const QString &icao) : m_aircraftDesignator(icao.trimmed().toUpper()) {}
|
||||
|
||||
/*!
|
||||
* Constructor.
|
||||
* \param icao "B737"
|
||||
@@ -62,7 +65,7 @@ namespace BlackMisc
|
||||
bool hasAirlineDesignator() const { return !this->m_airlineDesignator.isEmpty(); }
|
||||
|
||||
//! Airline and Aircraft designator?
|
||||
bool hasAircraftAndAirlineDsignator() const { return this->hasAirlineDesignator() && this->hasAircraftDesignator(); }
|
||||
bool hasAircraftAndAirlineDesignator() const { return this->hasAirlineDesignator() && this->hasAircraftDesignator(); }
|
||||
|
||||
//! Get livery
|
||||
const QString &getLivery() const { return this->m_livery; }
|
||||
@@ -149,6 +152,9 @@ namespace BlackMisc
|
||||
//! \copydoc CValueObject::setPropertyByIndex(variant, index)
|
||||
virtual void setPropertyByIndex(const QVariant &variant, int index) override;
|
||||
|
||||
//! Valid designator?
|
||||
static bool isValidDesignator(const QString &designator);
|
||||
|
||||
protected:
|
||||
//! \copydoc CValueObject::convertToQString
|
||||
virtual QString convertToQString(bool i18n = false) const override;
|
||||
|
||||
@@ -47,6 +47,16 @@ namespace BlackMisc
|
||||
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
|
||||
*/
|
||||
@@ -75,27 +85,26 @@ namespace BlackMisc
|
||||
/*
|
||||
* Merge with aircraft
|
||||
*/
|
||||
int CAircraftList::updateFromVatsimDataFileAircraft(CAircraft &aircraftToBeUpdated) const
|
||||
bool CAircraftList::updateFromVatsimDataFileAircraft(CAircraft &aircraftToBeUpdated) const
|
||||
{
|
||||
if (this->isEmpty()) return 0;
|
||||
if (aircraftToBeUpdated.hasValidRealName() && aircraftToBeUpdated.hasValidId()) return 0;
|
||||
if (this->isEmpty()) return false;
|
||||
if (aircraftToBeUpdated.hasValidRealName() && aircraftToBeUpdated.hasValidId() && aircraftToBeUpdated.hasValidAircraftAndAirlineDesignator()) return false;
|
||||
|
||||
int c = 0;
|
||||
for (auto i = this->begin(); i != this->end(); ++i)
|
||||
{
|
||||
CAircraft currentDataFileAircraft = *i;
|
||||
if (currentDataFileAircraft.getCallsign() != aircraftToBeUpdated.getCallsign()) continue;
|
||||
CAircraft currentDataFileAircraft = this->findFirstByCallsign(aircraftToBeUpdated.getCallsign());
|
||||
if (currentDataFileAircraft.getCallsign().isEmpty()) return false;
|
||||
|
||||
CUser user = aircraftToBeUpdated.getPilot();
|
||||
if (!aircraftToBeUpdated.hasValidRealName()) user.setRealName(currentDataFileAircraft.getPilotRealname());
|
||||
if (!aircraftToBeUpdated.hasValidId()) user.setId(currentDataFileAircraft.getPilotId());
|
||||
aircraftToBeUpdated.setPilot(user);
|
||||
c++;
|
||||
}
|
||||
CUser user = aircraftToBeUpdated.getPilot();
|
||||
if (!aircraftToBeUpdated.hasValidRealName()) user.setRealName(currentDataFileAircraft.getPilotRealname());
|
||||
if (!aircraftToBeUpdated.hasValidId()) user.setId(currentDataFileAircraft.getPilotId());
|
||||
aircraftToBeUpdated.setPilot(user);
|
||||
|
||||
// normally 1 expected, as I should find
|
||||
// only one online station for this booking
|
||||
return c;
|
||||
CAircraftIcao icao = aircraftToBeUpdated.getIcaoInfo();
|
||||
CAircraftIcao dataFileIcao = currentDataFileAircraft.getIcaoInfo();
|
||||
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
|
||||
|
||||
@@ -43,6 +43,9 @@ namespace BlackMisc
|
||||
//! Find 0..n stations by callsign
|
||||
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)
|
||||
BlackMisc::Network::CUserList getPilots() const;
|
||||
|
||||
@@ -59,7 +62,7 @@ namespace BlackMisc
|
||||
|
||||
//! \brief Update aircraft with data from VATSIM data file
|
||||
//! \remarks The list used needs to contain the VATSIM data file objects
|
||||
int updateFromVatsimDataFileAircraft(CAircraft &aircraftToBeUpdated) const;
|
||||
bool updateFromVatsimDataFileAircraft(CAircraft &aircraftToBeUpdated) const;
|
||||
};
|
||||
|
||||
} //namespace
|
||||
|
||||
Reference in New Issue
Block a user