WMIProvider/WMIProvider.cs
author paulwerelds
Sat, 16 Oct 2010 13:29:06 +0000
changeset 223 39f73ac8c2f4
child 224 be9534663a55
permissions -rw-r--r--
Added a WMI provider, documentation to follow.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Management.Instrumentation;
     4 using OpenHardwareMonitor.Hardware;
     5 
     6 [assembly: Instrumented("root/OpenHardwareMonitor")]
     7 
     8 [System.ComponentModel.RunInstaller(true)]
     9 public class InstanceInstaller : DefaultManagementProjectInstaller { }
    10 
    11 namespace OpenHardwareMonitor.WMIProvider {
    12   public class WmiProvider : IDisposable {
    13     private List<IWmiClass> _activeInstances;
    14 
    15     public WmiProvider(IComputer computer) {
    16       _activeInstances = new List<IWmiClass>();
    17 
    18       foreach (IHardware hardware in computer.Hardware)
    19         ComputerHardwareAdded(hardware);
    20 
    21       computer.HardwareAdded += ComputerHardwareAdded;
    22       computer.HardwareRemoved += ComputerHardwareRemoved;
    23     }
    24 
    25     public void Update() {
    26       foreach (IWmiClass instance in _activeInstances)
    27         instance.Update();
    28     }
    29 
    30     #region Eventhandlers
    31     
    32     private void ComputerHardwareAdded(IHardware hardware) {
    33       if (!Exists(hardware.Identifier.ToString())) {
    34         foreach (ISensor sensor in hardware.Sensors)
    35           HardwareSensorAdded(sensor);
    36 
    37         hardware.SensorAdded += HardwareSensorAdded;
    38         hardware.SensorRemoved += HardwareSensorRemoved;
    39 
    40         Hardware hw = new Hardware(hardware);
    41         _activeInstances.Add(hw);
    42 
    43         Instrumentation.Publish(hw);
    44       }
    45 
    46       foreach (IHardware subHardware in hardware.SubHardware)
    47         ComputerHardwareAdded(subHardware);
    48     }
    49 
    50     private void HardwareSensorAdded(ISensor data) {
    51       Sensor sensor = new Sensor(data);
    52       _activeInstances.Add(sensor);
    53 
    54       Instrumentation.Publish(sensor);
    55     }
    56 
    57     private void ComputerHardwareRemoved(IHardware hardware) {
    58       hardware.SensorAdded -= HardwareSensorAdded;
    59       hardware.SensorRemoved -= HardwareSensorRemoved;
    60       
    61       foreach (ISensor sensor in hardware.Sensors) 
    62         HardwareSensorRemoved(sensor);
    63       
    64       foreach (IHardware subHardware in hardware.SubHardware)
    65         ComputerHardwareRemoved(subHardware);
    66 
    67       RevokeInstance(hardware.Identifier.ToString());
    68     }
    69 
    70     private void HardwareSensorRemoved(ISensor sensor) {
    71       RevokeInstance(sensor.Identifier.ToString());
    72     }
    73 
    74     #endregion
    75 
    76     #region Helpers
    77     
    78     private bool Exists(string identifier) {
    79       return _activeInstances.Exists(h => h.Identifier == identifier);
    80     }
    81 
    82     private void RevokeInstance(string identifier) {
    83       int instanceIndex = _activeInstances.FindIndex(
    84         item => item.Identifier == identifier.ToString()
    85       );
    86 
    87       Instrumentation.Revoke(_activeInstances[instanceIndex]);
    88 
    89       _activeInstances.RemoveAt(instanceIndex);
    90     }
    91 
    92     #endregion
    93 
    94     public void Dispose() {
    95       foreach (IWmiClass instance in _activeInstances)
    96         Instrumentation.Revoke(instance);
    97       _activeInstances = null;
    98     }
    99   }
   100 }