File indexing completed on 2024-04-28 05:31:44
0001 /* 0002 SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org> 0003 SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #pragma once 0009 0010 #include <memory> 0011 0012 #include <QObject> 0013 #include <QQmlParserStatus> 0014 #include <QString> 0015 #include <QVariant> 0016 0017 #include "formatter/Unit.h" 0018 0019 #include "sensors_export.h" 0020 0021 namespace KSysGuard 0022 { 0023 class SensorData; 0024 class SensorInfo; 0025 class SensorQuery; 0026 0027 /** 0028 * An object encapsulating a backend sensor. 0029 * 0030 * This class represents a sensor as exposed by the backend. It allows querying 0031 * various metadata properties of the sensor as well as the current value. 0032 */ 0033 class SENSORS_EXPORT Sensor : public QObject, public QQmlParserStatus 0034 { 0035 Q_OBJECT 0036 Q_INTERFACES(QQmlParserStatus) 0037 0038 /** 0039 * The path to the backend sensor this Sensor represents. 0040 */ 0041 Q_PROPERTY(QString sensorId READ sensorId WRITE setSensorId NOTIFY sensorIdChanged) 0042 /** 0043 * The user-visible name of this Sensor. 0044 */ 0045 Q_PROPERTY(QString name READ name NOTIFY metaDataChanged) 0046 /** 0047 * A shortened name that can be displayed when space is constrained. 0048 * 0049 * The value is the same as name if shortName was not provided by the backend. 0050 */ 0051 Q_PROPERTY(QString shortName READ shortName NOTIFY metaDataChanged) 0052 /** 0053 * A description of the Sensor. 0054 */ 0055 Q_PROPERTY(QString description READ description NOTIFY metaDataChanged) 0056 /** 0057 * The unit of this Sensor. 0058 */ 0059 Q_PROPERTY(KSysGuard::Unit unit READ unit NOTIFY metaDataChanged) 0060 /** 0061 * The minimum value this Sensor can have. 0062 */ 0063 Q_PROPERTY(qreal minimum READ minimum NOTIFY metaDataChanged) 0064 /** 0065 * The maximum value this Sensor can have. 0066 */ 0067 Q_PROPERTY(qreal maximum READ maximum NOTIFY metaDataChanged) 0068 /** 0069 * The QVariant type for this sensor. 0070 * 0071 * This is used to create proper default values. 0072 */ 0073 Q_PROPERTY(QVariant::Type type READ type NOTIFY metaDataChanged) 0074 /** 0075 * The status of the sensor. 0076 * 0077 * Due to the asynchronous nature of the underlying code, sensors are not 0078 * immediately available on construction. Instead, they need to request data 0079 * from the daemon and wait for it to arrive. This property reflects where 0080 * in that process this sensor is. 0081 */ 0082 Q_PROPERTY(Status status READ status NOTIFY statusChanged) 0083 /** 0084 * The current value of this sensor. 0085 */ 0086 Q_PROPERTY(QVariant value READ value NOTIFY valueChanged) 0087 /** 0088 * A formatted version of \property value. 0089 */ 0090 Q_PROPERTY(QString formattedValue READ formattedValue NOTIFY valueChanged) 0091 /** 0092 * Should this Sensor check for changes? 0093 * 0094 * Note that if set to true, the sensor will only be enabled when the parent 0095 * is also enabled. 0096 */ 0097 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) 0098 /** 0099 * The time in milliseconds between each update of the sensor. 0100 * 0101 * This is primarily intended to match sampling rate to the update rate of 0102 * the sensor, like when used by KQuickCharts's HistorySource. 0103 * 0104 * \note Currently, the update rate of the backend is fixed and this method 0105 * simply matches the value from the backend. Eventually the idea is 0106 * that we can support per-sensor update rates. 0107 */ 0108 Q_PROPERTY(uint updateInterval READ updateInterval NOTIFY updateIntervalChanged) 0109 /** 0110 * The minimum time between updates, in milliseconds. 0111 * 0112 * If this is set to a positive non-zero value, at least this many 0113 * milliseconds need to elapse before another value update happens, otherwise 0114 * it is ignored. This effectively rate-limits the updates and will prevent 0115 * value updates. 0116 */ 0117 Q_PROPERTY(int updateRateLimit READ updateRateLimit WRITE setUpdateRateLimit NOTIFY updateRateLimitChanged RESET resetUpdateRateLimit) 0118 0119 public: 0120 /** 0121 * This enum type is used to specify status of the Sensor. 0122 */ 0123 enum class Status { 0124 Unknown, ///< The sensor has no ID assigned. 0125 Loading, ///< The sensor is currently being loaded. 0126 Ready, ///< The sensor has been loaded. 0127 Error, ///< An error occurred or the sensor has been removed. 0128 Removed, ///< Removed from backend 0129 }; 0130 Q_ENUM(Status) 0131 0132 explicit Sensor(QObject *parent = nullptr); 0133 explicit Sensor(const QString &id, QObject *parent = nullptr); 0134 /** 0135 * Construct a Sensor from a SensorQuery result and index. 0136 * 0137 * This avoids an extra lookup for the sensor metadata. 0138 */ 0139 Sensor(const SensorQuery &query, int index, QObject *parent = nullptr); 0140 ~Sensor() override; 0141 0142 bool event(QEvent *event) override; 0143 0144 QString sensorId() const; 0145 void setSensorId(const QString &id); 0146 Q_SIGNAL void sensorIdChanged() const; 0147 0148 Status status() const; 0149 Q_SIGNAL void statusChanged() const; 0150 0151 QString name() const; 0152 QString shortName() const; 0153 QString description() const; 0154 KSysGuard::Unit unit() const; 0155 qreal minimum() const; 0156 qreal maximum() const; 0157 QVariant::Type type() const; 0158 /** 0159 * This signal is emitted when any of the metadata properties change. 0160 */ 0161 Q_SIGNAL void metaDataChanged() const; 0162 0163 /** 0164 * Returns the output of the sensor. 0165 * 0166 * The returned value is the most recent sensor data received from the ksysguard 0167 * daemon, it's not necessarily the actual current output value. 0168 * 0169 * The client can't control how often the sensor data is sampled. The ksysguard 0170 * daemon is in charge of picking the sample rate. When the Sensor receives new 0171 * output value, dataChanged signal will be emitted. 0172 * 0173 * @see dataChanged 0174 */ 0175 QVariant value() const; 0176 QString formattedValue() const; 0177 Q_SIGNAL void valueChanged() const; 0178 0179 bool enabled() const; 0180 void setEnabled(bool newEnabled); 0181 Q_SIGNAL void enabledChanged(); 0182 0183 uint updateInterval() const; 0184 Q_SIGNAL void updateIntervalChanged(); 0185 0186 int updateRateLimit() const; 0187 void setUpdateRateLimit(int newUpdateRateLimit); 0188 void resetUpdateRateLimit(); 0189 Q_SIGNAL void updateRateLimitChanged(); 0190 0191 void classBegin() override; 0192 void componentComplete() override; 0193 0194 private: 0195 void onMetaDataChanged(const QString &sensorId, const SensorInfo &metaData); 0196 void onValueChanged(const QString &sensorId, const QVariant &value); 0197 void onEnabledChanged(); 0198 0199 class Private; 0200 const std::unique_ptr<Private> d; 0201 }; 0202 0203 } // namespace KSysGuard