Libksysguard

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