WMI/WmiProvider.cs
author moel.mich
Sun, 28 Oct 2012 14:06:50 +0000
changeset 386 7094d5dd924b
parent 240 631ab73d1ae1
child 390 88d699a65cc2
permissions -rw-r--r--
Changed the source formatting of the HttpServer class to match the rest of the project.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2010 Paul Werelds <paul@werelds.net>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Management.Instrumentation;
    14 using OpenHardwareMonitor.Hardware;
    15 
    16 [assembly: Instrumented("root/OpenHardwareMonitor")]
    17 
    18 [System.ComponentModel.RunInstaller(true)]
    19 public class InstanceInstaller : DefaultManagementProjectInstaller { }
    20 
    21 namespace OpenHardwareMonitor.WMI {
    22   /// <summary>
    23   /// The WMI Provider.
    24   /// This class is not exposed to WMI itself.
    25   /// </summary>
    26   public class WmiProvider : IDisposable {
    27     private List<IWmiObject> activeInstances;
    28 
    29     public WmiProvider(IComputer computer) {
    30       activeInstances = new List<IWmiObject>();
    31 
    32       foreach (IHardware hardware in computer.Hardware)
    33         ComputerHardwareAdded(hardware);
    34 
    35       computer.HardwareAdded += ComputerHardwareAdded;
    36       computer.HardwareRemoved += ComputerHardwareRemoved;
    37     }
    38 
    39     public void Update() {
    40       foreach (IWmiObject instance in activeInstances)
    41         instance.Update();
    42     }
    43 
    44     #region Eventhandlers
    45     
    46     private void ComputerHardwareAdded(IHardware hardware) {
    47       if (!Exists(hardware.Identifier.ToString())) {
    48         foreach (ISensor sensor in hardware.Sensors)
    49           HardwareSensorAdded(sensor);
    50 
    51         hardware.SensorAdded += HardwareSensorAdded;
    52         hardware.SensorRemoved += HardwareSensorRemoved;
    53 
    54         Hardware hw = new Hardware(hardware);
    55         activeInstances.Add(hw);
    56 
    57         try {
    58           Instrumentation.Publish(hw);
    59         } catch (Exception) { }
    60       }
    61 
    62       foreach (IHardware subHardware in hardware.SubHardware)
    63         ComputerHardwareAdded(subHardware);
    64     }
    65 
    66     private void HardwareSensorAdded(ISensor data) {
    67       Sensor sensor = new Sensor(data);
    68       activeInstances.Add(sensor);
    69 
    70       try {
    71         Instrumentation.Publish(sensor);
    72       } catch (Exception) { }
    73     }
    74 
    75     private void ComputerHardwareRemoved(IHardware hardware) {
    76       hardware.SensorAdded -= HardwareSensorAdded;
    77       hardware.SensorRemoved -= HardwareSensorRemoved;
    78       
    79       foreach (ISensor sensor in hardware.Sensors) 
    80         HardwareSensorRemoved(sensor);
    81       
    82       foreach (IHardware subHardware in hardware.SubHardware)
    83         ComputerHardwareRemoved(subHardware);
    84 
    85       RevokeInstance(hardware.Identifier.ToString());
    86     }
    87 
    88     private void HardwareSensorRemoved(ISensor sensor) {
    89       RevokeInstance(sensor.Identifier.ToString());
    90     }
    91 
    92     #endregion
    93 
    94     #region Helpers
    95     
    96     private bool Exists(string identifier) {
    97       return activeInstances.Exists(h => h.Identifier == identifier);
    98     }
    99 
   100     private void RevokeInstance(string identifier) {
   101       int instanceIndex = activeInstances.FindIndex(
   102         item => item.Identifier == identifier.ToString()
   103       );
   104 
   105       try {
   106         Instrumentation.Revoke(activeInstances[instanceIndex]);
   107       } catch (Exception) { }
   108 
   109       activeInstances.RemoveAt(instanceIndex);
   110     }
   111 
   112     #endregion
   113 
   114     public void Dispose() {
   115       foreach (IWmiObject instance in activeInstances) {
   116         try {
   117           Instrumentation.Revoke(instance);
   118         } catch (Exception) { }
   119       }
   120       activeInstances = null;
   121     }
   122   }
   123 }