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