WMI/WmiProvider.cs
changeset 229 940d86b60be7
parent 227 97757a798918
child 240 631ab73d1ae1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/WMI/WmiProvider.cs	Sun Oct 17 16:10:08 2010 +0000
     1.3 @@ -0,0 +1,141 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Paul Werelds <paul@werelds.net>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Management.Instrumentation;
    1.44 +using OpenHardwareMonitor.Hardware;
    1.45 +
    1.46 +[assembly: Instrumented("root/OpenHardwareMonitor")]
    1.47 +
    1.48 +[System.ComponentModel.RunInstaller(true)]
    1.49 +public class InstanceInstaller : DefaultManagementProjectInstaller { }
    1.50 +
    1.51 +namespace OpenHardwareMonitor.WMI {
    1.52 +  /// <summary>
    1.53 +  /// The WMI Provider.
    1.54 +  /// This class is not exposed to WMI itself.
    1.55 +  /// </summary>
    1.56 +  public class WmiProvider : IDisposable {
    1.57 +    private List<IWmiObject> activeInstances;
    1.58 +
    1.59 +    public WmiProvider(IComputer computer) {
    1.60 +      activeInstances = new List<IWmiObject>();
    1.61 +
    1.62 +      foreach (IHardware hardware in computer.Hardware)
    1.63 +        ComputerHardwareAdded(hardware);
    1.64 +
    1.65 +      computer.HardwareAdded += ComputerHardwareAdded;
    1.66 +      computer.HardwareRemoved += ComputerHardwareRemoved;
    1.67 +    }
    1.68 +
    1.69 +    public void Update() {
    1.70 +      foreach (IWmiObject instance in activeInstances)
    1.71 +        instance.Update();
    1.72 +    }
    1.73 +
    1.74 +    #region Eventhandlers
    1.75 +    
    1.76 +    private void ComputerHardwareAdded(IHardware hardware) {
    1.77 +      if (!Exists(hardware.Identifier.ToString())) {
    1.78 +        foreach (ISensor sensor in hardware.Sensors)
    1.79 +          HardwareSensorAdded(sensor);
    1.80 +
    1.81 +        hardware.SensorAdded += HardwareSensorAdded;
    1.82 +        hardware.SensorRemoved += HardwareSensorRemoved;
    1.83 +
    1.84 +        Hardware hw = new Hardware(hardware);
    1.85 +        activeInstances.Add(hw);
    1.86 +
    1.87 +        Instrumentation.Publish(hw);
    1.88 +      }
    1.89 +
    1.90 +      foreach (IHardware subHardware in hardware.SubHardware)
    1.91 +        ComputerHardwareAdded(subHardware);
    1.92 +    }
    1.93 +
    1.94 +    private void HardwareSensorAdded(ISensor data) {
    1.95 +      Sensor sensor = new Sensor(data);
    1.96 +      activeInstances.Add(sensor);
    1.97 +
    1.98 +      Instrumentation.Publish(sensor);
    1.99 +    }
   1.100 +
   1.101 +    private void ComputerHardwareRemoved(IHardware hardware) {
   1.102 +      hardware.SensorAdded -= HardwareSensorAdded;
   1.103 +      hardware.SensorRemoved -= HardwareSensorRemoved;
   1.104 +      
   1.105 +      foreach (ISensor sensor in hardware.Sensors) 
   1.106 +        HardwareSensorRemoved(sensor);
   1.107 +      
   1.108 +      foreach (IHardware subHardware in hardware.SubHardware)
   1.109 +        ComputerHardwareRemoved(subHardware);
   1.110 +
   1.111 +      RevokeInstance(hardware.Identifier.ToString());
   1.112 +    }
   1.113 +
   1.114 +    private void HardwareSensorRemoved(ISensor sensor) {
   1.115 +      RevokeInstance(sensor.Identifier.ToString());
   1.116 +    }
   1.117 +
   1.118 +    #endregion
   1.119 +
   1.120 +    #region Helpers
   1.121 +    
   1.122 +    private bool Exists(string identifier) {
   1.123 +      return activeInstances.Exists(h => h.Identifier == identifier);
   1.124 +    }
   1.125 +
   1.126 +    private void RevokeInstance(string identifier) {
   1.127 +      int instanceIndex = activeInstances.FindIndex(
   1.128 +        item => item.Identifier == identifier.ToString()
   1.129 +      );
   1.130 +
   1.131 +      Instrumentation.Revoke(activeInstances[instanceIndex]);
   1.132 +
   1.133 +      activeInstances.RemoveAt(instanceIndex);
   1.134 +    }
   1.135 +
   1.136 +    #endregion
   1.137 +
   1.138 +    public void Dispose() {
   1.139 +      foreach (IWmiObject instance in activeInstances)
   1.140 +        Instrumentation.Revoke(instance);
   1.141 +      activeInstances = null;
   1.142 +    }
   1.143 +  }
   1.144 +}