/
HWMonitor.cs
164 lines (147 loc) · 4.83 KB
/
HWMonitor.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using OpenHardwareMonitor.Hardware;
using Microsoft.VisualBasic.Devices;
using Computer = OpenHardwareMonitor.Hardware.Computer;
namespace HW_Monitor_Server
{
class HWMonitor
{
private Computer computer;
private int cpu_count;
[JsonProperty]
private float[] cpu_usages;
[JsonProperty(PropertyName = "cpu_temps")]
private float[] cpu_temperatures;
[JsonProperty]
private long memory_available;
private long memory_total;
[JsonProperty]
private float[] drive_usages;
private DriveInfo[] drives;
public HWMonitor()
{
this.computer = new Computer
{
CPUEnabled = true,
GPUEnabled = true,
RAMEnabled = true
};
computer.Open();
this.cpu_count = _GetCPUCount();
this.cpu_usages = new float[cpu_count];
this.cpu_temperatures = new float[cpu_count];
this.memory_total = (long) new ComputerInfo().TotalPhysicalMemory;
this.drives = DriveInfo.GetDrives();
this.drive_usages = new float[drives.Length];
}
public string GetStaticInfo()
{
drives = DriveInfo.GetDrives();
Drive[] disk_partitions = new Drive[drives.Length];
for (int i = 0; i < drives.Length; i++)
{
DriveInfo df = drives[i];
disk_partitions[i] = new Drive(df.Name, df.DriveFormat, df.TotalSize);
}
return JsonConvert.SerializeObject(new
{
cpu_count = cpu_count - 1, // Minus one for CPU package
memory_total = memory_total,
disk_partitions = disk_partitions
});
}
public string GetDynamicInfo()
{
// Get data from OpenHardwareMonitor
foreach (var hardwareItem in computer.Hardware)
{
hardwareItem.Update();
foreach (IHardware subHardware in hardwareItem.SubHardware)
{
subHardware.Update();
}
switch (hardwareItem.HardwareType)
{
case HardwareType.CPU:
_UpdateCPU(hardwareItem);
break;
}
}
// Update using data from Windows
_UpdateRAM();
_UpdateDrives();
return GetAsJSON();
}
private int _GetCPUCount()
{
int cpu_count = 0;
Computer computer = new Computer
{
CPUEnabled = true
};
computer.Open();
foreach (var hardwareItem in computer.Hardware)
{
foreach (var sensor in hardwareItem.Sensors)
if (sensor.SensorType == SensorType.Load) cpu_count++;
}
return cpu_count;
}
private void _UpdateCPU(IHardware hardwareItem)
{
if (hardwareItem.HardwareType != HardwareType.CPU) return;
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Load)
{
cpu_usages[sensor.Index] = sensor.Value ?? cpu_temperatures[sensor.Index];
}
else if (sensor.SensorType == SensorType.Temperature)
{
cpu_temperatures[sensor.Index] = sensor.Value ?? cpu_temperatures[sensor.Index];
}
}
}
private void _UpdateRAM()
{
memory_available = (long) new ComputerInfo().AvailablePhysicalMemory;
}
private void _UpdateDrives()
{
drives = DriveInfo.GetDrives();
if (drive_usages.Length == drives.Length)
{
for (int i = 0; i < drives.Length; i++)
{
drive_usages[i] = drives[i].TotalSize - drives[i].TotalFreeSpace;
}
}
else
{
throw new Exception("Drives changed - not implemented");
}
}
public string GetAsJSON()
{
return JsonConvert.SerializeObject(this);
}
private class Drive
{
public readonly string device;
public readonly string fstype;
public readonly long capacity;
public Drive(string device, string fstype, long capacity)
{
this.device = device;
this.fstype = fstype;
this.capacity = capacity;
}
}
}
}