QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgssensormanager.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssensormanager.cpp
3 ------------------
4 Date : March 2023
5 Copyright : (C) 2023 Mathieu Pellerin
6 Email : mathieu at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgssensormanager.h"
17
18#include "qgsapplication.h"
19#include "qgssensorregistry.h"
20
21#include <QString>
22
23#include "moc_qgssensormanager.cpp"
24
25using namespace Qt::StringLiterals;
26
28 : QObject( parent )
29{
30}
31
36
38{
39 const QList<QgsAbstractSensor *> sensors = mSensors;
41 {
42 if ( sensor )
43 {
44 removeSensor( sensor->id() );
45 }
46 }
47 mSensors.clear();
48 mSensorsData.clear();
49}
50
51QList<QgsAbstractSensor *> QgsSensorManager::sensors() const
52{
53 return mSensors;
54}
55
57{
58 for ( QgsAbstractSensor *sensor : mSensors )
59 {
60 if ( sensor->id() == id )
61 {
62 return sensor;
63 }
64 }
65
66 return nullptr;
67}
68
70{
71 return mSensorsData.value( name );
72}
73
74QMap<QString, QgsAbstractSensor::SensorData> QgsSensorManager::sensorsData() const
75{
76 return mSensorsData;
77}
78
80{
81 QStringList names;
82 for ( const QgsAbstractSensor *sensor : std::as_const( mSensors ) )
83 {
84 names << sensor->name();
85 }
86 return names;
87}
88
90{
91 if ( !sensor || mSensors.contains( sensor ) )
92 return;
93
94 connect( sensor, &QgsAbstractSensor::nameChanged, this, &QgsSensorManager::handleSensorNameChanged );
95 connect( sensor, &QgsAbstractSensor::statusChanged, this, &QgsSensorManager::handleSensorStatusChanged );
96 connect( sensor, &QgsAbstractSensor::dataChanged, this, &QgsSensorManager::captureSensorData );
97 connect( sensor, &QgsAbstractSensor::errorOccurred, this, &QgsSensorManager::handleSensorErrorOccurred );
98 mSensors << sensor;
99
100 emit sensorAdded( sensor->id() );
101
102 return;
103}
104
105bool QgsSensorManager::removeSensor( const QString &id )
106{
107 for ( QgsAbstractSensor *sensor : mSensors )
108 {
109 if ( sensor->id() == id )
110 {
111 emit sensorAboutToBeRemoved( id );
112 mSensors.removeAll( sensor );
113 mSensorsData.remove( sensor->name() );
114 sensor->disconnectSensor();
115 sensor->deleteLater();
116 emit sensorRemoved( id );
117 return true;
118 }
119 }
120
121 return false;
122}
123
124void QgsSensorManager::handleSensorNameChanged()
125{
126 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
127 if ( !sensor )
128 return;
129
130 emit sensorNameChanged( sensor->id() );
131}
132
133void QgsSensorManager::handleSensorStatusChanged()
134{
135 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
136 if ( !sensor )
137 return;
138
140 {
141 mSensorsData.remove( sensor->name() );
142 }
143
144 emit sensorStatusChanged( sensor->id() );
145}
146
147void QgsSensorManager::captureSensorData()
148{
149 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
150 if ( !sensor )
151 return;
152
153 mSensorsData.insert( sensor->name(), sensor->data() );
154 emit sensorDataCaptured( sensor->id() );
155}
156
157void QgsSensorManager::handleSensorErrorOccurred( const QString & )
158{
159 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
160 if ( !sensor )
161 return;
162
163 emit sensorErrorOccurred( sensor->id() );
164}
165
166bool QgsSensorManager::readXml( const QDomElement &element, const QDomDocument &document )
167{
168 clear();
169
170 QDomElement sensorsElem = element;
171 if ( element.tagName() != "Sensors"_L1 )
172 {
173 sensorsElem = element.firstChildElement( u"Sensors"_s );
174 }
175
176 QDomNodeList sensorNodes = element.elementsByTagName( u"Sensor"_s );
177 for ( int i = 0; i < sensorNodes.size(); ++i )
178 {
179 const QDomElement sensorElement = sensorNodes.at( i ).toElement();
180 const QString sensorType = sensorElement.attribute( u"type"_s );
182 if ( !sensor )
183 {
184 continue;
185 }
186
187 sensor->readXml( sensorElement, document );
188 addSensor( sensor );
189 }
190
191 return true;
192}
193
194QDomElement QgsSensorManager::writeXml( QDomDocument &document ) const
195{
196 QDomElement sensorsElem = document.createElement( u"Sensors"_s );
197
198 for ( const QgsAbstractSensor *sensor : mSensors )
199 {
200 sensor->writeXml( sensorsElem, document );
201 }
202
203 return sensorsElem;
204}
@ Disconnected
Device is disconnected.
Definition qgis.h:1933
An abstract base class for sensors.
QString id() const
Returns the sensor ID.
void statusChanged()
Emitted when the sensor status has changed.
void errorOccurred(const QString &errorString)
Emitted when an error has occurred. The errorString describes the error.
void nameChanged()
Emitted when the sensor name has changed.
void dataChanged()
Emitted when the captured sensor data has changed.
static QgsSensorRegistry * sensorRegistry()
Returns the application's sensor registry, used for sensor types.
void addSensor(QgsAbstractSensor *sensor)
Registers a new sensor.
void sensorRemoved(const QString &id)
Emitted when a sensor has been removed.
bool removeSensor(const QString &id)
Removes a registered sensor matching a given id.
void sensorErrorOccurred(const QString &id)
Emitted when a sensor error has occurred.
QgsSensorManager(QObject *parent=nullptr)
Constructor for QgsSensorManager, with the specified parent object.
QMap< QString, QgsAbstractSensor::SensorData > sensorsData() const
Returns the last captured data of all registered sensors.
void sensorNameChanged(const QString &id)
Emitted when a sensor name has changed.
QList< QgsAbstractSensor * > sensors() const
Returns a list of pointers to all registered sensors.
void sensorDataCaptured(const QString &id)
Emitted when newly captured data from a sensor has occurred.
QDomElement writeXml(QDomDocument &document) const
Returns a DOM element representing the state of the manager.
QgsAbstractSensor * sensor(const QString &id) const
Returns a registered sensor pointer matching a given id.
QStringList sensorNames() const
Returns a list of registered sensor names.
QgsAbstractSensor::SensorData sensorData(const QString &name) const
Returns the last captured data from a registered sensor matching a given name.
void clear()
Deregisters and removes all sensors from the manager.
void sensorAdded(const QString &id)
Emitted when a sensor has been registered.
~QgsSensorManager() override
void sensorAboutToBeRemoved(const QString &id)
Emitted when a sensor is about to be removed.
void sensorStatusChanged(const QString &id)
Emitted when a sensor status has changed.
bool readXml(const QDomElement &element, const QDomDocument &document)
Reads the manager's state from a DOM element, restoring all sensors present in the XML document.
QgsAbstractSensor * createSensor(const QString &type, QObject *parent=nullptr) const
Creates a new instance of a sensor given the type.
Contains details of a sensor data capture.