QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
43 Q_OBJECT
44
45 public:
46
50 explicit QgsIODeviceSensor( QObject *parent = nullptr ) : QgsAbstractSensor( parent ) {}
51 ~QgsIODeviceSensor() override;
52
56 QIODevice *iODevice() const;
57
58 protected:
59
64 void initIODevice( QIODevice *device SIP_TRANSFER );
65
66 protected slots:
67
71 virtual void parseData();
72
73 private:
74
75 std::unique_ptr<QIODevice> mIODevice;
76
77};
78
85class CORE_EXPORT QgsTcpSocketSensor : public QgsIODeviceSensor
86{
87
88 Q_OBJECT
89
90 public:
91
95 explicit QgsTcpSocketSensor( QObject *parent = nullptr );
96 ~QgsTcpSocketSensor() override = default;
97
103 static QgsTcpSocketSensor *create( QObject *parent ) SIP_FACTORY;
104
105 QString type() const override;
106
110 QString hostName() const;
111
116 void setHostName( const QString &hostName );
117
121 int port() const;
122
126 void setPort( int port );
127
128 bool writePropertiesToElement( QDomElement &element, QDomDocument &document ) const override;
129 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document ) override;
130
131 protected:
132
133 void handleConnect() override;
134 void handleDisconnect() override;
135
136 private slots:
137
138 void socketStateChanged( const QAbstractSocket::SocketState socketState );
139 void handleError( QAbstractSocket::SocketError error );
140
141 private:
142
143 QTcpSocket *mTcpSocket = nullptr;
144
145 QString mHostName;
146 int mPort = 0;
147
148};
149
156class CORE_EXPORT QgsUdpSocketSensor : public QgsIODeviceSensor
157{
158
159 Q_OBJECT
160
161 public:
162
166 explicit QgsUdpSocketSensor( QObject *parent = nullptr );
167 ~QgsUdpSocketSensor() override = default;
168
174 static QgsUdpSocketSensor *create( QObject *parent ) SIP_FACTORY;
175
176 QString type() const override;
177
181 QString hostName() const;
182
187 void setHostName( const QString &hostName );
188
192 int port() const;
193
197 void setPort( int port );
198
199 bool writePropertiesToElement( QDomElement &element, QDomDocument &document ) const override;
200 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document ) override;
201
202 protected:
203
204 void handleConnect() override;
205 void handleDisconnect() override;
206
207 private slots:
208
209 void socketStateChanged( const QAbstractSocket::SocketState socketState );
210 void handleError( QAbstractSocket::SocketError error );
211
212 private:
213
214 std::unique_ptr<QUdpSocket> mUdpSocket;
215 QBuffer *mBuffer = nullptr;
216
217 QString mHostName;
218 int mPort = 0;
219
220};
221
222#if defined( HAVE_QTSERIALPORT )
223SIP_IF_FEATURE( HAVE_QTSERIALPORT )
224
225
231class CORE_EXPORT QgsSerialPortSensor : public QgsIODeviceSensor
232{
233
234 Q_OBJECT
235
236 public:
237
241 explicit QgsSerialPortSensor( QObject *parent = nullptr );
242 ~QgsSerialPortSensor() override = default;
243
249 static QgsSerialPortSensor *create( QObject *parent ) SIP_FACTORY;
250
251 QString type() const override;
252
256 QString portName() const;
257
262 void setPortName( const QString &portName );
263
268 QSerialPort::BaudRate baudRate() const;
269
275 void setBaudRate( const QSerialPort::BaudRate &baudRate );
276
282 QByteArray delimiter() const;
283
291 void setDelimiter( const QByteArray &delimiter );
292
293 bool writePropertiesToElement( QDomElement &element, QDomDocument &document ) const override;
294 bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document ) override;
295
296 protected:
297
298 void handleConnect() override;
299 void handleDisconnect() override;
300
301 protected slots:
302
303 void parseData() override;
304
305 private slots:
306
307 void handleError( QSerialPort::SerialPortError error );
308
309 private:
310
311 QSerialPort *mSerialPort = nullptr;
312
313 QString mPortName;
314 QSerialPort::BaudRate mBaudRate = QSerialPort::Baud9600;
315 QByteArray mDelimiter;
316 bool mFirstDelimiterHit = false;
317 QByteArray mDataBuffer;
318
319};
321#endif
322
323#endif //QGSIODEVICESENSOR_H
324
325
326
An abstract base class for sensor classes.
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 QIODevice-based sensor classes.
QgsIODeviceSensor(QObject *parent=nullptr)
Constructor for a abstract QIODevice-based sensor, bound to the specified parent.
A TCP socket sensor class.
~QgsTcpSocketSensor() override=default
A UDP socket sensor class.
~QgsUdpSocketSensor() override=default
#define SIP_IF_FEATURE(feature)
Definition: qgis_sip.h:181
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:208