15#include "formatter/Formatter.h"
16#include "systemstats/SensorInfo.h"
19#include "SensorDaemonInterface_p.h"
20#include "SensorQuery.h"
22using namespace KSysGuard;
23namespace chrono = std::chrono;
25class Q_DECL_HIDDEN
Sensor::Private
28 SensorInfo sensorInfo;
33 bool usedByQml =
false;
34 bool componentComplete =
false;
41 std::optional<int> updateRateLimit;
42 chrono::steady_clock::time_point lastUpdate;
54 connect(
this, &Sensor::statusChanged,
this, &Sensor::valueChanged);
56 connect(
this, &Sensor::enabledChanged,
this, &Sensor::onEnabledChanged);
64 if (index >= 0 && index < query.result().
size()) {
65 auto result = query.result().
at(index);
67 onMetaDataChanged(d->id, result.second);
72bool Sensor::event(
QEvent *event)
78 connect(
parent(), SIGNAL(enabledChanged()),
this, SIGNAL(enabledChanged()));
87 SensorDaemonInterface::instance()->unsubscribe(d->id);
95void Sensor::setSensorId(
const QString &
id)
101 if (d->usedByQml && !d->componentComplete) {
110 SensorDaemonInterface::instance()->requestMetaData(
id);
111 connect(SensorDaemonInterface::instance(), &SensorDaemonInterface::metaDataChanged,
this, &Sensor::onMetaDataChanged,
Qt::UniqueConnection);
115 SensorDaemonInterface::instance()->subscribe(
id);
116 SensorDaemonInterface::instance()->requestValue(
id);
117 connect(SensorDaemonInterface::instance(), &SensorDaemonInterface::valueChanged,
this, &Sensor::onValueChanged,
Qt::UniqueConnection);
131 return d->sensorInfo.name;
136 if (d->sensorInfo.shortName.isEmpty()) {
137 return d->sensorInfo.name;
140 return d->sensorInfo.shortName;
145 return d->sensorInfo.description;
150 return d->sensorInfo.unit;
155 return d->sensorInfo.min;
160 return d->sensorInfo.max;
165 return d->sensorInfo.variantType;
170 if (!d->value.isValid()) {
171 return QVariant{d->sensorInfo.variantType};
176QString Sensor::formattedValue()
const
183 if (d->enabled &&
parent()) {
185 if (parentEnabled.isValid()) {
186 return parentEnabled.
toBool();
193void Sensor::setEnabled(
bool newEnabled)
195 if (newEnabled == d->enabled) {
199 d->enabled = newEnabled;
206 return BackendUpdateInterval;
211 return d->updateRateLimit.value_or(-1);
214void Sensor::setUpdateRateLimit(
int newUpdateRateLimit)
218 if (newUpdateRateLimit <= 0) {
219 if (!d->updateRateLimit) {
223 d->updateRateLimit.reset();
225 if (d->updateRateLimit && d->updateRateLimit.value() == newUpdateRateLimit) {
229 d->updateRateLimit = newUpdateRateLimit;
232 d->lastUpdate = chrono::steady_clock::now();
233 Q_EMIT updateRateLimitChanged();
236void KSysGuard::Sensor::resetUpdateRateLimit()
238 setUpdateRateLimit(-1);
241void Sensor::classBegin()
246void Sensor::componentComplete()
248 d->componentComplete =
true;
250 setSensorId(d->pendingId);
253 connect(
parent(), SIGNAL(enabledChanged()),
this, SIGNAL(enabledChanged()));
257void Sensor::onMetaDataChanged(
const QString &sensorId,
const SensorInfo &metaData)
263 d->sensorInfo = metaData;
273void Sensor::onValueChanged(
const QString &sensorId,
const QVariant &value)
279 if (d->updateRateLimit && d->value.isValid()) {
280 auto updateRateLimit = chrono::steady_clock::duration(chrono::milliseconds(d->updateRateLimit.value()));
281 auto now = chrono::steady_clock::now();
293void Sensor::onEnabledChanged()
296 connect(SensorDaemonInterface::instance(), &SensorDaemonInterface::metaDataChanged,
this, &Sensor::onMetaDataChanged,
Qt::UniqueConnection);
297 connect(SensorDaemonInterface::instance(), &SensorDaemonInterface::valueChanged,
this, &Sensor::onValueChanged,
Qt::UniqueConnection);
299 SensorDaemonInterface::instance()->subscribe(d->id);
302 SensorDaemonInterface::instance()->requestMetaData(d->id);
303 SensorDaemonInterface::instance()->requestValue(d->id);
305 disconnect(SensorDaemonInterface::instance(), &SensorDaemonInterface::metaDataChanged,
this, &Sensor::onMetaDataChanged);
306 disconnect(SensorDaemonInterface::instance(), &SensorDaemonInterface::valueChanged,
this, &Sensor::onValueChanged);
307 SensorDaemonInterface::instance()->unsubscribe(d->id);
An object to query the daemon for a list of sensors and their metadata.
An object encapsulating a backend sensor.
int updateRateLimit
The minimum time between updates, in milliseconds.
QVariant::Type type
The QVariant type for this sensor.
Status
This enum type is used to specify status of the Sensor.
@ Loading
The sensor is currently being loaded.
@ Ready
The sensor has been loaded.
qreal maximum
The maximum value this Sensor can have.
KSysGuard::Unit unit
The unit of this Sensor.
QString shortName
A shortened name that can be displayed when space is constrained.
QVariant value
The current value of this sensor.
QString description
A description of the Sensor.
QString name
The user-visible name of this Sensor.
bool enabled
Should this Sensor check for changes?
QString sensorId
The path to the backend sensor this Sensor represents.
qreal minimum
The minimum value this Sensor can have.
Q_SIGNAL void metaDataChanged() const
This signal is emitted when any of the metadata properties change.
Status status
The status of the sensor.
uint updateInterval
The time in milliseconds between each update of the sensor.
Q_SCRIPTABLE CaptureState status()
const_reference at(qsizetype i) const const
qsizetype size() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
virtual bool event(QEvent *e)
QObject * parent() const const
QVariant property(const char *name) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool toBool() const const