WMIProvider/WMIProvider.cs
changeset 223 39f73ac8c2f4
child 224 be9534663a55
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/WMIProvider/WMIProvider.cs	Sat Oct 16 13:29:06 2010 +0000
     1.3 @@ -0,0 +1,100 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Management.Instrumentation;
     1.7 +using OpenHardwareMonitor.Hardware;
     1.8 +
     1.9 +[assembly: Instrumented("root/OpenHardwareMonitor")]
    1.10 +
    1.11 +[System.ComponentModel.RunInstaller(true)]
    1.12 +public class InstanceInstaller : DefaultManagementProjectInstaller { }
    1.13 +
    1.14 +namespace OpenHardwareMonitor.WMIProvider {
    1.15 +  public class WmiProvider : IDisposable {
    1.16 +    private List<IWmiClass> _activeInstances;
    1.17 +
    1.18 +    public WmiProvider(IComputer computer) {
    1.19 +      _activeInstances = new List<IWmiClass>();
    1.20 +
    1.21 +      foreach (IHardware hardware in computer.Hardware)
    1.22 +        ComputerHardwareAdded(hardware);
    1.23 +
    1.24 +      computer.HardwareAdded += ComputerHardwareAdded;
    1.25 +      computer.HardwareRemoved += ComputerHardwareRemoved;
    1.26 +    }
    1.27 +
    1.28 +    public void Update() {
    1.29 +      foreach (IWmiClass instance in _activeInstances)
    1.30 +        instance.Update();
    1.31 +    }
    1.32 +
    1.33 +    #region Eventhandlers
    1.34 +    
    1.35 +    private void ComputerHardwareAdded(IHardware hardware) {
    1.36 +      if (!Exists(hardware.Identifier.ToString())) {
    1.37 +        foreach (ISensor sensor in hardware.Sensors)
    1.38 +          HardwareSensorAdded(sensor);
    1.39 +
    1.40 +        hardware.SensorAdded += HardwareSensorAdded;
    1.41 +        hardware.SensorRemoved += HardwareSensorRemoved;
    1.42 +
    1.43 +        Hardware hw = new Hardware(hardware);
    1.44 +        _activeInstances.Add(hw);
    1.45 +
    1.46 +        Instrumentation.Publish(hw);
    1.47 +      }
    1.48 +
    1.49 +      foreach (IHardware subHardware in hardware.SubHardware)
    1.50 +        ComputerHardwareAdded(subHardware);
    1.51 +    }
    1.52 +
    1.53 +    private void HardwareSensorAdded(ISensor data) {
    1.54 +      Sensor sensor = new Sensor(data);
    1.55 +      _activeInstances.Add(sensor);
    1.56 +
    1.57 +      Instrumentation.Publish(sensor);
    1.58 +    }
    1.59 +
    1.60 +    private void ComputerHardwareRemoved(IHardware hardware) {
    1.61 +      hardware.SensorAdded -= HardwareSensorAdded;
    1.62 +      hardware.SensorRemoved -= HardwareSensorRemoved;
    1.63 +      
    1.64 +      foreach (ISensor sensor in hardware.Sensors) 
    1.65 +        HardwareSensorRemoved(sensor);
    1.66 +      
    1.67 +      foreach (IHardware subHardware in hardware.SubHardware)
    1.68 +        ComputerHardwareRemoved(subHardware);
    1.69 +
    1.70 +      RevokeInstance(hardware.Identifier.ToString());
    1.71 +    }
    1.72 +
    1.73 +    private void HardwareSensorRemoved(ISensor sensor) {
    1.74 +      RevokeInstance(sensor.Identifier.ToString());
    1.75 +    }
    1.76 +
    1.77 +    #endregion
    1.78 +
    1.79 +    #region Helpers
    1.80 +    
    1.81 +    private bool Exists(string identifier) {
    1.82 +      return _activeInstances.Exists(h => h.Identifier == identifier);
    1.83 +    }
    1.84 +
    1.85 +    private void RevokeInstance(string identifier) {
    1.86 +      int instanceIndex = _activeInstances.FindIndex(
    1.87 +        item => item.Identifier == identifier.ToString()
    1.88 +      );
    1.89 +
    1.90 +      Instrumentation.Revoke(_activeInstances[instanceIndex]);
    1.91 +
    1.92 +      _activeInstances.RemoveAt(instanceIndex);
    1.93 +    }
    1.94 +
    1.95 +    #endregion
    1.96 +
    1.97 +    public void Dispose() {
    1.98 +      foreach (IWmiClass instance in _activeInstances)
    1.99 +        Instrumentation.Revoke(instance);
   1.100 +      _activeInstances = null;
   1.101 +    }
   1.102 +  }
   1.103 +}