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