-
Notifications
You must be signed in to change notification settings - Fork 586
/
OpenHardwareMonitorWmi.cs
249 lines (218 loc) · 9.77 KB
/
OpenHardwareMonitorWmi.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Management;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using UnitsNet;
using UnitsNet.Units;
// We have a check on all constructors to ensure that we throw when not on Windows, so disabling warning on calling windows-only apis.
#pragma warning disable CA1416 // Validate platform compatibility
namespace Iot.Device.HardwareMonitor
{
/// <summary>
/// This class connects to a running instance of OpenHardwareMonitor and reads out all available values using WMI.
/// This works only if OpenHardwareMonitor (https://openhardwaremonitor.org/) is currently running.
/// While the tool needs to be run with elevated permissions, the application using this binding does not.
/// The connection using WMI has proven to be unreliable (the WMI classes are sometimes just not visible), therefore
/// OHM versions greater > 0.10.0 use a rest api instead.
/// </summary>
internal sealed class OpenHardwareMonitorWmi : IDisposable, IOpenHardwareMonitorInternal
{
private OpenHardwareMonitor.Hardware? _cpu;
/// <summary>
/// Constructs a new instance of this class.
/// The class can be constructed even if no sensors are available or OpenHardwareMonitor is not running (yet).
/// </summary>
/// <exception cref="PlatformNotSupportedException">The operating system is not Windows.</exception>
public OpenHardwareMonitorWmi()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new PlatformNotSupportedException("This class is only supported on Windows operating systems");
}
InitHardwareMonitor();
}
public SensorUpdateStrategy UpdateStrategy { get; set; }
public TimeSpan UpdateInterval { get; set; }
public void UpdateSensors(bool refreshSensorList)
{
if (refreshSensorList)
{
_cpu = null;
InitHardwareMonitor();
}
}
public bool HasHardware()
{
return _cpu != null;
}
private void InitHardwareMonitor()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\OpenHardwareMonitor", "SELECT * FROM Sensor");
foreach (var hardware in GetHardwareComponents())
{
if (hardware.Type != null && hardware.Type.Equals("CPU", StringComparison.OrdinalIgnoreCase))
{
_cpu = hardware;
}
}
searcher.Dispose();
}
catch (Exception x) when (x is IOException || x is UnauthorizedAccessException || x is ManagementException)
{
// Nothing to do - WMI not available for this element or missing permissions.
// WMI enumeration may require elevated rights.
}
}
/// <summary>
/// Query the list of all available sensors.
/// </summary>
/// <returns>A list of <see cref="OpenHardwareMonitor.Sensor"/> instances. May be empty.</returns>
/// <exception cref="ManagementException">The WMI objects required are not available. Is OpenHardwareMonitor running?</exception>
public IList<OpenHardwareMonitor.Sensor> GetSensorList()
{
List<OpenHardwareMonitor.Sensor> ret = new List<OpenHardwareMonitor.Sensor>();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\OpenHardwareMonitor", "SELECT * FROM Sensor");
foreach (ManagementObject sensor in searcher.Get())
{
string? name = Convert.ToString(sensor["Name"]);
string? identifier = Convert.ToString(sensor["Identifier"]);
// This is not expected to really happen
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(identifier))
{
continue;
}
string? parent = Convert.ToString(sensor["Parent"]);
string? type = Convert.ToString(sensor["SensorType"]);
SensorType typeEnum;
if (!Enum.TryParse(type, true, out typeEnum))
{
typeEnum = SensorType.Unknown;
}
ret.Add(new SensorWmi(sensor, name, identifier, parent, typeEnum));
}
return ret;
}
/// <summary>
/// Returns a list of hardware components, such as "CPU", "GPU" or "Mainboard"
/// </summary>
public IList<OpenHardwareMonitor.Hardware> GetHardwareComponents()
{
IList<OpenHardwareMonitor.Hardware> ret = new List<OpenHardwareMonitor.Hardware>();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\OpenHardwareMonitor", "SELECT * FROM Hardware");
if (searcher.Get().Count > 0)
{
foreach (ManagementObject sensor in searcher.Get())
{
string? name = Convert.ToString(sensor["Name"]);
string? identifier = Convert.ToString(sensor["Identifier"]);
if (name == null || identifier == null)
{
continue;
}
string? parent = Convert.ToString(sensor["Parent"]);
string? type = Convert.ToString(sensor["HardwareType"]);
ret.Add(new OpenHardwareMonitor.Hardware(name, identifier, parent, type));
}
}
return ret;
}
/// <summary>
/// Get the list of sensors for a specific piece of hardware
/// </summary>
/// <param name="forHardware">The module that should be queried</param>
/// <returns>A list of sensors</returns>
public IEnumerable<OpenHardwareMonitor.Sensor> GetSensorList(OpenHardwareMonitor.Hardware? forHardware)
{
if (forHardware == null)
{
throw new ArgumentNullException(nameof(forHardware));
}
return GetSensorList().Where(x => x.Identifier != null && x.Identifier.StartsWith(forHardware.Identifier ?? string.Empty)).OrderBy(y => y.Identifier);
}
/// <inheritdoc/>
public void Dispose()
{
_cpu = null;
}
/// <summary>
/// Represents a single Wmi sensor
/// </summary>
public class SensorWmi : OpenHardwareMonitor.Sensor, IDisposable
{
private readonly ManagementObject _instance;
private bool _valueRead;
/// <summary>
/// Creates a sensor instance
/// </summary>
public SensorWmi(ManagementObject instance, string name, string identifier, string? parent, SensorType typeEnum)
: base(name, identifier, parent, typeEnum)
{
_instance = instance;
InstanceId = 0;
_valueRead = false;
if (!string.IsNullOrWhiteSpace(instance.Path.RelativePath))
{
int instanceBegin = instance.Path.RelativePath.IndexOf("InstanceId=\"", StringComparison.OrdinalIgnoreCase) + 12;
int instanceEnd = instance.Path.RelativePath.IndexOf('\"', instanceBegin);
if (Int32.TryParse(instance.Path.RelativePath.Substring(instanceBegin, instanceEnd - instanceBegin), out int id))
{
InstanceId = id;
}
}
}
public int InstanceId { get; private set; }
private ManagementObjectSearcher? ActiveCollection
{
get;
set;
}
protected override bool UpdateValue(out double value)
{
if (_valueRead && InstanceId != 0)
{
// Cache the searcher. We have to re-query the instances each time, or the value stays the same.
ActiveCollection = new ManagementObjectSearcher(@"root\OpenHardwareMonitor", $"SELECT Value FROM Sensor WHERE InstanceId='{InstanceId}'");
}
if (_valueRead && InstanceId != 0 && ActiveCollection != null)
{
value = 0;
// We expect exactly one instance, but unfortunately, the returned ManagementObjectCollection doesn't implement IEnumerable or IList
foreach (var inst in ActiveCollection.Get())
{
value = Convert.ToSingle(inst.GetPropertyValue("Value"));
}
}
else
{
// The artificial instances auto-update. And if the object is new, we also don't need a refresh
value = Convert.ToSingle(_instance.GetPropertyValue("Value"));
_valueRead = true;
}
return true;
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
_instance.Dispose();
if (ActiveCollection != null)
{
ActiveCollection.Dispose();
ActiveCollection = null;
}
}
}
}
}
#pragma warning restore CA1416 // Validate platform compatibility