22#if defined( Q_OS_ANDROID ) || defined( Q_OS_LINUX )
23#include <sys/socket.h>
32 mIODevice.reset( device );
42 return mIODevice.get();
57 , mTcpSocket( new QTcpSocket() )
59 connect( mTcpSocket, &QAbstractSocket::stateChanged,
this, &QgsTcpSocketSensor::socketStateChanged );
61#if QT_VERSION < QT_VERSION_CHECK( 5, 15, 0 )
62 connect( mTcpSocket, qOverload<QAbstractSocket::SocketError>( &QAbstractSocket::error ),
this, &QgsTcpSocketSensor::handleError );
64 connect( mTcpSocket, qOverload<QAbstractSocket::SocketError>( &QAbstractSocket::errorOccurred ),
this, &QgsTcpSocketSensor::handleError );
77 return QLatin1String(
"tcp_socket" );
108 if ( mHostName.isEmpty() || mPort == 0 )
114 mTcpSocket->connectToHost( mHostName, mPort, QTcpSocket::ReadOnly );
122void QgsTcpSocketSensor::handleError( QAbstractSocket::SocketError error )
126 case QAbstractSocket::HostNotFoundError:
129 case QAbstractSocket::NetworkError:
130 mErrorString = tr(
"Attempt to read or write from socket returned an error" );
132 case QAbstractSocket::ConnectionRefusedError:
133 mErrorString = tr(
"The connection was refused by the remote host" );
136 mErrorString = tr(
"%1" ).arg( QMetaEnum::fromType<QAbstractSocket::SocketError>().valueToKey( error ) );
143void QgsTcpSocketSensor::socketStateChanged(
const QAbstractSocket::SocketState socketState )
145 switch ( socketState )
147 case QAbstractSocket::ConnectedState:
152 case QAbstractSocket::UnconnectedState:
164 element.setAttribute( QStringLiteral(
"hostName" ), mHostName );
165 element.setAttribute( QStringLiteral(
"port" ), QString::number( mPort ) );
172 mHostName = element.attribute( QStringLiteral(
"hostName" ) );
173 mPort = element.attribute( QStringLiteral(
"port" ) ).toInt();
182 , mUdpSocket( std::make_unique<QUdpSocket>() )
183 , mBuffer( new QBuffer() )
185#if defined( Q_OS_ANDROID ) || defined( Q_OS_LINUX )
186 int sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
188 setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR,
189 (
void * ) &optval,
sizeof( optval ) );
190 mUdpSocket->setSocketDescriptor( sockfd, QUdpSocket::UnconnectedState );
193 connect( mUdpSocket.get(), &QAbstractSocket::stateChanged,
this, &QgsUdpSocketSensor::socketStateChanged );
194 connect( mUdpSocket.get(), &QUdpSocket::readyRead,
this, [ = ]()
197 while ( mUdpSocket->hasPendingDatagrams() )
199 datagram.resize( int( mUdpSocket->pendingDatagramSize() ) );
200 mUdpSocket->readDatagram( datagram.data(), datagram.size() );
202 mBuffer->buffer().clear();
204 mBuffer->write( datagram );
209#if QT_VERSION < QT_VERSION_CHECK( 5, 15, 0 )
210 connect( mUdpSocket.get(), qOverload<QAbstractSocket::SocketError>( &QAbstractSocket::error ),
this, &QgsUdpSocketSensor::handleError );
212 connect( mUdpSocket.get(), qOverload<QAbstractSocket::SocketError>( &QAbstractSocket::errorOccurred ),
this, &QgsUdpSocketSensor::handleError );
215 initIODevice( mBuffer );
225 return QLatin1String(
"udp_socket" );
256 if ( mHostName.isEmpty() || mPort == 0 )
262 mBuffer->open( QIODevice::ReadWrite );
263 mUdpSocket->bind( QHostAddress( mHostName ), mPort, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint );
264 mUdpSocket->joinMulticastGroup( QHostAddress( mHostName ) );
273void QgsUdpSocketSensor::handleError( QAbstractSocket::SocketError error )
277 case QAbstractSocket::HostNotFoundError:
280 case QAbstractSocket::NetworkError:
281 mErrorString = tr(
"Attempt to read or write from socket returned an error" );
283 case QAbstractSocket::ConnectionRefusedError:
284 mErrorString = tr(
"The connection was refused by the remote host" );
287 mErrorString = tr(
"%1" ).arg( QMetaEnum::fromType<QAbstractSocket::SocketError>().valueToKey( error ) );
294void QgsUdpSocketSensor::socketStateChanged(
const QAbstractSocket::SocketState socketState )
296 switch ( socketState )
298 case QAbstractSocket::ConnectedState:
299 case QAbstractSocket::BoundState:
304 case QAbstractSocket::UnconnectedState:
316 element.setAttribute( QStringLiteral(
"hostName" ), mHostName );
317 element.setAttribute( QStringLiteral(
"port" ), QString::number( mPort ) );
324 mHostName = element.attribute( QStringLiteral(
"hostName" ) );
325 mPort = element.attribute( QStringLiteral(
"port" ) ).toInt();
332#if defined( HAVE_QTSERIALPORT )
333QgsSerialPortSensor::QgsSerialPortSensor( QObject *parent )
335 , mSerialPort( new QSerialPort() )
337 connect( mSerialPort, qOverload<QSerialPort::SerialPortError>( &QSerialPort::errorOccurred ),
this, &QgsSerialPortSensor::handleError );
342QgsSerialPortSensor *QgsSerialPortSensor::create( QObject *parent )
344 return new QgsSerialPortSensor( parent );
347QString QgsSerialPortSensor::type()
const
349 return QLatin1String(
"serial_port" );
352QString QgsSerialPortSensor::portName()
const
357void QgsSerialPortSensor::setPortName(
const QString &portName )
359 if ( mPortName == portName )
362 mPortName = portName;
365void QgsSerialPortSensor::handleConnect()
367 mSerialPort->setPortName( mPortName );
368 mSerialPort->setBaudRate( QSerialPort::Baud9600 );
369 if ( mSerialPort->open( QIODevice::ReadOnly ) )
379void QgsSerialPortSensor::handleDisconnect()
381 mSerialPort->close();
384void QgsSerialPortSensor::handleError( QSerialPort::SerialPortError error )
386 if ( error == QSerialPort::NoError )
393 case QSerialPort::DeviceNotFoundError:
394 mErrorString = tr(
"Could not find the serial port device" );
396 case QSerialPort::ReadError:
397 mErrorString = tr(
"Attempt to read from the serial port returned an error" );
399 case QSerialPort::PermissionError:
400 mErrorString = tr(
"The connection was refused due to not having enough permission" );
403 mErrorString = tr(
"%1" ).arg( QMetaEnum::fromType<QSerialPort::SerialPortError>().valueToKey( error ) );
407 emit errorOccurred( mErrorString );
410bool QgsSerialPortSensor::writePropertiesToElement( QDomElement &element, QDomDocument & )
const
412 element.setAttribute( QStringLiteral(
"portName" ), mPortName );
417bool QgsSerialPortSensor::readPropertiesFromElement(
const QDomElement &element,
const QDomDocument & )
419 mPortName = element.attribute( QStringLiteral(
"portName" ) );
@ Connected
Device is successfully connected.
@ Disconnected
Device is disconnected.
QgsAbstractSensor::SensorData data() const
Returns the latest captured data from the sensor.
void setStatus(Qgis::DeviceConnectionStatus status)
Sets the current sensor status.
void errorOccurred(const QString &errorString)
Emitted when an error has occurred. The errorString describes the error.
void setData(const QgsAbstractSensor::SensorData &data)
Sets the latest captured data from the sensor.
An abstract class QIODevice-based sensor classes.
void initIODevice(QIODevice *device)
Initiates the I/O device.
virtual void parseData()
Parses the data read from the device when available.
QIODevice * iODevice() const
Returns the I/O device.
~QgsIODeviceSensor() override
A TCP socket sensor class.
QgsTcpSocketSensor(QObject *parent=nullptr)
Constructor for a TCP socket sensor, bound to the specified parent.
void setHostName(const QString &hostName)
Sets the host name the socket connects to.
int port() const
Returns the port the socket connects to.
void handleConnect() override
Handles the connection to the sensor.
QString type() const override
Returns the sensor type.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document) const override
Write specific sensor type properties into a DOM element.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document) override
Restores specific sensor type properties from a DOM element.
void handleDisconnect() override
Handles the disconnection from the sensor.
static QgsTcpSocketSensor * create(QObject *parent)
Returns a new TCP socket sensor.
void setPort(int port)
Sets the port the socket connects to.
QString hostName() const
Returns the host name the socket connects to.
A UDP socket sensor class.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document) override
Restores specific sensor type properties from a DOM element.
static QgsUdpSocketSensor * create(QObject *parent)
Returns a new UDP socket sensor.
QString type() const override
Returns the sensor type.
void handleDisconnect() override
Handles the disconnection from the sensor.
QgsUdpSocketSensor(QObject *parent=nullptr)
Constructor for a UDP socket sensor, bound to the specified parent.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document) const override
Write specific sensor type properties into a DOM element.
void setHostName(const QString &hostName)
Sets the host name the socket connects to.
int port() const
Returns the port the socket connects to.
void handleConnect() override
Handles the connection to the sensor.
void setPort(int port)
Sets the port the socket connects to.
QString hostName() const
Returns the host name the socket connects to.
Contains details of a sensor data capture.
QVariant lastValue
Last captured sensor value stored as a QVariant.
QDateTime lastTimestamp
Timestamp of last captured sensor value.