author | moel.mich |
Sun, 22 Jul 2012 18:07:11 +0000 | |
changeset 369 | 5077ed7ddca8 |
parent 240 | 631ab73d1ae1 |
child 390 | 88d699a65cc2 |
permissions | -rw-r--r-- |
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@344 | 8 |
|
paulwerelds@224 | 9 |
*/ |
paulwerelds@224 | 10 |
|
paulwerelds@223 | 11 |
using System; |
paulwerelds@223 | 12 |
using System.Collections.Generic; |
paulwerelds@223 | 13 |
using System.Management.Instrumentation; |
paulwerelds@223 | 14 |
using OpenHardwareMonitor.Hardware; |
paulwerelds@223 | 15 |
|
paulwerelds@223 | 16 |
[assembly: Instrumented("root/OpenHardwareMonitor")] |
paulwerelds@223 | 17 |
|
paulwerelds@223 | 18 |
[System.ComponentModel.RunInstaller(true)] |
paulwerelds@223 | 19 |
public class InstanceInstaller : DefaultManagementProjectInstaller { } |
paulwerelds@223 | 20 |
|
paulwerelds@227 | 21 |
namespace OpenHardwareMonitor.WMI { |
paulwerelds@224 | 22 |
/// <summary> |
paulwerelds@224 | 23 |
/// The WMI Provider. |
paulwerelds@224 | 24 |
/// This class is not exposed to WMI itself. |
paulwerelds@224 | 25 |
/// </summary> |
paulwerelds@223 | 26 |
public class WmiProvider : IDisposable { |
paulwerelds@225 | 27 |
private List<IWmiObject> activeInstances; |
paulwerelds@223 | 28 |
|
paulwerelds@223 | 29 |
public WmiProvider(IComputer computer) { |
paulwerelds@225 | 30 |
activeInstances = new List<IWmiObject>(); |
paulwerelds@223 | 31 |
|
paulwerelds@223 | 32 |
foreach (IHardware hardware in computer.Hardware) |
paulwerelds@223 | 33 |
ComputerHardwareAdded(hardware); |
paulwerelds@223 | 34 |
|
paulwerelds@223 | 35 |
computer.HardwareAdded += ComputerHardwareAdded; |
paulwerelds@223 | 36 |
computer.HardwareRemoved += ComputerHardwareRemoved; |
paulwerelds@223 | 37 |
} |
paulwerelds@223 | 38 |
|
paulwerelds@223 | 39 |
public void Update() { |
paulwerelds@225 | 40 |
foreach (IWmiObject instance in activeInstances) |
paulwerelds@223 | 41 |
instance.Update(); |
paulwerelds@223 | 42 |
} |
paulwerelds@223 | 43 |
|
paulwerelds@223 | 44 |
#region Eventhandlers |
paulwerelds@223 | 45 |
|
paulwerelds@223 | 46 |
private void ComputerHardwareAdded(IHardware hardware) { |
paulwerelds@223 | 47 |
if (!Exists(hardware.Identifier.ToString())) { |
paulwerelds@223 | 48 |
foreach (ISensor sensor in hardware.Sensors) |
paulwerelds@223 | 49 |
HardwareSensorAdded(sensor); |
paulwerelds@223 | 50 |
|
paulwerelds@223 | 51 |
hardware.SensorAdded += HardwareSensorAdded; |
paulwerelds@223 | 52 |
hardware.SensorRemoved += HardwareSensorRemoved; |
paulwerelds@223 | 53 |
|
paulwerelds@223 | 54 |
Hardware hw = new Hardware(hardware); |
paulwerelds@225 | 55 |
activeInstances.Add(hw); |
paulwerelds@223 | 56 |
|
moel@240 | 57 |
try { |
moel@240 | 58 |
Instrumentation.Publish(hw); |
moel@240 | 59 |
} catch (Exception) { } |
paulwerelds@223 | 60 |
} |
paulwerelds@223 | 61 |
|
paulwerelds@223 | 62 |
foreach (IHardware subHardware in hardware.SubHardware) |
paulwerelds@223 | 63 |
ComputerHardwareAdded(subHardware); |
paulwerelds@223 | 64 |
} |
paulwerelds@223 | 65 |
|
paulwerelds@223 | 66 |
private void HardwareSensorAdded(ISensor data) { |
paulwerelds@223 | 67 |
Sensor sensor = new Sensor(data); |
paulwerelds@225 | 68 |
activeInstances.Add(sensor); |
paulwerelds@223 | 69 |
|
moel@240 | 70 |
try { |
moel@240 | 71 |
Instrumentation.Publish(sensor); |
moel@240 | 72 |
} catch (Exception) { } |
paulwerelds@223 | 73 |
} |
paulwerelds@223 | 74 |
|
paulwerelds@223 | 75 |
private void ComputerHardwareRemoved(IHardware hardware) { |
paulwerelds@223 | 76 |
hardware.SensorAdded -= HardwareSensorAdded; |
paulwerelds@223 | 77 |
hardware.SensorRemoved -= HardwareSensorRemoved; |
paulwerelds@223 | 78 |
|
paulwerelds@223 | 79 |
foreach (ISensor sensor in hardware.Sensors) |
paulwerelds@223 | 80 |
HardwareSensorRemoved(sensor); |
paulwerelds@223 | 81 |
|
paulwerelds@223 | 82 |
foreach (IHardware subHardware in hardware.SubHardware) |
paulwerelds@223 | 83 |
ComputerHardwareRemoved(subHardware); |
paulwerelds@223 | 84 |
|
paulwerelds@223 | 85 |
RevokeInstance(hardware.Identifier.ToString()); |
paulwerelds@223 | 86 |
} |
paulwerelds@223 | 87 |
|
paulwerelds@223 | 88 |
private void HardwareSensorRemoved(ISensor sensor) { |
paulwerelds@223 | 89 |
RevokeInstance(sensor.Identifier.ToString()); |
paulwerelds@223 | 90 |
} |
paulwerelds@223 | 91 |
|
paulwerelds@223 | 92 |
#endregion |
paulwerelds@223 | 93 |
|
paulwerelds@223 | 94 |
#region Helpers |
paulwerelds@223 | 95 |
|
paulwerelds@223 | 96 |
private bool Exists(string identifier) { |
paulwerelds@225 | 97 |
return activeInstances.Exists(h => h.Identifier == identifier); |
paulwerelds@223 | 98 |
} |
paulwerelds@223 | 99 |
|
paulwerelds@223 | 100 |
private void RevokeInstance(string identifier) { |
paulwerelds@225 | 101 |
int instanceIndex = activeInstances.FindIndex( |
paulwerelds@223 | 102 |
item => item.Identifier == identifier.ToString() |
paulwerelds@223 | 103 |
); |
paulwerelds@223 | 104 |
|
moel@240 | 105 |
try { |
moel@240 | 106 |
Instrumentation.Revoke(activeInstances[instanceIndex]); |
moel@240 | 107 |
} catch (Exception) { } |
paulwerelds@223 | 108 |
|
paulwerelds@225 | 109 |
activeInstances.RemoveAt(instanceIndex); |
paulwerelds@223 | 110 |
} |
paulwerelds@223 | 111 |
|
paulwerelds@223 | 112 |
#endregion |
paulwerelds@223 | 113 |
|
paulwerelds@223 | 114 |
public void Dispose() { |
moel@240 | 115 |
foreach (IWmiObject instance in activeInstances) { |
moel@240 | 116 |
try { |
moel@240 | 117 |
Instrumentation.Revoke(instance); |
moel@240 | 118 |
} catch (Exception) { } |
moel@240 | 119 |
} |
paulwerelds@225 | 120 |
activeInstances = null; |
paulwerelds@223 | 121 |
} |
paulwerelds@223 | 122 |
} |
paulwerelds@223 | 123 |
} |