QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 "moc_qgssensormanager.cpp"
22
24 : QObject( parent )
25{
26}
27
32
34{
35 const QList<QgsAbstractSensor *> sensors = mSensors;
37 {
38 if ( sensor )
39 {
40 removeSensor( sensor->id() );
41 }
42 }
43 mSensors.clear();
44 mSensorsData.clear();
45}
46
47QList<QgsAbstractSensor *> QgsSensorManager::sensors() const
48{
49 return mSensors;
50}
51
53{
54 for ( QgsAbstractSensor *sensor : mSensors )
55 {
56 if ( sensor->id() == id )
57 {
58 return sensor;
59 }
60 }
61
62 return nullptr;
63}
64
66{
67 return mSensorsData.value( name );
68}
69
70QMap<QString, QgsAbstractSensor::SensorData> QgsSensorManager::sensorsData() const
71{
72 return mSensorsData;
73}
74
76{
77 QStringList names;
78 for ( const QgsAbstractSensor *sensor : std::as_const( mSensors ) )
79 {
80 names << sensor->name();
81 }
82 return names;
83}
84
86{
87 if ( !sensor || mSensors.contains( sensor ) )
88 return;
89
90 connect( sensor, &QgsAbstractSensor::nameChanged, this, &QgsSensorManager::handleSensorNameChanged );
91 connect( sensor, &QgsAbstractSensor::statusChanged, this, &QgsSensorManager::handleSensorStatusChanged );
92 connect( sensor, &QgsAbstractSensor::dataChanged, this, &QgsSensorManager::captureSensorData );
93 connect( sensor, &QgsAbstractSensor::errorOccurred, this, &QgsSensorManager::handleSensorErrorOccurred );
94 mSensors << sensor;
95
96 emit sensorAdded( sensor->id() );
97
98 return;
99}
100
101bool QgsSensorManager::removeSensor( const QString &id )
102{
103 for ( QgsAbstractSensor *sensor : mSensors )
104 {
105 if ( sensor->id() == id )
106 {
107 emit sensorAboutToBeRemoved( id );
108 mSensors.removeAll( sensor );
109 mSensorsData.remove( sensor->name() );
110 sensor->disconnectSensor();
111 sensor->deleteLater();
112 emit sensorRemoved( id );
113 return true;
114 }
115 }
116
117 return false;
118}
119
120void QgsSensorManager::handleSensorNameChanged()
121{
122 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
123 if ( !sensor )
124 return;
125
126 emit sensorNameChanged( sensor->id() );
127}
128
129void QgsSensorManager::handleSensorStatusChanged()
130{
131 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
132 if ( !sensor )
133 return;
134
136 {
137 mSensorsData.remove( sensor->name() );
138 }
139
140 emit sensorStatusChanged( sensor->id() );
141}
142
143void QgsSensorManager::captureSensorData()
144{
145 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
146 if ( !sensor )
147 return;
148
149 mSensorsData.insert( sensor->name(), sensor->data() );
150 emit sensorDataCaptured( sensor->id() );
151}
152
153void QgsSensorManager::handleSensorErrorOccurred( const QString & )
154{
155 const QgsAbstractSensor *sensor = qobject_cast<QgsAbstractSensor *>( sender() );
156 if ( !sensor )
157 return;
158
159 emit sensorErrorOccurred( sensor->id() );
160}
161
162bool QgsSensorManager::readXml( const QDomElement &element, const QDomDocument &document )
163{
164 clear();
165
166 QDomElement sensorsElem = element;
167 if ( element.tagName() != QLatin1String( "Sensors" ) )
168 {
169 sensorsElem = element.firstChildElement( QStringLiteral( "Sensors" ) );
170 }
171
172 QDomNodeList sensorNodes = element.elementsByTagName( QStringLiteral( "Sensor" ) );
173 for ( int i = 0; i < sensorNodes.size(); ++i )
174 {
175 const QDomElement sensorElement = sensorNodes.at( i ).toElement();
176 const QString sensorType = sensorElement.attribute( QStringLiteral( "type" ) );
178 if ( !sensor )
179 {
180 continue;
181 }
182
183 sensor->readXml( sensorElement, document );
184 addSensor( sensor );
185 }
186
187 return true;
188}
189
190QDomElement QgsSensorManager::writeXml( QDomDocument &document ) const
191{
192 QDomElement sensorsElem = document.createElement( QStringLiteral( "Sensors" ) );
193
194 for ( const QgsAbstractSensor *sensor : mSensors )
195 {
196 sensor->writeXml( sensorsElem, document );
197 }
198
199 return sensorsElem;
200}
@ Disconnected
Device is disconnected.
Definition qgis.h:1875
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.