Repository navigation
Expand file tree
/
Copy pathSensorMonitor.cs
More file actions
137 lines (121 loc) · 5.46 KB
/
Copy pathSensorMonitor.cs
File metadata and controls
137 lines (121 loc) · 5.46 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
using System.Collections.ObjectModel;
using LibreHardwareMonitor.Hardware;
namespace ResetExplode;
/// <summary>Encapsula o LibreHardwareMonitorLib e mantém a lista de sensores atualizada.</summary>
public sealed class SensorMonitor : IDisposable
{
private readonly Computer _computer;
private readonly Dictionary<string, SensorRow> _index = new();
private volatile bool _open;
public ObservableCollection<SensorRow> Rows { get; } = new();
public bool IsOpen => _open;
public SensorMonitor()
{
_computer = new Computer
{
IsCpuEnabled = true,
IsGpuEnabled = true,
IsMemoryEnabled = true,
IsMotherboardEnabled = true,
IsStorageEnabled = true,
IsNetworkEnabled = true,
IsControllerEnabled = true
};
}
/// <summary>Detecta o hardware (leva de 0,5 a alguns segundos) fora da thread da janela. Até terminar, Refresh não faz nada.</summary>
public Task OpenAsync() => Task.Run(() => { _computer.Open(); _open = true; });
public void Refresh()
{
if (!_open) return;
foreach (var hw in _computer.Hardware)
Walk(hw);
}
private void Walk(IHardware hw)
{
hw.Update();
foreach (var s in hw.Sensors)
{
string id = s.Identifier.ToString();
if (!_index.TryGetValue(id, out var row))
{
row = new SensorRow
{
Id = id,
Hardware = hw.Name,
Category = SensorRow.CategoryName(s.SensorType),
Name = s.Name,
Type = s.SensorType,
HardwareType = hw.HardwareType
};
_index[id] = row;
Rows.Add(row);
}
row.Update(s.Value, s.Min, s.Max);
}
foreach (var sub in hw.SubHardware)
Walk(sub);
}
/// <summary>Temperatura principal da CPU: Package (Intel) / Tctl-Tdie (AMD), senão a maior entre os núcleos.</summary>
public float? CpuTemperature
{
get
{
var temps = Rows.Where(r => r.HardwareType == HardwareType.Cpu
&& r.Type == SensorType.Temperature
&& r.RawValue is > 0).ToList();
var main = temps.FirstOrDefault(r => r.Name.Contains("Package") || r.Name.Contains("Tctl") || r.Name.Contains("Tdie"));
return main?.RawValue ?? temps.Max(r => r.RawValue);
}
}
/// <summary>Carga total da CPU em %.</summary>
public float? CpuLoad => Rows.FirstOrDefault(r => r.HardwareType == HardwareType.Cpu
&& r.Type == SensorType.Load
&& r.Name == "CPU Total")?.RawValue;
/// <summary>Consumo do pacote da CPU em W.</summary>
public float? CpuPower => Rows.FirstOrDefault(r => r.HardwareType == HardwareType.Cpu
&& r.Type == SensorType.Power
&& r.Name.Contains("Package"))?.RawValue;
/// <summary>
/// Driver PawnIO (necessário para temperatura/consumo da CPU via MSR/SMN).
/// A biblioteca procura exatamente esta chave de desinstalação.
/// </summary>
public static bool IsPawnIOInstalled
{
get
{
using var hklm = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine,
Microsoft.Win32.RegistryView.Registry64);
using var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PawnIO");
return key != null;
}
}
public static bool IsElevated =>
new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent())
.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
public enum CpuSensorProblem { None, NotElevated, PawnIOMissing, ReadFailed }
/// <summary>Por que a temperatura/consumo da CPU não aparecem (o motivo mais provável primeiro).</summary>
public CpuSensorProblem DiagnoseCpuSensors()
{
if (CpuTemperature != null) return CpuSensorProblem.None;
if (!IsElevated) return CpuSensorProblem.NotElevated;
if (!IsPawnIOInstalled) return CpuSensorProblem.PawnIOMissing;
return CpuSensorProblem.ReadFailed;
}
public static string Explain(CpuSensorProblem p) => p switch
{
CpuSensorProblem.NotElevated =>
"O RESET EXPLODE não está rodando como administrador, e o Windows só libera os sensores internos da CPU " +
"nesse modo. Feche e abra de novo aceitando o pedido de administrador.",
CpuSensorProblem.PawnIOMissing =>
"Falta o driver PawnIO, que lê a temperatura e o consumo da CPU. Ele é gratuito, assinado e usado também " +
"pelo LibreHardwareMonitor e pelo FanControl. Depois de instalar, feche e abra o RESET EXPLODE.",
CpuSensorProblem.ReadFailed =>
"O PawnIO está instalado, mas a leitura falhou. Se acabou de instalar, reinicie o PC. Se continuar, " +
"confira se o antivírus não bloqueou o driver ou se a CPU é muito antiga/recente para a biblioteca de sensores.",
_ => ""
};
public void Dispose()
{
if (_open) _computer.Close();
}
}