refs #395, improved synchronization of booked and online stations

* moved sync functionality into CAtcStation
* consolidated function names
This commit is contained in:
Klaus Basan
2015-04-22 23:50:28 +02:00
committed by Mathew Sutcliffe
parent f7158f17f9
commit 79e2a7b805
8 changed files with 161 additions and 142 deletions

View File

@@ -30,7 +30,7 @@ namespace BlackCore
: QObject(parent),
COwnAircraftAwareReadOnly(ownAircraftProvider),
m_network(network), m_vatsimBookingReader(bookings), m_vatsimDataFileReader(dataFile),
m_analyzer(new CAirspaceAnalyzer(network, this))
m_analyzer(new CAirspaceAnalyzer(ownAircraftProvider, this, network, this))
{
this->connect(this->m_network, &INetwork::atcPositionUpdate, this, &CAirspaceMonitor::ps_atcPositionUpdate);
this->connect(this->m_network, &INetwork::atisReplyReceived, this, &CAirspaceMonitor::ps_atisReceived);
@@ -549,15 +549,19 @@ namespace BlackCore
void CAirspaceMonitor::ps_receivedBookings(const CAtcStationList &bookedStations)
{
Q_ASSERT(BlackCore::isCurrentThreadCreatingThread(this));
this->m_atcStationsBooked.clear();
foreach(CAtcStation bookedStation, bookedStations)
if (bookedStations.isEmpty())
{
// complete by VATSIM data file data
this->m_vatsimDataFileReader->getAtcStations().updateFromVatsimDataFileStation(bookedStation);
// exchange booking and online data, both sides are updated
this->m_atcStationsOnline.mergeWithBooking(bookedStation);
// into list
this->m_atcStationsBooked.push_back(bookedStation);
this->m_atcStationsBooked.clear();
}
else
{
CAtcStationList newBookedStations(bookedStations); // modifyable copy
for (CAtcStation &bookedStation : newBookedStations)
{
// exchange booking and online data, both sides are updated
this->m_atcStationsOnline.syncronizeWithBookedStation(bookedStation);
}
this->m_atcStationsBooked = newBookedStations;
}
emit this->changedAtcStationsBooked(); // all booked stations reloaded
}
@@ -614,17 +618,26 @@ namespace BlackCore
CAtcStationList stationsWithCallsign = this->m_atcStationsOnline.findByCallsign(callsign);
if (stationsWithCallsign.isEmpty())
{
// new station
CAtcStation station;
// new station, init with data from data file
CAtcStation station(this->m_vatsimDataFileReader->getAtcStationsForCallsign(callsign).frontOrDefault());
station.setCallsign(callsign);
station.setRange(range);
station.setFrequency(frequency);
station.setPosition(position);
station.setOnline(true);
station.calculcateDistanceAndBearingToOwnAircraft(ownAircraft().getPosition());
this->m_vatsimDataFileReader->getAtcStations().updateFromVatsimDataFileStation(station); // prefill
// sync with bookings
if (this->m_atcStationsBooked.containsCallsign(callsign))
{
CAtcStation bookedStation(this->m_atcStationsBooked.findFirstByCallsign(callsign));
station.syncronizeWithBookedStation(bookedStation);
this->m_atcStationsBooked.replaceIf(&CAtcStation::getCallsign, callsign, bookedStation);
}
this->m_atcStationsOnline.push_back(station);
// subsequent queries
if (this->m_network->isConnected())
{
emit this->m_network->sendRealNameQuery(callsign);
@@ -633,7 +646,7 @@ namespace BlackCore
}
emit this->changedAtcStationsOnline();
// Remark: this->changedAtcStationOnlineConnectionStatus(station, true);
// Remark: this->changedAtcStationOnlineConnectionStatus
// will be sent in psFsdAtisVoiceRoomReceived
}
else

View File

@@ -229,14 +229,20 @@ namespace BlackCore
private slots:
//! Create aircraft in range, this is the only place where a new aircraft should be added
void ps_aircraftUpdateReceived(const BlackMisc::Aviation::CAircraftSituation &situation, const BlackMisc::Aviation::CTransponder &transponder);
void ps_aircraftInterimUpdateReceived(const BlackMisc::Aviation::CAircraftSituation &situation);
//! Create ATC station, this is the only place where an online ATC station should be added
void ps_atcPositionUpdate(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::PhysicalQuantities::CFrequency &frequency, const BlackMisc::Geo::CCoordinateGeodetic &position, const BlackMisc::PhysicalQuantities::CLength &range);
//! Send the information if aircraft and(!) client are available
//! \note it can take some time to obtain all data for model matching, so function recursively calls itself if something is still missing (trial)
void ps_sendReadyForModelMatching(const BlackMisc::Aviation::CCallsign &callsign, int trial);
void ps_realNameReplyReceived(const BlackMisc::Aviation::CCallsign &callsign, const QString &realname);
void ps_capabilitiesReplyReceived(const BlackMisc::Aviation::CCallsign &callsign, quint32 flags);
void ps_customFSinnPacketReceived(const BlackMisc::Aviation::CCallsign &callsign, const QString &p1, const QString &aircraftDesignator, const QString &combinedAircraftType, const QString &model);
void ps_serverReplyReceived(const BlackMisc::Aviation::CCallsign &callsign, const QString &server);
void ps_metarReceived(const QString &metarMessage);
void ps_flightPlanReceived(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::Aviation::CFlightPlan &flightPlan);
void ps_atcPositionUpdate(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::PhysicalQuantities::CFrequency &frequency, const BlackMisc::Geo::CCoordinateGeodetic &position, const BlackMisc::PhysicalQuantities::CLength &range);
void ps_atcControllerDisconnected(const BlackMisc::Aviation::CCallsign &callsign);
void ps_atisReceived(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::Aviation::CInformationMessage &atisMessage);
void ps_atisVoiceRoomReceived(const BlackMisc::Aviation::CCallsign &callsign, const QString &url);
@@ -247,10 +253,7 @@ namespace BlackCore
void ps_receivedBookings(const BlackMisc::Aviation::CAtcStationList &bookedStations);
void ps_receivedDataFile();
void ps_aircraftConfigReceived(const BlackMisc::Aviation::CCallsign &callsign, const QJsonObject &jsonObject, bool isFull);
//! Send the information if aircraft and(!) client are vailable
void ps_sendReadyForModelMatching(const BlackMisc::Aviation::CCallsign &callsign, int trial);
void ps_aircraftInterimUpdateReceived(const BlackMisc::Aviation::CAircraftSituation &situation);
void ps_sendInterimPosition();
};

View File

@@ -45,6 +45,17 @@ namespace BlackCore
return this->m_atcStations;
}
CAtcStationList CVatsimDataFileReader::getAtcStationsForCallsign(const CCallsign &callsign) const
{
CCallsignSet cs({callsign});
return this->getAtcStationsForCallsigns(cs);
}
CAtcStationList CVatsimDataFileReader::getAtcStationsForCallsigns(const CCallsignSet &callsigns) const
{
return this->getAtcStations().findByCallsigns(callsigns);
}
CServerList CVatsimDataFileReader::getVoiceServers() const
{
QReadLocker rl(&this->m_lock);
@@ -64,8 +75,7 @@ namespace BlackCore
CUserList CVatsimDataFileReader::getPilotsForCallsign(const CCallsign &callsign)
{
CCallsignSet callsigns;
callsigns.push_back(callsign);
CCallsignSet callsigns({callsign});
return this->getPilotsForCallsigns(callsigns);
}
@@ -95,16 +105,8 @@ namespace BlackCore
CUserList CVatsimDataFileReader::getControllersForCallsign(const CCallsign &callsign)
{
CCallsignSet callsigns;
callsigns.push_back(callsign);
return this->getControllersForCallsigns(callsigns);
}
CUserList CVatsimDataFileReader::getUsersForCallsign(const CCallsign &callsign)
{
CCallsignSet callsigns;
callsigns.push_back(callsign);
return this->getUsersForCallsigns(callsigns);
CCallsignSet cs({callsign});
return this->getControllersForCallsigns(cs);
}
CUserList CVatsimDataFileReader::getControllersForCallsigns(const CCallsignSet &callsigns)
@@ -112,6 +114,12 @@ namespace BlackCore
return this->getAtcStations().findByCallsigns(callsigns).transform(Predicates::MemberTransform(&CAtcStation::getController));
}
CUserList CVatsimDataFileReader::getUsersForCallsign(const CCallsign &callsign)
{
CCallsignSet callsigns({callsign});
return this->getUsersForCallsigns(callsigns);
}
CUserList CVatsimDataFileReader::getUsersForCallsigns(const CCallsignSet &callsigns)
{
CUserList users;

View File

@@ -28,9 +28,7 @@
namespace BlackCore
{
/*!
* Read bookings from VATSIM
*/
//! Read bookings from VATSIM
class BLACKCORE_EXPORT CVatsimDataFileReader : public BlackMisc::CThreadedReader
{
Q_OBJECT
@@ -39,14 +37,22 @@ namespace BlackCore
//! Constructor
explicit CVatsimDataFileReader(QObject *owner, const QStringList &urls);
//! Get aircrafts
//! Get aircraft
//! \threadsafe
BlackMisc::Aviation::CAircraftList getAircraft() const;
//! Get aircrafts
//! Get ATC station
//! \threadsafe
BlackMisc::Aviation::CAtcStationList getAtcStations() const;
//! Get ATC stations for callsign
//! \threadsafe
BlackMisc::Aviation::CAtcStationList getAtcStationsForCallsign(const BlackMisc::Aviation::CCallsign &callsign) const;
//! Get ATC stations for callsigns
//! \threadsafe
BlackMisc::Aviation::CAtcStationList getAtcStationsForCallsigns(const BlackMisc::Aviation::CCallsignSet &callsigns) const;
//! Get all voice servers
//! \threadsafe
BlackMisc::Network::CServerList getVoiceServers() const;
@@ -103,7 +109,7 @@ namespace BlackCore
private:
QNetworkAccessManager *m_networkManager = nullptr;
QStringList m_serviceUrls; /*!< URL of the service */
QStringList m_serviceUrls; //!< URL of the service
int m_currentUrlIndex;
BlackMisc::Network::CServerList m_voiceServers;
BlackMisc::Network::CServerList m_fsdServers;

View File

@@ -180,12 +180,88 @@ namespace BlackMisc
void CAtcStation::syncronizeControllerData(CAtcStation &otherStation)
{
if (this->m_controller == otherStation.getController()) return;
if (this->m_controller == otherStation.getController()) { return; }
CUser otherController = otherStation.getController();
this->m_controller.syncronizeData(otherController);
otherStation.setController(otherController);
}
void CAtcStation::syncronizeWithBookedStation(CAtcStation &bookedStation)
{
if (bookedStation.getCallsign() != this->getCallsign()) { return; }
// from online to booking
bookedStation.setOnline(true);
bookedStation.setFrequency(this->getFrequency());
// Logoff Zulu Time set?
// comes directly from the online controller and is most likely more accurate
if (!this->getBookedUntilUtc().isNull())
{
bookedStation.setBookedUntilUtc(this->getBookedUntilUtc());
}
// from booking to online
// booked now stations have valid data and need no update
if (!this->isBookedNow() && bookedStation.hasValidBookingTimes())
{
if (this->hasValidBookingTimes())
{
if (bookedStation.isBookedNow())
{
// can't get any better, we just copy from / to over
this->setBookedFromUntil(bookedStation);
}
else
{
// we already have some booking dates, we will verify those now
// and will set the most appropriate booking dates
CTime timeDiffBooking(bookedStation.bookedWhen());
CTime timeDiffOnline(this->bookedWhen()); // diff to now
if (timeDiffBooking.isNegativeWithEpsilonConsidered() && timeDiffOnline.isNegativeWithEpsilonConsidered())
{
// both in past
if (timeDiffBooking > timeDiffOnline)
{
this->setBookedFromUntil(bookedStation);
}
}
else if (timeDiffBooking.isPositiveWithEpsilonConsidered() && timeDiffOnline.isPositiveWithEpsilonConsidered())
{
// both in future
if (timeDiffBooking < timeDiffOnline)
{
this->setBookedFromUntil(bookedStation);
}
}
else if (timeDiffBooking.isPositiveWithEpsilonConsidered() && timeDiffOnline.isNegativeWithEpsilonConsidered())
{
// future booking is better than past booking
this->setBookedFromUntil(bookedStation);
}
}
}
else
{
// no booking info so far, so we just copy over
this->setBookedFromUntil(bookedStation);
}
}
// both ways
this->syncronizeControllerData(bookedStation);
if (this->hasValidDistance())
{
bookedStation.setDistanceToOwnAircraft(this->getDistanceToOwnAircraft());
bookedStation.setBearingToOwnAircraft(this->getBearingToOwnAircraft());
}
else if (bookedStation.hasValidDistance())
{
this->setDistanceToOwnAircraft(bookedStation.getDistanceToOwnAircraft());
this->setBearingToOwnAircraft(bookedStation.getBearingToOwnAircraft());
}
}
bool CAtcStation::isInRange() const
{
if (m_range.isNull() || !hasValidDistance()) { return false; }
@@ -206,10 +282,10 @@ namespace BlackMisc
bool CAtcStation::isBookedNow() const
{
if (!this->hasValidBookingTimes()) return false;
if (!this->hasValidBookingTimes()) { return false; }
QDateTime now = QDateTime::currentDateTimeUtc();
if (this->m_bookedFromUtc > now) return false;
if (now > this->m_bookedUntilUtc) return false;
if (this->m_bookedFromUtc > now) { return false; }
if (now > this->m_bookedUntilUtc) { return false; }
return true;
}

View File

@@ -134,9 +134,15 @@ namespace BlackMisc
void setPosition(const BlackMisc::Geo::CCoordinateGeodetic &position) { this->m_position = position; }
//! Syncronize controller data
//! Updates two stations (namely a booked and online ATC station) with complementary data
//! Updates two stations (normally a booked and online ATC station) with complementary data
void syncronizeControllerData(CAtcStation &otherStation);
//! Syncronize station data
//! Updates the two stations (a booked and online ATC station) with complementary data
//! \pre this object is the online station, the passed station the booked station
void syncronizeWithBookedStation(CAtcStation &bookedStation);
//! Get the radius of the controller's area of visibility.
const BlackMisc::PhysicalQuantities::CLength &getRange() const { return m_range; }

View File

@@ -53,7 +53,7 @@ namespace BlackMisc
return this->findBy(Predicates::MemberValid(&CAtcStation::getController)).transform(Predicates::MemberTransform(&CAtcStation::getController));
}
int CAtcStationList::mergeWithBooking(CAtcStation &bookedAtcStation)
int CAtcStationList::syncronizeWithBookedStation(CAtcStation &bookedAtcStation)
{
int c = 0;
bookedAtcStation.setOnline(false); // reset
@@ -61,105 +61,16 @@ namespace BlackMisc
for (auto i = this->begin(); i != this->end(); ++i)
{
CAtcStation onlineAtcStation = *i;
if (onlineAtcStation.getCallsign() != bookedAtcStation.getCallsign()) continue;
// from online to booking
bookedAtcStation.setOnline(true);
bookedAtcStation.setFrequency(onlineAtcStation.getFrequency());
// Logoff Zulu Time set?
// comes directly from the online controller and is most likely more accurate
if (!onlineAtcStation.getBookedUntilUtc().isNull())
bookedAtcStation.setBookedUntilUtc(onlineAtcStation.getBookedUntilUtc());
// from booking to online
if (!onlineAtcStation.isBookedNow() && bookedAtcStation.hasValidBookingTimes())
{
if (onlineAtcStation.hasValidBookingTimes())
{
if (bookedAtcStation.isBookedNow())
{
// can't get any better
onlineAtcStation.setBookedFromUntil(bookedAtcStation);
}
else
{
// we already have some booking dates
CTime timeDiffBooking = bookedAtcStation.bookedWhen();
CTime timeDiffOnline = onlineAtcStation.bookedWhen();
if (timeDiffBooking.isNegativeWithEpsilonConsidered() && timeDiffOnline.isNegativeWithEpsilonConsidered())
{
// both in past
if (timeDiffBooking > timeDiffOnline)
onlineAtcStation.setBookedFromUntil(bookedAtcStation);
}
else if (timeDiffBooking.isPositiveWithEpsilonConsidered() && timeDiffOnline.isPositiveWithEpsilonConsidered())
{
// both in future
if (timeDiffBooking < timeDiffOnline)
onlineAtcStation.setBookedFromUntil(bookedAtcStation);
}
else if (timeDiffBooking.isPositiveWithEpsilonConsidered() && timeDiffOnline.isNegativeWithEpsilonConsidered())
{
// future booking is better than past booking
onlineAtcStation.setBookedFromUntil(bookedAtcStation);
}
}
}
else
{
// no booking info so far
onlineAtcStation.setBookedFromUntil(bookedAtcStation);
}
}
// both ways
onlineAtcStation.syncronizeControllerData(bookedAtcStation);
if (onlineAtcStation.hasValidDistance())
{
bookedAtcStation.setDistanceToOwnAircraft(onlineAtcStation.getDistanceToOwnAircraft());
bookedAtcStation.setBearingToOwnAircraft(onlineAtcStation.getBearingToOwnAircraft());
}
else if (bookedAtcStation.hasValidDistance())
{
onlineAtcStation.setDistanceToOwnAircraft(bookedAtcStation.getDistanceToOwnAircraft());
onlineAtcStation.setBearingToOwnAircraft(bookedAtcStation.getBearingToOwnAircraft());
}
// update
*i = onlineAtcStation;
if (i->getCallsign() != bookedAtcStation.getCallsign()) { continue; }
i->syncronizeWithBookedStation(bookedAtcStation);
c++;
}
// normally 1 expected, as I should find
// only one online station for this booking
Q_ASSERT(c == 0 || c == 1);
Q_ASSERT_X(c == 0 || c == 1, Q_FUNC_INFO, "Found >1 matching station");
return c;
}
bool CAtcStationList::updateFromVatsimDataFileStation(CAtcStation &stationToBeUpdated) const
{
if (this->isEmpty()) return false;
if (stationToBeUpdated.hasValidRealName() && stationToBeUpdated.hasValidId() && stationToBeUpdated.hasValidFrequency()) return 0;
CAtcStation dataFileStation = this->findFirstByCallsign(stationToBeUpdated.getCallsign());
if (dataFileStation.getCallsign().isEmpty()) return false; // not found
if (!stationToBeUpdated.hasValidRealName() || !stationToBeUpdated.hasValidId())
{
CUser user = stationToBeUpdated.getController();
if (!stationToBeUpdated.hasValidRealName()) user.setRealName(dataFileStation.getControllerRealName());
if (!stationToBeUpdated.hasValidId()) user.setId(dataFileStation.getControllerId());
stationToBeUpdated.setController(user);
}
if (!stationToBeUpdated.hasValidFrequency())
{
stationToBeUpdated.setFrequency(dataFileStation.getFrequency());
}
return true;
}
} // namespace
} // namespace

View File

@@ -50,14 +50,10 @@ namespace BlackMisc
//! All controllers (with valid data)
BlackMisc::Network::CUserList getControllers() const;
//! Merge with ATC station representing booking information.
//! Syncronize with ATC station representing booking information.
//! Both sides (booking, online station) will be updated.
//! \remarks Can be used if the stored data in this list are online ATC stations
int mergeWithBooking(CAtcStation &bookedAtcStation);
//! Merge with the data from the VATSIM data file
//! \remarks Can be used if the stored data in this list are VATSIM data file stations
bool updateFromVatsimDataFileStation(CAtcStation &stationToBeUpdated) const;
//! \pre Can be used only if the stored data in this list are online ATC stations
int syncronizeWithBookedStation(CAtcStation &bookedAtcStation);
//! \copydoc CValueObject::toQVariant
QVariant toQVariant() const { return QVariant::fromValue(*this); }