diff --git a/src/blackcore/airspace_analyzer.cpp b/src/blackcore/airspace_analyzer.cpp index 35f64719c..687dec991 100644 --- a/src/blackcore/airspace_analyzer.cpp +++ b/src/blackcore/airspace_analyzer.cpp @@ -70,10 +70,10 @@ namespace BlackCore m_aircraftCallsignTimestamps[situation.getCallsign()] = QDateTime::currentMSecsSinceEpoch(); } - void CAirspaceAnalyzer::ps_watchdogTouchAtcCallsign(const CCallsign &callsign, const CFrequency &freq, const Geo::CCoordinateGeodetic &pos, const CLength &range) + void CAirspaceAnalyzer::ps_watchdogTouchAtcCallsign(const CCallsign &callsign, const CFrequency &frequency, const Geo::CCoordinateGeodetic &position, const CLength &range) { - Q_UNUSED(freq); - Q_UNUSED(pos); + Q_UNUSED(frequency); + Q_UNUSED(position); Q_UNUSED(range); m_atcCallsignTimestamps[callsign] = QDateTime::currentMSecsSinceEpoch(); } @@ -150,7 +150,9 @@ namespace BlackCore void CAirspaceAnalyzer::analyzeAirspace() { - CAirspaceAircraftSnapshot snapshot(getAircraftInRange()); // thread safe copy + CAirspaceAircraftSnapshot snapshot( + getAircraftInRange() // thread safe copy + ); // lock block { diff --git a/src/blackcore/airspace_analyzer.h b/src/blackcore/airspace_analyzer.h index 676fc51e2..a1cd3c6a9 100644 --- a/src/blackcore/airspace_analyzer.h +++ b/src/blackcore/airspace_analyzer.h @@ -24,7 +24,7 @@ namespace BlackCore { - //! Class monitoring and analyzing (closests aircraft, outdated aircraft / watchdog) airspace + //! Class monitoring and analyzing (closest aircraft, outdated aircraft / watchdog) airspace //! in background. //! //! \details Watchdog functionality: This class was introduced due to a flaw in the VATSIM server implementation: Every client needs to send an add/delete packet on its own to inform other @@ -76,8 +76,8 @@ namespace BlackCore void ps_watchdogTouchAircraftCallsign(const BlackMisc::Aviation::CAircraftSituation &situation, const BlackMisc::Aviation::CTransponder &transponder); //! Reset timestamp for callsign - void ps_watchdogTouchAtcCallsign(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::PhysicalQuantities::CFrequency &freq, - const BlackMisc::Geo::CCoordinateGeodetic &pos, const BlackMisc::PhysicalQuantities::CLength &range); + void ps_watchdogTouchAtcCallsign(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::PhysicalQuantities::CFrequency &frequency, + const BlackMisc::Geo::CCoordinateGeodetic &position, const BlackMisc::PhysicalQuantities::CLength &range); //! Connection status of network changed void ps_onConnectionStatusChanged(BlackCore::INetwork::ConnectionStatus oldStatus, BlackCore::INetwork::ConnectionStatus newStatus); diff --git a/src/blackcore/airspace_monitor.h b/src/blackcore/airspace_monitor.h index ade82eb92..f67a8f9ed 100644 --- a/src/blackcore/airspace_monitor.h +++ b/src/blackcore/airspace_monitor.h @@ -209,7 +209,7 @@ namespace BlackCore INetwork *m_network = nullptr; CVatsimBookingReader *m_vatsimBookingReader = nullptr; CVatsimDataFileReader *m_vatsimDataFileReader = nullptr; - CAirspaceAnalyzer *m_analyzer = nullptr; + CAirspaceAnalyzer *m_analyzer = nullptr; //!< owned analyzer bool m_serverSupportsNameQuery = false; //!< not all servers support name query bool m_connected = false; //!< retrieve data bool m_sendInterimPositions = false; diff --git a/src/blackcore/context_network_impl.h b/src/blackcore/context_network_impl.h index 539564765..6d518b111 100644 --- a/src/blackcore/context_network_impl.h +++ b/src/blackcore/context_network_impl.h @@ -92,6 +92,9 @@ namespace BlackCore //! \ingroup remoteaircraftprovider virtual BlackMisc::Simulation::CAirspaceAircraftSnapshot getLatestAirspaceAircraftSnapshot() const override; + //! Network library + INetwork* network() const { return m_network; } + public slots: //! \copydoc IContextNetwork::updateAircraftEnabled //! \ingroup remoteaircraftprovider diff --git a/src/blackcore/context_simulator_impl.cpp b/src/blackcore/context_simulator_impl.cpp index 28494ffe6..e89d63ab1 100644 --- a/src/blackcore/context_simulator_impl.cpp +++ b/src/blackcore/context_simulator_impl.cpp @@ -401,10 +401,10 @@ namespace BlackCore Q_ASSERT(networkContext); Q_ASSERT(networkContext->isLocalObject()); - for (const CSimulatedAircraft &simAircraft : networkContext->getAircraftInRange()) + for (const CSimulatedAircraft &simulatedAircraft : networkContext->getAircraftInRange()) { - Q_ASSERT(!simAircraft.getCallsign().isEmpty()); - m_simulatorPlugin->simulator->addRemoteAircraft(simAircraft); + Q_ASSERT(!simulatedAircraft.getCallsign().isEmpty()); + m_simulatorPlugin->simulator->logicallyAddRemoteAircraft(simulatedAircraft); } // apply latest settings @@ -565,7 +565,7 @@ namespace BlackCore Q_ASSERT(m_simulatorPlugin->simulator); Q_ASSERT(!remoteAircraft.getCallsign().isEmpty()); - m_simulatorPlugin->simulator->addRemoteAircraft(remoteAircraft); + m_simulatorPlugin->simulator->logicallyAddRemoteAircraft(remoteAircraft); } void CContextSimulator::ps_removedRemoteAircraft(const CCallsign &callsign) @@ -580,8 +580,7 @@ namespace BlackCore } Q_ASSERT(m_simulatorPlugin->simulator); - - m_simulatorPlugin->simulator->removeRemoteAircraft(callsign); + m_simulatorPlugin->simulator->logicallyRemoveRemoteAircraft(callsign); } void CContextSimulator::ps_onSimulatorStatusChanged(int status) @@ -615,10 +614,9 @@ namespace BlackCore } } - void CContextSimulator::ps_cockitChangedFromSim(const CSimulatedAircraft &ownAircraft) + void CContextSimulator::ps_cockpitChangedFromSimulator(const CSimulatedAircraft &ownAircraft) { Q_ASSERT(getIContextOwnAircraft()); - getIContextOwnAircraft()->changedAircraftCockpit(ownAircraft, IContextSimulator::InterfaceName()); } diff --git a/src/blackcore/context_simulator_impl.h b/src/blackcore/context_simulator_impl.h index 8c114014a..e61b0ad78 100644 --- a/src/blackcore/context_simulator_impl.h +++ b/src/blackcore/context_simulator_impl.h @@ -180,7 +180,7 @@ namespace BlackCore void ps_simulatorStarted(QObject *listener); //! Simulator has changed cockpit - void ps_cockitChangedFromSim(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft); + void ps_cockpitChangedFromSimulator(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft); //! Changed remote aircraft model void ps_changedRemoteAircraftModel(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, const QString &originator); diff --git a/src/blackcore/simulator.cpp b/src/blackcore/simulator.cpp index 1da568121..5909d828f 100644 --- a/src/blackcore/simulator.cpp +++ b/src/blackcore/simulator.cpp @@ -20,47 +20,14 @@ using namespace BlackMisc::Simulation; namespace BlackCore { - CSimulatorCommon::CSimulatorCommon(const CSimulatorPluginInfo &info, - BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, - BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, - QObject *parent) - : ISimulator(parent), - COwnAircraftAware(ownAircraftProvider), - CRemoteAircraftAware(remoteAircraftProvider), - m_simulatorPluginInfo(info) + const QString &ISimulator::simulatorOriginator() { - this->setObjectName(info.getIdentifier()); - m_oneSecondTimer = new QTimer(this); - m_oneSecondTimer->setObjectName(this->objectName().append(":m_oneSecondTimer")); - connect(this->m_oneSecondTimer, &QTimer::timeout, this, &CSimulatorCommon::ps_oneSecondTimer); - m_oneSecondTimer->start(1000); - - // provider signals - bool c = remoteAircraftProvider->connectRemoteAircraftProviderSignals( - std::bind(&CSimulatorCommon::ps_remoteProviderAddAircraftSituation, this, std::placeholders::_1), - std::bind(&CSimulatorCommon::ps_remoteProvideraddAircraftParts, this, std::placeholders::_1), - std::bind(&CSimulatorCommon::ps_remoteProviderRemovedAircraft, this, std::placeholders::_1), - std::bind(&CSimulatorCommon::ps_remoteProviderAircraftSnapshot, this, std::placeholders::_1) - ); - Q_ASSERT(c); - Q_UNUSED(c); - - // info - CLogMessage(this).info("Initialized simulator driver %1") << m_simulatorPluginInfo.toQString(); + // string is generated once, the timestamp allows to use multiple + // components (as long as they are not generated at the same ms) + static const QString o = QString("SIMULATOR:").append(QString::number(QDateTime::currentMSecsSinceEpoch())); + return o; } - void ISimulator::ps_remoteProviderAddAircraftSituation(const CAircraftSituation &situation) - { Q_UNUSED(situation); } - - void ISimulator::ps_remoteProvideraddAircraftParts(const CAircraftParts &parts) - { Q_UNUSED(parts); } - - void ISimulator::ps_remoteProviderRemovedAircraft(const CCallsign &callsign) - { Q_UNUSED(callsign); } - - void ISimulator::ps_remoteProviderAircraftSnapshot(const CAirspaceAircraftSnapshot &aircraftSnapshot) - { Q_UNUSED(aircraftSnapshot); } - void ISimulator::emitSimulatorCombinedStatus() { int status = @@ -70,225 +37,7 @@ namespace BlackCore emit simulatorStatusChanged(status); } - void CSimulatorCommon::blinkHighlightedAircraft() - { - if (m_highlightedAircraft.isEmpty() || m_highlightEndTimeMsEpoch < 1) { return; } - m_blinkCycle = !m_blinkCycle; - - if (QDateTime::currentMSecsSinceEpoch() < m_highlightEndTimeMsEpoch) - { - // blink mode, toggle aircraft - for (const CSimulatedAircraft &aircraft : m_highlightedAircraft) - { - if (m_blinkCycle) - { - this->removeRemoteAircraft(aircraft.getCallsign()); - } - else - { - this->addRemoteAircraft(aircraft); - } - } - } - else - { - // restore - for (const CSimulatedAircraft &aircraft : m_highlightedAircraft) - { - // get the current state for this aircraft - // it might has been removed in the mean time - const CCallsign cs(aircraft.getCallsign()); - resetAircraftFromBacked(cs); - } - m_highlightedAircraft.clear(); - m_highlightEndTimeMsEpoch = 0; - } - } - - void CSimulatorCommon::recalculateRestrictedAircraft() - { - if (!isMaxAircraftRestricted()) { return; } - if (!isRenderingEnabled()) { return; } - - CSimulatedAircraftList newAircraftInRange(getAircraftInRange().getClosestObjects(getMaxRenderedAircraft())); - CCallsignSet newAircraftCallsigns(newAircraftInRange.getCallsigns()); - CCallsignSet toBeRemovedCallsigns(m_callsignsToBeRendered.difference(newAircraftCallsigns)); - CCallsignSet toBeAddedCallsigns(newAircraftCallsigns.difference(m_callsignsToBeRendered)); - for (const CCallsign &cs : toBeRemovedCallsigns) - { - removeRemoteAircraft(cs); - } - for (const CCallsign &cs : toBeAddedCallsigns) - { - addRemoteAircraft(newAircraftInRange.findFirstByCallsign(cs)); - } - this->m_callsignsToBeRendered = newAircraftCallsigns; - } - - void CSimulatorCommon::resetAircraftFromBacked(const CCallsign &callsign) - { - CSimulatedAircraft aircraft(this->getAircraftInRangeForCallsign(callsign)); - bool enabled = aircraft.isEnabled(); - if (enabled) - { - // are we already visible? - if (!isRenderedAircraft(callsign)) - { - this->addRemoteAircraft(aircraft); - } - } - else - { - removeRemoteAircraft(callsign); - } - } - - void CSimulatorCommon::setInitialAircraftSituationAndParts(CSimulatedAircraft &aircraft) const - { - if (!this->m_interpolator) { return; } - - const CCallsign callsign(aircraft.getCallsign()); - if (!(this->remoteAircraftSituationsCount(callsign) < 1)) { return; } - - // with an interpolator the interpolated situation is used - // to avoid position jittering - qint64 time = QDateTime::currentMSecsSinceEpoch(); - IInterpolator::InterpolationStatus is; - CAircraftSituation as(m_interpolator->getInterpolatedSituation(callsign, time, aircraft.isVtol(), is)); - if (is.interpolationSucceeded) { aircraft.setSituation(as); } - - } - - int CSimulatorCommon::getMaxRenderedAircraft() const - { - return (m_maxRenderedAircraft <= MaxAircraftInfinite) ? m_maxRenderedAircraft : MaxAircraftInfinite; - } - - void CSimulatorCommon::setMaxRenderedAircraft(int maxRenderedAircraft) - { - if (maxRenderedAircraft == m_maxRenderedAircraft) { return; } - if (maxRenderedAircraft < 1) - { - m_maxRenderedAircraft = 0; - } - else if (maxRenderedAircraft >= MaxAircraftInfinite) - { - m_maxRenderedAircraft = MaxAircraftInfinite; - } - else - { - m_maxRenderedAircraft = maxRenderedAircraft; - } - - bool r = isRenderingRestricted(); - emit restrictedRenderingChanged(r); - } - - void CSimulatorCommon::setMaxRenderedDistance(CLength &distance) - { - if (distance == m_maxRenderedDistance) { return; } - if (distance.isNull() || distance >= getRenderedDistanceBoundary()) - { - m_maxRenderedDistance = CLength(0.0, CLengthUnit::nullUnit()); - } - else - { - Q_ASSERT(distance.isPositiveWithEpsilonConsidered()); - m_maxRenderedDistance = distance; - } - - bool r = isRenderingRestricted(); - emit restrictedRenderingChanged(r); - } - - CLength CSimulatorCommon::getMaxRenderedDistance() const - { - if (m_maxRenderedDistance.isNull()) { return getRenderedDistanceBoundary(); } - return m_maxRenderedDistance; - } - - CLength CSimulatorCommon::getRenderedDistanceBoundary() const - { - return CLength(20.0, CLengthUnit::NM()); - } - - bool CSimulatorCommon::isMaxAircraftRestricted() const - { - return m_maxRenderedAircraft < MaxAircraftInfinite && isRenderingEnabled(); - } - - bool CSimulatorCommon::isMaxDistanceRestricted() const - { - return !m_maxRenderedDistance.isNull(); - } - - void CSimulatorCommon::enableDebugMessages(bool driverMessages, bool interpolatorMessages) - { - this->m_debugMessages = driverMessages; - Q_UNUSED(interpolatorMessages); - } - - int CSimulatorCommon::getInstalledModelsCount() const - { - return getInstalledModels().size(); - } - - void CSimulatorCommon::highlightAircraft(const BlackMisc::Simulation::CSimulatedAircraft &aircraftToHighlight, bool enableHighlight, const BlackMisc::PhysicalQuantities::CTime &displayTime) - { - CCallsign cs(aircraftToHighlight.getCallsign()); - this->m_highlightedAircraft.removeByCallsign(cs); - if (enableHighlight) - { - qint64 deltaT = displayTime.valueRounded(CTimeUnit::ms(), 0); - this->m_highlightEndTimeMsEpoch = QDateTime::currentMSecsSinceEpoch() + deltaT; - this->m_highlightedAircraft.push_back(aircraftToHighlight); - } - } - - bool CSimulatorCommon::isRenderingEnabled() const - { - if (m_maxRenderedAircraft < 1) { return false; } - if (!isMaxDistanceRestricted()) { return true; } - - return m_maxRenderedDistance.valueRounded(CLengthUnit::NM(), 2) > 0.1; - } - - bool CSimulatorCommon::isRenderingRestricted() const - { - return this->isMaxDistanceRestricted() || this->isMaxAircraftRestricted(); - } - - void CSimulatorCommon::deleteAllRenderingRestrictions() - { - if (!isRenderingEnabled()) { return; } - this->m_maxRenderedDistance = CLength(0, CLengthUnit::nullUnit()); - this->m_maxRenderedAircraft = MaxAircraftInfinite; - emit restrictedRenderingChanged(false); - } - - void CSimulatorCommon::ps_oneSecondTimer() - { - m_timerCounter++; - blinkHighlightedAircraft(); - - // any seconds - if (m_timerCounter % 10 == 0) - { - recalculateRestrictedAircraft(); - } - } - ISimulatorListener::ISimulatorListener(QObject *parent) : QObject(parent) { } - const CSimulatorPluginInfo &CSimulatorCommon::getSimulatorPluginInfo() const - { - return m_simulatorPluginInfo; - } - - const CSimulatorSetup &CSimulatorCommon::getSimulatorSetup() const - { - return m_simulatorSetup; - } - } // namespace diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index ccd0f5be8..5ccc66f3e 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -34,6 +34,7 @@ namespace BlackCore class BLACKCORE_EXPORT ISimulator : public QObject { Q_OBJECT + public: //! ISimulator status enum SimulatorStatus @@ -80,15 +81,14 @@ namespace BlackCore //! Disconnect from simulator virtual bool disconnectFrom() = 0; - //! Add new remote aircraft to the simulator - //! \sa changeRemoteAircraftEnabled to hide a remote aircraft - virtual bool addRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &remoteAircraft) = 0; + //! Logically add a new aircraft. Depending on max. aircraft, enabled status etc. + //! it will physically added to the simulator. + //! \sa physicallyAddRemoteAircraft + virtual bool logicallyAddRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &remoteAircraft) = 0; - //! Remove remote aircraft from simulator - virtual bool removeRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) = 0; - - //! Remove all remote aircraft - virtual void removeAllRemoteAircraft() = 0; + //! Logically remove remote aircraft from simulator. Depending on max. aircraft, enabled status etc. + //! it will physically added to the simulator. + virtual bool logicallyRemoveRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) = 0; //! Change remote aircraft per property virtual bool changeRemoteAircraftModel(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, const QString &originator) = 0; @@ -114,10 +114,10 @@ namespace BlackCore //! Count of aircraft models for available remote aircrafts virtual int getInstalledModelsCount() const = 0; - //! Reload the installed models + //! Reload the installed models from disk virtual void reloadInstalledModels() = 0; - //! Airports in range + //! Airports in range from simulator virtual BlackMisc::Aviation::CAirportList getAirportsInRange() const = 0; //! Set time synchronization between simulator and user's computer time @@ -161,7 +161,12 @@ namespace BlackCore virtual void enableDebugMessages(bool driver, bool interpolator) = 0; //! Is the aircraft rendered (displayed in simulator)? - virtual bool isRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const = 0; + //! This shall only return true if the aircraft is really visible in the simulator + virtual bool isPhysicallyRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const = 0; + + //! Physically rendered (displayed in simulator)? + //! This shall only return aircraft really visible in the simulator + virtual BlackMisc::Aviation::CCallsignSet physicallyRenderedAircraft() const = 0; //! Highlight the aircraft for given time (or disable highlight) virtual void highlightAircraft(const BlackMisc::Simulation::CSimulatedAircraft &aircraftToHighlight, bool enableHighlight, const BlackMisc::PhysicalQuantities::CTime &displayTime) = 0; @@ -170,13 +175,7 @@ namespace BlackCore virtual bool isRenderingEnabled() const = 0; //! Originator - const QString &simulatorOriginator() - { - // string is generated once, the timestamp allows to use multiple - // components (as long as they are not generated at the same ms) - static const QString o = QString("SIMULATOR:").append(QString::number(QDateTime::currentMSecsSinceEpoch())); - return o; - } + const QString &simulatorOriginator(); signals: //! Simulator combined status @@ -194,23 +193,20 @@ namespace BlackCore //! Installed aircraft models ready or changed void installedAircraftModelsChanged(); - protected slots: - //! Provider added situation - virtual void ps_remoteProviderAddAircraftSituation(const BlackMisc::Aviation::CAircraftSituation &situation); - - //! Provider added parts - virtual void ps_remoteProvideraddAircraftParts(const BlackMisc::Aviation::CAircraftParts &parts); - - //! Provider removed aircraft - virtual void ps_remoteProviderRemovedAircraft(const BlackMisc::Aviation::CCallsign &callsign); - - //! Provider aircraft snapshot - virtual void ps_remoteProviderAircraftSnapshot(const BlackMisc::Simulation::CAirspaceAircraftSnapshot &aircraftSnapshot); - protected: //! Default constructor ISimulator(QObject *parent = nullptr) : QObject(parent) {} + //! Add new remote aircraft physically to the simulator + //! \sa changeRemoteAircraftEnabled to hide a remote aircraft + virtual bool physicallyAddRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &remoteAircraft) = 0; + + //! Remove remote aircraft from simulator + virtual bool physicallyRemoveRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) = 0; + + //! Remove all remote aircraft + virtual void physicallyRemoveAllRemoteAircraft() = 0; + //! Emit the combined status //! \sa simulatorStatusChanged; void emitSimulatorCombinedStatus(); @@ -271,99 +267,6 @@ namespace BlackCore virtual ISimulatorListener *createListener(QObject *parent = nullptr) = 0; }; - - //! Common base class with providers, interface and some base functionality - class BLACKCORE_EXPORT CSimulatorCommon : - public BlackCore::ISimulator, - public BlackMisc::Simulation::COwnAircraftAware, // gain access to in memor own aircraft data - public BlackMisc::Simulation::CRemoteAircraftAware // gain access to in memory remote aircraft data - { - - Q_OBJECT - - public: - //! \copydoc ISimulator::getMaxRenderedAircraft - virtual int getMaxRenderedAircraft() const override; - - //! \copydoc ISimulator::setMaxRenderedAircraft - virtual void setMaxRenderedAircraft(int maxRenderedAircraft) override; - - //! \copydoc ISimulator::setMaxRenderedDistance - virtual void setMaxRenderedDistance(BlackMisc::PhysicalQuantities::CLength &distance) override; - - //! \copydoc ISimulator::getMaxRenderedDistance - virtual BlackMisc::PhysicalQuantities::CLength getMaxRenderedDistance() const override; - - //! \copydoc ISimulator::getRenderedDistanceBoundary - virtual BlackMisc::PhysicalQuantities::CLength getRenderedDistanceBoundary() const override; - - //! \copydoc ISimulator::isMaxAircraftRestricted - virtual bool isMaxAircraftRestricted() const override; - - //! \copydoc ISimulator::isMaxDistanceRestricted - virtual bool isMaxDistanceRestricted() const override; - - //! \copydoc ISimulator::enableDebuggingMessages - virtual void enableDebugMessages(bool driverMessages, bool interpolatorMessages) override; - - //! \copydoc ISimulator::getInstalledModelsCount - virtual int getInstalledModelsCount() const override; - - //! \copydoc IContextSimulator::highlightAircraft - virtual void highlightAircraft(const BlackMisc::Simulation::CSimulatedAircraft &aircraftToHighlight, bool enableHighlight, const BlackMisc::PhysicalQuantities::CTime &displayTime) override; - - //! \copydoc IContextSimulator::isRenderingEnabled - virtual bool isRenderingEnabled() const override; - - //! \copydoc IContextSimulator::isRenderingRestricted - virtual bool isRenderingRestricted() const override; - - //! \copydoc IContextSimulator::getSimulatorPluginInfo - virtual const BlackMisc::Simulation::CSimulatorPluginInfo &getSimulatorPluginInfo() const override; - - //! \copydoc IContextSimulator::getSimulatorSetup - virtual const BlackMisc::Simulation::CSimulatorSetup &getSimulatorSetup() const override; - - //! \copydoc IContextSimulator::deleteAllRenderingRestrictions - virtual void deleteAllRenderingRestrictions(); - - protected slots: - //! Slow timer used to highlight aircraft, can be used for other things too - virtual void ps_oneSecondTimer(); - - protected: - //! Constructor - CSimulatorCommon(const BlackMisc::Simulation::CSimulatorPluginInfo &info, - BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, - BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, - QObject *parent = nullptr); - - //! Blink the highlighted aircraft - void blinkHighlightedAircraft(); - - //! Recalculate the restricted aircraft - void recalculateRestrictedAircraft(); - - //! Restore aircraft from backedn data - void resetAircraftFromBacked(const BlackMisc::Aviation::CCallsign &callsign); - - //! Override parts and situation from current interpolator values, if any! - void setInitialAircraftSituationAndParts(BlackMisc::Simulation::CSimulatedAircraft &aircraft) const; - - bool m_debugMessages = false; //!< Display debug messages - bool m_blinkCycle = false; //!< use for highlighting - IInterpolator *m_interpolator = nullptr; //!< interpolator instance - qint64 m_highlightEndTimeMsEpoch = 0; //!< end highlighting - int m_timerCounter = 0; //!< allows to calculate n seconds - QTimer *m_oneSecondTimer = nullptr; //!< timer - BlackMisc::Simulation::CSimulatorPluginInfo m_simulatorPluginInfo; //!< info object - BlackMisc::Simulation::CSimulatorSetup m_simulatorSetup; //!< setup object - BlackMisc::Simulation::CSimulatedAircraftList m_highlightedAircraft; //!< all other aircraft are to be ignored - BlackMisc::Aviation::CCallsignSet m_callsignsToBeRendered; //!< callsigns which will be rendered - int m_maxRenderedAircraft = MaxAircraftInfinite; //!< max.rendered aircraft - BlackMisc::PhysicalQuantities::CLength m_maxRenderedDistance { 0.0, BlackMisc::PhysicalQuantities::CLengthUnit::nullUnit()}; //!< max.distance for rendering - }; - } // namespace Q_DECLARE_INTERFACE(BlackCore::ISimulatorFactory, "org.swift-project.blackcore.simulatorinterface") diff --git a/src/blackcore/simulator_common.cpp b/src/blackcore/simulator_common.cpp new file mode 100644 index 000000000..8820a4a8f --- /dev/null +++ b/src/blackcore/simulator_common.cpp @@ -0,0 +1,322 @@ +/* Copyright (C) 2013 + * 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 "simulator_common.h" +#include "interpolator.h" +#include "blackmisc/logmessage.h" +#include "blackmisc/collection.h" + +using namespace BlackMisc; +using namespace BlackMisc::Aviation; +using namespace BlackMisc::Simulation; +using namespace BlackMisc::PhysicalQuantities; +using namespace BlackMisc::Simulation; + +namespace BlackCore +{ + CSimulatorCommon::CSimulatorCommon(const CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent) + : ISimulator(parent), + COwnAircraftAware(ownAircraftProvider), + CRemoteAircraftAware(remoteAircraftProvider), + m_simulatorPluginInfo(info) + { + this->setObjectName(info.getIdentifier()); + this->m_oneSecondTimer = new QTimer(this); + this->m_oneSecondTimer->setObjectName(this->objectName().append(":m_oneSecondTimer")); + connect(this->m_oneSecondTimer, &QTimer::timeout, this, &CSimulatorCommon::ps_oneSecondTimer); + this->m_oneSecondTimer->start(1000); + + // provider signals + bool c = remoteAircraftProvider->connectRemoteAircraftProviderSignals( + std::bind(&CSimulatorCommon::ps_remoteProviderAddAircraftSituation, this, std::placeholders::_1), + std::bind(&CSimulatorCommon::ps_remoteProviderAddAircraftParts, this, std::placeholders::_1), + std::bind(&CSimulatorCommon::ps_remoteProviderRemovedAircraft, this, std::placeholders::_1), + std::bind(&CSimulatorCommon::ps_remoteProviderAircraftSnapshot, this, std::placeholders::_1) + ); + Q_ASSERT(c); + Q_UNUSED(c); + + // info + CLogMessage(this).info("Initialized simulator driver %1") << m_simulatorPluginInfo.toQString(); + } + + bool CSimulatorCommon::logicallyAddRemoteAircraft(const CSimulatedAircraft &remoteAircraft) + { + if (!remoteAircraft.isEnabled()) { return false; } + + // if not restriced, directly change + if (!isRenderingRestricted()) { return this->physicallyAddRemoteAircraft(remoteAircraft); } + + //! \todo Go thru logic + return this->physicallyAddRemoteAircraft(remoteAircraft); + } + + bool CSimulatorCommon::logicallyRemoveRemoteAircraft(const CCallsign &callsign) + { + // if not restriced, directly change + if (!isRenderingRestricted()) { return this->physicallyRemoveRemoteAircraft(callsign); } + + //! \todo Go thru logic + return this->physicallyRemoveRemoteAircraft(callsign); + } + + void CSimulatorCommon::blinkHighlightedAircraft() + { + if (m_highlightedAircraft.isEmpty() || m_highlightEndTimeMsEpoch < 1) { return; } + m_blinkCycle = !m_blinkCycle; + + if (QDateTime::currentMSecsSinceEpoch() < m_highlightEndTimeMsEpoch) + { + // blink mode, toggle aircraft + for (const CSimulatedAircraft &aircraft : m_highlightedAircraft) + { + if (m_blinkCycle) + { + this->physicallyRemoveRemoteAircraft(aircraft.getCallsign()); + } + else + { + this->physicallyAddRemoteAircraft(aircraft); + } + } + } + else + { + // restore + for (const CSimulatedAircraft &aircraft : m_highlightedAircraft) + { + // get the current state for this aircraft + // it might has been removed in the mean time + const CCallsign cs(aircraft.getCallsign()); + resetAircraftFromBacked(cs); + } + m_highlightedAircraft.clear(); + m_highlightEndTimeMsEpoch = 0; + } + } + + void CSimulatorCommon::recalculateRestrictedAircraft() + { + if (!isMaxAircraftRestricted()) { return; } + if (!isRenderingEnabled()) { return; } + + CSimulatedAircraftList newAircraftInRange(getAircraftInRange().getClosestObjects(getMaxRenderedAircraft())); + CCallsignSet newAircraftCallsigns(newAircraftInRange.getCallsigns()); + CCallsignSet toBeRemovedCallsigns(m_callsignsToBeRendered.difference(newAircraftCallsigns)); + CCallsignSet toBeAddedCallsigns(newAircraftCallsigns.difference(m_callsignsToBeRendered)); + for (const CCallsign &cs : toBeRemovedCallsigns) + { + physicallyRemoveRemoteAircraft(cs); + } + for (const CCallsign &cs : toBeAddedCallsigns) + { + physicallyAddRemoteAircraft(newAircraftInRange.findFirstByCallsign(cs)); + } + this->m_callsignsToBeRendered = newAircraftCallsigns; + } + + void CSimulatorCommon::resetAircraftFromBacked(const CCallsign &callsign) + { + CSimulatedAircraft aircraft(this->getAircraftInRangeForCallsign(callsign)); + bool enabled = aircraft.isEnabled(); + if (enabled) + { + // are we already visible? + if (!isPhysicallyRenderedAircraft(callsign)) + { + this->physicallyAddRemoteAircraft(aircraft); + } + } + else + { + physicallyRemoveRemoteAircraft(callsign); + } + } + + void CSimulatorCommon::setInitialAircraftSituationAndParts(CSimulatedAircraft &aircraft) const + { + if (!this->m_interpolator) { return; } + + const CCallsign callsign(aircraft.getCallsign()); + if (!(this->remoteAircraftSituationsCount(callsign) < 1)) { return; } + + // with an interpolator the interpolated situation is used + // to avoid position jittering + qint64 time = QDateTime::currentMSecsSinceEpoch(); + IInterpolator::InterpolationStatus is; + CAircraftSituation as(m_interpolator->getInterpolatedSituation(callsign, time, aircraft.isVtol(), is)); + if (is.interpolationSucceeded) { aircraft.setSituation(as); } + + } + + int CSimulatorCommon::getMaxRenderedAircraft() const + { + return (m_maxRenderedAircraft <= MaxAircraftInfinite) ? m_maxRenderedAircraft : MaxAircraftInfinite; + } + + void CSimulatorCommon::setMaxRenderedAircraft(int maxRenderedAircraft) + { + if (maxRenderedAircraft == m_maxRenderedAircraft) { return; } + if (maxRenderedAircraft < 1) + { + m_maxRenderedAircraft = 0; + } + else if (maxRenderedAircraft >= MaxAircraftInfinite) + { + m_maxRenderedAircraft = MaxAircraftInfinite; + } + else + { + m_maxRenderedAircraft = maxRenderedAircraft; + } + + bool r = isRenderingRestricted(); + emit restrictedRenderingChanged(r); + } + + void CSimulatorCommon::setMaxRenderedDistance(CLength &distance) + { + if (distance == m_maxRenderedDistance) { return; } + if (distance.isNull() || distance >= getRenderedDistanceBoundary()) + { + m_maxRenderedDistance = CLength(0.0, CLengthUnit::nullUnit()); + } + else + { + Q_ASSERT(distance.isPositiveWithEpsilonConsidered()); + m_maxRenderedDistance = distance; + } + + bool r = isRenderingRestricted(); + emit restrictedRenderingChanged(r); + } + + CLength CSimulatorCommon::getMaxRenderedDistance() const + { + if (m_maxRenderedDistance.isNull()) { return getRenderedDistanceBoundary(); } + return m_maxRenderedDistance; + } + + const CSimulatorPluginInfo &CSimulatorCommon::getSimulatorPluginInfo() const + { + return m_simulatorPluginInfo; + } + + const CSimulatorSetup &CSimulatorCommon::getSimulatorSetup() const + { + return m_simulatorSetup; + } + + CLength CSimulatorCommon::getRenderedDistanceBoundary() const + { + return CLength(20.0, CLengthUnit::NM()); + } + + bool CSimulatorCommon::isMaxAircraftRestricted() const + { + return m_maxRenderedAircraft < MaxAircraftInfinite && isRenderingEnabled(); + } + + bool CSimulatorCommon::isMaxDistanceRestricted() const + { + return !m_maxRenderedDistance.isNull(); + } + + void CSimulatorCommon::enableDebugMessages(bool driverMessages, bool interpolatorMessages) + { + this->m_debugMessages = driverMessages; + Q_UNUSED(interpolatorMessages); + } + + int CSimulatorCommon::getInstalledModelsCount() const + { + return getInstalledModels().size(); + } + + void CSimulatorCommon::highlightAircraft(const BlackMisc::Simulation::CSimulatedAircraft &aircraftToHighlight, bool enableHighlight, const BlackMisc::PhysicalQuantities::CTime &displayTime) + { + CCallsign cs(aircraftToHighlight.getCallsign()); + this->m_highlightedAircraft.removeByCallsign(cs); + if (enableHighlight) + { + qint64 deltaT = displayTime.valueRounded(CTimeUnit::ms(), 0); + this->m_highlightEndTimeMsEpoch = QDateTime::currentMSecsSinceEpoch() + deltaT; + this->m_highlightedAircraft.push_back(aircraftToHighlight); + } + } + + bool CSimulatorCommon::isRenderingEnabled() const + { + if (m_maxRenderedAircraft < 1) { return false; } + if (!isMaxDistanceRestricted()) { return true; } + + return m_maxRenderedDistance.valueRounded(CLengthUnit::NM(), 2) > 0.1; + } + + bool CSimulatorCommon::isRenderingRestricted() const + { + return this->isMaxDistanceRestricted() || this->isMaxAircraftRestricted(); + } + + void CSimulatorCommon::deleteAllRenderingRestrictions() + { + if (!isRenderingEnabled()) { return; } + this->m_maxRenderedDistance = CLength(0, CLengthUnit::nullUnit()); + this->m_maxRenderedAircraft = MaxAircraftInfinite; + emit restrictedRenderingChanged(false); + } + + void CSimulatorCommon::ps_oneSecondTimer() + { + m_timerCounter++; + blinkHighlightedAircraft(); + + // any seconds + if (m_timerCounter % 10 == 0) + { + recalculateRestrictedAircraft(); + } + } + + void CSimulatorCommon::ps_recalculateRenderedAircraft() + { + this->ps_recalculateRenderedAircraft(getLatestAirspaceAircraftSnapshot()); + } + + void CSimulatorCommon::ps_recalculateRenderedAircraft(const CAirspaceAircraftSnapshot &snapshot) + { + if (!snapshot.isValidSnapshot()) { return;} + CCallsignSet callsignsInSimuator(physicallyRenderedAircraft()); + + } + + void CSimulatorCommon::ps_remoteProviderAddAircraftSituation(const CAircraftSituation &situation) + { + Q_UNUSED(situation); + } + + void CSimulatorCommon::ps_remoteProviderAddAircraftParts(const CAircraftParts &parts) + { + Q_UNUSED(parts); + } + + void CSimulatorCommon::ps_remoteProviderRemovedAircraft(const CCallsign &callsign) + { + Q_UNUSED(callsign); + } + + void CSimulatorCommon::ps_remoteProviderAircraftSnapshot(const CAirspaceAircraftSnapshot &aircraftSnapshot) + { + ps_recalculateRenderedAircraft(aircraftSnapshot); + } + +} // namespace diff --git a/src/blackcore/simulator_common.h b/src/blackcore/simulator_common.h new file mode 100644 index 000000000..80c9a8bd6 --- /dev/null +++ b/src/blackcore/simulator_common.h @@ -0,0 +1,151 @@ +/* Copyright (C) 2013 + * 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. + */ + +//! \file + +#ifndef BLACKCORE_SIMULATOR_COMMON_H +#define BLACKCORE_SIMULATOR_COMMON_H + +#include "blackcore/interpolator.h" +#include "blackcore/simulator.h" +#include "blackmisc/simulation/simulatorplugininfo.h" +#include "blackmisc/simulation/simulatorsetup.h" +#include "blackmisc/simulation/simulatedaircraftlist.h" +#include "blackmisc/simulation/aircraftmodellist.h" +#include "blackmisc/simulation/ownaircraftprovider.h" +#include "blackmisc/simulation/remoteaircraftprovider.h" +#include "blackmisc/statusmessagelist.h" +#include "blackmisc/aviation/airportlist.h" +#include "blackmisc/network/textmessage.h" +#include "blackmisc/network/client.h" +#include "blackmisc/pixmap.h" +#include + +namespace BlackCore +{ + + //! Common base class with providers, interface and some base functionality + class CSimulatorCommon : + public BlackCore::ISimulator, + public BlackMisc::Simulation::COwnAircraftAware, // gain access to in memor own aircraft data + public BlackMisc::Simulation::CRemoteAircraftAware // gain access to in memory remote aircraft data + { + + Q_OBJECT + + public: + //! \copydoc ISimulator::getMaxRenderedAircraft + virtual int getMaxRenderedAircraft() const override; + + //! \copydoc ISimulator::setMaxRenderedAircraft + virtual void setMaxRenderedAircraft(int maxRenderedAircraft) override; + + //! \copydoc ISimulator::setMaxRenderedDistance + virtual void setMaxRenderedDistance(BlackMisc::PhysicalQuantities::CLength &distance) override; + + //! \copydoc ISimulator::getMaxRenderedDistance + virtual BlackMisc::PhysicalQuantities::CLength getMaxRenderedDistance() const override; + + //! \copydoc ISimulator::getRenderedDistanceBoundary + virtual BlackMisc::PhysicalQuantities::CLength getRenderedDistanceBoundary() const override; + + //! \copydoc ISimulator::isMaxAircraftRestricted + virtual bool isMaxAircraftRestricted() const override; + + //! \copydoc ISimulator::isMaxDistanceRestricted + virtual bool isMaxDistanceRestricted() const override; + + //! \copydoc ISimulator::enableDebuggingMessages + virtual void enableDebugMessages(bool driverMessages, bool interpolatorMessages) override; + + //! \copydoc ISimulator::getInstalledModelsCount + virtual int getInstalledModelsCount() const override; + + //! \copydoc IContextSimulator::highlightAircraft + virtual void highlightAircraft(const BlackMisc::Simulation::CSimulatedAircraft &aircraftToHighlight, bool enableHighlight, const BlackMisc::PhysicalQuantities::CTime &displayTime) override; + + //! \copydoc IContextSimulator::isRenderingEnabled + virtual bool isRenderingEnabled() const override; + + //! \copydoc IContextSimulator::isRenderingRestricted + virtual bool isRenderingRestricted() const override; + + //! \copydoc IContextSimulator::getSimulatorPluginInfo + virtual const BlackMisc::Simulation::CSimulatorPluginInfo &getSimulatorPluginInfo() const override; + + //! \copydoc IContextSimulator::getSimulatorSetup + virtual const BlackMisc::Simulation::CSimulatorSetup &getSimulatorSetup() const override; + + //! \copydoc IContextSimulator::deleteAllRenderingRestrictions + virtual void deleteAllRenderingRestrictions(); + + protected slots: + //! Slow timer used to highlight aircraft, can be used for other things too + virtual void ps_oneSecondTimer(); + + //! Recalculate the rendered aircraft + virtual void ps_recalculateRenderedAircraft(); + + //! Recalculate the rendered aircraft + virtual void ps_recalculateRenderedAircraft(const BlackMisc::Simulation::CAirspaceAircraftSnapshot &snapshot); + + //! Provider added situation + virtual void ps_remoteProviderAddAircraftSituation(const BlackMisc::Aviation::CAircraftSituation &situation); + + //! Provider added parts + virtual void ps_remoteProviderAddAircraftParts(const BlackMisc::Aviation::CAircraftParts &parts); + + //! Provider removed aircraft + virtual void ps_remoteProviderRemovedAircraft(const BlackMisc::Aviation::CCallsign &callsign); + + //! Provider aircraft snapshot + virtual void ps_remoteProviderAircraftSnapshot(const BlackMisc::Simulation::CAirspaceAircraftSnapshot &aircraftSnapshot); + + protected: + //! Constructor + CSimulatorCommon(const BlackMisc::Simulation::CSimulatorPluginInfo &info, + BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, + QObject *parent = nullptr); + + //! \copydoc IContextSimulator::logicallyAddRemoteAircraft + virtual bool logicallyAddRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &remoteAircraft) override; + + //! \copydoc IContextSimulator::logicallyRemoveRemoteAircraft + virtual bool logicallyRemoveRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) override; + + //! Blink the highlighted aircraft + void blinkHighlightedAircraft(); + + //! Recalculate the restricted aircraft + void recalculateRestrictedAircraft(); + + //! Restore aircraft from backedn data + void resetAircraftFromBacked(const BlackMisc::Aviation::CCallsign &callsign); + + //! Override parts and situation from current interpolator values, if any! + void setInitialAircraftSituationAndParts(BlackMisc::Simulation::CSimulatedAircraft &aircraft) const; + + bool m_debugMessages = false; //!< Display debug messages + bool m_blinkCycle = false; //!< use for highlighting + IInterpolator *m_interpolator = nullptr; //!< interpolator instance + qint64 m_highlightEndTimeMsEpoch = 0; //!< end highlighting + int m_timerCounter = 0; //!< allows to calculate n seconds + QTimer *m_oneSecondTimer = nullptr; //!< timer + BlackMisc::Simulation::CSimulatorPluginInfo m_simulatorPluginInfo; //!< info object + BlackMisc::Simulation::CSimulatorSetup m_simulatorSetup; //!< setup object + BlackMisc::Simulation::CSimulatedAircraftList m_highlightedAircraft; //!< all other aircraft are to be ignored + BlackMisc::Aviation::CCallsignSet m_callsignsToBeRendered; //!< callsigns which will be rendered + int m_maxRenderedAircraft = MaxAircraftInfinite; //!< max.rendered aircraft + BlackMisc::PhysicalQuantities::CLength m_maxRenderedDistance { 0.0, BlackMisc::PhysicalQuantities::CLengthUnit::nullUnit()}; //!< max.distance for rendering + }; + +} // namespace + +#endif // guard diff --git a/src/blackmisc/simulation/airspaceaircraftsnapshot.cpp b/src/blackmisc/simulation/airspaceaircraftsnapshot.cpp index 90cecf7f9..846c9a505 100644 --- a/src/blackmisc/simulation/airspaceaircraftsnapshot.cpp +++ b/src/blackmisc/simulation/airspaceaircraftsnapshot.cpp @@ -18,7 +18,8 @@ namespace BlackMisc CAirspaceAircraftSnapshot::CAirspaceAircraftSnapshot() { } - CAirspaceAircraftSnapshot::CAirspaceAircraftSnapshot(const CSimulatedAircraftList &allAircraft) + CAirspaceAircraftSnapshot::CAirspaceAircraftSnapshot(const CSimulatedAircraftList &allAircraft) : + m_timestampMsSinceEpoch(QDateTime::currentMSecsSinceEpoch()) { if (!allAircraft.isEmpty()) { @@ -33,6 +34,11 @@ namespace BlackMisc } } + bool CAirspaceAircraftSnapshot::isValidSnapshot() const + { + return m_timestampMsSinceEpoch > 0; + } + CVariant CAirspaceAircraftSnapshot::propertyByIndex(const CPropertyIndex &index) const { if (index.isMyself()) { return this->toCVariant(); } diff --git a/src/blackmisc/simulation/airspaceaircraftsnapshot.h b/src/blackmisc/simulation/airspaceaircraftsnapshot.h index f532a753c..90499c103 100644 --- a/src/blackmisc/simulation/airspaceaircraftsnapshot.h +++ b/src/blackmisc/simulation/airspaceaircraftsnapshot.h @@ -49,6 +49,9 @@ namespace BlackMisc //! VTOL aircraft callsigns by distance, only enabled aircraft const BlackMisc::Aviation::CCallsignSet &getEnabledVtolAircraftCallsignsByDistance() const { return m_enabledVtolAircraftCallsignsByDistance; } + //! Valid snapshot? + bool isValidSnapshot() const; + //! \copydoc CValueObject::propertyByIndex virtual CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override; @@ -61,7 +64,7 @@ namespace BlackMisc private: BLACK_ENABLE_TUPLE_CONVERSION(CAirspaceAircraftSnapshot) - qint64 m_timestampMsSinceEpoch; + qint64 m_timestampMsSinceEpoch = -1; // remark closest aircraft always first BlackMisc::Aviation::CCallsignSet m_aircraftCallsignsByDistance; diff --git a/src/blackmisc/simulation/remoteaircraftprovider.cpp b/src/blackmisc/simulation/remoteaircraftprovider.cpp index d226121b6..b725d3e37 100644 --- a/src/blackmisc/simulation/remoteaircraftprovider.cpp +++ b/src/blackmisc/simulation/remoteaircraftprovider.cpp @@ -34,6 +34,12 @@ namespace BlackMisc return this->m_remoteAircraftProvider->getAircraftInRangeForCallsign(callsign); } + CAirspaceAircraftSnapshot CRemoteAircraftAware::getLatestAirspaceAircraftSnapshot() const + { + Q_ASSERT_X(this->m_remoteAircraftProvider, Q_FUNC_INFO, "No object available"); + return this->m_remoteAircraftProvider->getLatestAirspaceAircraftSnapshot(); + } + CAircraftSituationList CRemoteAircraftAware::remoteAircraftSituations(const BlackMisc::Aviation::CCallsign &callsign) const { Q_ASSERT_X(this->m_remoteAircraftProvider, Q_FUNC_INFO, "No object available"); diff --git a/src/blackmisc/simulation/simulatedaircraftlist.cpp b/src/blackmisc/simulation/simulatedaircraftlist.cpp index 13f80c53b..5d4c86bc6 100644 --- a/src/blackmisc/simulation/simulatedaircraftlist.cpp +++ b/src/blackmisc/simulation/simulatedaircraftlist.cpp @@ -49,6 +49,11 @@ namespace BlackMisc return this->findBy(&CSimulatedAircraft::isEnabled, enabled); } + CSimulatedAircraftList CSimulatedAircraftList::findByRendered(bool rendered) const + { + return this->findBy(&CSimulatedAircraft::isRendered, rendered); + } + CSimulatedAircraftList CSimulatedAircraftList::findByVtol(bool vtol) const { return this->findBy(&CSimulatedAircraft::isVtol, vtol); diff --git a/src/blackmisc/simulation/simulatedaircraftlist.h b/src/blackmisc/simulation/simulatedaircraftlist.h index 9288f017d..6e0a86751 100644 --- a/src/blackmisc/simulation/simulatedaircraftlist.h +++ b/src/blackmisc/simulation/simulatedaircraftlist.h @@ -47,6 +47,9 @@ namespace BlackMisc //! Enabled / disabled aircraft CSimulatedAircraftList findByEnabled(bool enabled) const; + //! Rendered / not rendered aircraft + CSimulatedAircraftList findByRendered(bool rendered) const; + //! VTOL / non VTOL aircraft CSimulatedAircraftList findByVtol(bool vtol) const; @@ -56,7 +59,7 @@ namespace BlackMisc //! Mark all aircraft as unrendered void markAllAsNotRendered(); - //! Mark as rendered + //! Mark given callsign as rendered int setRendered(const BlackMisc::Aviation::CCallsign &callsign, bool rendered); //! Set aircraft parts diff --git a/src/plugins/simulator/fs9/simulator_fs9.cpp b/src/plugins/simulator/fs9/simulator_fs9.cpp index 1d7989c5e..81486dae2 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.cpp +++ b/src/plugins/simulator/fs9/simulator_fs9.cpp @@ -93,13 +93,13 @@ namespace BlackSimPlugin return true; } - bool CSimulatorFs9::addRemoteAircraft(const CSimulatedAircraft &newRemoteAircraft) + bool CSimulatorFs9::physicallyAddRemoteAircraft(const CSimulatedAircraft &newRemoteAircraft) { CCallsign callsign = newRemoteAircraft.getCallsign(); if (m_hashFs9Clients.contains(callsign)) { // already exists, remove first - this->removeRemoteAircraft(callsign); + this->physicallyRemoveRemoteAircraft(callsign); } CFs9Client *client = new CFs9Client(m_interpolator, this, callsign.toQString(), CTime(25, CTimeUnit::ms())); @@ -113,7 +113,7 @@ namespace BlackSimPlugin return true; } - bool CSimulatorFs9::removeRemoteAircraft(const CCallsign &callsign) + bool CSimulatorFs9::physicallyRemoveRemoteAircraft(const CCallsign &callsign) { if (!m_hashFs9Clients.contains(callsign)) { return false; } @@ -125,15 +125,20 @@ namespace BlackSimPlugin return true; } - void CSimulatorFs9::removeAllRemoteAircraft() + void CSimulatorFs9::physicallyRemoveAllRemoteAircraft() { QList callsigns(this->m_hashFs9Clients.keys()); for (const CCallsign &cs : callsigns) { - removeRemoteAircraft(cs); + physicallyRemoveRemoteAircraft(cs); } } + CCallsignSet CSimulatorFs9::physicallyRenderedAircraft() const + { + return this->m_hashFs9Clients.keys(); + } + bool CSimulatorFs9::updateOwnSimulatorCockpit(const CAircraft &ownAircraft, const QString &originator) { if (originator == this->simulatorOriginator()) { return false; } @@ -200,7 +205,7 @@ namespace BlackSimPlugin this->displayStatusMessage(message.asStatusMessage(true, true)); } - bool CSimulatorFs9::isRenderedAircraft(const CCallsign &callsign) const + bool CSimulatorFs9::isPhysicallyRenderedAircraft(const CCallsign &callsign) const { return m_hashFs9Clients.contains(callsign); } @@ -273,7 +278,7 @@ namespace BlackSimPlugin // Stop all FS9 client tasks for (auto fs9Client : m_hashFs9Clients.keys()) { - removeRemoteAircraft(fs9Client); + physicallyRemoveRemoteAircraft(fs9Client); } } diff --git a/src/plugins/simulator/fs9/simulator_fs9.h b/src/plugins/simulator/fs9/simulator_fs9.h index c2f3eaeb5..9520c3e42 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.h +++ b/src/plugins/simulator/fs9/simulator_fs9.h @@ -66,14 +66,20 @@ namespace BlackSimPlugin //! \copydoc ISimulator::disconnectFrom() virtual bool disconnectFrom() override; - //! \copydoc ISimulator::addRemoteAircraft() - virtual bool addRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &newRemoteAircraft) override; + //! \copydoc ISimulator::physicallyAddRemoteAircraft() + virtual bool physicallyAddRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &newRemoteAircraft) override; - //! \copydoc ISimulator::removeRemoteAircraft() - virtual bool removeRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) override; + //! \copydoc ISimulator::physicallyRemoveRemoteAircraft() + virtual bool physicallyRemoveRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) override; - //! \copydoc BlackCore::ISimulator::removeAllRemoteAircraft - virtual void removeAllRemoteAircraft() override; + //! \copydoc BlackCore::ISimulator::physicallyRemoveAllRemoteAircraft + virtual void physicallyRemoveAllRemoteAircraft() override; + + //! \copydoc ISimulator::physicallyRenderedAircraft + virtual BlackMisc::Aviation::CCallsignSet physicallyRenderedAircraft() const override; + + //! \copydoc ISimulator::isPhysicallyRenderedAircraft + virtual bool isPhysicallyRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const override; //! \copydoc ISimulator::updateOwnSimulatorCockpit() virtual bool updateOwnSimulatorCockpit(const BlackMisc::Aviation::CAircraft &ownAircraft, const QString &originator) override; @@ -84,9 +90,6 @@ namespace BlackSimPlugin //! \copydoc ISimulator::displayTextMessage() virtual void displayTextMessage(const BlackMisc::Network::CTextMessage &message) const override; - //! \copydoc ISimulator::isRenderedAircraft - virtual bool isRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const override; - protected: //! Timer event virtual void timerEvent(QTimerEvent *event); diff --git a/src/plugins/simulator/fscommon/simulator_fscommon.cpp b/src/plugins/simulator/fscommon/simulator_fscommon.cpp index 71de9de04..dc2ac9e8c 100644 --- a/src/plugins/simulator/fscommon/simulator_fscommon.cpp +++ b/src/plugins/simulator/fscommon/simulator_fscommon.cpp @@ -26,8 +26,8 @@ namespace BlackSimPlugin namespace FsCommon { CSimulatorFsCommon::CSimulatorFsCommon( - const BlackMisc::Simulation::CSimulatorPluginInfo &info, - BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, + const CSimulatorPluginInfo &info, + IOwnAircraftProvider *ownAircraftProvider, IRemoteAircraftProvider *renderedAircraftProvider, QObject *parent) : CSimulatorCommon(info, ownAircraftProvider, renderedAircraftProvider, parent), @@ -100,7 +100,8 @@ namespace BlackSimPlugin { // reverse lookup of ICAO CAircraftIcao icao = mapperInstance()->getIcaoForModelString(model.getModelString()); - icao.updateMissingParts(icao); + icao.updateMissingParts(model.getIcao()); + model.setIcao(icao); // now best ICAO info in model } } @@ -259,7 +260,7 @@ namespace BlackSimPlugin if (originator == simulatorOriginator()) { return false; } // remove upfront, and then enable / disable again - this->removeRemoteAircraft(aircraft.getCallsign()); + this->physicallyRemoveRemoteAircraft(aircraft.getCallsign()); return this->changeRemoteAircraftEnabled(aircraft, originator); } @@ -268,11 +269,11 @@ namespace BlackSimPlugin if (originator == simulatorOriginator()) { return false; } if (aircraft.isEnabled()) { - this->addRemoteAircraft(aircraft); + this->physicallyAddRemoteAircraft(aircraft); } else { - this->removeRemoteAircraft(aircraft.getCallsign()); + this->physicallyRemoveRemoteAircraft(aircraft.getCallsign()); } return true; } diff --git a/src/plugins/simulator/fscommon/simulator_fscommon.h b/src/plugins/simulator/fscommon/simulator_fscommon.h index 0df6c21ab..523f54959 100644 --- a/src/plugins/simulator/fscommon/simulator_fscommon.h +++ b/src/plugins/simulator/fscommon/simulator_fscommon.h @@ -12,7 +12,7 @@ #ifndef BLACKSIMPLUGIN_SIMULATOR_COMMON_H #define BLACKSIMPLUGIN_SIMULATOR_COMMON_H -#include "blackcore/simulator.h" +#include "blackcore/simulator_common.h" #include "blackcore/interpolator.h" #include "blackmisc/simulation/fscommon/aircraftmapper.h" #include "fsuipc.h" diff --git a/src/plugins/simulator/fsx/simulator_fsx.cpp b/src/plugins/simulator/fsx/simulator_fsx.cpp index 5af970170..e3f5de0fa 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.cpp +++ b/src/plugins/simulator/fsx/simulator_fsx.cpp @@ -137,7 +137,7 @@ namespace BlackSimPlugin return connect; } - bool CSimulatorFsx::addRemoteAircraft(const Simulation::CSimulatedAircraft &newRemoteAircraft) + bool CSimulatorFsx::physicallyAddRemoteAircraft(const Simulation::CSimulatedAircraft &newRemoteAircraft) { CCallsign callsign = newRemoteAircraft.getCallsign(); Q_ASSERT(!callsign.isEmpty()); @@ -147,7 +147,7 @@ namespace BlackSimPlugin if (aircraftAlreadyExistsInSim) { // remove first - this->removeRemoteAircraft(callsign); + this->physicallyRemoveRemoteAircraft(callsign); CLogMessage(this).warning("Have to remove aircraft %1 before I can add it") << callsign; } @@ -294,11 +294,16 @@ namespace BlackSimPlugin this->displayStatusMessage(message.asStatusMessage(true, true)); } - bool CSimulatorFsx::isRenderedAircraft(const CCallsign &callsign) const + bool CSimulatorFsx::isPhysicallyRenderedAircraft(const CCallsign &callsign) const { return this->m_simConnectObjects.contains(callsign); } + CCallsignSet CSimulatorFsx::physicallyRenderedAircraft() const + { + return this->m_simConnectObjects.keys(); + } + void CSimulatorFsx::onSimRunning() { if (m_simRunning) { return; } @@ -511,23 +516,23 @@ namespace BlackSimPlugin } } - bool CSimulatorFsx::removeRemoteAircraft(const CCallsign &callsign) + bool CSimulatorFsx::physicallyRemoveRemoteAircraft(const CCallsign &callsign) { // only remove from sim if (!m_simConnectObjects.contains(callsign)) { return false; } - return removeRemoteAircraft(m_simConnectObjects.value(callsign)); + return physicallyRemoveRemoteAircraft(m_simConnectObjects.value(callsign)); } - void CSimulatorFsx::removeAllRemoteAircraft() + void CSimulatorFsx::physicallyRemoveAllRemoteAircraft() { QList callsigns(m_simConnectObjects.keys()); for (const CCallsign &cs : callsigns) { - removeRemoteAircraft(cs); + physicallyRemoveRemoteAircraft(cs); } } - bool CSimulatorFsx::removeRemoteAircraft(const CSimConnectObject &simObject) + bool CSimulatorFsx::physicallyRemoveRemoteAircraft(const CSimConnectObject &simObject) { CCallsign callsign(simObject.getCallsign()); m_simConnectObjects.remove(callsign); diff --git a/src/plugins/simulator/fsx/simulator_fsx.h b/src/plugins/simulator/fsx/simulator_fsx.h index 23c7b25e3..95bc48d63 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.h +++ b/src/plugins/simulator/fsx/simulator_fsx.h @@ -103,14 +103,14 @@ namespace BlackSimPlugin //! \copydoc ISimulator::disconnectFrom() virtual bool disconnectFrom() override; - //! \copydoc ISimulator::addRemoteAircraft() - virtual bool addRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &newRemoteAircraft) override; + //! \copydoc ISimulator::physicallyAddRemoteAircraft() + virtual bool physicallyAddRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &newRemoteAircraft) override; - //! \copydoc ISimulator::remoteRenderedAircraft() - virtual bool removeRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) override; + //! \copydoc ISimulator::physicallyRemoveRemoteAircraft() + virtual bool physicallyRemoveRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) override; - //! \copydoc BlackCore::ISimulator::removeAllRemoteAircraft - virtual void removeAllRemoteAircraft() override; + //! \copydoc BlackCore::ISimulator::physicallyRemoveAllRemoteAircraft + virtual void physicallyRemoveAllRemoteAircraft() override; //! \copydoc ISimulator::updateOwnCockpit virtual bool updateOwnSimulatorCockpit(const BlackMisc::Aviation::CAircraft &ownAircraft, const QString &originator) override; @@ -121,8 +121,11 @@ namespace BlackSimPlugin //! \copydoc ISimulator::displayTextMessage() virtual void displayTextMessage(const BlackMisc::Network::CTextMessage &message) const override; - //! \copydoc ISimulator::isRenderedAircraft - virtual bool isRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const override; + //! \copydoc ISimulator::isPhysicallyRenderedAircraft + virtual bool isPhysicallyRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const override; + + //! \copydoc ISimulator::physicallyRenderedAircraft + virtual BlackMisc::Aviation::CCallsignSet physicallyRenderedAircraft() const override; //! Called when sim has started void onSimRunning(); @@ -159,7 +162,7 @@ namespace BlackSimPlugin private: //! Remove a remote aircraft - bool removeRemoteAircraft(const CSimConnectObject &simObject); + bool physicallyRemoveRemoteAircraft(const CSimConnectObject &simObject); //! Init when connected HRESULT initWhenConnected(); diff --git a/src/plugins/simulator/xplane/simulator_xplane.cpp b/src/plugins/simulator/xplane/simulator_xplane.cpp index 574073721..04b4780b8 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.cpp +++ b/src/plugins/simulator/xplane/simulator_xplane.cpp @@ -247,21 +247,17 @@ namespace BlackSimPlugin void CSimulatorXPlane::displayStatusMessage(const BlackMisc::CStatusMessage &message) const { /* We do not assert here as status message may come because of network problems */ - if (!isConnected()) - return; + if (!isConnected()) { return; } - // TODO XPLMSpeakString()? - // http://www.xsquawkbox.net/xpsdk/mediawiki/XPLMSpeakString + //! \todo XP driver, display text message: XPLMSpeakString()? http://www.xsquawkbox.net/xpsdk/mediawiki/XPLMSpeakString Q_UNUSED(message); } void CSimulatorXPlane::displayTextMessage(const BlackMisc::Network::CTextMessage &message) const { - if (!isConnected()) - return; + if (!isConnected()) { return; } - // TODO XPLMSpeakString()? - // http://www.xsquawkbox.net/xpsdk/mediawiki/XPLMSpeakString + //! \todo XP driver, display text message: XPLMSpeakString()? http://www.xsquawkbox.net/xpsdk/mediawiki/XPLMSpeakString Q_UNUSED(message); } @@ -270,7 +266,6 @@ namespace BlackSimPlugin Q_ASSERT(isConnected()); //! \todo XP driver, function not available CLogMessage(this).error("Function not avialable"); - return {}; } @@ -302,7 +297,7 @@ namespace BlackSimPlugin BlackMisc::Aviation::CAirportList CSimulatorXPlane::getAirportsInRange() const { auto copy = m_airportsInRange; - //! \todo Check if units match, xPlaneData has now hints what the values are + //! \todo XP driver: Check if units match, xPlaneData has now hints what the values are copy.sortByRange(CCoordinateGeodetic(m_xplaneData.latitude, m_xplaneData.longitude, 0), true); copy.truncate(20); return copy; @@ -323,10 +318,9 @@ namespace BlackSimPlugin return CPixmap(); } - bool CSimulatorXPlane::isRenderedAircraft(const CCallsign &callsign) const + bool CSimulatorXPlane::isPhysicallyRenderedAircraft(const CCallsign &callsign) const { - //! \todo XP implement isRenderedAircraft correctly - // work around, but not really telling me if callsign is really(!) visible in SIM + //! \todo XP implement isRenderedAircraft correctly. This work around, but not really telling me if callsign is really(!) visible in SIM return getAircraftInRangeForCallsign(callsign).isRendered(); } @@ -358,7 +352,7 @@ namespace BlackSimPlugin return false; } - bool CSimulatorXPlane::addRemoteAircraft(const CSimulatedAircraft &newRemoteAircraft) + bool CSimulatorXPlane::physicallyAddRemoteAircraft(const CSimulatedAircraft &newRemoteAircraft) { Q_ASSERT(isConnected()); //! \todo XPlane driver check if already exists, how? @@ -385,7 +379,7 @@ namespace BlackSimPlugin situation.getHeading().value(CAngleUnit::deg())); } - void CSimulatorXPlane::ps_remoteProvideraddAircraftParts(const BlackMisc::Aviation::CAircraftParts &parts) + void CSimulatorXPlane::ps_remoteProviderAddAircraftParts(const BlackMisc::Aviation::CAircraftParts &parts) { Q_ASSERT(isConnected()); m_traffic->setPlaneSurfaces(parts.getCallsign().asString(), true, 0, 0, 0, 0, 0, 0, 0, 0, 0, true, true, true, true, 0); // TODO landing gear, lights, control surfaces @@ -398,7 +392,7 @@ namespace BlackSimPlugin //! \todo call removeRemoteAircraft or just let removeRemoteAircraft handle it? } - bool CSimulatorXPlane::removeRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) + bool CSimulatorXPlane::physicallyRemoveRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) { Q_ASSERT(isConnected()); m_traffic->removePlane(callsign.asString()); @@ -407,13 +401,19 @@ namespace BlackSimPlugin return true; } - void CSimulatorXPlane::removeAllRemoteAircraft() + void CSimulatorXPlane::physicallyRemoveAllRemoteAircraft() { m_traffic->removeAllPlanes(); updateMarkAllAsNotRendered(simulatorOriginator()); CLogMessage(this).info("XP: Removed all aircraft"); } + CCallsignSet CSimulatorXPlane::physicallyRenderedAircraft() const + { + //! \todo XP driver, return list of callsigns really present in the simulator + return getAircraftInRange().findByRendered(true).getCallsigns(); // just a poor workaround + } + bool CSimulatorXPlane::changeRemoteAircraftModel(const CSimulatedAircraft &aircraft, const QString &originator) { return this->changeRemoteAircraftEnabled(aircraft, originator); @@ -430,11 +430,11 @@ namespace BlackSimPlugin if (originator == simulatorOriginator()) { return false; } if (aircraft.isEnabled()) { - this->addRemoteAircraft(aircraft); + this->physicallyAddRemoteAircraft(aircraft); } else { - this->removeRemoteAircraft(aircraft.getCallsign()); + this->physicallyRemoveRemoteAircraft(aircraft.getCallsign()); } return true; } diff --git a/src/plugins/simulator/xplane/simulator_xplane.h b/src/plugins/simulator/xplane/simulator_xplane.h index ccceccf8c..84ead8483 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.h +++ b/src/plugins/simulator/xplane/simulator_xplane.h @@ -12,7 +12,7 @@ #ifndef BLACKSIMPLUGIN_SIMULATOR_XPLANE_H #define BLACKSIMPLUGIN_SIMULATOR_XPLANE_H -#include "blackcore/simulator.h" +#include "blackcore/simulator_common.h" #include "blackmisc/simulation/ownaircraftprovider.h" #include "blackmisc/pixmap.h" #include @@ -65,14 +65,20 @@ namespace BlackSimPlugin //! \copydoc BlackCore::ISimulator::disconnectFrom virtual bool disconnectFrom() override; - //! \copydoc ISimulator::addRemoteAircraft() - virtual bool addRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &newRemoteAircraft) override; + //! \copydoc ISimulator::physicallyAddRemoteAircraft() + virtual bool physicallyAddRemoteAircraft(const BlackMisc::Simulation::CSimulatedAircraft &newRemoteAircraft) override; - //! \copydoc BlackCore::ISimulator::removeRemoteAircraft - virtual bool removeRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) override; + //! \copydoc BlackCore::ISimulator::physicallyRemoveRemoteAircraft + virtual bool physicallyRemoveRemoteAircraft(const BlackMisc::Aviation::CCallsign &callsign) override; - //! \copydoc BlackCore::ISimulator::removeAllRemoteAircraft - virtual void removeAllRemoteAircraft() override; + //! \copydoc BlackCore::ISimulator::physicallyRemoveAllRemoteAircraft + virtual void physicallyRemoveAllRemoteAircraft() override; + + //! \copydoc ISimulator::physicallyRenderedAircraft + virtual BlackMisc::Aviation::CCallsignSet physicallyRenderedAircraft() const override; + + //! \copydoc ISimulator::isPhysicallyRenderedAircraft + virtual bool isPhysicallyRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const override; //! \copydoc ISimulator::changeRenderedAircraftModel virtual bool changeRemoteAircraftModel(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, const QString &originator) override; @@ -107,15 +113,12 @@ namespace BlackSimPlugin //! \copydoc ISimulator::iconForModel virtual BlackMisc::CPixmap iconForModel(const QString &modelString) const override; - //! \copydoc ISimulator::isRenderedAircraft - virtual bool isRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const override; - protected slots: //! \copydoc CSimulatorCommon::ps_remoteProviderAddAircraftSituation virtual void ps_remoteProviderAddAircraftSituation(const BlackMisc::Aviation::CAircraftSituation &situation) override; //! \copydoc CSimulatorCommon::ps_remoteProvideraddAircraftParts - virtual void ps_remoteProvideraddAircraftParts(const BlackMisc::Aviation::CAircraftParts &parts) override; + virtual void ps_remoteProviderAddAircraftParts(const BlackMisc::Aviation::CAircraftParts &parts) override; //! \copydoc CSimulatorCommon::ps_remoteProviderRemovedAircraft virtual void ps_remoteProviderRemovedAircraft(const BlackMisc::Aviation::CCallsign &callsign) override;