paulwerelds@224: /* paulwerelds@224: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. paulwerelds@224: moel@344: Copyright (C) 2009-2010 Paul Werelds moel@390: Copyright (C) 2012 Michael Möller moel@390: paulwerelds@224: */ paulwerelds@224: paulwerelds@223: using System; paulwerelds@223: using System.Collections.Generic; paulwerelds@223: using System.Management.Instrumentation; paulwerelds@223: using OpenHardwareMonitor.Hardware; paulwerelds@223: paulwerelds@223: [assembly: Instrumented("root/OpenHardwareMonitor")] paulwerelds@223: paulwerelds@223: [System.ComponentModel.RunInstaller(true)] paulwerelds@223: public class InstanceInstaller : DefaultManagementProjectInstaller { } paulwerelds@223: paulwerelds@227: namespace OpenHardwareMonitor.WMI { paulwerelds@224: /// paulwerelds@224: /// The WMI Provider. paulwerelds@224: /// This class is not exposed to WMI itself. paulwerelds@224: /// paulwerelds@223: public class WmiProvider : IDisposable { paulwerelds@225: private List activeInstances; paulwerelds@223: paulwerelds@223: public WmiProvider(IComputer computer) { paulwerelds@225: activeInstances = new List(); paulwerelds@223: paulwerelds@223: foreach (IHardware hardware in computer.Hardware) paulwerelds@223: ComputerHardwareAdded(hardware); paulwerelds@223: paulwerelds@223: computer.HardwareAdded += ComputerHardwareAdded; paulwerelds@223: computer.HardwareRemoved += ComputerHardwareRemoved; paulwerelds@223: } paulwerelds@223: paulwerelds@223: public void Update() { paulwerelds@225: foreach (IWmiObject instance in activeInstances) paulwerelds@223: instance.Update(); paulwerelds@223: } paulwerelds@223: paulwerelds@223: #region Eventhandlers paulwerelds@223: paulwerelds@223: private void ComputerHardwareAdded(IHardware hardware) { paulwerelds@223: if (!Exists(hardware.Identifier.ToString())) { paulwerelds@223: foreach (ISensor sensor in hardware.Sensors) paulwerelds@223: HardwareSensorAdded(sensor); paulwerelds@223: paulwerelds@223: hardware.SensorAdded += HardwareSensorAdded; paulwerelds@223: hardware.SensorRemoved += HardwareSensorRemoved; paulwerelds@223: paulwerelds@223: Hardware hw = new Hardware(hardware); paulwerelds@225: activeInstances.Add(hw); paulwerelds@223: moel@240: try { moel@240: Instrumentation.Publish(hw); moel@240: } catch (Exception) { } paulwerelds@223: } paulwerelds@223: paulwerelds@223: foreach (IHardware subHardware in hardware.SubHardware) paulwerelds@223: ComputerHardwareAdded(subHardware); paulwerelds@223: } paulwerelds@223: paulwerelds@223: private void HardwareSensorAdded(ISensor data) { paulwerelds@223: Sensor sensor = new Sensor(data); paulwerelds@225: activeInstances.Add(sensor); paulwerelds@223: moel@240: try { moel@240: Instrumentation.Publish(sensor); moel@240: } catch (Exception) { } paulwerelds@223: } paulwerelds@223: paulwerelds@223: private void ComputerHardwareRemoved(IHardware hardware) { paulwerelds@223: hardware.SensorAdded -= HardwareSensorAdded; paulwerelds@223: hardware.SensorRemoved -= HardwareSensorRemoved; paulwerelds@223: paulwerelds@223: foreach (ISensor sensor in hardware.Sensors) paulwerelds@223: HardwareSensorRemoved(sensor); paulwerelds@223: paulwerelds@223: foreach (IHardware subHardware in hardware.SubHardware) paulwerelds@223: ComputerHardwareRemoved(subHardware); paulwerelds@223: paulwerelds@223: RevokeInstance(hardware.Identifier.ToString()); paulwerelds@223: } paulwerelds@223: paulwerelds@223: private void HardwareSensorRemoved(ISensor sensor) { paulwerelds@223: RevokeInstance(sensor.Identifier.ToString()); paulwerelds@223: } paulwerelds@223: paulwerelds@223: #endregion paulwerelds@223: paulwerelds@223: #region Helpers paulwerelds@223: paulwerelds@223: private bool Exists(string identifier) { paulwerelds@225: return activeInstances.Exists(h => h.Identifier == identifier); paulwerelds@223: } paulwerelds@223: paulwerelds@223: private void RevokeInstance(string identifier) { paulwerelds@225: int instanceIndex = activeInstances.FindIndex( paulwerelds@223: item => item.Identifier == identifier.ToString() paulwerelds@223: ); paulwerelds@223: moel@390: if (instanceIndex == -1) moel@390: return; moel@390: moel@240: try { moel@240: Instrumentation.Revoke(activeInstances[instanceIndex]); moel@240: } catch (Exception) { } paulwerelds@223: paulwerelds@225: activeInstances.RemoveAt(instanceIndex); paulwerelds@223: } paulwerelds@223: paulwerelds@223: #endregion paulwerelds@223: paulwerelds@223: public void Dispose() { moel@240: foreach (IWmiObject instance in activeInstances) { moel@240: try { moel@240: Instrumentation.Revoke(instance); moel@240: } catch (Exception) { } moel@240: } paulwerelds@225: activeInstances = null; paulwerelds@223: } paulwerelds@223: } paulwerelds@223: }