QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsiodevicesensor.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsiodevicesensor.h
3 ---------------------------
4 begin : March 2023
5 copyright : (C) 2023 by Mathieu Pellerin
6 email : mathieu at opengis dot ch
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#ifndef QGSIODEVICESENSOR_H
17#define QGSIODEVICESENSOR_H
18
19#include "qgsconfig.h"
20
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgsabstractsensor.h"
24
25#include <QBuffer>
26#include <QDomElement>
27#include <QTcpSocket>
28#include <QUdpSocket>
29
30#if defined( HAVE_QTSERIALPORT )
31#include <QSerialPort>
32#endif
33
40class CORE_EXPORT QgsIODeviceSensor : public QgsAbstractSensor
41{
42 Q_OBJECT
43
44 public:
48 explicit QgsIODeviceSensor( QObject *parent = nullptr )
49 : QgsAbstractSensor( parent )
50 {}
51 ~QgsIODeviceSensor() override;
52
56 QIODevice *iODevice() const;
57
58 protected:
63 void initIODevice( QIODevice *device SIP_TRANSFER );
64
65 protected slots:
66
70 virtual void parseData();
71
72 private:
73 std::unique_ptr<QIODevice> mIODevice;
74};
75
82class CORE_EXPORT QgsTcpSocketSensor : public QgsIODeviceSensor
83{
84 Q_OBJECT
85
86 public:
90 explicit QgsTcpSocketSensor( QObject *parent = nullptr );
91 ~QgsTcpSocketSensor() override = default;
92
98 static QgsTcpSocketSensor *create( QObject *parent ) SIP_FACTORY;
99
100 QString type() const override;
101
105 QString hostName() const;
106
111 void setHostName( const QString &hostName );
112
116 int port() const;
117
121 void setPort( int port );
122
123 bool writePropertiesToElement( QDomElement &element, QDomDocument &document ) const override;
124 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document ) override;
125
126 protected:
127 void handleConnect() override;
128 void handleDisconnect() override;
129
130 private slots:
131
132 void socketStateChanged( const QAbstractSocket::SocketState socketState );
133 void handleError( QAbstractSocket::SocketError error );
134
135 private:
136 QTcpSocket *mTcpSocket = nullptr;
137
138 QString mHostName;
139 int mPort = 0;
140};
141
148class CORE_EXPORT QgsUdpSocketSensor : public QgsIODeviceSensor
149{
150 Q_OBJECT
151
152 public:
156 explicit QgsUdpSocketSensor( QObject *parent = nullptr );
157 ~QgsUdpSocketSensor() override = default;
158
164 static QgsUdpSocketSensor *create( QObject *parent ) SIP_FACTORY;
165
166 QString type() const override;
167
171 QString hostName() const;
172
177 void setHostName( const QString &hostName );
178
182 int port() const;
183
187 void setPort( int port );
188
189 bool writePropertiesToElement( QDomElement &element, QDomDocument &document ) const override;
190 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document ) override;
191
192 protected:
193 void handleConnect() override;
194 void handleDisconnect() override;
195
196 private slots:
197
198 void socketStateChanged( const QAbstractSocket::SocketState socketState );
199 void handleError( QAbstractSocket::SocketError error );
200
201 private:
202 std::unique_ptr<QUdpSocket> mUdpSocket;
203 QBuffer *mBuffer = nullptr;
204
205 QString mHostName;
206 int mPort = 0;
207};
208
209#if defined( HAVE_QTSERIALPORT )
210SIP_IF_FEATURE( HAVE_QTSERIALPORT )
211
212
218class CORE_EXPORT QgsSerialPortSensor : public QgsIODeviceSensor
219{
220 Q_OBJECT
221
222 public:
226 explicit QgsSerialPortSensor( QObject *parent = nullptr );
227 ~QgsSerialPortSensor() override = default;
228
234 static QgsSerialPortSensor *create( QObject *parent ) SIP_FACTORY;
235
236 QString type() const override;
237
241 QString portName() const;
242
247 void setPortName( const QString &portName );
248
253 QSerialPort::BaudRate baudRate() const;
254
260 void setBaudRate( const QSerialPort::BaudRate &baudRate );
261
267 QByteArray delimiter() const;
268
276 void setDelimiter( const QByteArray &delimiter );
277
278 bool writePropertiesToElement( QDomElement &element, QDomDocument &document ) const override;
279 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document ) override;
280
281 protected:
282 void handleConnect() override;
283 void handleDisconnect() override;
284
285 protected slots:
286
287 void parseData() override;
288
289 private slots:
290
291 void handleError( QSerialPort::SerialPortError error );
292
293 private:
294 QSerialPort *mSerialPort = nullptr;
295
296 QString mPortName;
297 QSerialPort::BaudRate mBaudRate = QSerialPort::Baud9600;
298 QByteArray mDelimiter;
299 bool mFirstDelimiterHit = false;
300 QByteArray mDataBuffer;
301};
303#endif
304
305#endif //QGSIODEVICESENSOR_H
QgsAbstractSensor(QObject *parent=nullptr)
Constructor for an abstract sensor, bound to the specified parent.
virtual void handleDisconnect()=0
Handles the disconnection from the sensor.
virtual QString type() const
Returns the sensor type.
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document) const
Write specific sensor type properties into a DOM element.
virtual void handleConnect()=0
Handles the connection to the sensor.
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document)
Restores specific sensor type properties from a DOM element.
An abstract class for QIODevice-based sensors.
QgsIODeviceSensor(QObject *parent=nullptr)
Constructor for a abstract QIODevice-based sensor, bound to the specified parent.
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.
~QgsTcpSocketSensor() override=default
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.
~QgsUdpSocketSensor() override=default
static QgsUdpSocketSensor * create(QObject *parent)
Returns a new UDP socket sensor.
QgsUdpSocketSensor(QObject *parent=nullptr)
Constructor for a UDP 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 setPort(int port)
Sets the port the socket connects to.
QString hostName() const
Returns the host name the socket connects to.
#define SIP_IF_FEATURE(feature)
Definition qgis_sip.h:188
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_END
Definition qgis_sip.h:215