Wmi/WmiProvider.cs
author paulwerelds
Sat, 16 Oct 2010 18:24:20 +0000
changeset 225 04dbd92d1116
parent 224 WMIProvider/WMIProvider.cs@be9534663a55
child 227 97757a798918
permissions -rw-r--r--
More refactoring.
     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 using OpenHardwareMonitor.Wmi;
    43 
    44 [assembly: Instrumented("root/OpenHardwareMonitor")]
    45 
    46 [System.ComponentModel.RunInstaller(true)]
    47 public class InstanceInstaller : DefaultManagementProjectInstaller { }
    48 
    49 namespace OpenHardwareMonitor.Wmi {
    50   /// <summary>
    51   /// The WMI Provider.
    52   /// This class is not exposed to WMI itself.
    53   /// </summary>
    54   public class WmiProvider : IDisposable {
    55     private List<IWmiObject> activeInstances;
    56 
    57     public WmiProvider(IComputer computer) {
    58       activeInstances = new List<IWmiObject>();
    59 
    60       foreach (IHardware hardware in computer.Hardware)
    61         ComputerHardwareAdded(hardware);
    62 
    63       computer.HardwareAdded += ComputerHardwareAdded;
    64       computer.HardwareRemoved += ComputerHardwareRemoved;
    65     }
    66 
    67     public void Update() {
    68       foreach (IWmiObject instance in activeInstances)
    69         instance.Update();
    70     }
    71 
    72     #region Eventhandlers
    73     
    74     private void ComputerHardwareAdded(IHardware hardware) {
    75       if (!Exists(hardware.Identifier.ToString())) {
    76         foreach (ISensor sensor in hardware.Sensors)
    77           HardwareSensorAdded(sensor);
    78 
    79         hardware.SensorAdded += HardwareSensorAdded;
    80         hardware.SensorRemoved += HardwareSensorRemoved;
    81 
    82         Hardware hw = new Hardware(hardware);
    83         activeInstances.Add(hw);
    84 
    85         Instrumentation.Publish(hw);
    86       }
    87 
    88       foreach (IHardware subHardware in hardware.SubHardware)
    89         ComputerHardwareAdded(subHardware);
    90     }
    91 
    92     private void HardwareSensorAdded(ISensor data) {
    93       Sensor sensor = new Sensor(data);
    94       activeInstances.Add(sensor);
    95 
    96       Instrumentation.Publish(sensor);
    97     }
    98 
    99     private void ComputerHardwareRemoved(IHardware hardware) {
   100       hardware.SensorAdded -= HardwareSensorAdded;
   101       hardware.SensorRemoved -= HardwareSensorRemoved;
   102       
   103       foreach (ISensor sensor in hardware.Sensors) 
   104         HardwareSensorRemoved(sensor);
   105       
   106       foreach (IHardware subHardware in hardware.SubHardware)
   107         ComputerHardwareRemoved(subHardware);
   108 
   109       RevokeInstance(hardware.Identifier.ToString());
   110     }
   111 
   112     private void HardwareSensorRemoved(ISensor sensor) {
   113       RevokeInstance(sensor.Identifier.ToString());
   114     }
   115 
   116     #endregion
   117 
   118     #region Helpers
   119     
   120     private bool Exists(string identifier) {
   121       return activeInstances.Exists(h => h.Identifier == identifier);
   122     }
   123 
   124     private void RevokeInstance(string identifier) {
   125       int instanceIndex = activeInstances.FindIndex(
   126         item => item.Identifier == identifier.ToString()
   127       );
   128 
   129       Instrumentation.Revoke(activeInstances[instanceIndex]);
   130 
   131       activeInstances.RemoveAt(instanceIndex);
   132     }
   133 
   134     #endregion
   135 
   136     public void Dispose() {
   137       foreach (IWmiObject instance in activeInstances)
   138         Instrumentation.Revoke(instance);
   139       activeInstances = null;
   140     }
   141   }
   142 }