From dea087cb6f0f7e2c5041dc13310c71e454cedd14 Mon Sep 17 00:00:00 2001 From: Mathieu Pellerin Date: Fri, 20 Dec 2024 16:03:35 +0700 Subject: [PATCH] Avoid hardcoded receiver strings in favor of static class identifiers --- src/core/positioning/egenioussreceiver.cpp | 2 ++ src/core/positioning/egenioussreceiver.h | 2 ++ src/core/positioning/positioning.cpp | 14 +++++++++++++- src/core/positioning/positioningdevicemodel.cpp | 16 ++++++++++++---- src/core/positioning/positioningsource.cpp | 8 ++++---- src/core/positioning/serialportreceiver.cpp | 2 ++ src/core/positioning/serialportreceiver.h | 2 ++ src/core/positioning/tcpreceiver.cpp | 2 ++ src/core/positioning/tcpreceiver.h | 2 ++ src/core/positioning/udpreceiver.cpp | 2 ++ src/core/positioning/udpreceiver.h | 2 ++ 11 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/core/positioning/egenioussreceiver.cpp b/src/core/positioning/egenioussreceiver.cpp index 1ca06e5e62..ba85970016 100644 --- a/src/core/positioning/egenioussreceiver.cpp +++ b/src/core/positioning/egenioussreceiver.cpp @@ -21,6 +21,8 @@ #include #include +QLatin1String EgenioussReceiver::identifier = QLatin1String( "egeniouss" ); + EgenioussReceiver::EgenioussReceiver( QObject *parent ) : AbstractGnssReceiver( parent ), mTcpSocket( new QTcpSocket() ) { diff --git a/src/core/positioning/egenioussreceiver.h b/src/core/positioning/egenioussreceiver.h index 6c858a0322..e6aca943ad 100644 --- a/src/core/positioning/egenioussreceiver.h +++ b/src/core/positioning/egenioussreceiver.h @@ -35,6 +35,8 @@ class EgenioussReceiver : public AbstractGnssReceiver QList> details() const override; + static QLatin1String identifier; + private: void handleConnectDevice() override; void handleDisconnectDevice() override; diff --git a/src/core/positioning/positioning.cpp b/src/core/positioning/positioning.cpp index 69f13add08..8e84309509 100644 --- a/src/core/positioning/positioning.cpp +++ b/src/core/positioning/positioning.cpp @@ -16,6 +16,11 @@ #include "positioning.h" #include "positioningutils.h" +#include "tcpreceiver.h" +#include "udpreceiver.h" +#ifdef WITH_SERIALPORT +#include "serialportreceiver.h" +#endif #include #include @@ -137,11 +142,18 @@ QString Positioning::deviceSocketStateString() const AbstractGnssReceiver::Capabilities Positioning::deviceCapabilities() const { const QString deviceId = mPositioningSourceReplica->property( "deviceId" ).toString(); - if ( !deviceId.isEmpty() || deviceId.startsWith( QStringLiteral( "tcp:" ) ) || deviceId.startsWith( QStringLiteral( "ucp:" ) ) || deviceId.startsWith( QStringLiteral( "serial:" ) ) ) + if ( !deviceId.isEmpty() || deviceId.startsWith( TcpReceiver::identifier + ":" ) || deviceId.startsWith( UdpReceiver::identifier + ":" ) ) { // NMEA-based devices return AbstractGnssReceiver::Capabilities() | AbstractGnssReceiver::OrthometricAltitude | AbstractGnssReceiver::Logging; } +#ifdef WITH_SERIALPORT + else if ( deviceId.startsWith( SerialPortReceiver::identifier + ":" ) ) + { + // NMEA-based device + return AbstractGnssReceiver::Capabilities() | AbstractGnssReceiver::OrthometricAltitude | AbstractGnssReceiver::Logging; + } +#endif return AbstractGnssReceiver::NoCapabilities; } diff --git a/src/core/positioning/positioningdevicemodel.cpp b/src/core/positioning/positioningdevicemodel.cpp index 553f5e22b6..f19d5166d5 100644 --- a/src/core/positioning/positioningdevicemodel.cpp +++ b/src/core/positioning/positioningdevicemodel.cpp @@ -14,7 +14,13 @@ * * ***************************************************************************/ +#include "egenioussreceiver.h" #include "positioningdevicemodel.h" +#include "tcpreceiver.h" +#include "udpreceiver.h" +#ifdef WITH_SERIALPORT +#include "serialportreceiver.h" +#endif #include @@ -164,16 +170,18 @@ const QString PositioningDeviceModel::deviceId( const Device &device ) const return device.settings.value( QStringLiteral( "address" ) ).toString(); case TcpDevice: - return QStringLiteral( "tcp:%1:%2" ).arg( device.settings.value( QStringLiteral( "address" ) ).toString(), QString::number( device.settings.value( QStringLiteral( "port" ) ).toInt() ) ); + return QStringLiteral( "%1:%2:%3" ).arg( TcpReceiver::identifier, device.settings.value( QStringLiteral( "address" ) ).toString(), QString::number( device.settings.value( QStringLiteral( "port" ) ).toInt() ) ); case UdpDevice: - return QStringLiteral( "udp:%1:%2" ).arg( device.settings.value( QStringLiteral( "address" ) ).toString(), QString::number( device.settings.value( QStringLiteral( "port" ) ).toInt() ) ); + return QStringLiteral( "%1:%2:%3" ).arg( UdpReceiver::identifier, device.settings.value( QStringLiteral( "address" ) ).toString(), QString::number( device.settings.value( QStringLiteral( "port" ) ).toInt() ) ); +#ifdef WITH_SERIALPORT case SerialPortDevice: - return QStringLiteral( "serial:%1" ).arg( device.settings.value( QStringLiteral( "address" ) ).toString() ); + return QStringLiteral( "%1:%2" ).arg( SerialPortReceiver::identifier, device.settings.value( QStringLiteral( "address" ) ).toString() ); +#endif case EgenioussDevice: - return QStringLiteral( "egeniouss:" ); + return QStringLiteral( "%1:" ).arg( EgenioussReceiver::identifier ); } return QString(); diff --git a/src/core/positioning/positioningsource.cpp b/src/core/positioning/positioningsource.cpp index b6ceff1c21..40802b0a93 100644 --- a/src/core/positioning/positioningsource.cpp +++ b/src/core/positioning/positioningsource.cpp @@ -177,26 +177,26 @@ void PositioningSource::setupDevice() } else { - if ( mDeviceId.startsWith( QStringLiteral( "tcp:" ) ) ) + if ( mDeviceId.startsWith( TcpReceiver::identifier + ":" ) ) { const qsizetype portSeparator = mDeviceId.lastIndexOf( ':' ); const QString address = mDeviceId.mid( 4, portSeparator - 4 ); const int port = mDeviceId.mid( portSeparator + 1 ).toInt(); mReceiver = new TcpReceiver( address, port, this ); } - else if ( mDeviceId.startsWith( QStringLiteral( "udp:" ) ) ) + else if ( mDeviceId.startsWith( UdpReceiver::identifier + ":" ) ) { const qsizetype portSeparator = mDeviceId.lastIndexOf( ':' ); const QString address = mDeviceId.mid( 4, portSeparator - 4 ); const int port = mDeviceId.mid( portSeparator + 1 ).toInt(); mReceiver = new UdpReceiver( address, port, this ); } - else if ( mDeviceId.startsWith( QStringLiteral( "egeniouss:" ) ) ) + else if ( mDeviceId.startsWith( EgenioussReceiver::identifier + ":" ) ) { mReceiver = new EgenioussReceiver( this ); } #ifdef WITH_SERIALPORT - else if ( mDeviceId.startsWith( QStringLiteral( "serial:" ) ) ) + else if ( mDeviceId.startsWith( SerialPortReceiver::identifier + ":" ) ) { const QString address = mDeviceId.mid( 7 ); mReceiver = new SerialPortReceiver( address, this ); diff --git a/src/core/positioning/serialportreceiver.cpp b/src/core/positioning/serialportreceiver.cpp index 48028ce8da..e1d2679c04 100644 --- a/src/core/positioning/serialportreceiver.cpp +++ b/src/core/positioning/serialportreceiver.cpp @@ -16,6 +16,8 @@ #include "serialportreceiver.h" +QLatin1String SerialPortReceiver::identifier = QLatin1String( "serial" ); + SerialPortReceiver::SerialPortReceiver( const QString &address, QObject *parent ) : NmeaGnssReceiver( parent ) , mAddress( address ) diff --git a/src/core/positioning/serialportreceiver.h b/src/core/positioning/serialportreceiver.h index 128e4e308c..f6fe99a7fa 100644 --- a/src/core/positioning/serialportreceiver.h +++ b/src/core/positioning/serialportreceiver.h @@ -34,6 +34,8 @@ class SerialPortReceiver : public NmeaGnssReceiver explicit SerialPortReceiver( const QString &address = QString(), QObject *parent = nullptr ); ~SerialPortReceiver() override; + static QLatin1String identifier; + private: void handleConnectDevice() override; void handleDisconnectDevice() override; diff --git a/src/core/positioning/tcpreceiver.cpp b/src/core/positioning/tcpreceiver.cpp index 55e28e6840..172514fcc9 100644 --- a/src/core/positioning/tcpreceiver.cpp +++ b/src/core/positioning/tcpreceiver.cpp @@ -16,6 +16,8 @@ #include "tcpreceiver.h" +QLatin1String TcpReceiver::identifier = QLatin1String( "tcp" ); + TcpReceiver::TcpReceiver( const QString &address, const int port, QObject *parent ) : NmeaGnssReceiver( parent ) , mAddress( address ) diff --git a/src/core/positioning/tcpreceiver.h b/src/core/positioning/tcpreceiver.h index 62e93ec110..8254305a35 100644 --- a/src/core/positioning/tcpreceiver.h +++ b/src/core/positioning/tcpreceiver.h @@ -35,6 +35,8 @@ class TcpReceiver : public NmeaGnssReceiver explicit TcpReceiver( const QString &address = QString(), const int port = 0, QObject *parent = nullptr ); ~TcpReceiver() override; + static QLatin1String identifier; + public slots: QString socketStateString() override; diff --git a/src/core/positioning/udpreceiver.cpp b/src/core/positioning/udpreceiver.cpp index 1bbf96ea6e..ff3ff87b7a 100644 --- a/src/core/positioning/udpreceiver.cpp +++ b/src/core/positioning/udpreceiver.cpp @@ -20,6 +20,8 @@ #include #endif +QLatin1String UdpReceiver::identifier = QLatin1String( "udp" ); + UdpReceiver::UdpReceiver( const QString &address, const int port, QObject *parent ) : NmeaGnssReceiver( parent ) , mAddress( address ) diff --git a/src/core/positioning/udpreceiver.h b/src/core/positioning/udpreceiver.h index 8ad03978f6..db138b8cc3 100644 --- a/src/core/positioning/udpreceiver.h +++ b/src/core/positioning/udpreceiver.h @@ -36,6 +36,8 @@ class UdpReceiver : public NmeaGnssReceiver explicit UdpReceiver( const QString &address = QString(), const int port = 0, QObject *parent = nullptr ); ~UdpReceiver() override; + static QLatin1String identifier; + public slots: QString socketStateString() override;