diff --git a/src/blackcore/context/contextownaircraftimpl.cpp b/src/blackcore/context/contextownaircraftimpl.cpp index 92f006a8c..7d4103b65 100644 --- a/src/blackcore/context/contextownaircraftimpl.cpp +++ b/src/blackcore/context/contextownaircraftimpl.cpp @@ -324,11 +324,8 @@ namespace BlackCore void CContextOwnAircraft::ps_allSwiftWebDataRead() { - const CAircraftModel model = this->getOwnAircraftModel(); - if (model.isLoadedFromDb()) { return; } - - // a reverse lookup of the model could make sense - this->updateOwnModel(model); // force reverse lookup + // we should already receive a reverse lookup model + // from the driver } void CContextOwnAircraft::setAudioVoiceRoomOverrideUrls(const QString &voiceRoom1Url, const QString &voiceRoom2Url) diff --git a/src/blackcore/db/databaseutils.cpp b/src/blackcore/db/databaseutils.cpp index 4c1e75868..90fee482e 100644 --- a/src/blackcore/db/databaseutils.cpp +++ b/src/blackcore/db/databaseutils.cpp @@ -47,6 +47,7 @@ namespace BlackCore Q_ASSERT_X(sApp->hasWebDataServices(), Q_FUNC_INFO, "No web services"); if (modified) { *modified = false; } + if (!hasDbAircraftData()) { return model; } if (!model.hasModelString()) { return model; } if (!force && model.hasValidDbKey()) { return model; } const int distributorOrder = model.getDistributorOrder(); // later restore that order @@ -213,5 +214,10 @@ namespace BlackCore } return stashModels; } + + bool CDatabaseUtils::hasDbAircraftData() + { + return sApp && sApp->hasWebDataServices() && sApp->getWebDataServices()->hasDbAircraftData(); + } } // ns } // ns diff --git a/src/blackcore/db/databaseutils.h b/src/blackcore/db/databaseutils.h index 326c5f6fd..788dc545a 100644 --- a/src/blackcore/db/databaseutils.h +++ b/src/blackcore/db/databaseutils.h @@ -50,6 +50,9 @@ namespace BlackCore //! Create stash models if the DB models miss that simulator static BlackMisc::Simulation::CAircraftModelList updateSimulatorForFsFamily(const BlackMisc::Simulation::CAircraftModelList &ownModels, int maxToStash = -1, BlackCore::IProgressIndicator *progressIndicator = nullptr, bool processEvents = true); + + //! Convenience function + static bool hasDbAircraftData(); }; } // ns } // ns diff --git a/src/blackcore/simulatorcommon.cpp b/src/blackcore/simulatorcommon.cpp index 11dfd83da..6b70be022 100644 --- a/src/blackcore/simulatorcommon.cpp +++ b/src/blackcore/simulatorcommon.cpp @@ -9,6 +9,8 @@ #include "blackcore/simulatorcommon.h" #include "blackcore/db/databaseutils.h" +#include "blackcore/db/databaseutils.h" +#include "blackcore/webdataservices.h" #include "blackmisc/aviation/aircraftsituation.h" #include "blackmisc/aviation/callsign.h" #include "blackmisc/interpolator.h" @@ -31,6 +33,7 @@ using namespace BlackMisc::Simulation; using namespace BlackMisc::PhysicalQuantities; using namespace BlackMisc::Simulation; using namespace BlackMisc::Weather; +using namespace BlackCore; using namespace BlackCore::Db; namespace BlackCore @@ -63,6 +66,12 @@ namespace BlackCore connect(&m_oneSecondTimer, &QTimer::timeout, this, &CSimulatorCommon::ps_oneSecondTimer); this->m_oneSecondTimer.start(1000); + // swift data + if (sApp && sApp->getWebDataServices()) + { + connect(sApp->getWebDataServices(), &CWebDataServices::allSwiftDbDataRead, this, &CSimulatorCommon::ps_allSwiftDataRead); + } + // info CLogMessage(this).info("Initialized simulator driver %1") << m_simulatorPluginInfo.toQString(); } @@ -164,6 +173,41 @@ namespace BlackCore } } + void CSimulatorCommon::reverseLookupAndUpdateOwnAircraftModel(const QString &modelString) + { + CAircraftModel model = getOwnAircraftModel(); + model.setModelString(modelString); + this->reverseLookupAndUpdateOwnAircraftModel(model); + } + + void CSimulatorCommon::reverseLookupAndUpdateOwnAircraftModel(const BlackMisc::Simulation::CAircraftModel &model) + { + Q_ASSERT_X(sApp, Q_FUNC_INFO, "Missing sApp"); + Q_ASSERT_X(sApp->hasWebDataServices(), Q_FUNC_INFO, "Missing web services"); + + if (!model.hasModelString()) { return; } + if (this->getOwnAircraftModel() != model) + { + if (CDatabaseUtils::hasDbAircraftData()) + { + const CAircraftModel newModel = reverseLookupModel(model); + const bool updated = this->updateOwnModel(newModel); // update in provider (normally the context) + if (updated) + { + emit this->ownAircraftModelChanged(this->getOwnAircraftModel()); + } + } + else + { + // we wait for the data + connect(sApp->getWebDataServices(), &CWebDataServices::allSwiftDbDataRead, this, [ = ] + { + this->reverseLookupAndUpdateOwnAircraftModel(model); + }); + } + } + } + CAircraftModel CSimulatorCommon::reverseLookupModel(const CAircraftModel &model) { bool modified = false; @@ -171,6 +215,11 @@ namespace BlackCore return reverseModel; } + void CSimulatorCommon::ps_allSwiftDataRead() + { + // void + } + CAircraftModel CSimulatorCommon::getDefaultModel() const { return m_defaultModel; diff --git a/src/blackcore/simulatorcommon.h b/src/blackcore/simulatorcommon.h index 02f9688e7..b39786ada 100644 --- a/src/blackcore/simulatorcommon.h +++ b/src/blackcore/simulatorcommon.h @@ -125,6 +125,12 @@ namespace BlackCore //! Override situation from current interpolator values, if any! bool setInitialAircraftSituation(BlackMisc::Simulation::CSimulatedAircraft &aircraft) const; + //! Set own model + void reverseLookupAndUpdateOwnAircraftModel(const BlackMisc::Simulation::CAircraftModel &model); + + //! Set own model + void reverseLookupAndUpdateOwnAircraftModel(const QString &modelString); + protected: BlackMisc::IInterpolator *m_interpolator = nullptr; //!< interpolator instance bool m_pausedSimFreezesInterpolation = false; //!< paused simulator will also pause interpolation (so AI aircraft will hold) @@ -137,6 +143,10 @@ namespace BlackCore //! Lookup against DB data static BlackMisc::Simulation::CAircraftModel reverseLookupModel(const BlackMisc::Simulation::CAircraftModel &model); + private slots: + //! All swift data read from DB + void ps_allSwiftDataRead(); + private: bool m_debugMessages = false; //!< Display debug messages bool m_blinkCycle = false; //!< use for highlighting diff --git a/src/blackcore/webdataservices.cpp b/src/blackcore/webdataservices.cpp index 56459b94d..a54df1de8 100644 --- a/src/blackcore/webdataservices.cpp +++ b/src/blackcore/webdataservices.cpp @@ -170,6 +170,11 @@ namespace BlackCore return false; } + bool CWebDataServices::hasDbAircraftData() const + { + return (this->getModelsCount() > 0) && (this->getLiveriesCount() > 0) && (this->getDistributorsCount() > 0) && (this->getAircraftIcaoCodesCount() > 0); + } + void CWebDataServices::synchronizeDbCaches(CEntityFlags::Entity entities) { if (this->m_infoDataReader) { this->m_infoDataReader->synchronizeCaches(entities); } diff --git a/src/blackcore/webdataservices.h b/src/blackcore/webdataservices.h index 083a7a1f2..4d195073b 100644 --- a/src/blackcore/webdataservices.h +++ b/src/blackcore/webdataservices.h @@ -336,6 +336,9 @@ namespace BlackCore //! Can connect to swift DB? bool canConnectSwiftDb() const; + //! All DB data for an aircraft entity available? + bool hasDbAircraftData() const; + //! Synchronize all DB caches void synchronizeDbCaches(BlackMisc::Network::CEntityFlags::Entity entities); diff --git a/src/plugins/simulator/fs9/simulatorfs9.cpp b/src/plugins/simulator/fs9/simulatorfs9.cpp index 29b4e9593..57d238208 100644 --- a/src/plugins/simulator/fs9/simulatorfs9.cpp +++ b/src/plugins/simulator/fs9/simulatorfs9.cpp @@ -320,7 +320,7 @@ namespace BlackSimPlugin { MPChangePlayerPlane mpChangePlayerPlane; MultiPlayerPacketParser::readMessage(message, mpChangePlayerPlane); - setOwnAircraftModel(mpChangePlayerPlane.aircraft_name); + reverseLookupAndUpdateOwnAircraftModel(mpChangePlayerPlane.aircraft_name); break; } case CFs9Sdk::MULTIPLAYER_PACKET_ID_POSITION_VELOCITY: diff --git a/src/plugins/simulator/fscommon/simulatorfscommon.cpp b/src/plugins/simulator/fscommon/simulatorfscommon.cpp index b542c171f..00093c428 100644 --- a/src/plugins/simulator/fscommon/simulatorfscommon.cpp +++ b/src/plugins/simulator/fscommon/simulatorfscommon.cpp @@ -67,27 +67,6 @@ namespace BlackSimPlugin return m_airportsInRange; } - void CSimulatorFsCommon::setOwnAircraftModel(const QString &modelName) - { - CAircraftModel model = getOwnAircraftModel(); - model.setModelString(modelName); - this->setOwnAircraftModel(model); - } - - void CSimulatorFsCommon::setOwnAircraftModel(const BlackMisc::Simulation::CAircraftModel &model) - { - if (!model.hasModelString()) { return; } - if (this->getOwnAircraftModel() != model) - { - const CAircraftModel newModel = reverseLookupModel(model); - const bool updated = this->updateOwnModel(newModel); // update in provider (normally the context) - if (updated) - { - emit this->ownAircraftModelChanged(this->getOwnAircraftModel()); - } - } - } - bool CSimulatorFsCommon::changeRemoteAircraftModel(const CSimulatedAircraft &aircraft) { // remove upfront, and then enable / disable again diff --git a/src/plugins/simulator/fscommon/simulatorfscommon.h b/src/plugins/simulator/fscommon/simulatorfscommon.h index 3b3826d40..59654adcd 100644 --- a/src/plugins/simulator/fscommon/simulatorfscommon.h +++ b/src/plugins/simulator/fscommon/simulatorfscommon.h @@ -66,12 +66,6 @@ namespace BlackSimPlugin BlackMisc::Aviation::CComSystem m_simCom1; //!< cockpit COM1 state in simulator BlackMisc::Aviation::CComSystem m_simCom2; //!< cockpit COM2 state in simulator BlackMisc::Aviation::CTransponder m_simTransponder; //!< cockpit xpdr state in simulator - - //! Set own model - void setOwnAircraftModel(const BlackMisc::Simulation::CAircraftModel &model); - - //! Set own model - void setOwnAircraftModel(const QString &modelName); }; } // namespace } // namespace diff --git a/src/plugins/simulator/fsx/simulatorfsxsimconnectproc.cpp b/src/plugins/simulator/fsx/simulatorfsxsimconnectproc.cpp index de96d140d..8a69d3447 100644 --- a/src/plugins/simulator/fsx/simulatorfsxsimconnectproc.cpp +++ b/src/plugins/simulator/fsx/simulatorfsxsimconnectproc.cpp @@ -172,7 +172,7 @@ namespace BlackSimPlugin CAircraftModel model; model.setModelString(dataDefinitionModel->title); model.setModelType(CAircraftModel::TypeOwnSimulatorModel); - simulatorFsx->setOwnAircraftModel(model); + simulatorFsx->reverseLookupAndUpdateOwnAircraftModel(model); break; } case CSimConnectDefinitions::RequestSimEnvironment: diff --git a/src/plugins/simulator/xplane/simulatorxplane.cpp b/src/plugins/simulator/xplane/simulatorxplane.cpp index 0ca68a56e..f9d31e47e 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.cpp +++ b/src/plugins/simulator/xplane/simulatorxplane.cpp @@ -310,9 +310,7 @@ namespace BlackSimPlugin model.setFileName(path + "/" + filename); model.setLivery(CLivery("XPLANE." + livery, airlineIcaoCode, "XP livery", "", "", false)); - // updated model. - // Hint: will update in own model context by using reverse lookup - emit ownAircraftModelChanged(model); + this->reverseLookupAndUpdateOwnAircraftModel(model); } void CSimulatorXPlane::displayStatusMessage(const BlackMisc::CStatusMessage &message) const