Skip to content

Commit

Permalink
Avoid hardcoded receiver strings in favor of static class identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Dec 20, 2024
1 parent 99187e2 commit dea087c
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/core/positioning/egenioussreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <QJsonValue>
#include <QTimeZone>

QLatin1String EgenioussReceiver::identifier = QLatin1String( "egeniouss" );

EgenioussReceiver::EgenioussReceiver( QObject *parent )
: AbstractGnssReceiver( parent ), mTcpSocket( new QTcpSocket() )
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/positioning/egenioussreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class EgenioussReceiver : public AbstractGnssReceiver

QList<QPair<QString, QVariant>> details() const override;

static QLatin1String identifier;

private:
void handleConnectDevice() override;
void handleDisconnectDevice() override;
Expand Down
14 changes: 13 additions & 1 deletion src/core/positioning/positioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

#include "positioning.h"
#include "positioningutils.h"
#include "tcpreceiver.h"
#include "udpreceiver.h"
#ifdef WITH_SERIALPORT
#include "serialportreceiver.h"
#endif

#include <QScreen>
#include <qgsapplication.h>
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 12 additions & 4 deletions src/core/positioning/positioningdevicemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* *
***************************************************************************/

#include "egenioussreceiver.h"
#include "positioningdevicemodel.h"
#include "tcpreceiver.h"
#include "udpreceiver.h"
#ifdef WITH_SERIALPORT
#include "serialportreceiver.h"
#endif

#include <QSettings>

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/core/positioning/positioningsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions src/core/positioning/serialportreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "serialportreceiver.h"

QLatin1String SerialPortReceiver::identifier = QLatin1String( "serial" );

SerialPortReceiver::SerialPortReceiver( const QString &address, QObject *parent )
: NmeaGnssReceiver( parent )
, mAddress( address )
Expand Down
2 changes: 2 additions & 0 deletions src/core/positioning/serialportreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/core/positioning/tcpreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
2 changes: 2 additions & 0 deletions src/core/positioning/tcpreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/core/positioning/udpreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <sys/socket.h>
#endif

QLatin1String UdpReceiver::identifier = QLatin1String( "udp" );

UdpReceiver::UdpReceiver( const QString &address, const int port, QObject *parent )
: NmeaGnssReceiver( parent )
, mAddress( address )
Expand Down
2 changes: 2 additions & 0 deletions src/core/positioning/udpreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit dea087c

Please sign in to comment.