From eb0fa92e7e5b722f5a106b920bd9e1aa16f01cde Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 18 Apr 2018 04:51:51 +0200 Subject: [PATCH] Ref T260, function to add an offset to received situations for testing * "dot" command * context function * provider functions --- src/blackcore/application.cpp | 8 +++ src/blackcore/application.h | 8 ++- src/blackcore/context/contextnetwork.h | 3 ++ src/blackcore/context/contextnetworkempty.h | 9 ++++ src/blackcore/context/contextnetworkimpl.cpp | 36 ++++++++++++- src/blackcore/context/contextnetworkimpl.h | 5 +- src/blackcore/context/contextnetworkproxy.cpp | 7 ++- src/blackcore/context/contextnetworkproxy.h | 1 + src/blackcore/context/contextsimulatorimpl.h | 5 +- src/blackmisc/simplecommandparser.h | 2 +- .../simulation/remoteaircraftprovider.cpp | 51 +++++++++++++++++-- .../simulation/remoteaircraftprovider.h | 12 +++++ 12 files changed, 134 insertions(+), 13 deletions(-) diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index 1c50dcab7..f74331f9b 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -11,6 +11,7 @@ #include "blackcore/application.h" #include "blackcore/db/networkwatchdog.h" #include "blackcore/context/contextnetwork.h" +#include "blackcore/context/contextsimulatorimpl.h" #include "blackcore/context/contextapplication.h" #include "blackcore/cookiemanager.h" #include "blackcore/corefacade.h" @@ -1277,6 +1278,13 @@ namespace BlackCore return args.join(' '); } + ISimulator *CApplication::getISimulator() const + { + if (!this->getCoreFacade()) { return nullptr; } + if (!this->getCoreFacade()->getCContextSimulator()) { return nullptr; } + return this->getCoreFacade()->getCContextSimulator()->simulator(); + } + void CApplication::cmdLineHelpMessage() { m_parser.showHelp(); // terminates diff --git a/src/blackcore/application.h b/src/blackcore/application.h index 54273b2a0..2e3926fc6 100644 --- a/src/blackcore/application.h +++ b/src/blackcore/application.h @@ -15,8 +15,8 @@ #include "blackcore/blackcoreexport.h" #include "blackcore/cookiemanager.h" #include "blackcore/corefacadeconfig.h" -#include "blackcore/data/globalsetup.h" #include "blackcore/db/databasereaderconfig.h" +#include "blackcore/data/globalsetup.h" #include "blackcore/application/applicationsettings.h" #include "blackcore/webreaderflags.h" #include "blackmisc/db/updateinfo.h" @@ -64,6 +64,7 @@ namespace BlackCore class CCoreFacade; class CSetupReader; class CWebDataServices; + class ISimulator; namespace Context { class IContextApplication; @@ -295,6 +296,11 @@ namespace BlackCore virtual QString cmdLineArgumentsAsString(bool withExecutable = true); //! @} + // ----------------------- simulator ---------------------------------------- + + //! The simulator plugin, if available + ISimulator *getISimulator() const; + // ----------------------- contexts ---------------------------------------- //! \name Context / core facade related diff --git a/src/blackcore/context/contextnetwork.h b/src/blackcore/context/contextnetwork.h index c24ef774c..59aafb8c3 100644 --- a/src/blackcore/context/contextnetwork.h +++ b/src/blackcore/context/contextnetwork.h @@ -307,6 +307,9 @@ namespace BlackCore //! Request parts for callsign (from another client) virtual void testRequestAircraftConfig(const BlackMisc::Aviation::CCallsign &callsign) = 0; + //! Add altitude offset for testing + virtual bool testAddAltitudeOffset(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::PhysicalQuantities::CLength &offset = BlackMisc::PhysicalQuantities::CLength::null()) = 0; + public: //! Raw FSD message receiver functor using RawFsdMessageReceivedSlot = std::function; diff --git a/src/blackcore/context/contextnetworkempty.h b/src/blackcore/context/contextnetworkempty.h index dba702b91..ee2df9f5d 100644 --- a/src/blackcore/context/contextnetworkempty.h +++ b/src/blackcore/context/contextnetworkempty.h @@ -258,6 +258,15 @@ namespace BlackCore logEmptyContextWarning(Q_FUNC_INFO); } + //! \copydoc IContextNetwork::testAddAltitudeOffset + virtual bool testAddAltitudeOffset(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::PhysicalQuantities::CLength &offset = BlackMisc::PhysicalQuantities::CLength::null()) override + { + Q_UNUSED(callsign); + Q_UNUSED(offset); + logEmptyContextWarning(Q_FUNC_INFO); + return false; + } + //! \copydoc IContextNetwork::parseCommandLine virtual bool parseCommandLine(const QString &commandLine, const BlackMisc::CIdentifier &originator) override { diff --git a/src/blackcore/context/contextnetworkimpl.cpp b/src/blackcore/context/contextnetworkimpl.cpp index ecc1bb321..b32ffbd9d 100644 --- a/src/blackcore/context/contextnetworkimpl.cpp +++ b/src/blackcore/context/contextnetworkimpl.cpp @@ -234,7 +234,8 @@ namespace BlackCore { Q_UNUSED(originator;) if (commandLine.isEmpty()) { return false; } - CSimpleCommandParser parser({ ".msg", ".m" }); + static const QStringList cmds({ ".msg", ".m", ".altos", ".altoffset" }); + CSimpleCommandParser parser(cmds); parser.parse(commandLine); if (!parser.isKnownCommand()) { return false; } if (parser.matchesCommand(".msg", ".m")) @@ -314,6 +315,33 @@ namespace BlackCore this->sendTextMessages(tml); return true; } + else if (parser.matchesCommand(".altos", ".altoffet")) + { + if (!m_airspace) { return false; } + if (parser.countParts() < 2) { return false; } + const CCallsign cs(parser.part(1)); + if (!m_airspace->isAircraftInRange(cs)) + { + CLogMessage(this).validationError("Altitude offset unknown callsign"); + return false; + } + + CLength os(CLength::null()); + if (parser.hasPart(2)) + { + os.parseFromString(parser.part(2)); + } + const bool added = this->testAddAltitudeOffset(cs, os); + if (added) + { + CLogMessage(this).info("Added altitude offset %1 for %2") << os.valueRoundedWithUnit(2) << cs.asString(); + } + else + { + CLogMessage(this).info("Removed altitude offset %1") << cs.asString(); + } + return true; + } return false; } @@ -609,6 +637,12 @@ namespace BlackCore return m_airspace->partsLastModified(callsign); } + bool CContextNetwork::testAddAltitudeOffset(const CCallsign &callsign, const CLength &offset) + { + if (this->isDebugEnabled()) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO; } + return m_airspace->testAddAltitudeOffset(callsign, offset); + } + CAtcStation CContextNetwork::getOnlineStationForCallsign(const CCallsign &callsign) const { if (this->isDebugEnabled()) { CLogMessage(this, CLogCategory::contextSlot()).debug() << Q_FUNC_INFO << callsign; } diff --git a/src/blackcore/context/contextnetworkimpl.h b/src/blackcore/context/contextnetworkimpl.h index 776ef84ef..97ba53224 100644 --- a/src/blackcore/context/contextnetworkimpl.h +++ b/src/blackcore/context/contextnetworkimpl.h @@ -134,6 +134,7 @@ namespace BlackCore virtual int aircraftPartsAdded() const override; virtual qint64 situationsLastModified(const BlackMisc::Aviation::CCallsign &callsign) const override; virtual qint64 partsLastModified(const BlackMisc::Aviation::CCallsign &callsign) const override; + virtual bool testAddAltitudeOffset(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::PhysicalQuantities::CLength &offset = BlackMisc::PhysicalQuantities::CLength::null()) override; //! @} //! In transition state, e.g. connecting, disconnecting. @@ -145,7 +146,8 @@ namespace BlackCore //! \addtogroup swiftdotcommands //! @{ //!
-            //! .m .msg   message text
+            //! .m .msg           message text
+            //! .altos .altoffset altitude offset for testing
             //! 
//! @} //! \copydoc IContextNetwork::parseCommandLine @@ -158,6 +160,7 @@ namespace BlackCore BlackMisc::CSimpleCommandParser::registerCommand({".m", "alias: .msg"}); BlackMisc::CSimpleCommandParser::registerCommand({".m message ", "send text message"}); BlackMisc::CSimpleCommandParser::registerCommand({".m callsign message ", "send text message"}); + BlackMisc::CSimpleCommandParser::registerCommand({".altos callsign offsetvalue", "set altitude offset value (testing)"}); } //! \publicsection diff --git a/src/blackcore/context/contextnetworkproxy.cpp b/src/blackcore/context/contextnetworkproxy.cpp index 795d80432..574761b2b 100644 --- a/src/blackcore/context/contextnetworkproxy.cpp +++ b/src/blackcore/context/contextnetworkproxy.cpp @@ -33,7 +33,7 @@ namespace BlackCore CContextNetworkProxy::CContextNetworkProxy(const QString &serviceName, QDBusConnection &connection, CCoreFacadeConfig::ContextMode mode, CCoreFacade *runtime) : IContextNetwork(mode, runtime), m_dBusInterface(nullptr) { m_dBusInterface = new CGenericDBusInterface( - serviceName , IContextNetwork::ObjectPath(), IContextNetwork::InterfaceName(), + serviceName, IContextNetwork::ObjectPath(), IContextNetwork::InterfaceName(), connection, this); this->relaySignals(serviceName, connection); } @@ -290,6 +290,11 @@ namespace BlackCore m_dBusInterface->callDBus(QLatin1String("testRequestAircraftConfig"), callsign); } + bool CContextNetworkProxy::testAddAltitudeOffset(const CCallsign &callsign, const PhysicalQuantities::CLength &offset) + { + return m_dBusInterface->callDBusRet(QLatin1String("testAddAltitudeOffset"), callsign, offset); + } + CStatusMessage CContextNetworkProxy::connectToNetwork(const CServer &server, INetwork::LoginMode loginMode) { return m_dBusInterface->callDBusRet(QLatin1String("connectToNetwork"), server, loginMode); diff --git a/src/blackcore/context/contextnetworkproxy.h b/src/blackcore/context/contextnetworkproxy.h index 895c01abf..76ae2b065 100644 --- a/src/blackcore/context/contextnetworkproxy.h +++ b/src/blackcore/context/contextnetworkproxy.h @@ -122,6 +122,7 @@ namespace BlackCore virtual void testAddAircraftParts(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::Aviation::CAircraftParts &parts, bool incremental) override; virtual void testReceivedTextMessages(const BlackMisc::Network::CTextMessageList &textMessages) override; virtual void testRequestAircraftConfig(const BlackMisc::Aviation::CCallsign &callsign) override; + virtual bool testAddAltitudeOffset(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::PhysicalQuantities::CLength &offset = BlackMisc::PhysicalQuantities::CLength::null()) override; //! @} public: diff --git a/src/blackcore/context/contextsimulatorimpl.h b/src/blackcore/context/contextsimulatorimpl.h index 9e50b1b22..971d58aaa 100644 --- a/src/blackcore/context/contextsimulatorimpl.h +++ b/src/blackcore/context/contextsimulatorimpl.h @@ -12,9 +12,9 @@ #ifndef BLACKCORE_CONTEXT_CONTEXTSIMULATOR_IMPL_H #define BLACKCORE_CONTEXT_CONTEXTSIMULATOR_IMPL_H -#include "blackcore/corefacadeconfig.h" #include "blackcore/context/contextsimulator.h" #include "blackcore/application/applicationsettings.h" +#include "blackcore/corefacadeconfig.h" #include "blackcore/aircraftmatcher.h" #include "blackcore/blackcoreexport.h" #include "blackcore/weathermanager.h" @@ -51,7 +51,6 @@ namespace BlackCore { class CCoreFacade; class CPluginManagerSimulator; - class ISimulator; namespace Context { @@ -125,7 +124,7 @@ namespace BlackCore //! Gracefully shut down, e.g. for plugin unloading void gracefulShutdown(); - //! Simulator object + //! Access to simulator (i.e. the plugin) ISimulator *simulator() const; //! Register dot commands diff --git a/src/blackmisc/simplecommandparser.h b/src/blackmisc/simplecommandparser.h index e3d660bcb..6e8847f29 100644 --- a/src/blackmisc/simplecommandparser.h +++ b/src/blackmisc/simplecommandparser.h @@ -28,7 +28,7 @@ namespace BlackMisc //! Constructor CSimpleCommandParser(const QStringList &knownCommands); - //! Known command + //! Known command? bool isKnownCommand() const { return m_knownCommand; } //! Parse diff --git a/src/blackmisc/simulation/remoteaircraftprovider.cpp b/src/blackmisc/simulation/remoteaircraftprovider.cpp index 7f578d270..f20087e2f 100644 --- a/src/blackmisc/simulation/remoteaircraftprovider.cpp +++ b/src/blackmisc/simulation/remoteaircraftprovider.cpp @@ -164,21 +164,26 @@ namespace BlackMisc void CRemoteAircraftProvider::storeAircraftSituation(const CAircraftSituation &situation) { - if (situation.getCallsign().isEmpty()) { return; } + const CCallsign cs = situation.getCallsign(); + if (cs.isEmpty()) { return; } const qint64 ts = QDateTime::currentMSecsSinceEpoch(); + // for testing only + CAircraftSituation situationOs(situation); + this->testAddAltitudeOffsetToSituation(situationOs); + // list from new to old QWriteLocker lock(&m_lockSituations); m_situationsAdded++; - m_situationsLastModified[situation.getCallsign()] = ts; - CAircraftSituationList &situationList = m_situationsByCallsign[situation.getCallsign()]; + m_situationsLastModified[cs] = ts; + CAircraftSituationList &situationList = m_situationsByCallsign[cs]; if (situationList.isEmpty()) { - situationList.prefillLatestAdjustedFirst(situation, IRemoteAircraftProvider::MaxSituationsPerCallsign); + situationList.prefillLatestAdjustedFirst(situationOs, IRemoteAircraftProvider::MaxSituationsPerCallsign); } else { - situationList.push_frontKeepLatestFirstAdjustOffset(situation, IRemoteAircraftProvider::MaxSituationsPerCallsign); + situationList.push_frontKeepLatestFirstAdjustOffset(situationOs, IRemoteAircraftProvider::MaxSituationsPerCallsign); } // unify all inbound ground information @@ -420,6 +425,28 @@ namespace BlackMisc this->removeAllAircraft(); } + bool CRemoteAircraftProvider::hasTestAltitudeOffset(const CCallsign &callsign) const + { + if (callsign.isEmpty()) { return false; } + QReadLocker l(&m_lockSituations); + return m_testOffset.contains(callsign); + } + + bool CRemoteAircraftProvider::testAddAltitudeOffsetToSituation(CAircraftSituation &situation) const + { + if (!this->hasTestAltitudeOffset(situation.getCallsign())) { return false; } + const CCallsign cs(situation.getCallsign()); + + CLength os; + { + QReadLocker l(&m_lockSituations); + os = m_testOffset.value(cs); + } + const CAltitude newAlt = situation.getAltitude().withOffset(os); + situation.setAltitude(newAlt); + return true; + } + CStatusMessageList CRemoteAircraftProvider::getAircraftPartsHistory(const CCallsign &callsign) const { QReadLocker l(&m_lockPartsHistory); @@ -456,6 +483,20 @@ namespace BlackMisc return m_partsLastModified.value(callsign, -1); } + bool CRemoteAircraftProvider::testAddAltitudeOffset(const CCallsign &callsign, const CLength &offset) + { + const bool remove = offset.isNull() || offset.isZeroEpsilonConsidered(); + QWriteLocker l(&m_lockSituations); + if (remove) + { + m_testOffset.remove(callsign); + return false; + } + + m_testOffset[callsign] = offset; + return true; + } + int CRemoteAircraftProvider::aircraftPartsAdded() const { QReadLocker l(&m_lockParts); diff --git a/src/blackmisc/simulation/remoteaircraftprovider.h b/src/blackmisc/simulation/remoteaircraftprovider.h index 89fc1182a..6ddd7009e 100644 --- a/src/blackmisc/simulation/remoteaircraftprovider.h +++ b/src/blackmisc/simulation/remoteaircraftprovider.h @@ -295,6 +295,14 @@ namespace BlackMisc //! Clear all data void clear(); + // ------------------- testing --------------- + + //! Has test offset value? + bool hasTestAltitudeOffset(const Aviation::CCallsign &callsign) const; + + //! Offset for callsign + bool testAddAltitudeOffset(const Aviation::CCallsign &callsign, const PhysicalQuantities::CLength &offset); + signals: //! A new aircraft appeared void addedAircraft(const CSimulatedAircraft &remoteAircraft); @@ -356,6 +364,9 @@ namespace BlackMisc void storeAircraftParts(const Aviation::CCallsign &callsign, const QJsonObject &jsonObject, int currentOffset); //! @} + //! Add an offset for testing + bool testAddAltitudeOffsetToSituation(Aviation::CAircraftSituation &situation) const; + private: // hashs, because not sorted by key but keeping order CSituationsPerCallsign m_situationsByCallsign; //!< situations, for performance reasons per callsign, thread safe access required @@ -369,6 +380,7 @@ namespace BlackMisc QMap m_aircraftPartsHistory; QMap m_situationsLastModified; QMap m_partsLastModified; + QMap m_testOffset; bool m_enableReverseLookupMsgs = false; //!< shall we log. information about the matching process bool m_enableAircraftPartsHistory = true; //!< shall we keep a history of aircraft parts