paulwerelds@224: /* paulwerelds@224: paulwerelds@224: Version: MPL 1.1/GPL 2.0/LGPL 2.1 paulwerelds@224: paulwerelds@224: The contents of this file are subject to the Mozilla Public License Version paulwerelds@224: 1.1 (the "License"); you may not use this file except in compliance with paulwerelds@224: the License. You may obtain a copy of the License at paulwerelds@224: paulwerelds@224: http://www.mozilla.org/MPL/ paulwerelds@224: paulwerelds@224: Software distributed under the License is distributed on an "AS IS" basis, paulwerelds@224: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License paulwerelds@224: for the specific language governing rights and limitations under the License. paulwerelds@224: paulwerelds@224: The Original Code is the Open Hardware Monitor code. paulwerelds@224: paulwerelds@224: The Initial Developer of the Original Code is paulwerelds@224: Paul Werelds . paulwerelds@224: Portions created by the Initial Developer are Copyright (C) 2009-2010 paulwerelds@224: the Initial Developer. All Rights Reserved. paulwerelds@224: paulwerelds@224: Contributor(s): paulwerelds@224: paulwerelds@224: Alternatively, the contents of this file may be used under the terms of paulwerelds@224: either the GNU General Public License Version 2 or later (the "GPL"), or paulwerelds@224: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), paulwerelds@224: in which case the provisions of the GPL or the LGPL are applicable instead paulwerelds@224: of those above. If you wish to allow use of your version of this file only paulwerelds@224: under the terms of either the GPL or the LGPL, and not to allow others to paulwerelds@224: use your version of this file under the terms of the MPL, indicate your paulwerelds@224: decision by deleting the provisions above and replace them with the notice paulwerelds@224: and other provisions required by the GPL or the LGPL. If you do not delete paulwerelds@224: the provisions above, a recipient may use your version of this file under paulwerelds@224: the terms of any one of the MPL, the GPL or the LGPL. paulwerelds@224: 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: paulwerelds@223: Instrumentation.Publish(hw); 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: paulwerelds@223: Instrumentation.Publish(sensor); 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: paulwerelds@225: Instrumentation.Revoke(activeInstances[instanceIndex]); paulwerelds@223: paulwerelds@225: activeInstances.RemoveAt(instanceIndex); paulwerelds@223: } paulwerelds@223: paulwerelds@223: #endregion paulwerelds@223: paulwerelds@223: public void Dispose() { paulwerelds@225: foreach (IWmiObject instance in activeInstances) paulwerelds@223: Instrumentation.Revoke(instance); paulwerelds@225: activeInstances = null; paulwerelds@223: } paulwerelds@223: } paulwerelds@223: }