From 4ba9e4633bd660884284c96e5ce3754b09d57795 Mon Sep 17 00:00:00 2001 From: Mohsen Date: Fri, 4 Oct 2024 11:34:31 +0330 Subject: [PATCH] Address review (part1) --- src/core/CMakeLists.txt | 1 + src/core/positioning/abstractgnssreceiver.cpp | 32 ++++++++++++++++++ src/core/positioning/abstractgnssreceiver.h | 33 +++++++------------ src/core/positioning/bluetoothreceiver.cpp | 26 +++++++-------- src/core/positioning/bluetoothreceiver.h | 4 +-- src/core/positioning/egenioussreceiver.cpp | 26 +++++++-------- src/core/positioning/egenioussreceiver.h | 6 ++-- src/core/positioning/internalgnssreceiver.cpp | 8 +---- src/core/positioning/internalgnssreceiver.h | 4 --- src/core/positioning/nmeagnssreceiver.cpp | 2 +- src/core/positioning/nmeagnssreceiver.h | 2 +- src/core/positioning/serialportreceiver.cpp | 33 +++++++------------ src/core/positioning/serialportreceiver.h | 7 +--- src/core/positioning/tcpreceiver.cpp | 29 +++++++--------- src/core/positioning/tcpreceiver.h | 5 +-- src/core/positioning/udpreceiver.cpp | 29 +++++++--------- src/core/positioning/udpreceiver.h | 6 +--- 17 files changed, 114 insertions(+), 139 deletions(-) create mode 100644 src/core/positioning/abstractgnssreceiver.cpp diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 1da760b818..bf14a41400 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -26,6 +26,7 @@ set(QFIELD_CORE_SRCS locator/gotolocatorfilter.cpp locator/helplocatorfilter.cpp locator/locatormodelsuperbridge.cpp + positioning/abstractgnssreceiver.cpp positioning/gnsspositioninformation.cpp positioning/internalgnssreceiver.cpp positioning/nmeagnssreceiver.cpp diff --git a/src/core/positioning/abstractgnssreceiver.cpp b/src/core/positioning/abstractgnssreceiver.cpp new file mode 100644 index 0000000000..c992c92243 --- /dev/null +++ b/src/core/positioning/abstractgnssreceiver.cpp @@ -0,0 +1,32 @@ +#include "abstractgnssreceiver.h" + +AbstractGnssReceiver::AbstractGnssReceiver( QObject *parent ) + : QObject( parent ) +{ +} + +QString AbstractGnssReceiver::socketStateString() +{ + switch ( mSocketState ) + { + case QAbstractSocket::ConnectingState: + case QAbstractSocket::HostLookupState: + return tr( "Connecting…" ); + case QAbstractSocket::ConnectedState: + case QAbstractSocket::BoundState: + return tr( "Successfully connected" ); + case QAbstractSocket::UnconnectedState: + return tr( "Disconnected" ); + default: + return tr( "Socket state %1" ).arg( static_cast( socketState() ) ); + } +} + +void AbstractGnssReceiver::setSocketState( const QAbstractSocket::SocketState &state ) +{ + if ( mSocketState == state ) + return; + mSocketState = state; + emit socketStateChanged( mSocketState ); + emit socketStateStringChanged( socketStateString() ); +} diff --git a/src/core/positioning/abstractgnssreceiver.h b/src/core/positioning/abstractgnssreceiver.h index d0bb2ce7e4..0b347e9152 100644 --- a/src/core/positioning/abstractgnssreceiver.h +++ b/src/core/positioning/abstractgnssreceiver.h @@ -26,6 +26,8 @@ class AbstractGnssReceiver : public QObject Q_OBJECT Q_PROPERTY( GnssPositionInformation lastGnssPositionInformation READ lastGnssPositionInformation NOTIFY lastGnssPositionInformationChanged ) + Q_PROPERTY( QAbstractSocket::SocketState socketState READ socketState WRITE setSocketState NOTIFY socketStateChanged ) + Q_PROPERTY( QString socketStateString READ socketStateString NOTIFY socketStateStringChanged ) Q_PROPERTY( QString lastError READ lastError NOTIFY lastErrorChanged ) public: @@ -38,8 +40,7 @@ class AbstractGnssReceiver : public QObject Q_DECLARE_FLAGS( Capabilities, Capability ) Q_FLAGS( Capabilities ) - explicit AbstractGnssReceiver( QObject *parent = nullptr ) - : QObject( parent ) {} + explicit AbstractGnssReceiver( QObject *parent = nullptr ); virtual ~AbstractGnssReceiver() = default; bool valid() const { return mValid; } @@ -57,33 +58,20 @@ class AbstractGnssReceiver : public QObject Q_INVOKABLE virtual AbstractGnssReceiver::Capabilities capabilities() const { return NoCapabilities; } - virtual QList> details() { return {}; } + virtual QList> details() const { return {}; } + virtual QAbstractSocket::SocketState socketState() const { return mSocketState; } + public slots: - virtual QAbstractSocket::SocketState socketState() { return QAbstractSocket::SocketState::UnconnectedState; } - virtual QString socketStateString() - { - switch ( socketState() ) - { - case QAbstractSocket::ConnectingState: - case QAbstractSocket::HostLookupState: - return tr( "Connecting…" ); - case QAbstractSocket::ConnectedState: - case QAbstractSocket::BoundState: - return tr( "Successfully connected" ); - case QAbstractSocket::UnconnectedState: - return tr( "Disconnected" ); - default: - return tr( "Socket state %1" ).arg( static_cast( socketState() ) ); - } - } + virtual QString socketStateString(); + void setSocketState( const QAbstractSocket::SocketState &newSocketState ); signals: void validChanged(); - void lastGnssPositionInformationChanged( GnssPositionInformation &lastGnssPositionInformation ); + void lastGnssPositionInformationChanged( const GnssPositionInformation &lastGnssPositionInformation ); void socketStateChanged( const QAbstractSocket::SocketState socketState ); void socketStateStringChanged( const QString &socketStateString ); - void lastErrorChanged( QString &lastError ); + void lastErrorChanged( const QString &lastError ); private: friend class InternalGnssReceiver; @@ -102,6 +90,7 @@ class AbstractGnssReceiver : public QObject bool mValid = false; GnssPositionInformation mLastGnssPositionInformation; + QAbstractSocket::SocketState mSocketState = QAbstractSocket::UnconnectedState; QString mLastError; }; diff --git a/src/core/positioning/bluetoothreceiver.cpp b/src/core/positioning/bluetoothreceiver.cpp index 21c0bb8a0c..4016450a3e 100644 --- a/src/core/positioning/bluetoothreceiver.cpp +++ b/src/core/positioning/bluetoothreceiver.cpp @@ -27,8 +27,10 @@ BluetoothReceiver::BluetoothReceiver( const QString &address, QObject *parent ) , mLocalDevice( std::make_unique() ) , mSocket( new QBluetoothSocket( QBluetoothServiceInfo::RfcommProtocol ) ) { - connect( mSocket, &QBluetoothSocket::stateChanged, this, &BluetoothReceiver::setSocketState ); connect( mSocket, qOverload( &QBluetoothSocket::errorOccurred ), this, &BluetoothReceiver::handleError ); + connect( mSocket, &QBluetoothSocket::stateChanged, this, [=]( QBluetoothSocket::SocketState state ) { + setSocketState( static_cast( state ) ); + } ); connect( mLocalDevice.get(), &QBluetoothLocalDevice::pairingFinished, this, &BluetoothReceiver::pairingFinished ); connect( mLocalDevice.get(), &QBluetoothLocalDevice::errorOccurred, [=]( QBluetoothLocalDevice::Error error ) { @@ -57,8 +59,6 @@ BluetoothReceiver::BluetoothReceiver( const QString &address, QObject *parent ) BluetoothReceiver::~BluetoothReceiver() { - disconnect( mSocket, &QBluetoothSocket::stateChanged, this, &BluetoothReceiver::setSocketState ); - mSocket->deleteLater(); mSocket = nullptr; } @@ -161,15 +161,13 @@ void BluetoothReceiver::doConnectDevice() repairDevice( QBluetoothAddress( mAddress ) ); } -void BluetoothReceiver::setSocketState( const QBluetoothSocket::SocketState socketState ) +QAbstractSocket::SocketState BluetoothReceiver::socketState() const { - emit socketStateChanged( static_cast( socketState ) ); - emit socketStateStringChanged( socketStateString() ); -} - -QAbstractSocket::SocketState BluetoothReceiver::socketState() -{ - return mSocket ? static_cast( mSocket->state() ) : QAbstractSocket::UnconnectedState; + if ( !mSocket ) + { + return QAbstractSocket::UnconnectedState; + } + return static_cast( mSocket->state() ); } QString BluetoothReceiver::socketStateString() @@ -184,12 +182,12 @@ QString BluetoothReceiver::socketStateString() return tr( "Successfully connected" ); case QAbstractSocket::UnconnectedState: { - QString mSocketStateString = tr( "Disconnected" ); + QString socketStateString = tr( "Disconnected" ); if ( !mDisconnecting && mSocket->error() != QBluetoothSocket::SocketError::NoSocketError ) - mSocketStateString.append( QStringLiteral( ": %1" ).arg( mSocket->errorString() ) ); + socketStateString.append( QStringLiteral( ": %1" ).arg( mSocket->errorString() ) ); if ( mConnectOnDisconnect ) doConnectDevice(); - return mSocketStateString; + return socketStateString; } default: return tr( "Socket state %1" ).arg( static_cast( currentState ) ); diff --git a/src/core/positioning/bluetoothreceiver.h b/src/core/positioning/bluetoothreceiver.h index a3a8e8e663..aa79126f46 100644 --- a/src/core/positioning/bluetoothreceiver.h +++ b/src/core/positioning/bluetoothreceiver.h @@ -33,9 +33,9 @@ class BluetoothReceiver : public NmeaGnssReceiver public: explicit BluetoothReceiver( const QString &address = QString(), QObject *parent = nullptr ); ~BluetoothReceiver() override; + QAbstractSocket::SocketState socketState() const override; public slots: - QAbstractSocket::SocketState socketState() override; QString socketStateString() override; private slots: @@ -46,8 +46,6 @@ class BluetoothReceiver : public NmeaGnssReceiver */ void pairingFinished( const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing status ); - void setSocketState( const QBluetoothSocket::SocketState socketState ); - private: void handleConnectDevice() override; void handleDisconnectDevice() override; diff --git a/src/core/positioning/egenioussreceiver.cpp b/src/core/positioning/egenioussreceiver.cpp index 040bbab489..c5235575a6 100644 --- a/src/core/positioning/egenioussreceiver.cpp +++ b/src/core/positioning/egenioussreceiver.cpp @@ -9,13 +9,17 @@ EgenioussReceiver::EgenioussReceiver( QObject *parent ) { connect( mTcpSocket, &QTcpSocket::readyRead, this, &EgenioussReceiver::onReadyRead ); connect( mTcpSocket, &QTcpSocket::errorOccurred, this, &EgenioussReceiver::handleError ); - connect( mTcpSocket, &QTcpSocket::stateChanged, this, &EgenioussReceiver::setSocketState ); + connect( mTcpSocket, &QTcpSocket::stateChanged, this, [=]( QAbstractSocket::SocketState state ) { + setSocketState( state ); + } ); + setValid( true ); } EgenioussReceiver::~EgenioussReceiver() { - disconnect( mTcpSocket, &QTcpSocket::stateChanged, this, &EgenioussReceiver::setSocketState ); + mTcpSocket->deleteLater(); + mTcpSocket = nullptr; } void EgenioussReceiver::handleConnectDevice() @@ -28,18 +32,16 @@ void EgenioussReceiver::handleDisconnectDevice() mTcpSocket->disconnectFromHost(); } -QAbstractSocket::SocketState EgenioussReceiver::socketState() -{ - return mTcpSocket ? mTcpSocket->state() : QAbstractSocket::UnconnectedState; -} - -void EgenioussReceiver::setSocketState( const QAbstractSocket::SocketState socketState ) +QAbstractSocket::SocketState EgenioussReceiver::socketState() const { - emit socketStateChanged( socketState ); - emit socketStateStringChanged( socketStateString() ); + if ( mTcpSocket == nullptr ) + { + return QAbstractSocket::UnconnectedState; + } + return mTcpSocket->state(); } -QList> EgenioussReceiver::details() +QList> EgenioussReceiver::details() const { QList> detailsList; @@ -130,8 +132,6 @@ void EgenioussReceiver::handleError( QAbstractSocket::SocketError error ) mLastError = tr( "TCP receiver error (%1)" ).arg( QMetaEnum::fromType().valueToKey( error ) ); break; } - setSocketState( QAbstractSocket::UnconnectedState ); - qInfo() << QStringLiteral( "EgenioussReceiver: Error: %1" ).arg( mLastError ); emit lastErrorChanged( mLastError ); diff --git a/src/core/positioning/egenioussreceiver.h b/src/core/positioning/egenioussreceiver.h index d3d09829e0..ecf67a26c6 100644 --- a/src/core/positioning/egenioussreceiver.h +++ b/src/core/positioning/egenioussreceiver.h @@ -14,18 +14,16 @@ class EgenioussReceiver : public AbstractGnssReceiver explicit EgenioussReceiver( QObject *parent = nullptr ); ~EgenioussReceiver(); - public slots: - QAbstractSocket::SocketState socketState() override; + QList> details() const override; + QAbstractSocket::SocketState socketState() const override; private: void handleConnectDevice() override; void handleDisconnectDevice() override; - QList> details() override; private slots: void onReadyRead(); void handleError( QAbstractSocket::SocketError error ); - void setSocketState( const QAbstractSocket::SocketState socketState ); private: void processReceivedData(); diff --git a/src/core/positioning/internalgnssreceiver.cpp b/src/core/positioning/internalgnssreceiver.cpp index 4491ab8984..7b8eaba433 100644 --- a/src/core/positioning/internalgnssreceiver.cpp +++ b/src/core/positioning/internalgnssreceiver.cpp @@ -35,7 +35,7 @@ InternalGnssReceiver::InternalGnssReceiver( QObject *parent ) connect( mGeoPositionSource.get(), &QGeoPositionInfoSource::positionUpdated, this, &InternalGnssReceiver::handlePositionUpdated ); connect( mGeoPositionSource.get(), qOverload( &QGeoPositionInfoSource::errorOccurred ), this, &InternalGnssReceiver::handleError ); - mSocketState = QAbstractSocket::ConnectedState; + setSocketState( QAbstractSocket::ConnectedState ); setValid( true ); } @@ -52,12 +52,6 @@ InternalGnssReceiver::InternalGnssReceiver( QObject *parent ) connect( QgsApplication::instance(), &QGuiApplication::applicationStateChanged, this, &InternalGnssReceiver::onApplicationStateChanged ); } - -QAbstractSocket::SocketState InternalGnssReceiver::socketState() -{ - return mSocketState; -} - void InternalGnssReceiver::onApplicationStateChanged( Qt::ApplicationState state ) { #ifdef Q_OS_ANDROID diff --git a/src/core/positioning/internalgnssreceiver.h b/src/core/positioning/internalgnssreceiver.h index 1a73a00f4f..8f5296698c 100644 --- a/src/core/positioning/internalgnssreceiver.h +++ b/src/core/positioning/internalgnssreceiver.h @@ -31,9 +31,6 @@ class InternalGnssReceiver : public AbstractGnssReceiver explicit InternalGnssReceiver( QObject *parent = nullptr ); ~InternalGnssReceiver() override = default; - public slots: - QAbstractSocket::SocketState socketState() override; - private slots: void onApplicationStateChanged( Qt::ApplicationState state ); @@ -60,7 +57,6 @@ class InternalGnssReceiver : public AbstractGnssReceiver QList mSatellitesID; QList mSatellitesInfo; bool mSatelliteInformationValid = true; - QAbstractSocket::SocketState mSocketState = QAbstractSocket::UnconnectedState; }; #endif // INTERNALGNSSRECEIVER_H diff --git a/src/core/positioning/nmeagnssreceiver.cpp b/src/core/positioning/nmeagnssreceiver.cpp index d482242930..d976403188 100644 --- a/src/core/positioning/nmeagnssreceiver.cpp +++ b/src/core/positioning/nmeagnssreceiver.cpp @@ -116,7 +116,7 @@ void NmeaGnssReceiver::handleStopLogging() mLogFile.close(); } -QList> NmeaGnssReceiver::details() +QList> NmeaGnssReceiver::details() const { QList> dataList; diff --git a/src/core/positioning/nmeagnssreceiver.h b/src/core/positioning/nmeagnssreceiver.h index ec389f3427..20d6c3e147 100644 --- a/src/core/positioning/nmeagnssreceiver.h +++ b/src/core/positioning/nmeagnssreceiver.h @@ -50,7 +50,7 @@ class NmeaGnssReceiver : public AbstractGnssReceiver private: void handleStartLogging() override; void handleStopLogging() override; - QList> details() override; + QList> details() const override; void processImuSentence( const QString &sentence ); diff --git a/src/core/positioning/serialportreceiver.cpp b/src/core/positioning/serialportreceiver.cpp index 128b809daf..48028ce8da 100644 --- a/src/core/positioning/serialportreceiver.cpp +++ b/src/core/positioning/serialportreceiver.cpp @@ -19,34 +19,27 @@ SerialPortReceiver::SerialPortReceiver( const QString &address, QObject *parent ) : NmeaGnssReceiver( parent ) , mAddress( address ) - , mSocket( new QSerialPort() ) + , mSerialPort( new QSerialPort() ) { - connect( mSocket, qOverload( &QSerialPort::errorOccurred ), this, &SerialPortReceiver::handleError ); + connect( mSerialPort, qOverload( &QSerialPort::errorOccurred ), this, &SerialPortReceiver::handleError ); - initNmeaConnection( mSocket ); + initNmeaConnection( mSerialPort ); setValid( !mAddress.isEmpty() ); } SerialPortReceiver::~SerialPortReceiver() { - mSocket->deleteLater(); - mSocket = nullptr; -} - -QAbstractSocket::SocketState SerialPortReceiver::socketState() -{ - return mSocketState; + mSerialPort->deleteLater(); + mSerialPort = nullptr; } void SerialPortReceiver::handleDisconnectDevice() { - if ( mSocketState == QAbstractSocket::ConnectedState ) + if ( socketState() == QAbstractSocket::ConnectedState ) { - mSocket->close(); - mSocketState = QAbstractSocket::UnconnectedState; - emit socketStateChanged( mSocketState ); - emit socketStateStringChanged( socketStateString() ); + mSerialPort->close(); + setSocketState( QAbstractSocket::UnconnectedState ); } } @@ -58,13 +51,11 @@ void SerialPortReceiver::handleConnectDevice() } qInfo() << "SerialPortReceiver: Initiating connection to port name: " << mAddress; - mSocket->setPortName( mAddress ); - mSocket->setBaudRate( QSerialPort::Baud9600 ); - if ( mSocket->open( QIODevice::ReadOnly ) ) + mSerialPort->setPortName( mAddress ); + mSerialPort->setBaudRate( QSerialPort::Baud9600 ); + if ( mSerialPort->open( QIODevice::ReadOnly ) ) { - mSocketState = QAbstractSocket::ConnectedState; - emit socketStateChanged( mSocketState ); - emit socketStateStringChanged( socketStateString() ); + setSocketState( QAbstractSocket::ConnectedState ); } } diff --git a/src/core/positioning/serialportreceiver.h b/src/core/positioning/serialportreceiver.h index 5b315e9110..4849df2873 100644 --- a/src/core/positioning/serialportreceiver.h +++ b/src/core/positioning/serialportreceiver.h @@ -33,9 +33,6 @@ class SerialPortReceiver : public NmeaGnssReceiver explicit SerialPortReceiver( const QString &address = QString(), QObject *parent = nullptr ); ~SerialPortReceiver() override; - public slots: - QAbstractSocket::SocketState socketState() override; - private: void handleConnectDevice() override; void handleDisconnectDevice() override; @@ -43,9 +40,7 @@ class SerialPortReceiver : public NmeaGnssReceiver QString mAddress; - QSerialPort *mSocket = nullptr; - - QAbstractSocket::SocketState mSocketState = QAbstractSocket::UnconnectedState; + QSerialPort *mSerialPort = nullptr; }; #endif // SERIALPORTRECEIVER_H diff --git a/src/core/positioning/tcpreceiver.cpp b/src/core/positioning/tcpreceiver.cpp index efa3b3b4d1..2ed8e5bad6 100644 --- a/src/core/positioning/tcpreceiver.cpp +++ b/src/core/positioning/tcpreceiver.cpp @@ -22,8 +22,10 @@ TcpReceiver::TcpReceiver( const QString &address, const int port, QObject *paren , mPort( port ) , mSocket( new QTcpSocket() ) { - connect( mSocket, &QAbstractSocket::stateChanged, this, &TcpReceiver::setSocketState ); connect( mSocket, qOverload( &QAbstractSocket::errorOccurred ), this, &TcpReceiver::handleError ); + connect( mSocket, &QTcpSocket::stateChanged, this, [=]( QAbstractSocket::SocketState state ) { + setSocketState( state ); + } ); connect( mSocket, &QAbstractSocket::connected, this, [=] { // This line enables gpsd's NMEA through TCP @@ -41,8 +43,6 @@ TcpReceiver::TcpReceiver( const QString &address, const int port, QObject *paren TcpReceiver::~TcpReceiver() { - disconnect( mSocket, &QAbstractSocket::stateChanged, this, &TcpReceiver::setSocketState ); - mSocket->deleteLater(); mSocket = nullptr; } @@ -63,15 +63,13 @@ void TcpReceiver::handleDisconnectDevice() mSocket->disconnectFromHost(); } -void TcpReceiver::setSocketState( const QAbstractSocket::SocketState socketState ) -{ - emit socketStateChanged( socketState ); - emit socketStateStringChanged( socketStateString() ); -} - -QAbstractSocket::SocketState TcpReceiver::socketState() +QAbstractSocket::SocketState TcpReceiver::socketState() const { - return mSocket ? mSocket->state() : QAbstractSocket::UnconnectedState; + if ( mSocket == nullptr ) + { + return QAbstractSocket::UnconnectedState; + } + return mSocket->state(); } QString TcpReceiver::socketStateString() @@ -86,13 +84,13 @@ QString TcpReceiver::socketStateString() return tr( "Successfully connected" ); case QAbstractSocket::UnconnectedState: { - QString mSocketStateString = tr( "Disconnected" ); + QString socketStateString = tr( "Disconnected" ); if ( mReconnectOnDisconnect ) { - mSocketStateString.append( QStringLiteral( ": %1" ).arg( mSocket->errorString() ) ); + socketStateString.append( QStringLiteral( ": %1" ).arg( mSocket->errorString() ) ); mReconnectTimer.start( 2000 ); } - return mSocketStateString; + return socketStateString; } default: return tr( "Socket state %1" ).arg( static_cast( currentState ) ); @@ -116,9 +114,6 @@ void TcpReceiver::handleError( QAbstractSocket::SocketError error ) mLastError = tr( "TCP receiver error (%1)" ).arg( QMetaEnum::fromType().valueToKey( error ) ); break; } - - setSocketState( QAbstractSocket::UnconnectedState ); - qInfo() << QStringLiteral( "TcpReceiver: Error: %1" ).arg( mLastError ); emit lastErrorChanged( mLastError ); diff --git a/src/core/positioning/tcpreceiver.h b/src/core/positioning/tcpreceiver.h index d79ed6d012..4ed7c36843 100644 --- a/src/core/positioning/tcpreceiver.h +++ b/src/core/positioning/tcpreceiver.h @@ -33,14 +33,11 @@ class TcpReceiver : public NmeaGnssReceiver public: explicit TcpReceiver( const QString &address = QString(), const int port = 0, QObject *parent = nullptr ); ~TcpReceiver() override; + QAbstractSocket::SocketState socketState() const override; public slots: - QAbstractSocket::SocketState socketState() override; QString socketStateString() override; - private slots: - void setSocketState( QAbstractSocket::SocketState socketState ); - private: void handleConnectDevice() override; void handleDisconnectDevice() override; diff --git a/src/core/positioning/udpreceiver.cpp b/src/core/positioning/udpreceiver.cpp index 7584c9f3a2..f72223a8db 100644 --- a/src/core/positioning/udpreceiver.cpp +++ b/src/core/positioning/udpreceiver.cpp @@ -35,8 +35,10 @@ UdpReceiver::UdpReceiver( const QString &address, const int port, QObject *paren mSocket->setSocketDescriptor( sockfd, QUdpSocket::UnconnectedState ); #endif - connect( mSocket, &QAbstractSocket::stateChanged, this, &UdpReceiver::setSocketState ); connect( mSocket, qOverload( &QAbstractSocket::errorOccurred ), this, &UdpReceiver::handleError ); + connect( mSocket, &QUdpSocket::stateChanged, this, [=]( QAbstractSocket::SocketState state ) { + setSocketState( state ); + } ); connect( mSocket, &QUdpSocket::readyRead, this, [=]() { QByteArray datagram; @@ -63,8 +65,6 @@ UdpReceiver::UdpReceiver( const QString &address, const int port, QObject *paren UdpReceiver::~UdpReceiver() { - disconnect( mSocket, &QAbstractSocket::stateChanged, this, &UdpReceiver::setSocketState ); - mSocket->deleteLater(); mSocket = nullptr; mBuffer->deleteLater(); @@ -90,15 +90,13 @@ void UdpReceiver::handleDisconnectDevice() mSocket->close(); } -void UdpReceiver::setSocketState( const QAbstractSocket::SocketState socketState ) -{ - emit socketStateChanged( socketState ); - emit socketStateStringChanged( socketStateString() ); -} - -QAbstractSocket::SocketState UdpReceiver::socketState() +QAbstractSocket::SocketState UdpReceiver::socketState() const { - return mSocket ? mSocket->state() : QAbstractSocket::UnconnectedState; + if ( mSocket == nullptr ) + { + return QAbstractSocket::UnconnectedState; + } + return mSocket->state(); } QString UdpReceiver::socketStateString() @@ -114,13 +112,13 @@ QString UdpReceiver::socketStateString() return tr( "Successfully connected" ); case QAbstractSocket::UnconnectedState: { - QString mSocketStateString = tr( "Disconnected" ); + QString socketStateString = tr( "Disconnected" ); if ( mReconnectOnDisconnect ) { - mSocketStateString.append( QStringLiteral( ": %1" ).arg( mSocket->errorString() ) ); + socketStateString.append( QStringLiteral( ": %1" ).arg( mSocket->errorString() ) ); mReconnectTimer.start( 2000 ); } - return mSocketStateString; + return socketStateString; } default: return tr( "Socket state %1" ).arg( static_cast( currentState ) ); @@ -144,9 +142,6 @@ void UdpReceiver::handleError( QAbstractSocket::SocketError error ) mLastError = tr( "UDP receiver error (%1)" ).arg( QMetaEnum::fromType().valueToKey( error ) ); break; } - - setSocketState( QAbstractSocket::UnconnectedState ); - qInfo() << QStringLiteral( "UdpReceiver: Error: %1" ).arg( mLastError ); emit lastErrorChanged( mLastError ); diff --git a/src/core/positioning/udpreceiver.h b/src/core/positioning/udpreceiver.h index a24e3c432d..128c4e7c29 100644 --- a/src/core/positioning/udpreceiver.h +++ b/src/core/positioning/udpreceiver.h @@ -34,15 +34,11 @@ class UdpReceiver : public NmeaGnssReceiver public: explicit UdpReceiver( const QString &address = QString(), const int port = 0, QObject *parent = nullptr ); ~UdpReceiver() override; + QAbstractSocket::SocketState socketState() const override; public slots: - QAbstractSocket::SocketState socketState() override; QString socketStateString() override; - private slots: - - void setSocketState( QAbstractSocket::SocketState socketState ); - private: void handleConnectDevice() override; void handleDisconnectDevice() override;