Libksysguard

Sensor.h
1/*
2 SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
3 SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#pragma once
9
10#include <memory>
11
12#include <QObject>
13#include <QQmlParserStatus>
14#include <QString>
15#include <QVariant>
16#include <qqmlregistration.h>
17
18#include "formatter/Unit.h"
19
20#include "sensors_export.h"
21
22namespace KSysGuard
23{
24class SensorData;
25class SensorInfo;
26class SensorQuery;
27
28/**
29 * An object encapsulating a backend sensor.
30 *
31 * This class represents a sensor as exposed by the backend. It allows querying
32 * various metadata properties of the sensor as well as the current value.
33 */
34class SENSORS_EXPORT Sensor : public QObject, public QQmlParserStatus
35{
36 Q_OBJECT
37 QML_ELEMENT
38 Q_INTERFACES(QQmlParserStatus)
39
40 /**
41 * The path to the backend sensor this Sensor represents.
42 */
43 Q_PROPERTY(QString sensorId READ sensorId WRITE setSensorId NOTIFY sensorIdChanged)
44 /**
45 * The user-visible name of this Sensor.
46 */
47 Q_PROPERTY(QString name READ name NOTIFY metaDataChanged)
48 /**
49 * A shortened name that can be displayed when space is constrained.
50 *
51 * The value is the same as name if shortName was not provided by the backend.
52 */
53 Q_PROPERTY(QString shortName READ shortName NOTIFY metaDataChanged)
54 /**
55 * A description of the Sensor.
56 */
57 Q_PROPERTY(QString description READ description NOTIFY metaDataChanged)
58 /**
59 * The unit of this Sensor.
60 */
61 Q_PROPERTY(KSysGuard::Unit unit READ unit NOTIFY metaDataChanged)
62 /**
63 * The minimum value this Sensor can have.
64 */
65 Q_PROPERTY(qreal minimum READ minimum NOTIFY metaDataChanged)
66 /**
67 * The maximum value this Sensor can have.
68 */
69 Q_PROPERTY(qreal maximum READ maximum NOTIFY metaDataChanged)
70 /**
71 * The QVariant type for this sensor.
72 *
73 * This is used to create proper default values.
74 */
75 Q_PROPERTY(QVariant::Type type READ type NOTIFY metaDataChanged)
76 /**
77 * The status of the sensor.
78 *
79 * Due to the asynchronous nature of the underlying code, sensors are not
80 * immediately available on construction. Instead, they need to request data
81 * from the daemon and wait for it to arrive. This property reflects where
82 * in that process this sensor is.
83 */
84 Q_PROPERTY(Status status READ status NOTIFY statusChanged)
85 /**
86 * The current value of this sensor.
87 */
88 Q_PROPERTY(QVariant value READ value NOTIFY valueChanged)
89 /**
90 * A formatted version of \property value.
91 */
92 Q_PROPERTY(QString formattedValue READ formattedValue NOTIFY valueChanged)
93 /**
94 * Should this Sensor check for changes?
95 *
96 * Note that if set to true, the sensor will only be enabled when the parent
97 * is also enabled.
98 */
99 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
100 /**
101 * The time in milliseconds between each update of the sensor.
102 *
103 * This is primarily intended to match sampling rate to the update rate of
104 * the sensor, like when used by KQuickCharts's HistorySource.
105 *
106 * \note Currently, the update rate of the backend is fixed and this method
107 * simply matches the value from the backend. Eventually the idea is
108 * that we can support per-sensor update rates.
109 */
110 Q_PROPERTY(uint updateInterval READ updateInterval NOTIFY updateIntervalChanged)
111 /**
112 * The minimum time between updates, in milliseconds.
113 *
114 * If this is set to a positive non-zero value, at least this many
115 * milliseconds need to elapse before another value update happens, otherwise
116 * it is ignored. This effectively rate-limits the updates and will prevent
117 * value updates.
118 */
119 Q_PROPERTY(int updateRateLimit READ updateRateLimit WRITE setUpdateRateLimit NOTIFY updateRateLimitChanged RESET resetUpdateRateLimit)
120
121public:
122 /**
123 * This enum type is used to specify status of the Sensor.
124 */
125 enum class Status {
126 Unknown, ///< The sensor has no ID assigned.
127 Loading, ///< The sensor is currently being loaded.
128 Ready, ///< The sensor has been loaded.
129 Error, ///< An error occurred or the sensor has been removed.
130 Removed, ///< Removed from backend
131 };
132 Q_ENUM(Status)
133
134 explicit Sensor(QObject *parent = nullptr);
135 explicit Sensor(const QString &id, QObject *parent = nullptr);
136 /**
137 * Construct a Sensor from a SensorQuery result and index.
138 *
139 * This avoids an extra lookup for the sensor metadata.
140 */
141 Sensor(const SensorQuery &query, int index, QObject *parent = nullptr);
142 ~Sensor() override;
143
144 bool event(QEvent *event) override;
145
146 QString sensorId() const;
147 void setSensorId(const QString &id);
148 Q_SIGNAL void sensorIdChanged() const;
149
150 Status status() const;
151 Q_SIGNAL void statusChanged() const;
152
153 QString name() const;
154 QString shortName() const;
155 QString description() const;
156 KSysGuard::Unit unit() const;
157 qreal minimum() const;
158 qreal maximum() const;
159 QVariant::Type type() const;
160 /**
161 * This signal is emitted when any of the metadata properties change.
162 */
163 Q_SIGNAL void metaDataChanged() const;
164
165 /**
166 * Returns the output of the sensor.
167 *
168 * The returned value is the most recent sensor data received from the ksysguard
169 * daemon, it's not necessarily the actual current output value.
170 *
171 * The client can't control how often the sensor data is sampled. The ksysguard
172 * daemon is in charge of picking the sample rate. When the Sensor receives new
173 * output value, dataChanged signal will be emitted.
174 *
175 * @see dataChanged
176 */
177 QVariant value() const;
178 QString formattedValue() const;
179 Q_SIGNAL void valueChanged() const;
180
181 bool enabled() const;
182 void setEnabled(bool newEnabled);
183 Q_SIGNAL void enabledChanged();
184
185 uint updateInterval() const;
186 Q_SIGNAL void updateIntervalChanged();
187
188 int updateRateLimit() const;
189 void setUpdateRateLimit(int newUpdateRateLimit);
190 void resetUpdateRateLimit();
191 Q_SIGNAL void updateRateLimitChanged();
192
193 void classBegin() override;
194 void componentComplete() override;
195
196private:
197 void onMetaDataChanged(const QString &sensorId, const SensorInfo &metaData);
198 void onValueChanged(const QString &sensorId, const QVariant &value);
199 void onEnabledChanged();
200
201 class Private;
202 const std::unique_ptr<Private> d;
203};
204
205} // namespace KSysGuard
An object to query the daemon for a list of sensors and their metadata.
Definition SensorQuery.h:26
An object encapsulating a backend sensor.
Definition Sensor.h:35
Status
This enum type is used to specify status of the Sensor.
Definition Sensor.h:125
Q_SIGNAL void metaDataChanged() const
This signal is emitted when any of the metadata properties change.
Q_SCRIPTABLE CaptureState status()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:17:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.