WMI/WmiProvider.cs
author StephaneLenclud
Thu, 18 Apr 2013 23:25:10 +0200
changeset 402 ded1323b61ee
parent 344 3145aadca3d2
permissions -rw-r--r--
Front View plug-in does not init if no sensor added.
Fixing some format to make strings shorter.
Now trying to start SoundGraphAccess.exe process from same directory.
Packed mode now can display three sensors along with the current time.
     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 	Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     9 
    10 */
    11 
    12 using System;
    13 using System.Collections.Generic;
    14 using System.Management.Instrumentation;
    15 using OpenHardwareMonitor.Hardware;
    16 
    17 [assembly: Instrumented("root/OpenHardwareMonitor")]
    18 
    19 [System.ComponentModel.RunInstaller(true)]
    20 public class InstanceInstaller : DefaultManagementProjectInstaller { }
    21 
    22 namespace OpenHardwareMonitor.WMI {
    23   /// <summary>
    24   /// The WMI Provider.
    25   /// This class is not exposed to WMI itself.
    26   /// </summary>
    27   public class WmiProvider : IDisposable {
    28     private List<IWmiObject> activeInstances;
    29 
    30     public WmiProvider(IComputer computer) {
    31       activeInstances = new List<IWmiObject>();
    32 
    33       foreach (IHardware hardware in computer.Hardware)
    34         ComputerHardwareAdded(hardware);
    35 
    36       computer.HardwareAdded += ComputerHardwareAdded;
    37       computer.HardwareRemoved += ComputerHardwareRemoved;
    38     }
    39 
    40     public void Update() {
    41       foreach (IWmiObject instance in activeInstances)
    42         instance.Update();
    43     }
    44 
    45     #region Eventhandlers
    46     
    47     private void ComputerHardwareAdded(IHardware hardware) {
    48       if (!Exists(hardware.Identifier.ToString())) {
    49         foreach (ISensor sensor in hardware.Sensors)
    50           HardwareSensorAdded(sensor);
    51 
    52         hardware.SensorAdded += HardwareSensorAdded;
    53         hardware.SensorRemoved += HardwareSensorRemoved;
    54 
    55         Hardware hw = new Hardware(hardware);
    56         activeInstances.Add(hw);
    57 
    58         try {
    59           Instrumentation.Publish(hw);
    60         } catch (Exception) { }
    61       }
    62 
    63       foreach (IHardware subHardware in hardware.SubHardware)
    64         ComputerHardwareAdded(subHardware);
    65     }
    66 
    67     private void HardwareSensorAdded(ISensor data) {
    68       Sensor sensor = new Sensor(data);
    69       activeInstances.Add(sensor);
    70 
    71       try {
    72         Instrumentation.Publish(sensor);
    73       } catch (Exception) { }
    74     }
    75 
    76     private void ComputerHardwareRemoved(IHardware hardware) {
    77       hardware.SensorAdded -= HardwareSensorAdded;
    78       hardware.SensorRemoved -= HardwareSensorRemoved;
    79       
    80       foreach (ISensor sensor in hardware.Sensors) 
    81         HardwareSensorRemoved(sensor);
    82       
    83       foreach (IHardware subHardware in hardware.SubHardware)
    84         ComputerHardwareRemoved(subHardware);
    85 
    86       RevokeInstance(hardware.Identifier.ToString());
    87     }
    88 
    89     private void HardwareSensorRemoved(ISensor sensor) {
    90       RevokeInstance(sensor.Identifier.ToString());
    91     }
    92 
    93     #endregion
    94 
    95     #region Helpers
    96     
    97     private bool Exists(string identifier) {
    98       return activeInstances.Exists(h => h.Identifier == identifier);
    99     }
   100 
   101     private void RevokeInstance(string identifier) {
   102       int instanceIndex = activeInstances.FindIndex(
   103         item => item.Identifier == identifier.ToString()
   104       );
   105 
   106       if (instanceIndex == -1)
   107         return;
   108 
   109       try {
   110         Instrumentation.Revoke(activeInstances[instanceIndex]);
   111       } catch (Exception) { }
   112 
   113       activeInstances.RemoveAt(instanceIndex);
   114     }
   115 
   116     #endregion
   117 
   118     public void Dispose() {
   119       foreach (IWmiObject instance in activeInstances) {
   120         try {
   121           Instrumentation.Revoke(instance);
   122         } catch (Exception) { }
   123       }
   124       activeInstances = null;
   125     }
   126   }
   127 }