File indexing completed on 2024-04-21 05:31:42

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #pragma once
0007 
0008 #include <QDBusArgument>
0009 #include <QDateTime>
0010 #include <QDebug>
0011 #include <QObject>
0012 #include <QVariant>
0013 
0014 #include "formatter/Unit.h"
0015 
0016 namespace KSysGuard
0017 {
0018 constexpr uint BackendUpdateInterval = 500;
0019 
0020 // Data that is static for the lifespan of the sensor
0021 class SensorInfo
0022 {
0023 public:
0024     SensorInfo() = default;
0025     QString name; // Translated name of the sensor.
0026     QString shortName; // Shorter translated name of the sensor, to use when space is constrained.
0027     QString description; // Translated description of the sensor.
0028     QVariant::Type variantType = QVariant::Invalid;
0029     KSysGuard::Unit unit = KSysGuard::UnitInvalid; // Both a format hint and implies data type (i.e double/string)
0030     qreal min = 0;
0031     qreal max = 0;
0032 };
0033 
0034 class Q_DECL_EXPORT SensorData
0035 {
0036 public:
0037     SensorData() = default;
0038     SensorData(const QString &_attribute, const QVariant &_payload)
0039         : attribute(_attribute)
0040         , payload(_payload)
0041     {
0042     }
0043     QString attribute;
0044     QVariant payload;
0045 };
0046 
0047 typedef QHash<QString, SensorInfo> SensorInfoMap;
0048 typedef QList<SensorData> SensorDataList;
0049 
0050 inline QDBusArgument &operator<<(QDBusArgument &argument, const SensorInfo &s)
0051 {
0052     argument.beginStructure();
0053     argument << s.name;
0054     argument << s.shortName;
0055     argument << s.description;
0056     argument << s.variantType;
0057     argument << s.unit;
0058     argument << s.min;
0059     argument << s.max;
0060     argument.endStructure();
0061     return argument;
0062 }
0063 
0064 inline const QDBusArgument &operator>>(const QDBusArgument &argument, SensorInfo &s)
0065 {
0066     argument.beginStructure();
0067     argument >> s.name;
0068     argument >> s.shortName;
0069     argument >> s.description;
0070     uint32_t t;
0071     argument >> t;
0072     s.variantType = static_cast<QVariant::Type>(t);
0073     argument >> t;
0074     s.unit = static_cast<KSysGuard::Unit>(t);
0075     argument >> s.min;
0076     argument >> s.max;
0077     argument.endStructure();
0078     return argument;
0079 }
0080 
0081 inline QDBusArgument &operator<<(QDBusArgument &argument, const SensorData &s)
0082 {
0083     argument.beginStructure();
0084     argument << s.attribute;
0085     argument << QDBusVariant(s.payload);
0086     argument.endStructure();
0087     return argument;
0088 }
0089 
0090 inline const QDBusArgument &operator>>(const QDBusArgument &argument, SensorData &s)
0091 {
0092     argument.beginStructure();
0093     argument >> s.attribute;
0094     argument >> s.payload;
0095     argument.endStructure();
0096     return argument;
0097 }
0098 
0099 } // namespace KSysGuard
0100 
0101 Q_DECLARE_METATYPE(KSysGuard::SensorInfo);
0102 Q_DECLARE_METATYPE(KSysGuard::SensorData);
0103 Q_DECLARE_METATYPE(KSysGuard::SensorDataList);