-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_monitor.h
326 lines (292 loc) · 9.17 KB
/
system_monitor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#ifndef SYSTEM_MONITOR_H
#define SYSTEM_MONITOR_H
#include <QObject>
#include <QMap>
#include <QThread>
#include <QJsonArray>
#include <QJsonObject>
#include <string>
#include <QQuickItem>
#include <QMetaObject>
#include <QMetaEnum>
#include <QString>
#include "kzp_keys.h"
using namespace SharedKeys;
class QTimer;
class QQmlProperty;
class HWSensorController;
class SensorMonitor; // Qml interface
namespace KZP{
static constexpr char RELEASE[] = "releaseSensor";
static constexpr char WATCH[] = "watchSensor";
static constexpr char WRITE[] = "writeSensor";
static constexpr char INIT[] = "initialize";
static constexpr char REQUEST[] = "requestAvailableSensors";
static constexpr char UPDATE[] = "updateSensor";
};
typedef int AdjustedID;
// updates
// SensorUpdate becomes Struct;
struct SensorUpdate{
~SensorUpdate(){
if(path) {
delete [] path;
path = nullptr;
}
}
AdjustedID sensor;
double value;
char* path = nullptr;
};
//typedef std::tuple<AdjustedID, double, QString> SensorUpdate; // pair ID->Value
typedef QList<SensorUpdate*> UpdateList; // list of updates, stored in thread and reference passed to main thread
namespace KZPSensors{
class DeviceTypes {
Q_GADGET
QML_ANONYMOUS
public:
enum class Device{
CPU = 86,
GPU = 102,
Motherboard = 126,
Storage = 134,
Memory = 198,
Network = 230,
Unknown = -5
};
Q_ENUM(Device)
private:
explicit DeviceTypes(){}
};
class CPU : public QObject {
Q_OBJECT
public:
enum class CPU_Sensor{
DieTemp = 230,
PackageTemp = 246,
AverageTemp = 262,
WorkLoad = 278,
WorkLoadTotal = 279,
Frequency = 294,
};
Q_ENUM(CPU_Sensor)
private:
explicit CPU(){}
};
class GPU : public QObject {
Q_OBJECT
public:
enum class GPU_Sensor{
CoreTemp = 310,
JunctionTemp = 334,
MemoryTemp = 358,
MemoryUsed = 359,
WorkLoad = 382 ,
MemorySpeed = 383,
FanSpeed = 384,
ClockSpeed = 385,
Frequency = 406,
Voltage = 407,
Wattage = 408
};
Q_ENUM(GPU_Sensor)
private:
explicit GPU(){}
};
class Storage : public QObject {
public:
enum class Storage_Info{
Temperature = 504,
Capacity = 510,
UsedPercentage = 514,
DataRate = 516,
TransferRate = 518
};
};
class Memory : public QObject {
public:
enum class Memory_Value{
};
};
class ValueTypes: public QObject {
Q_OBJECT
public:
enum class SuffixType {
Celcius = 5337,
Percentage,
Value,
RPM,
YoctoVoltage,
Megahertz,
Megabytes,
Gigahertz,
Gigabytes
};
Q_ENUM(SuffixType)
enum class ValueType {
Temperature = 6337,
Utilization,
Speed,
DataSize,
Frequency,
Voltage,
Wattage,
Current,
Minimum,
Maximum,
Value,
Highest,
Lowest,
TypeID,
Data,
DataRate
};
Q_ENUM(ValueType)
private:
explicit ValueTypes(){}
};
// Motherboard
// Memory
// Storage
} // End namespace KZPSensors
struct AdjustedSensorID{
explicit AdjustedSensorID(int s = 0, int d = 0) : sensor(s), device(d) {}
std::uint32_t sensor:24;
std::uint32_t device:8;
int adjusted() const{
return (sensor << 8) + device;
}
static int adjusted(uint32_t s, uint32_t d = 0){
return (s << 8) + d;
}
void setAdjusted(int data)
{
sensor = (data >> 8);
device = data & 0xff;
}
};
inline bool operator==(const AdjustedSensorID& left, const AdjustedSensorID& right)
{
return left.sensor == right.sensor && left.device == right.device;
}
class SystemMonitor;
class SensorDefinition : public QObject{
Q_OBJECT
friend class SystemMonitor;
Q_PROPERTY(double value READ value NOTIFY valueChanged MEMBER mValue)
public:
SensorDefinition(QObject* parent = nullptr) : QObject(parent), mValue(0.0), mWritable(false) {} // default empty
SensorDefinition(QString name_in, int type_in, QString path_in, QString subfix_in = QString{}, QObject* parent = nullptr) :
QObject(parent), mName(name_in), mType(type_in), mValue(0.0), mPath(path_in), mSubfix(subfix_in), mWritable(false)
{}
QString name() { return mName; }
int type() { return mType; }
double value() { return mValue; }
QString path() { return mPath; }
QString subfix() { return mSubfix; }
bool canWrite() {return mWritable; }
public slots:
signals:
void valueChanged(double value);
protected:
void setWritable(bool canWrite) {
mWritable = canWrite;
}
QString mName;
int mType;
double mValue;
QString mPath;
QString mSubfix;
bool mWritable;
protected slots:
void setValue(double v) {
if(v != mValue) {
mValue = v;
emit valueChanged(mValue);
}
}
};
struct DeviceDefinition{
explicit DeviceDefinition(KZPSensors::DeviceTypes::Device t = KZPSensors::DeviceTypes::Device::Unknown) : type(t){}
QString name;
KZPSensors::DeviceTypes::Device type;
QList<SensorDefinition*> sensors;
private: // do not permit copies
DeviceDefinition(const DeviceDefinition& rvalue) { Q_UNUSED(rvalue)}
DeviceDefinition& operator=(const DeviceDefinition& rvalue) { Q_UNUSED(rvalue); return *this;}
// allow moves
DeviceDefinition& operator=(DeviceDefinition&& rvalue)
{
name = rvalue.name; type = rvalue.type; sensors = rvalue.sensors; // implicit copy
rvalue.name = QLatin1String{};
rvalue.type = KZPSensors::DeviceTypes::Device::Unknown;
rvalue.sensors = QList<SensorDefinition*>{};
return *this;
}
};
typedef std::pair<QObject*,QQmlProperty*> MappedProperty;
typedef std::pair<double, QList<MappedProperty>* > MappedSensor;
typedef QList<DeviceDefinition*>* DeviceList;
typedef QMap<int, SensorDefinition* > SensorMap;
typedef SensorMap* MappingList;
// Singleton Ux interface object
class SystemMonitor : public QObject
{
Q_OBJECT
// Q_PROPERTY(quint32 pollDelay READ pollDelay WRITE setPollDelay NOTIFY pollDelayChanged MEMBER mPollDelay)
public:
explicit SystemMonitor(QObject *parent = nullptr);
~SystemMonitor();
// Loading from settings json object
Q_INVOKABLE QJsonObject jsonSettings();
// NEW Interface
// App interface
Q_INVOKABLE bool isReady() { return (mDeviceList && mDeviceList->size()>0); }
Q_INVOKABLE bool isAttached(int sensorID, int device = 0);
Q_INVOKABLE bool connectOn(int sensorID, int device, QObject& item);
Q_INVOKABLE bool releaseConnection(int sensorID, int device, QObject& item);
Q_INVOKABLE double readSensor(int sensorID, int device = 0, bool * valid = nullptr);
Q_INVOKABLE void writeToSensor(int sensorID, int device = 0, double value = 0.0, bool* written = nullptr);
Q_INVOKABLE bool sensorExists(int sensor, int device = 0);
Q_INVOKABLE void queryDevicesAvailable();
// Sensor Convenienve Methods
using CPU_Sensor = KZPSensors::CPU::CPU_Sensor;
using GPU_Sensor = KZPSensors::GPU::GPU_Sensor;
using ValueType = KZPSensors::ValueTypes::ValueType;
Q_INVOKABLE static QString sensorIconSource(int type);
Q_INVOKABLE static bool isValidSensor(int sensorID);
Q_INVOKABLE static QJsonArray sensorsAvailable();
Q_INVOKABLE static QJsonArray sensorValueTypes();
Q_INVOKABLE static QString sensorIDToString(int type);
Q_INVOKABLE static QString sensorTypeToString(int type);
void initializeSensors(QJsonObject sensorMap);
void aboutToExit();
signals:
void sensorsReady();
void availableDevices(QJsonArray devices);
protected:
// improve API and add declarative class
HWSensorController* mSensorController;
QThread mSensorThread;
// mAttached sensors utilize the SensorDefinitions inside the Device Definitions
SensorMap* mAttachedSensors; // Map for updates
// All Device Definitions will exist within the mDeviceList
QList<DeviceDefinition*>* mDeviceList; // Lookup list
quint32 mPollDelay;
QJsonObject mSettings;
// QStringList mDevices;
// QStringList mTypes;
// QMap<QString, MappedSensor> mMap;
void clearCache();
void initializeHwSensorController();
void releseHWSensorController();
protected slots:
void clearAttachedMap();
void clearDeviceList();
void releaseAll();
void receivedDevices(QJsonArray deviceArray);
void onSensorMapUpdate(QJsonArray deviceArray);
void onSensorUpdate(void* updates);
void onUpdate(int sensorID, double value);
};
#endif // SYSTEM_MONITOR_H