Skip to content

Commit

Permalink
Address review (part1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenD98 committed Oct 4, 2024
1 parent fb0767b commit 4ba9e46
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 139 deletions.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/core/positioning/abstractgnssreceiver.cpp
Original file line number Diff line number Diff line change
@@ -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<int>( socketState() ) );
}
}

void AbstractGnssReceiver::setSocketState( const QAbstractSocket::SocketState &state )
{
if ( mSocketState == state )
return;
mSocketState = state;
emit socketStateChanged( mSocketState );
emit socketStateStringChanged( socketStateString() );
}
33 changes: 11 additions & 22 deletions src/core/positioning/abstractgnssreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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; }
Expand All @@ -57,33 +58,20 @@ class AbstractGnssReceiver : public QObject

Q_INVOKABLE virtual AbstractGnssReceiver::Capabilities capabilities() const { return NoCapabilities; }

virtual QList<QPair<QString, QVariant>> details() { return {}; }
virtual QList<QPair<QString, QVariant>> 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<int>( 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;
Expand All @@ -102,6 +90,7 @@ class AbstractGnssReceiver : public QObject

bool mValid = false;
GnssPositionInformation mLastGnssPositionInformation;
QAbstractSocket::SocketState mSocketState = QAbstractSocket::UnconnectedState;
QString mLastError;
};

Expand Down
26 changes: 12 additions & 14 deletions src/core/positioning/bluetoothreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ BluetoothReceiver::BluetoothReceiver( const QString &address, QObject *parent )
, mLocalDevice( std::make_unique<QBluetoothLocalDevice>() )
, mSocket( new QBluetoothSocket( QBluetoothServiceInfo::RfcommProtocol ) )
{
connect( mSocket, &QBluetoothSocket::stateChanged, this, &BluetoothReceiver::setSocketState );
connect( mSocket, qOverload<QBluetoothSocket::SocketError>( &QBluetoothSocket::errorOccurred ), this, &BluetoothReceiver::handleError );
connect( mSocket, &QBluetoothSocket::stateChanged, this, [=]( QBluetoothSocket::SocketState state ) {
setSocketState( static_cast<QAbstractSocket::SocketState>( state ) );
} );

connect( mLocalDevice.get(), &QBluetoothLocalDevice::pairingFinished, this, &BluetoothReceiver::pairingFinished );
connect( mLocalDevice.get(), &QBluetoothLocalDevice::errorOccurred, [=]( QBluetoothLocalDevice::Error error ) {
Expand Down Expand Up @@ -57,8 +59,6 @@ BluetoothReceiver::BluetoothReceiver( const QString &address, QObject *parent )

BluetoothReceiver::~BluetoothReceiver()
{
disconnect( mSocket, &QBluetoothSocket::stateChanged, this, &BluetoothReceiver::setSocketState );

mSocket->deleteLater();
mSocket = nullptr;
}
Expand Down Expand Up @@ -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<QAbstractSocket::SocketState>( socketState ) );
emit socketStateStringChanged( socketStateString() );
}

QAbstractSocket::SocketState BluetoothReceiver::socketState()
{
return mSocket ? static_cast<QAbstractSocket::SocketState>( mSocket->state() ) : QAbstractSocket::UnconnectedState;
if ( !mSocket )
{
return QAbstractSocket::UnconnectedState;
}
return static_cast<QAbstractSocket::SocketState>( mSocket->state() );
}

QString BluetoothReceiver::socketStateString()
Expand All @@ -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<int>( currentState ) );
Expand Down
4 changes: 1 addition & 3 deletions src/core/positioning/bluetoothreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down
26 changes: 13 additions & 13 deletions src/core/positioning/egenioussreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<QPair<QString, QVariant>> EgenioussReceiver::details()
QList<QPair<QString, QVariant>> EgenioussReceiver::details() const
{
QList<QPair<QString, QVariant>> detailsList;

Expand Down Expand Up @@ -130,8 +132,6 @@ void EgenioussReceiver::handleError( QAbstractSocket::SocketError error )
mLastError = tr( "TCP receiver error (%1)" ).arg( QMetaEnum::fromType<QAbstractSocket::SocketError>().valueToKey( error ) );
break;
}
setSocketState( QAbstractSocket::UnconnectedState );

qInfo() << QStringLiteral( "EgenioussReceiver: Error: %1" ).arg( mLastError );

emit lastErrorChanged( mLastError );
Expand Down
6 changes: 2 additions & 4 deletions src/core/positioning/egenioussreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ class EgenioussReceiver : public AbstractGnssReceiver
explicit EgenioussReceiver( QObject *parent = nullptr );
~EgenioussReceiver();

public slots:
QAbstractSocket::SocketState socketState() override;
QList<QPair<QString, QVariant>> details() const override;
QAbstractSocket::SocketState socketState() const override;

private:
void handleConnectDevice() override;
void handleDisconnectDevice() override;
QList<QPair<QString, QVariant>> details() override;

private slots:
void onReadyRead();
void handleError( QAbstractSocket::SocketError error );
void setSocketState( const QAbstractSocket::SocketState socketState );

private:
void processReceivedData();
Expand Down
8 changes: 1 addition & 7 deletions src/core/positioning/internalgnssreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ InternalGnssReceiver::InternalGnssReceiver( QObject *parent )
connect( mGeoPositionSource.get(), &QGeoPositionInfoSource::positionUpdated, this, &InternalGnssReceiver::handlePositionUpdated );
connect( mGeoPositionSource.get(), qOverload<QGeoPositionInfoSource::Error>( &QGeoPositionInfoSource::errorOccurred ), this, &InternalGnssReceiver::handleError );

mSocketState = QAbstractSocket::ConnectedState;
setSocketState( QAbstractSocket::ConnectedState );

setValid( true );
}
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/core/positioning/internalgnssreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -60,7 +57,6 @@ class InternalGnssReceiver : public AbstractGnssReceiver
QList<int> mSatellitesID;
QList<QgsSatelliteInfo> mSatellitesInfo;
bool mSatelliteInformationValid = true;
QAbstractSocket::SocketState mSocketState = QAbstractSocket::UnconnectedState;
};

#endif // INTERNALGNSSRECEIVER_H
2 changes: 1 addition & 1 deletion src/core/positioning/nmeagnssreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void NmeaGnssReceiver::handleStopLogging()
mLogFile.close();
}

QList<QPair<QString, QVariant>> NmeaGnssReceiver::details()
QList<QPair<QString, QVariant>> NmeaGnssReceiver::details() const
{
QList<QPair<QString, QVariant>> dataList;

Expand Down
2 changes: 1 addition & 1 deletion src/core/positioning/nmeagnssreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class NmeaGnssReceiver : public AbstractGnssReceiver
private:
void handleStartLogging() override;
void handleStopLogging() override;
QList<QPair<QString, QVariant>> details() override;
QList<QPair<QString, QVariant>> details() const override;

void processImuSentence( const QString &sentence );

Expand Down
33 changes: 12 additions & 21 deletions src/core/positioning/serialportreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::SerialPortError>( &QSerialPort::errorOccurred ), this, &SerialPortReceiver::handleError );
connect( mSerialPort, qOverload<QSerialPort::SerialPortError>( &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 );
}
}

Expand All @@ -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 );
}
}

Expand Down
Loading

0 comments on commit 4ba9e46

Please sign in to comment.