From 3228bf9283d01fe213dd73c6fee2ac7fb42c7d07 Mon Sep 17 00:00:00 2001 From: Holden Date: Wed, 16 Oct 2024 15:45:22 -0400 Subject: [PATCH] Convert AirLinkManager & UTMSPManager to Singletons --- src/AirLink/AirLinkManager.cc | 99 +++++++------ src/AirLink/AirLinkManager.h | 45 +++--- src/AirLink/AirlinkLink.cc | 153 ++++++++++----------- src/AirLink/AirlinkLink.h | 24 ++-- src/QGCToolbox.cc | 19 --- src/QGCToolbox.h | 19 --- src/QmlControls/QGroundControlQmlGlobal.cc | 11 +- src/QmlControls/QGroundControlQmlGlobal.h | 15 +- src/UTMSP/CMakeLists.txt | 2 +- src/UTMSP/UTMSPManager.cpp | 24 ++-- src/UTMSP/UTMSPManager.h | 32 ++--- src/UTMSP/UTMSPVehicle.cpp | 3 +- src/UTMSP/UTMSPVehicle.h | 2 +- src/Vehicle/Vehicle.cc | 9 +- 14 files changed, 210 insertions(+), 247 deletions(-) diff --git a/src/AirLink/AirLinkManager.cc b/src/AirLink/AirLinkManager.cc index 76c388958104..109295a89a09 100644 --- a/src/AirLink/AirLinkManager.cc +++ b/src/AirLink/AirLinkManager.cc @@ -8,39 +8,39 @@ ****************************************************************************/ #include "AirLinkManager.h" +#include "QGCApplication.h" +#include "QGCToolbox.h" #include "SettingsManager.h" +#include "QGCLoggingCategory.h" +#include +#include #include +#include #include #include -#include -#include -#include + +QGC_LOGGING_CATEGORY(AirLinkManagerLog, "qgc.airlink.airlinkmanager"); const QString AirLinkManager::airlinkHost = "air-link.space"; -AirLinkManager::AirLinkManager(QGCApplication* app, QGCToolbox* toolbox) - : QGCTool(app, toolbox) -{ -} +Q_APPLICATION_STATIC(AirLinkManager, _airLinkManager); -AirLinkManager::~AirLinkManager() +AirLinkManager::AirLinkManager(QObject *parent) + : QObject(parent) + , _mngr(new QNetworkAccessManager(this)) { + // qCDebug(AirLinkManagerLog) << Q_FUNC_INFO << this; } -void AirLinkManager::setToolbox(QGCToolbox* toolbox) -{ - QGCTool::setToolbox(toolbox); -} - -QStringList AirLinkManager::droneList() const +AirLinkManager::~AirLinkManager() { - return _vehiclesFromServer.keys(); + // qCDebug(AirLinkManagerLog) << Q_FUNC_INFO << this; } -void AirLinkManager::updateDroneList(const QString &login, const QString &pass) +AirLinkManager *AirLinkManager::instance() { - connectToAirLinkServer(login, pass); + return _airLinkManager(); } bool AirLinkManager::isOnline(const QString &drone) @@ -52,10 +52,14 @@ bool AirLinkManager::isOnline(const QString &drone) } } -void AirLinkManager::connectToAirLinkServer(const QString &login, const QString &pass) +void AirLinkManager::updateCredentials(const QString &login, const QString &pass) { - QNetworkAccessManager *mngr = new QNetworkAccessManager(this); + qgcApp()->toolbox()->settingsManager()->appSettings()->loginAirLink()->setRawValue(login); + qgcApp()->toolbox()->settingsManager()->appSettings()->passAirLink()->setRawValue(pass); +} +void AirLinkManager::_connectToAirLinkServer(const QString &login, const QString &pass) +{ const QUrl url("https://air-link.space/api/gs/getModems"); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); @@ -64,48 +68,43 @@ void AirLinkManager::connectToAirLinkServer(const QString &login, const QString obj["login"] = login; obj["password"] = pass; QJsonDocument doc(obj); - QByteArray data = doc.toJson(); - - _reply = mngr->post(request, data); - - QObject::connect(_reply, &QNetworkReply::finished, [this](){ - _processReplyAirlinkServer(*_reply); - _reply->deleteLater(); - }); + const QByteArray data = doc.toJson(); - mngr = nullptr; - delete mngr; + QNetworkReply *const reply = _mngr->post(request, data); + (void) QObject::connect(reply, &QNetworkReply::finished, this, &AirLinkManager::_processReplyAirlinkServer); } -void AirLinkManager::updateCredentials(const QString &login, const QString &pass) +void AirLinkManager::_processReplyAirlinkServer() { - _toolbox->settingsManager()->appSettings()->loginAirLink()->setRawValue(login); - _toolbox->settingsManager()->appSettings()->passAirLink()->setRawValue(pass); + QNetworkReply* const reply = qobject_cast(sender()); + if (!reply) { + return; + } + reply->deleteLater(); + + if (reply->error() != QNetworkReply::NoError) { + qCDebug(AirLinkManagerLog) << "Airlink auth - network error"; + return; + } + + const QByteArray ba = reply->readAll(); + + if (!QJsonDocument::fromJson(ba)["modems"].toArray().isEmpty()) { + _parseAnswer(ba); + } else { + qCDebug(AirLinkManagerLog) << "No airlink modems in answer"; + } } void AirLinkManager::_parseAnswer(const QByteArray &ba) { _vehiclesFromServer.clear(); + for (const auto &arr : QJsonDocument::fromJson(ba)["modems"].toArray()) { - QString droneModem = arr.toObject()["name"].toString(); - bool isOnline = arr.toObject()["isOnline"].toBool(); + const QString droneModem = arr.toObject()["name"].toString(); + const bool isOnline = arr.toObject()["isOnline"].toBool(); _vehiclesFromServer[droneModem] = isOnline; } - emit droneListChanged(); -} -void AirLinkManager::_processReplyAirlinkServer(QNetworkReply &reply) -{ - QByteArray ba = reply.readAll(); - - if (reply.error() == QNetworkReply::NoError) { - if (!QJsonDocument::fromJson(ba)["modems"].toArray().isEmpty()) { - _parseAnswer(ba); - } else { - qDebug() << "No airlink modems in answer"; - } - } else { - qDebug() << "Airlink auth - network error"; - } + emit droneListChanged(); } - diff --git a/src/AirLink/AirLinkManager.h b/src/AirLink/AirLinkManager.h index 0b18dcf1abf7..b7be903f8c93 100644 --- a/src/AirLink/AirLinkManager.h +++ b/src/AirLink/AirLinkManager.h @@ -9,44 +9,47 @@ #pragma once -#include "QGCToolbox.h" - +#include #include +#include + +class QNetworkAccessManager; -class AppSettings; -class QGCApplication; -class LinkInterface; -class QNetworkReply; +Q_DECLARE_LOGGING_CATEGORY(AirLinkManagerLog) -//----------------------------------------------------------------------------- -class AirLinkManager : public QGCTool +class AirLinkManager : public QObject { Q_OBJECT -public: Q_PROPERTY(QStringList droneList READ droneList NOTIFY droneListChanged) - Q_INVOKABLE void updateDroneList(const QString &login, const QString &pass); + +public: + explicit AirLinkManager(QObject *parent = nullptr); + ~AirLinkManager(); + + /// Gets the singleton instance of AirLinkManager. + /// @return The singleton instance. + static AirLinkManager *instance(); + + Q_INVOKABLE void updateDroneList(const QString &login, const QString &pass) { _connectToAirLinkServer(login, pass); } Q_INVOKABLE bool isOnline(const QString &drone); - Q_INVOKABLE void connectToAirLinkServer(const QString &login, const QString &pass); Q_INVOKABLE void updateCredentials(const QString &login, const QString &pass); - explicit AirLinkManager(QGCApplication* app, QGCToolbox* toolbox); - ~AirLinkManager() override; - - void setToolbox (QGCToolbox* toolbox) override; - QStringList droneList() const; + QStringList droneList() const { return _vehiclesFromServer.keys(); } static const QString airlinkHost; static constexpr int airlinkPort = 10000; signals: - void droneListChanged(); + void droneListChanged(); -private: - void _parseAnswer (const QByteArray &ba); - void _processReplyAirlinkServer (QNetworkReply &reply); +private slots: + void _processReplyAirlinkServer(); private: + void _connectToAirLinkServer(const QString &login, const QString &pass); + void _parseAnswer(const QByteArray &ba); + + QNetworkAccessManager *_mngr = nullptr; QMap _vehiclesFromServer; - QNetworkReply* _reply; }; diff --git a/src/AirLink/AirlinkLink.cc b/src/AirLink/AirlinkLink.cc index 6d6ffff151f5..eee5e4c72f82 100644 --- a/src/AirLink/AirlinkLink.cc +++ b/src/AirLink/AirlinkLink.cc @@ -18,79 +18,9 @@ #include #include - -AirlinkConfiguration::AirlinkConfiguration(const QString &name) : UDPConfiguration(name) -{ - -} - -AirlinkConfiguration::AirlinkConfiguration(AirlinkConfiguration *source) : UDPConfiguration(source) -{ - _copyFrom(source); -} - -AirlinkConfiguration::~AirlinkConfiguration() -{ -} - -void AirlinkConfiguration::setUsername(QString username) -{ - _username = username; -} - -void AirlinkConfiguration::setPassword(QString password) -{ - _password = password; -} - -void AirlinkConfiguration::setModemName(QString modemName) -{ - _modemName = modemName; -} - -void AirlinkConfiguration::loadSettings(QSettings &settings, const QString &root) -{ - AppSettings *appSettings = qgcApp()->toolbox()->settingsManager()->appSettings(); - settings.beginGroup(root); - _username = settings.value(_usernameSettingsKey, appSettings->loginAirLink()->rawValueString()).toString(); - _password = settings.value(_passwordSettingsKey, appSettings->passAirLink()->rawValueString()).toString(); - _modemName = settings.value(_modemNameSettingsKey).toString(); - settings.endGroup(); -} - -void AirlinkConfiguration::saveSettings(QSettings &settings, const QString &root) -{ - settings.beginGroup(root); - settings.setValue(_usernameSettingsKey, _username); - settings.setValue(_passwordSettingsKey, _password); - settings.setValue(_modemNameSettingsKey, _modemName); - settings.endGroup(); -} - -void AirlinkConfiguration::copyFrom(LinkConfiguration *source) -{ - LinkConfiguration::copyFrom(source); - auto* udpSource = qobject_cast(source); - if (udpSource) { - UDPConfiguration::copyFrom(source); - } - _copyFrom(source); -} - -void AirlinkConfiguration::_copyFrom(LinkConfiguration *source) -{ - auto* airlinkSource = qobject_cast(source); - if (airlinkSource) { - _username = airlinkSource->username(); - _password = airlinkSource->password(); - _modemName = airlinkSource->modemName(); - } else { - qWarning() << "Internal error: cannot read AirlinkConfiguration from given source"; - } -} - - -AirlinkLink::AirlinkLink(SharedLinkConfigurationPtr &config) : UDPLink(config) +AirlinkLink::AirlinkLink(SharedLinkConfigurationPtr &config) + : UDPLink(config) + , _airlinkConfig(qobject_cast(config.get())) { _configureUdpSettings(); } @@ -158,7 +88,7 @@ void AirlinkLink::_configureUdpSettings() QUdpSocket udpSocket; while (!udpSocket.bind(QHostAddress::LocalHost, availablePort)) availablePort++; - UDPConfiguration* udpConfig = dynamic_cast(UDPLink::m_config.get()); + UDPConfiguration* udpConfig = dynamic_cast(UDPLink::_config.get()); udpConfig->addHost(AirLinkManager::airlinkHost, AirLinkManager::airlinkPort); udpConfig->setLocalPort(availablePort); udpConfig->setDynamic(false); @@ -169,9 +99,8 @@ void AirlinkLink::_sendLoginMsgToAirLink() __mavlink_airlink_auth_t auth; uint8_t buffer[MAVLINK_MAX_PACKET_LEN]; mavlink_message_t mavmsg; - AirlinkConfiguration* config = dynamic_cast(m_config.get()); - QString login = config->modemName(); ///< Connect not to account but to specific modem - QString pass = config->password(); + QString login = _airlinkConfig->modemName(); ///< Connect not to account but to specific modem + QString pass = _airlinkConfig->password(); memset(&auth.login, 0, sizeof(auth.login)); memset(&auth.password, 0, sizeof(auth.password)); @@ -198,3 +127,73 @@ void AirlinkLink::_setConnectFlag(bool connect) QMutexLocker locker(&_mutex); _needToConnect = connect; } + +AirlinkConfiguration::AirlinkConfiguration(const QString &name) : UDPConfiguration(name) +{ + +} + +AirlinkConfiguration::AirlinkConfiguration(const AirlinkConfiguration *source) : UDPConfiguration(source) +{ + _copyFrom(source); +} + +AirlinkConfiguration::~AirlinkConfiguration() +{ +} + +void AirlinkConfiguration::setUsername(const QString &username) +{ + _username = username; +} + +void AirlinkConfiguration::setPassword(const QString &password) +{ + _password = password; +} + +void AirlinkConfiguration::setModemName(const QString &modemName) +{ + _modemName = modemName; +} + +void AirlinkConfiguration::loadSettings(QSettings &settings, const QString &root) +{ + AppSettings *const appSettings = qgcApp()->toolbox()->settingsManager()->appSettings(); + settings.beginGroup(root); + _username = settings.value(_usernameSettingsKey, appSettings->loginAirLink()->rawValueString()).toString(); + _password = settings.value(_passwordSettingsKey, appSettings->passAirLink()->rawValueString()).toString(); + _modemName = settings.value(_modemNameSettingsKey).toString(); + settings.endGroup(); +} + +void AirlinkConfiguration::saveSettings(QSettings &settings, const QString &root) +{ + settings.beginGroup(root); + settings.setValue(_usernameSettingsKey, _username); + settings.setValue(_passwordSettingsKey, _password); + settings.setValue(_modemNameSettingsKey, _modemName); + settings.endGroup(); +} + +void AirlinkConfiguration::copyFrom(const LinkConfiguration *source) +{ + LinkConfiguration::copyFrom(source); + const auto* udpSource = qobject_cast(source); + if (udpSource) { + UDPConfiguration::copyFrom(source); + } + _copyFrom(source); +} + +void AirlinkConfiguration::_copyFrom(const LinkConfiguration *source) +{ + const auto* airlinkSource = qobject_cast(source); + if (airlinkSource) { + _username = airlinkSource->username(); + _password = airlinkSource->password(); + _modemName = airlinkSource->modemName(); + } else { + qWarning() << "Internal error: cannot read AirlinkConfiguration from given source"; + } +} diff --git a/src/AirLink/AirlinkLink.h b/src/AirLink/AirlinkLink.h index 2291fbcedaaa..dcadace39a47 100644 --- a/src/AirLink/AirlinkLink.h +++ b/src/AirLink/AirlinkLink.h @@ -16,30 +16,30 @@ class AirlinkConfiguration : public UDPConfiguration { Q_OBJECT -public: + Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged) Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged) Q_PROPERTY(QString modemName READ modemName WRITE setModemName NOTIFY modemNameChanged) - +public: AirlinkConfiguration(const QString& name); - AirlinkConfiguration(AirlinkConfiguration* source); + AirlinkConfiguration(const AirlinkConfiguration* source); ~AirlinkConfiguration(); QString username () const { return _username; } QString password () const { return _password; } QString modemName() const { return _modemName; } - void setUsername (QString username); - void setPassword (QString password); - void setModemName (QString modemName); + void setUsername (const QString &username); + void setPassword (const QString &password); + void setModemName (const QString &modemName); /// LinkConfiguration overrides - LinkType type (void) override { return LinkConfiguration::Airlink; } + LinkType type (void) const override { return LinkConfiguration::Airlink; } void loadSettings (QSettings& settings, const QString& root) override; void saveSettings (QSettings& settings, const QString& root) override; QString settingsURL (void) override { return "AirLinkSettings.qml"; } QString settingsTitle (void) override { return tr("Airlink Link Settings"); } - void copyFrom (LinkConfiguration* source) override; + void copyFrom (const LinkConfiguration* source) override; signals: @@ -48,7 +48,7 @@ class AirlinkConfiguration : public UDPConfiguration void modemNameChanged (void); private: - void _copyFrom (LinkConfiguration *source); + void _copyFrom (const LinkConfiguration *source); QString _username; @@ -63,9 +63,10 @@ class AirlinkConfiguration : public UDPConfiguration class AirlinkLink : public UDPLink { Q_OBJECT + public: AirlinkLink(SharedLinkConfigurationPtr& config); - virtual ~AirlinkLink(); + ~AirlinkLink(); /// LinkInterface overrides // bool isConnected(void) const override; @@ -83,7 +84,8 @@ class AirlinkLink : public UDPLink bool _stillConnecting(); void _setConnectFlag(bool connect); + const AirlinkConfiguration* _airlinkConfig; QMutex _mutex; /// Access this varible only with _mutex locked - bool _needToConnect {false}; + bool _needToConnect = false; }; diff --git a/src/QGCToolbox.cc b/src/QGCToolbox.cc index 09e933c72127..b5fd94da5ca3 100644 --- a/src/QGCToolbox.cc +++ b/src/QGCToolbox.cc @@ -23,18 +23,11 @@ #include "QGCCorePlugin.h" #include "SettingsManager.h" #include "QGCApplication.h" -#ifndef QGC_AIRLINK_DISABLED -#include "AirLinkManager.h" -#endif #if defined(QGC_CUSTOM_BUILD) #include CUSTOMHEADER #endif -#ifdef QGC_UTM_ADAPTER -#include "UTMSPManager.h" -#endif - QGCToolbox::QGCToolbox(QGCApplication* app) : QObject(app) { @@ -56,12 +49,6 @@ QGCToolbox::QGCToolbox(QGCApplication* app) _videoManager = new VideoManager (app, this); _mavlinkLogManager = new MAVLinkLogManager (app, this); -#ifndef QGC_AIRLINK_DISABLED - _airlinkManager = new AirLinkManager (app, this); -#endif -#ifdef QGC_UTM_ADAPTER - _utmspManager = new UTMSPManager (app, this); -#endif } void QGCToolbox::setChildToolboxes(void) @@ -82,12 +69,6 @@ void QGCToolbox::setChildToolboxes(void) _qgcPositionManager->setToolbox(this); _videoManager->setToolbox(this); _mavlinkLogManager->setToolbox(this); -#ifndef QGC_AIRLINK_DISABLED - _airlinkManager->setToolbox(this); -#endif -#ifdef QGC_UTM_ADAPTER - _utmspManager->setToolbox(this); -#endif } void QGCToolbox::_scanAndLoadPlugins(QGCApplication* app) diff --git a/src/QGCToolbox.h b/src/QGCToolbox.h index 634ee38d2135..3cb4a58dedba 100644 --- a/src/QGCToolbox.h +++ b/src/QGCToolbox.h @@ -26,12 +26,6 @@ class VideoManager; class MAVLinkLogManager; class QGCCorePlugin; class SettingsManager; -#ifndef QGC_AIRLINK_DISABLED -class AirLinkManager; -#endif -#ifdef QGC_UTM_ADAPTER -class UTMSPManager; -#endif /// This is used to manage all of our top level services/tools class QGCToolbox : public QObject { @@ -54,12 +48,6 @@ class QGCToolbox : public QObject { #ifndef NO_SERIAL_LINK GPSManager* gpsManager () { return _gpsManager; } #endif -#ifndef QGC_AIRLINK_DISABLED - AirLinkManager* airlinkManager () { return _airlinkManager; } -#endif -#ifdef QGC_UTM_ADAPTER - UTMSPManager* utmspManager () { return _utmspManager; } -#endif private: void setChildToolboxes(void); @@ -79,13 +67,6 @@ class QGCToolbox : public QObject { MAVLinkLogManager* _mavlinkLogManager = nullptr; QGCCorePlugin* _corePlugin = nullptr; SettingsManager* _settingsManager = nullptr; -#ifndef QGC_AIRLINK_DISABLED - AirLinkManager* _airlinkManager = nullptr; -#endif - -#ifdef QGC_UTM_ADAPTER - UTMSPManager* _utmspManager = nullptr; -#endif friend class QGCApplication; }; diff --git a/src/QmlControls/QGroundControlQmlGlobal.cc b/src/QmlControls/QGroundControlQmlGlobal.cc index ffd7923a57b3..081b1c39d5d1 100644 --- a/src/QmlControls/QGroundControlQmlGlobal.cc +++ b/src/QmlControls/QGroundControlQmlGlobal.cc @@ -24,7 +24,12 @@ #ifdef QT_DEBUG #include "MockLink.h" #endif - +#ifndef QGC_AIRLINK_DISABLED +#include "AirLinkManager.h" +#endif +#ifdef QGC_UTM_ADAPTER +#include "UTMSPManager.h" +#endif #include #include @@ -89,10 +94,10 @@ void QGroundControlQmlGlobal::setToolbox(QGCToolbox* toolbox) #endif _globalPalette = new QGCPalette(this); #ifndef QGC_AIRLINK_DISABLED - _airlinkManager = toolbox->airlinkManager(); + _airlinkManager = AirLinkManager::instance(); #endif #ifdef QGC_UTM_ADAPTER - _utmspManager = toolbox->utmspManager(); + _utmspManager = UTMSPManager::instance(); #endif } diff --git a/src/QmlControls/QGroundControlQmlGlobal.h b/src/QmlControls/QGroundControlQmlGlobal.h index e9e2b41d39cc..769d0d7e992c 100644 --- a/src/QmlControls/QGroundControlQmlGlobal.h +++ b/src/QmlControls/QGroundControlQmlGlobal.h @@ -185,15 +185,18 @@ class QGroundControlQmlGlobal : public QGCTool static QGeoCoordinate flightMapPosition () { return _coord; } static double flightMapZoom () { return _zoom; } - AirLinkManager* airlinkManager () { return _airlinkManager; } #ifndef QGC_AIRLINK_DISABLED + AirLinkManager* airlinkManager () { return _airlinkManager; } bool airlinkSupported () { return true; } #else bool airlinkSupported () { return false; } #endif #ifdef QGC_UTM_ADAPTER - UTMSPManager* utmspManager () {return _utmspManager;} + UTMSPManager* utmspManager () {return _utmspManager;} + bool utmspSupported () { return true; } +#else + bool utmspSupported () { return false; } #endif qreal zOrderTopMost () { return 1000; } @@ -239,12 +242,6 @@ class QGroundControlQmlGlobal : public QGCTool QString qgcVersion (void) const; -#ifdef QGC_UTM_ADAPTER - bool utmspSupported() { return true; } -#else - bool utmspSupported() { return false; } -#endif - // Overrides from QGCTool virtual void setToolbox(QGCToolbox* toolbox); @@ -271,7 +268,9 @@ class QGroundControlQmlGlobal : public QGCTool #ifndef NO_SERIAL_LINK FactGroup* _gpsRtkFactGroup = nullptr; #endif +#ifndef QGC_AIRLINK_DISABLED AirLinkManager* _airlinkManager = nullptr; +#endif ADSBVehicleManager* _adsbVehicleManager = nullptr; QGCPalette* _globalPalette = nullptr; QmlUnitsConversion _unitsConversion; diff --git a/src/UTMSP/CMakeLists.txt b/src/UTMSP/CMakeLists.txt index 006baffcffd6..34a0c3f9d724 100644 --- a/src/UTMSP/CMakeLists.txt +++ b/src/UTMSP/CMakeLists.txt @@ -8,7 +8,7 @@ if(QGC_UTM_ADAPTER) message(STATUS "UTMSP is Initialized") add_definitions(-DQGC_UTM_ADAPTER) - if(NOT nlohmann_json_FOUND) + if(NOT TARGET nlohmann_json) message(STATUS "UTMSP: Fetching nlohmann/json") include(FetchContent) FetchContent_Declare(json diff --git a/src/UTMSP/UTMSPManager.cpp b/src/UTMSP/UTMSPManager.cpp index c90b60ddb65a..2292f7d0f53d 100644 --- a/src/UTMSP/UTMSPManager.cpp +++ b/src/UTMSP/UTMSPManager.cpp @@ -14,12 +14,15 @@ #include "services/dispatcher.h" #include "Vehicle.h" #include "qqml.h" +#include -UTMSPManager::UTMSPManager(QGCApplication* app, QGCToolbox* toolbox) : - QGCTool(app, toolbox), - _dispatcher(std::make_shared()) +Q_APPLICATION_STATIC(UTMSPManager, _UTMSPManager); + +UTMSPManager::UTMSPManager(QObject *parent) + : QObject(parent) + , _dispatcher(std::make_shared()) + , _utmspAuthorization(new UTMSPAuthorization(this)) { - _utmspAuthorization = new UTMSPAuthorization(); qmlRegisterUncreatableType ("QGroundControl.UTMSP", 1, 0, "UTMSPManager", "Reference only"); qmlRegisterUncreatableType ("QGroundControl.UTMSP", 1, 0, "UTMSPVehicle", "Reference only"); qmlRegisterUncreatableType ("QGroundControl.UTMSP", 1, 0, "UTMSPAuthorization", "Reference only"); @@ -28,25 +31,22 @@ UTMSPManager::UTMSPManager(QGCApplication* app, QGCToolbox* toolbox) : UTMSPManager::~UTMSPManager() { UTMSP_LOG_DEBUG() << "UTMSPManager: Destructor called"; - delete _utmspAuthorization; - _utmspAuthorization = nullptr; } -void UTMSPManager::setToolbox(QGCToolbox* toolbox) +UTMSPManager *UTMSPManager::instance() { - UTMSP_LOG_INFO() << "UTMSPManager: ToolBox Set" ; - QGCTool::setToolbox(toolbox); + return _UTMSPManager(); } UTMSPVehicle* UTMSPManager::instantiateVehicle(const Vehicle& vehicle) { // TODO: Investigate safe deletion of pointer in this modification of having a member pointer - _vehicle = new UTMSPVehicle(_dispatcher,vehicle); + _vehicle = new UTMSPVehicle(_dispatcher,vehicle,vehicle); return _vehicle; } -UTMSPAuthorization* UTMSPManager::instantiateUTMSPAuthorization(){ - +UTMSPAuthorization* UTMSPManager::instantiateUTMSPAuthorization() +{ return _utmspAuthorization; } diff --git a/src/UTMSP/UTMSPManager.h b/src/UTMSP/UTMSPManager.h index adf172b3a8c5..0945120b6802 100644 --- a/src/UTMSP/UTMSPManager.h +++ b/src/UTMSP/UTMSPManager.h @@ -9,8 +9,6 @@ #pragma once -#include "QGCToolbox.h" - #include "UTMSPVehicle.h" class UTMSPVehicle; @@ -18,27 +16,29 @@ class Vehicle; class Dispatcher; class UTMSPAuthorization; -class UTMSPManager : public QGCTool +class UTMSPManager : public QObject { Q_OBJECT Q_MOC_INCLUDE("UTMSPAuthorization.h") -public: - UTMSPManager(QGCApplication* app, QGCToolbox* toolbox); - virtual ~UTMSPManager(); + Q_PROPERTY(UTMSPVehicle *utmspVehicle READ utmspVehicle CONSTANT) + Q_PROPERTY(UTMSPAuthorization *utmspAuthorization READ utmspAuthorization CONSTANT) - Q_PROPERTY(UTMSPVehicle* utmspVehicle READ utmspVehicle CONSTANT) - Q_PROPERTY(UTMSPAuthorization* utmspAuthorization READ utmspAuthorization CONSTANT) +public: + explicit UTMSPManager(QObject *parent = nullptr); + ~UTMSPManager(); - void setToolbox (QGCToolbox* toolbox); + /// Gets the singleton instance of UTMSPManager. + /// @return The singleton instance. + static UTMSPManager *instance(); - UTMSPVehicle* instantiateVehicle (const Vehicle& vehicle); - UTMSPAuthorization* instantiateUTMSPAuthorization (void); - UTMSPAuthorization* utmspAuthorization (void) { return _utmspAuthorization;} - UTMSPVehicle* utmspVehicle (void ) {return _vehicle;}; + UTMSPVehicle *instantiateVehicle(const Vehicle &vehicle); + UTMSPAuthorization *instantiateUTMSPAuthorization(); + UTMSPAuthorization *utmspAuthorization() { return _utmspAuthorization; } + UTMSPVehicle *utmspVehicle() { return _vehicle; }; private: - UTMSPVehicle* _vehicle = nullptr; - UTMSPAuthorization* _utmspAuthorization = nullptr; - std::shared_ptr _dispatcher; + UTMSPAuthorization *_utmspAuthorization = nullptr; + std::shared_ptr _dispatcher; + UTMSPVehicle *_vehicle = nullptr; }; diff --git a/src/UTMSP/UTMSPVehicle.cpp b/src/UTMSP/UTMSPVehicle.cpp index 8a7b5cf2a3ff..e099fd71ccb2 100644 --- a/src/UTMSP/UTMSPVehicle.cpp +++ b/src/UTMSP/UTMSPVehicle.cpp @@ -12,7 +12,8 @@ #include "services/dispatcher.h" #include "Vehicle.h" -UTMSPVehicle::UTMSPVehicle(std::shared_ptr dispatcher, const Vehicle& vehicle): +UTMSPVehicle::UTMSPVehicle(std::shared_ptr dispatcher, const Vehicle& vehicle, QObject *parent) + : UTMSPServiceController(parent), _dispatcher(dispatcher), _remoteIDFlag(false), _stopFlag(false), diff --git a/src/UTMSP/UTMSPVehicle.h b/src/UTMSP/UTMSPVehicle.h index 0cffeb9749e0..1abba9f2ac6e 100644 --- a/src/UTMSP/UTMSPVehicle.h +++ b/src/UTMSP/UTMSPVehicle.h @@ -24,7 +24,7 @@ class UTMSPVehicle : public UTMSPServiceController Q_PROPERTY(bool vehicleActivation READ vehicleActivation NOTIFY vehicleActivationChanged) public: - UTMSPVehicle (std::shared_ptr dispatcher,const Vehicle& vehicle); + UTMSPVehicle (std::shared_ptr dispatcher,const Vehicle& vehicle, QObject *parent = nullptr); ~UTMSPVehicle () override = default; Q_INVOKABLE void loadTelemetryFlag(bool value); diff --git a/src/Vehicle/Vehicle.cc b/src/Vehicle/Vehicle.cc index 27145ba2df88..403f7e06f6fb 100644 --- a/src/Vehicle/Vehicle.cc +++ b/src/Vehicle/Vehicle.cc @@ -149,10 +149,7 @@ Vehicle::Vehicle(LinkInterface* link, } #ifdef QGC_UTM_ADAPTER - UTMSPManager* utmspManager = _toolbox->utmspManager(); - if (utmspManager) { - _utmspVehicle = utmspManager->instantiateVehicle(*this); - } + _utmspVehicle = UTMSPManager::instance()->instantiateVehicle(*this); #endif _autopilotPlugin = _firmwarePlugin->autopilotPlugin(this); @@ -384,10 +381,6 @@ Vehicle::~Vehicle() delete _autopilotPlugin; _autopilotPlugin = nullptr; -#ifdef QGC_UTM_ADAPTER - delete _utmspVehicle; -#endif - deleteGimbalController(); }