Libksysguard

Sensor.cpp
1 /*
2  Copyright (C) 2019 Vlad Zahorodnii <[email protected]>
3  Copyright (C) 2020 Arjen Hiemstra <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <QEvent>
22 
23 #include "Sensor.h"
24 #include "SensorDaemonInterface_p.h"
25 #include "SensorInfo_p.h"
26 #include "formatter/Formatter.h"
27 #include "SensorQuery.h"
28 
29 using namespace KSysGuard;
30 
31 class Q_DECL_HIDDEN Sensor::Private
32 {
33 public:
34  SensorInfo sensorInfo;
35 
37  QVariant value;
38 
39  bool usedByQml = false;
40  bool componentComplete = false;
41 
42  QString pendingId;
43  QString id;
44 
45  bool enabled = true;
46 };
47 
48 Sensor::Sensor(QObject *parent)
49  : Sensor(QString{}, parent)
50 {
51 }
52 
53 Sensor::Sensor(const QString &id, QObject *parent)
54  : QObject(parent)
55  , d(new Private())
56 {
57  connect(this, &Sensor::statusChanged, this, &Sensor::valueChanged);
58  connect(this, &Sensor::statusChanged, this, &Sensor::metaDataChanged);
59  connect(this, &Sensor::enabledChanged, this, &Sensor::onEnabledChanged);
60 
61  setSensorId(id);
62 }
63 
64 Sensor::Sensor(const SensorQuery &query, int index, QObject *parent)
65  : Sensor(QString{}, parent)
66 {
67  if (index >= 0 && index < query.result().size()) {
68  auto result = query.result().at(index);
69  d->id = result.first;
70  onMetaDataChanged(d->id, result.second);
71  onEnabledChanged();
72  }
73 }
74 
75 bool Sensor::event(QEvent *event)
76 {
77  if (event->type() == QEvent::ParentAboutToChange && parent()) {
78  parent()->disconnect(this);
79  } else if (event->type() == QEvent::ParentChange && parent()) {
80  if (parent()->metaObject()->indexOfSignal("enabledChanged()") != -1) {
81  connect(parent(), SIGNAL(enabledChanged()), this, SIGNAL(enabledChanged()));
82  }
83  }
84 
85  return QObject::event(event);
86 }
87 
88 Sensor::~Sensor()
89 {
90  SensorDaemonInterface::instance()->unsubscribe(d->id);
91 }
92 
93 QString Sensor::sensorId() const
94 {
95  return d->id;
96 }
97 
98 void Sensor::setSensorId(const QString &id)
99 {
100  if (id == d->id) {
101  return;
102  }
103 
104  if (d->usedByQml && !d->componentComplete) {
105  d->pendingId = id;
106  return;
107  }
108 
109  d->id = id;
110  d->status = Sensor::Status::Loading;
111 
112  if (!id.isEmpty()) {
113  SensorDaemonInterface::instance()->requestMetaData(id);
114  connect(SensorDaemonInterface::instance(), &SensorDaemonInterface::metaDataChanged, this, &Sensor::onMetaDataChanged, Qt::UniqueConnection);
115  }
116 
117  if (enabled()) {
118  SensorDaemonInterface::instance()->subscribe(id);
119  SensorDaemonInterface::instance()->requestValue(id);
120  connect(SensorDaemonInterface::instance(), &SensorDaemonInterface::valueChanged, this, &Sensor::onValueChanged, Qt::UniqueConnection);
121  }
122 
123  Q_EMIT sensorIdChanged();
124  Q_EMIT statusChanged();
125 }
126 
127 Sensor::Status Sensor::status() const
128 {
129  return d->status;
130 }
131 
132 QString Sensor::name() const
133 {
134  return d->sensorInfo.name;
135 }
136 
137 QString Sensor::shortName() const
138 {
139  if (d->sensorInfo.shortName.isEmpty()) {
140  return d->sensorInfo.name;
141  }
142 
143  return d->sensorInfo.shortName;
144 }
145 
146 QString Sensor::description() const
147 {
148  return d->sensorInfo.description;
149 }
150 
151 Unit Sensor::unit() const
152 {
153  return d->sensorInfo.unit;
154 }
155 
156 qreal Sensor::minimum() const
157 {
158  return d->sensorInfo.min;
159 }
160 
161 qreal Sensor::maximum() const
162 {
163  return d->sensorInfo.max;
164 }
165 
166 QVariant::Type Sensor::type() const
167 {
168  return d->sensorInfo.variantType;
169 }
170 
171 QVariant Sensor::value() const
172 {
173  if (!d->value.isValid()) {
174  return QVariant{d->sensorInfo.variantType};
175  }
176  return d->value;
177 }
178 
179 QString Sensor::formattedValue() const
180 {
181  return Formatter::formatValue(value(), unit(), MetricPrefixAutoAdjust, FormatOptionShowNull);
182 }
183 
184 bool Sensor::enabled() const
185 {
186  if (d->enabled && parent()) {
187  auto parentEnabled = parent()->property("enabled");
188  if (parentEnabled.isValid()) {
189  return parentEnabled.toBool();
190  }
191  }
192 
193  return d->enabled;
194 }
195 
196 void Sensor::setEnabled(bool newEnabled)
197 {
198  if (newEnabled == d->enabled) {
199  return;
200  }
201 
202  d->enabled = newEnabled;
203  Q_EMIT enabledChanged();
204 }
205 
206 uint Sensor::updateInterval() const
207 {
208  //TODO: Make this dynamic once the backend supports that.
209  return BackendUpdateInterval;
210 }
211 
212 void Sensor::classBegin()
213 {
214  d->usedByQml = true;
215 }
216 
217 void Sensor::componentComplete()
218 {
219  d->componentComplete = true;
220 
221  setSensorId(d->pendingId);
222 
223  if (parent() && parent()->metaObject()->indexOfSignal("enabledChanged()") != -1) {
224  connect(parent(), SIGNAL(enabledChanged()), this, SIGNAL(enabledChanged()));
225  }
226 }
227 
228 void Sensor::onMetaDataChanged(const QString &sensorId, const SensorInfo &metaData)
229 {
230  if (sensorId != d->id || !enabled()) {
231  return;
232  }
233 
234  d->sensorInfo = metaData;
235 
236  if (d->status == Sensor::Status::Loading) {
237  d->status = Sensor::Status::Ready;
238  Q_EMIT statusChanged();
239  }
240 
242 }
243 
244 void Sensor::onValueChanged(const QString &sensorId, const QVariant &value)
245 {
246  if (sensorId != d->id || !enabled()) {
247  return;
248  }
249 
250  d->value = value;
251  Q_EMIT valueChanged();
252 }
253 
254 void Sensor::onEnabledChanged()
255 {
256  if (enabled()) {
257  connect(SensorDaemonInterface::instance(), &SensorDaemonInterface::metaDataChanged, this, &Sensor::onMetaDataChanged, Qt::UniqueConnection);
258  connect(SensorDaemonInterface::instance(), &SensorDaemonInterface::valueChanged, this, &Sensor::onValueChanged, Qt::UniqueConnection);
259 
260  SensorDaemonInterface::instance()->subscribe(d->id);
261  // Force an update of metadata and data, since that may have changed
262  // while we were disabled.
263  SensorDaemonInterface::instance()->requestMetaData(d->id);
264  SensorDaemonInterface::instance()->requestValue(d->id);
265  } else {
266  disconnect(SensorDaemonInterface::instance(), &SensorDaemonInterface::metaDataChanged, this, &Sensor::onMetaDataChanged);
267  disconnect(SensorDaemonInterface::instance(), &SensorDaemonInterface::valueChanged, this, &Sensor::onValueChanged);
268  SensorDaemonInterface::instance()->unsubscribe(d->id);
269  }
270 }
int indexOfSignal(const char *signal) const const
The sensor has no ID assigned.
Q_SIGNAL void metaDataChanged() const
This signal is emitted when any of the metadata properties change.
ParentAboutToChange
QEvent::Type type() const const
An object encapsulating a backend sensor.
Definition: Sensor.h:46
QVariant value() const
Returns the output of the sensor.
virtual const QMetaObject * metaObject() const const
static QString formatValue(const QVariant &value, Unit unit, MetricPrefix targetPrefix=MetricPrefixAutoAdjust, FormatOptions options=FormatOptionNone)
Converts value to the appropriate displayable string.
Definition: Formatter.cpp:285
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
virtual bool event(QEvent *e)
KSysGuard::Unit unit
The unit of this Sensor.
Definition: Sensor.h:72
QVariant property(const char *name) const const
An object to query the daemon for a list of sensors and their metadata.
Definition: SensorQuery.h:38
QString sensorId
The path to the backend sensor this Sensor represents.
Definition: Sensor.h:54
Status
This enum type is used to specify status of the Sensor.
Definition: Sensor.h:127
bool enabled
Should this Sensor check for changes?
Definition: Sensor.h:110
bool toBool() const const
UniqueConnection
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
Q_EMITQ_EMIT
The sensor has been loaded.
The sensor is currently being loaded.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Mar 4 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.