Wmi/WmiProvider.cs
author paulwerelds
Sat, 16 Oct 2010 18:26:47 +0000
changeset 226 1e8e0c82c1f5
parent 224 be9534663a55
child 227 97757a798918
permissions -rw-r--r--
AnkhSVN fails, TortoiseSVN to the rescue..
paulwerelds@224
     1
/*
paulwerelds@224
     2
  
paulwerelds@224
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
paulwerelds@224
     4
paulwerelds@224
     5
  The contents of this file are subject to the Mozilla Public License Version
paulwerelds@224
     6
  1.1 (the "License"); you may not use this file except in compliance with
paulwerelds@224
     7
  the License. You may obtain a copy of the License at
paulwerelds@224
     8
 
paulwerelds@224
     9
  http://www.mozilla.org/MPL/
paulwerelds@224
    10
paulwerelds@224
    11
  Software distributed under the License is distributed on an "AS IS" basis,
paulwerelds@224
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
paulwerelds@224
    13
  for the specific language governing rights and limitations under the License.
paulwerelds@224
    14
paulwerelds@224
    15
  The Original Code is the Open Hardware Monitor code.
paulwerelds@224
    16
paulwerelds@224
    17
  The Initial Developer of the Original Code is 
paulwerelds@224
    18
  Paul Werelds <paul@werelds.net>.
paulwerelds@224
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
paulwerelds@224
    20
  the Initial Developer. All Rights Reserved.
paulwerelds@224
    21
paulwerelds@224
    22
  Contributor(s):
paulwerelds@224
    23
paulwerelds@224
    24
  Alternatively, the contents of this file may be used under the terms of
paulwerelds@224
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
paulwerelds@224
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
paulwerelds@224
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
paulwerelds@224
    28
  of those above. If you wish to allow use of your version of this file only
paulwerelds@224
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
paulwerelds@224
    30
  use your version of this file under the terms of the MPL, indicate your
paulwerelds@224
    31
  decision by deleting the provisions above and replace them with the notice
paulwerelds@224
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
paulwerelds@224
    33
  the provisions above, a recipient may use your version of this file under
paulwerelds@224
    34
  the terms of any one of the MPL, the GPL or the LGPL.
paulwerelds@224
    35
 
paulwerelds@224
    36
*/
paulwerelds@224
    37
paulwerelds@223
    38
using System;
paulwerelds@223
    39
using System.Collections.Generic;
paulwerelds@223
    40
using System.Management.Instrumentation;
paulwerelds@223
    41
using OpenHardwareMonitor.Hardware;
paulwerelds@225
    42
using OpenHardwareMonitor.Wmi;
paulwerelds@223
    43
paulwerelds@223
    44
[assembly: Instrumented("root/OpenHardwareMonitor")]
paulwerelds@223
    45
paulwerelds@223
    46
[System.ComponentModel.RunInstaller(true)]
paulwerelds@223
    47
public class InstanceInstaller : DefaultManagementProjectInstaller { }
paulwerelds@223
    48
paulwerelds@225
    49
namespace OpenHardwareMonitor.Wmi {
paulwerelds@224
    50
  /// <summary>
paulwerelds@224
    51
  /// The WMI Provider.
paulwerelds@224
    52
  /// This class is not exposed to WMI itself.
paulwerelds@224
    53
  /// </summary>
paulwerelds@223
    54
  public class WmiProvider : IDisposable {
paulwerelds@225
    55
    private List<IWmiObject> activeInstances;
paulwerelds@223
    56
paulwerelds@223
    57
    public WmiProvider(IComputer computer) {
paulwerelds@225
    58
      activeInstances = new List<IWmiObject>();
paulwerelds@223
    59
paulwerelds@223
    60
      foreach (IHardware hardware in computer.Hardware)
paulwerelds@223
    61
        ComputerHardwareAdded(hardware);
paulwerelds@223
    62
paulwerelds@223
    63
      computer.HardwareAdded += ComputerHardwareAdded;
paulwerelds@223
    64
      computer.HardwareRemoved += ComputerHardwareRemoved;
paulwerelds@223
    65
    }
paulwerelds@223
    66
paulwerelds@223
    67
    public void Update() {
paulwerelds@225
    68
      foreach (IWmiObject instance in activeInstances)
paulwerelds@223
    69
        instance.Update();
paulwerelds@223
    70
    }
paulwerelds@223
    71
paulwerelds@223
    72
    #region Eventhandlers
paulwerelds@223
    73
    
paulwerelds@223
    74
    private void ComputerHardwareAdded(IHardware hardware) {
paulwerelds@223
    75
      if (!Exists(hardware.Identifier.ToString())) {
paulwerelds@223
    76
        foreach (ISensor sensor in hardware.Sensors)
paulwerelds@223
    77
          HardwareSensorAdded(sensor);
paulwerelds@223
    78
paulwerelds@223
    79
        hardware.SensorAdded += HardwareSensorAdded;
paulwerelds@223
    80
        hardware.SensorRemoved += HardwareSensorRemoved;
paulwerelds@223
    81
paulwerelds@223
    82
        Hardware hw = new Hardware(hardware);
paulwerelds@225
    83
        activeInstances.Add(hw);
paulwerelds@223
    84
paulwerelds@223
    85
        Instrumentation.Publish(hw);
paulwerelds@223
    86
      }
paulwerelds@223
    87
paulwerelds@223
    88
      foreach (IHardware subHardware in hardware.SubHardware)
paulwerelds@223
    89
        ComputerHardwareAdded(subHardware);
paulwerelds@223
    90
    }
paulwerelds@223
    91
paulwerelds@223
    92
    private void HardwareSensorAdded(ISensor data) {
paulwerelds@223
    93
      Sensor sensor = new Sensor(data);
paulwerelds@225
    94
      activeInstances.Add(sensor);
paulwerelds@223
    95
paulwerelds@223
    96
      Instrumentation.Publish(sensor);
paulwerelds@223
    97
    }
paulwerelds@223
    98
paulwerelds@223
    99
    private void ComputerHardwareRemoved(IHardware hardware) {
paulwerelds@223
   100
      hardware.SensorAdded -= HardwareSensorAdded;
paulwerelds@223
   101
      hardware.SensorRemoved -= HardwareSensorRemoved;
paulwerelds@223
   102
      
paulwerelds@223
   103
      foreach (ISensor sensor in hardware.Sensors) 
paulwerelds@223
   104
        HardwareSensorRemoved(sensor);
paulwerelds@223
   105
      
paulwerelds@223
   106
      foreach (IHardware subHardware in hardware.SubHardware)
paulwerelds@223
   107
        ComputerHardwareRemoved(subHardware);
paulwerelds@223
   108
paulwerelds@223
   109
      RevokeInstance(hardware.Identifier.ToString());
paulwerelds@223
   110
    }
paulwerelds@223
   111
paulwerelds@223
   112
    private void HardwareSensorRemoved(ISensor sensor) {
paulwerelds@223
   113
      RevokeInstance(sensor.Identifier.ToString());
paulwerelds@223
   114
    }
paulwerelds@223
   115
paulwerelds@223
   116
    #endregion
paulwerelds@223
   117
paulwerelds@223
   118
    #region Helpers
paulwerelds@223
   119
    
paulwerelds@223
   120
    private bool Exists(string identifier) {
paulwerelds@225
   121
      return activeInstances.Exists(h => h.Identifier == identifier);
paulwerelds@223
   122
    }
paulwerelds@223
   123
paulwerelds@223
   124
    private void RevokeInstance(string identifier) {
paulwerelds@225
   125
      int instanceIndex = activeInstances.FindIndex(
paulwerelds@223
   126
        item => item.Identifier == identifier.ToString()
paulwerelds@223
   127
      );
paulwerelds@223
   128
paulwerelds@225
   129
      Instrumentation.Revoke(activeInstances[instanceIndex]);
paulwerelds@223
   130
paulwerelds@225
   131
      activeInstances.RemoveAt(instanceIndex);
paulwerelds@223
   132
    }
paulwerelds@223
   133
paulwerelds@223
   134
    #endregion
paulwerelds@223
   135
paulwerelds@223
   136
    public void Dispose() {
paulwerelds@225
   137
      foreach (IWmiObject instance in activeInstances)
paulwerelds@223
   138
        Instrumentation.Revoke(instance);
paulwerelds@225
   139
      activeInstances = null;
paulwerelds@223
   140
    }
paulwerelds@223
   141
  }
paulwerelds@223
   142
}