From e669ad8fb4e570d3a23856a4abf7b1b33265db05 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Mon, 21 Apr 2014 15:01:24 +0200 Subject: [PATCH] Add connectTo/disconnectFrom/canConnect to Simulator drivers Removed checkConnection() as driver is passive only. refs #187 --- src/blackcore/simulator.h | 10 ++++ src/plugins/simulator/fsx/simulator_fsx.cpp | 63 +++++++++++++++------ src/plugins/simulator/fsx/simulator_fsx.h | 14 ++++- 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 0882f3a5f..3e8dc07d9 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -41,7 +41,17 @@ namespace BlackCore //! \brief Are we connected to the simulator? virtual bool isConnected() const = 0; + //! \brief Can we connect? + virtual bool canConnect() = 0; + public slots: + + //! \brief Connect to simulator + virtual bool connectTo() = 0; + + //! \brief Disconnect from simulator + virtual bool disconnectFrom() = 0; + //! Return user aircraft object virtual BlackMisc::Aviation::CAircraft getOwnAircraft() const = 0; diff --git a/src/plugins/simulator/fsx/simulator_fsx.cpp b/src/plugins/simulator/fsx/simulator_fsx.cpp index 1a59e6a48..c4d76de91 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.cpp +++ b/src/plugins/simulator/fsx/simulator_fsx.cpp @@ -31,12 +31,12 @@ namespace BlackSimPlugin m_simRunning(false), m_hSimConnect(nullptr), m_nextObjID(1), - m_simulatorInfo(CSimulatorInfo::FSX()) + m_simulatorInfo(CSimulatorInfo::FSX()), + m_simconnectTimerId(-1) { CFsxSimulatorSetup setup; setup.init(); // this fetches important setting on local side this->m_simulatorInfo.setSimulatorSetup(setup.getSettings()); - QTimer::singleShot(5000, this, SLOT(checkConnection())); } bool CSimulatorFsx::isConnected() const @@ -44,6 +44,49 @@ namespace BlackSimPlugin return m_isConnected; } + bool CSimulatorFsx::connectTo() + { + if(m_isConnected) + return true; + + if (FAILED(SimConnect_Open(&m_hSimConnect, "BlackBox", nullptr, 0, 0, 0))) + { + return false; + } + + initSystemEvents(); + initDataDefinitions(); + m_simconnectTimerId = startTimer(50); + m_isConnected = true; + + emit connectionChanged(true); + return true; + } + + bool CSimulatorFsx::disconnectFrom() + { + SimConnect_Close(m_hSimConnect); + killTimer(m_simconnectTimerId); + m_isConnected = false; + + emit connectionChanged(false); + return true; + } + + bool CSimulatorFsx::canConnect() + { + if (m_isConnected) + return true; + + if (FAILED(SimConnect_Open(&m_hSimConnect, "BlackBox", nullptr, 0, 0, 0))) + { + return false; + } + SimConnect_Close(m_hSimConnect); + + return true; + } + void CSimulatorFsx::addRemoteAircraft(const CCallsign &callsign, const QString &type, const CAircraftSituation &initialSituation) { Q_UNUSED(type); @@ -257,22 +300,6 @@ namespace BlackSimPlugin update(); } - void CSimulatorFsx::checkConnection() - { - if (FAILED(SimConnect_Open(&m_hSimConnect, "BlackBox", nullptr, 0, 0, 0))) - { - // QTimer::singleShot(5000, this, SLOT(checkConnection())); - return; - } - - initSystemEvents(); - initDataDefinitions(); - startTimer(50); - m_isConnected = true; - - emit connectionChanged(true); - } - void CSimulatorFsx::dispatch() { SimConnect_CallDispatch(m_hSimConnect, SimConnectProc, this); diff --git a/src/plugins/simulator/fsx/simulator_fsx.h b/src/plugins/simulator/fsx/simulator_fsx.h index 1b0a70f6d..1dd0cec08 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.h +++ b/src/plugins/simulator/fsx/simulator_fsx.h @@ -61,6 +61,16 @@ namespace BlackSimPlugin //! \copydoc ISimulator::isConnected() virtual bool isConnected() const override; + //! \copydoc ISimulator::canConnect() + virtual bool canConnect() override; + + public slots: + + //! \copydoc ISimulator::connectTo() + virtual bool connectTo() override; + + //! \copydoc ISimulator::disconnectFrom() + virtual bool disconnectFrom() override; //! \copydoc ISimulator::getOwnAircraft() virtual BlackMisc::Aviation::CAircraft getOwnAircraft() const override { return m_ownAircraft; } @@ -114,8 +124,6 @@ namespace BlackSimPlugin virtual void timerEvent(QTimerEvent *event); private slots: - //! \brief Try to connect - void checkConnection(); //! \brief Dispatch SimConnect messages void dispatch(); @@ -151,6 +159,8 @@ namespace BlackSimPlugin BlackSim::CSimulatorInfo m_simulatorInfo; BlackMisc::Aviation::CAircraft m_ownAircraft; //!< Object representing our own aircraft from simulator QHash m_simConnectObjects; + + int m_simconnectTimerId; }; }