Added a WMI provider, documentation to follow.
2 using System.Collections.Generic;
3 using System.Management.Instrumentation;
4 using OpenHardwareMonitor.Hardware;
6 [assembly: Instrumented("root/OpenHardwareMonitor")]
8 [System.ComponentModel.RunInstaller(true)]
9 public class InstanceInstaller : DefaultManagementProjectInstaller { }
11 namespace OpenHardwareMonitor.WMIProvider {
12 public class WmiProvider : IDisposable {
13 private List<IWmiClass> _activeInstances;
15 public WmiProvider(IComputer computer) {
16 _activeInstances = new List<IWmiClass>();
18 foreach (IHardware hardware in computer.Hardware)
19 ComputerHardwareAdded(hardware);
21 computer.HardwareAdded += ComputerHardwareAdded;
22 computer.HardwareRemoved += ComputerHardwareRemoved;
25 public void Update() {
26 foreach (IWmiClass instance in _activeInstances)
32 private void ComputerHardwareAdded(IHardware hardware) {
33 if (!Exists(hardware.Identifier.ToString())) {
34 foreach (ISensor sensor in hardware.Sensors)
35 HardwareSensorAdded(sensor);
37 hardware.SensorAdded += HardwareSensorAdded;
38 hardware.SensorRemoved += HardwareSensorRemoved;
40 Hardware hw = new Hardware(hardware);
41 _activeInstances.Add(hw);
43 Instrumentation.Publish(hw);
46 foreach (IHardware subHardware in hardware.SubHardware)
47 ComputerHardwareAdded(subHardware);
50 private void HardwareSensorAdded(ISensor data) {
51 Sensor sensor = new Sensor(data);
52 _activeInstances.Add(sensor);
54 Instrumentation.Publish(sensor);
57 private void ComputerHardwareRemoved(IHardware hardware) {
58 hardware.SensorAdded -= HardwareSensorAdded;
59 hardware.SensorRemoved -= HardwareSensorRemoved;
61 foreach (ISensor sensor in hardware.Sensors)
62 HardwareSensorRemoved(sensor);
64 foreach (IHardware subHardware in hardware.SubHardware)
65 ComputerHardwareRemoved(subHardware);
67 RevokeInstance(hardware.Identifier.ToString());
70 private void HardwareSensorRemoved(ISensor sensor) {
71 RevokeInstance(sensor.Identifier.ToString());
78 private bool Exists(string identifier) {
79 return _activeInstances.Exists(h => h.Identifier == identifier);
82 private void RevokeInstance(string identifier) {
83 int instanceIndex = _activeInstances.FindIndex(
84 item => item.Identifier == identifier.ToString()
87 Instrumentation.Revoke(_activeInstances[instanceIndex]);
89 _activeInstances.RemoveAt(instanceIndex);
94 public void Dispose() {
95 foreach (IWmiClass instance in _activeInstances)
96 Instrumentation.Revoke(instance);
97 _activeInstances = null;