-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskbarTemperatureReader.cs
More file actions
192 lines (167 loc) · 6.38 KB
/
Copy pathTaskbarTemperatureReader.cs
File metadata and controls
192 lines (167 loc) · 6.38 KB
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
using LibreHardwareMonitor.Hardware;
namespace TaskbarInfo;
public sealed record TaskbarTemperatureSnapshot(
double? CpuTemperatureCelsius,
double? GpuTemperatureCelsius,
double? DiskTemperatureCelsius,
IReadOnlyList<string>? GpuDevices = null,
IReadOnlyList<string>? DiskDevices = null)
{
public IReadOnlyList<string> GpuDeviceNames { get; } = NormalizeDeviceNames(GpuDevices);
public IReadOnlyList<string> DiskDeviceNames { get; } = NormalizeDeviceNames(DiskDevices);
public static TaskbarTemperatureSnapshot Empty { get; } = new(null, null, null);
public static TaskbarTemperatureSnapshot Merge(params TaskbarTemperatureSnapshot[] sources)
{
double? cpu = null;
double? gpu = null;
double? disk = null;
var gpuDevices = new List<string>();
var diskDevices = new List<string>();
foreach (TaskbarTemperatureSnapshot source in sources)
{
cpu ??= source.CpuTemperatureCelsius;
gpu ??= source.GpuTemperatureCelsius;
disk ??= source.DiskTemperatureCelsius;
gpuDevices.AddRange(source.GpuDeviceNames);
diskDevices.AddRange(source.DiskDeviceNames);
}
return new TaskbarTemperatureSnapshot(cpu, gpu, disk, gpuDevices, diskDevices);
}
private static IReadOnlyList<string> NormalizeDeviceNames(IEnumerable<string>? names) =>
names?
.Select(name => name?.Trim())
.Where(name => !string.IsNullOrWhiteSpace(name))
.Distinct(StringComparer.OrdinalIgnoreCase)
.Cast<string>()
.ToArray() ?? Array.Empty<string>();
}
/// <summary>
/// Reads hardware temperatures only when the performance collector asks for them.
/// Hardware and driver support varies, so missing sensor values are expected.
/// </summary>
public sealed class TaskbarTemperatureReader : IDisposable
{
private readonly Computer _computer = new()
{
IsCpuEnabled = true,
IsGpuEnabled = true,
IsStorageEnabled = true,
IsControllerEnabled = true
};
private readonly WindowsStorageTemperatureReader _storageReader = new();
private readonly TemperatureSensorHelperClient _helperClient = new();
private bool _enhanced;
private bool _opened;
private bool _disposed;
public TaskbarTemperatureSnapshot Read()
{
if (_disposed) return TaskbarTemperatureSnapshot.Empty;
TaskbarTemperatureSnapshot enhanced = _helperClient.Read(_enhanced);
TaskbarTemperatureSnapshot hardwareMonitor = ReadLibreHardwareMonitor();
TaskbarTemperatureSnapshot merged = TaskbarTemperatureSnapshot.Merge(enhanced, hardwareMonitor);
if (merged.DiskTemperatureCelsius.HasValue) return merged;
return TaskbarTemperatureSnapshot.Merge(
merged, new TaskbarTemperatureSnapshot(null, null, _storageReader.Read()));
}
public void SetEnhancedMode(bool enabled) => _enhanced = enabled;
private TaskbarTemperatureSnapshot ReadLibreHardwareMonitor()
{
try
{
if (!_opened)
{
_computer.Open();
_opened = true;
}
double? cpu = null;
double? gpu = null;
double? disk = null;
var gpuDevices = new List<string>();
var diskDevices = new List<string>();
foreach (IHardware hardware in _computer.Hardware)
{
ReadHardware(hardware, ref cpu, ref gpu, ref disk, gpuDevices, diskDevices);
}
return new TaskbarTemperatureSnapshot(cpu, gpu, disk, gpuDevices, diskDevices);
}
catch
{
return TaskbarTemperatureSnapshot.Empty;
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
try
{
if (_opened) _computer.Close();
}
catch
{
// Closing an unavailable hardware provider should not affect app shutdown.
}
_storageReader.Dispose();
_helperClient.Dispose();
}
private static void ReadHardware(
IHardware hardware,
ref double? cpu,
ref double? gpu,
ref double? disk,
List<string> gpuDevices,
List<string> diskDevices)
{
try
{
hardware.Update();
switch (hardware.HardwareType)
{
case HardwareType.GpuNvidia:
case HardwareType.GpuAmd:
case HardwareType.GpuIntel:
AddDeviceName(gpuDevices, hardware.Name);
break;
case HardwareType.Storage:
AddDeviceName(diskDevices, hardware.Name);
break;
}
foreach (ISensor sensor in hardware.Sensors)
{
if (sensor.SensorType != SensorType.Temperature || !IsValid(sensor.Value)) continue;
switch (hardware.HardwareType)
{
case HardwareType.Cpu:
cpu = Highest(cpu, sensor.Value!.Value);
break;
case HardwareType.GpuNvidia:
case HardwareType.GpuAmd:
case HardwareType.GpuIntel:
gpu = Highest(gpu, sensor.Value!.Value);
break;
case HardwareType.Storage:
disk = Highest(disk, sensor.Value!.Value);
break;
}
}
foreach (IHardware subHardware in hardware.SubHardware)
{
ReadHardware(subHardware, ref cpu, ref gpu, ref disk, gpuDevices, diskDevices);
}
}
catch
{
// Individual sensor providers may fail while the device is sleeping or removed.
}
}
private static bool IsValid(float? value) =>
value.HasValue && float.IsFinite(value.Value) && value.Value >= 0 && value.Value <= 150;
private static double Highest(double? current, float value) =>
!current.HasValue ? value : Math.Max(current.Value, value);
private static void AddDeviceName(List<string> names, string? name)
{
string normalized = name?.Trim() ?? string.Empty;
if (normalized.Length == 0 || names.Contains(normalized, StringComparer.OrdinalIgnoreCase)) return;
names.Add(normalized);
}
}