Added a WMI provider, documentation to follow.

This commit is contained in:
Paul Werelds
2010-10-16 13:29:06 +00:00
parent e7ab97fd84
commit ebc1e612d9
7 changed files with 243 additions and 23 deletions

View File

@@ -100,6 +100,8 @@ namespace OpenHardwareMonitor.GUI {
this.fahrenheitMenuItem = new System.Windows.Forms.MenuItem();
this.MenuItem4 = new System.Windows.Forms.MenuItem();
this.hddMenuItem = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.wmiMenuItem = new System.Windows.Forms.MenuItem();
this.helpMenuItem = new System.Windows.Forms.MenuItem();
this.aboutMenuItem = new System.Windows.Forms.MenuItem();
this.sensorContextMenu = new System.Windows.Forms.ContextMenu();
@@ -308,7 +310,9 @@ namespace OpenHardwareMonitor.GUI {
this.separatorMenuItem,
this.temperatureUnitsMenuItem,
this.MenuItem4,
this.hddMenuItem});
this.hddMenuItem,
this.menuItem5,
this.wmiMenuItem});
this.optionsMenuItem.Text = "Options";
//
// startMinMenuItem
@@ -366,6 +370,16 @@ namespace OpenHardwareMonitor.GUI {
this.hddMenuItem.Index = 7;
this.hddMenuItem.Text = "Read HDD sensors";
//
// menuItem5
//
this.menuItem5.Index = 8;
this.menuItem5.Text = "-";
//
// wmiMenuItem
//
this.wmiMenuItem.Index = 9;
this.wmiMenuItem.Text = "Enable WMI Provider";
//
// helpMenuItem
//
this.helpMenuItem.Index = 3;
@@ -522,6 +536,8 @@ namespace OpenHardwareMonitor.GUI {
private System.Windows.Forms.MenuItem MenuItem3;
private System.Windows.Forms.MenuItem gadgetMenuItem;
private System.Windows.Forms.MenuItem minCloseMenuItem;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem wmiMenuItem;
}
}

View File

@@ -19,7 +19,7 @@
Portions created by the Initial Developer are Copyright (C) 2009-2010
the Initial Developer. All Rights Reserved.
Contributor(s):
Contributor(s): Paul Werelds
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
@@ -38,15 +38,13 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Aga.Controls.Tree;
using Aga.Controls.Tree.NodeControls;
using OpenHardwareMonitor.Hardware;
using OpenHardwareMonitor.Utilities;
using OpenHardwareMonitor.WMIProvider;
namespace OpenHardwareMonitor.GUI {
public partial class MainForm : Form {
@@ -75,6 +73,9 @@ namespace OpenHardwareMonitor.GUI {
private UserOption autoStart;
private UserOption readHddSensors;
private UserOption showGadget;
private UserOption enableWmiProvider;
private WmiProvider wmiProvider;
public MainForm() {
InitializeComponent();
@@ -133,6 +134,10 @@ namespace OpenHardwareMonitor.GUI {
computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
if (settings.GetValue("enableWmiProvider", false))
wmiProvider = new WmiProvider(computer);
computer.Open();
timer.Enabled = true;
@@ -152,7 +157,8 @@ namespace OpenHardwareMonitor.GUI {
plotColorPalette[11] = Color.Olive;
plotColorPalette[12] = Color.Firebrick;
showHiddenSensors = new UserOption("hiddenMenuItem", false, hiddenMenuItem, settings);
showHiddenSensors = new UserOption("hiddenMenuItem", false,
hiddenMenuItem, settings);
showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
treeModel.ForceVisible = showHiddenSensors.Value;
};
@@ -163,7 +169,8 @@ namespace OpenHardwareMonitor.GUI {
treeView.Invalidate();
};
showValue = new UserOption("valueMenuItem", true, valueMenuItem, settings);
showValue = new UserOption("valueMenuItem", true, valueMenuItem,
settings);
showValue.Changed += delegate(object sender, EventArgs e) {
treeView.Columns[1].IsVisible = showValue.Value;
};
@@ -178,16 +185,20 @@ namespace OpenHardwareMonitor.GUI {
treeView.Columns[3].IsVisible = showMax.Value;
};
startMinimized = new UserOption("startMinMenuItem", false, startMinMenuItem, settings);
startMinimized = new UserOption("startMinMenuItem", false,
startMinMenuItem, settings);
minimizeToTray = new UserOption("minTrayMenuItem", true, minTrayMenuItem, settings);
minimizeToTray = new UserOption("minTrayMenuItem", true,
minTrayMenuItem, settings);
minimizeToTray.Changed += delegate(object sender, EventArgs e) {
systemTray.IsMainIconEnabled = minimizeToTray.Value;
};
minimizeOnClose = new UserOption("minCloseMenuItem", false, minCloseMenuItem, settings);
minimizeOnClose = new UserOption("minCloseMenuItem", false,
minCloseMenuItem, settings);
autoStart = new UserOption(null, startupManager.Startup, startupMenuItem, settings);
autoStart = new UserOption(null, startupManager.Startup,
startupMenuItem, settings);
autoStart.Changed += delegate(object sender, EventArgs e) {
try {
startupManager.Startup = autoStart.Value;
@@ -198,18 +209,31 @@ namespace OpenHardwareMonitor.GUI {
}
};
readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem, settings);
readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
settings);
readHddSensors.Changed += delegate(object sender, EventArgs e) {
computer.HDDEnabled = readHddSensors.Value;
UpdatePlotSelection(null, null);
};
showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem, settings);
showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
settings);
showGadget.Changed += delegate(object sender, EventArgs e) {
if (gadget != null)
gadget.Visible = showGadget.Value;
};
enableWmiProvider = new UserOption("enableWmiProvider", false,
wmiMenuItem, settings);
enableWmiProvider.Changed += delegate {
if (enableWmiProvider.Value && wmiProvider == null)
wmiProvider = new WmiProvider(computer);
else if (!enableWmiProvider.Value && wmiProvider != null) {
wmiProvider.Dispose();
wmiProvider = null;
}
};
celciusMenuItem.Checked =
unitManager.TemperatureUnit == TemperatureUnit.Celcius;
fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
@@ -229,8 +253,7 @@ namespace OpenHardwareMonitor.GUI {
IntPtr handle = Handle;
// Make sure the settings are saved when the user logs off
Microsoft.Win32.SystemEvents.SessionEnded +=
delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
Microsoft.Win32.SystemEvents.SessionEnded += delegate {
SaveConfiguration();
};
}
@@ -297,7 +320,8 @@ namespace OpenHardwareMonitor.GUI {
plotPanel.SetSensors(selected, colors);
}
private void nodeTextBoxText_EditorShowing(object sender, CancelEventArgs e)
private void nodeTextBoxText_EditorShowing(object sender,
CancelEventArgs e)
{
e.Cancel = !(treeView.CurrentNode != null &&
treeView.CurrentNode.Tag is SensorNode);
@@ -322,6 +346,9 @@ namespace OpenHardwareMonitor.GUI {
systemTray.Redraw();
if (gadget != null)
gadget.Redraw();
if (wmiProvider != null)
wmiProvider.Update();
}
private void SaveConfiguration() {
@@ -355,13 +382,13 @@ namespace OpenHardwareMonitor.GUI {
Height = settings.GetValue("mainForm.Height", 640)
};
Rectangle totalWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
int.MinValue, int.MinValue);
foreach (Screen screen in Screen.AllScreens)
totalWorkingArea = Rectangle.Union(totalWorkingArea, screen.Bounds);
fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
Rectangle intersection = Rectangle.Intersect(totalWorkingArea, newBounds);
Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
if (intersection.Width < 20 || intersection.Height < 20 ||
!settings.Contains("mainForm.Location.X")
) {
@@ -394,7 +421,9 @@ namespace OpenHardwareMonitor.GUI {
if (m == null || m.Button != MouseButtons.Right)
return;
NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
NodeControlInfo info = treeView.GetNodeControlInfoAt(
new Point(m.X, m.Y)
);
treeView.SelectedNode = info.Node;
if (info.Node != null) {
SensorNode node = info.Node.Tag as SensorNode;

View File

@@ -58,7 +58,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Aga.Controls, Version=1.7.0.0, Culture=neutral, PublicKeyToken=fcc90fbf924463a3">
@@ -126,6 +128,12 @@
<Compile Include="GUI\SensorNode.cs" />
<Compile Include="Utilities\EmbeddedResources.cs" />
<Compile Include="Utilities\IconFactory.cs" />
<Compile Include="WMIProvider\Hardware.cs" />
<Compile Include="WMIProvider\IWmiClass.cs" />
<Compile Include="WMIProvider\Sensor.cs" />
<Compile Include="WMIProvider\WMIProvider.cs">
<SubType>Component</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="GUI\AboutBox.resx">

19
WMIProvider/Hardware.cs Normal file
View File

@@ -0,0 +1,19 @@
using System.Management.Instrumentation;
using OpenHardwareMonitor.Hardware;
namespace OpenHardwareMonitor.WMIProvider {
[InstrumentationClass(InstrumentationType.Instance)]
public class Hardware : IWmiClass {
public string HardwareType { get; private set; }
public string Identifier { get; private set; }
public string Name { get; private set; }
public Hardware(IHardware hardware) {
Name = hardware.Name;
Identifier = hardware.Identifier.ToString();
HardwareType = hardware.HardwareType.ToString();
}
public void Update() { }
}
}

8
WMIProvider/IWmiClass.cs Normal file
View File

@@ -0,0 +1,8 @@
namespace OpenHardwareMonitor.WMIProvider {
interface IWmiClass {
string Name { get; }
string Identifier { get; }
void Update();
}
}

40
WMIProvider/Sensor.cs Normal file
View File

@@ -0,0 +1,40 @@
using System.Management.Instrumentation;
using OpenHardwareMonitor.Hardware;
namespace OpenHardwareMonitor.WMIProvider {
[InstrumentationClass(InstrumentationType.Instance)]
public class Sensor : IWmiClass {
private ISensor _sensor;
public string SensorType { get; private set; }
public string Identifier { get; private set; }
public string Parent { get; private set; }
public string Name { get; private set; }
public float Value { get; private set; }
public float Min { get; private set; }
public float Max { get; private set; }
public int Index { get; private set; }
public Sensor(ISensor sensor) {
Name = sensor.Name;
Index = sensor.Index;
SensorType = sensor.SensorType.ToString();
Identifier = sensor.Identifier.ToString();
Parent = sensor.Hardware.Identifier.ToString();
_sensor = sensor;
}
public void Update() {
Value = (_sensor.Value != null) ? (float)_sensor.Value : 0;
if (_sensor.Min != null)
Min = (float)_sensor.Min;
if (_sensor.Max != null)
Max = (float)_sensor.Max;
}
}
}

100
WMIProvider/WMIProvider.cs Normal file
View File

@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Management.Instrumentation;
using OpenHardwareMonitor.Hardware;
[assembly: Instrumented("root/OpenHardwareMonitor")]
[System.ComponentModel.RunInstaller(true)]
public class InstanceInstaller : DefaultManagementProjectInstaller { }
namespace OpenHardwareMonitor.WMIProvider {
public class WmiProvider : IDisposable {
private List<IWmiClass> _activeInstances;
public WmiProvider(IComputer computer) {
_activeInstances = new List<IWmiClass>();
foreach (IHardware hardware in computer.Hardware)
ComputerHardwareAdded(hardware);
computer.HardwareAdded += ComputerHardwareAdded;
computer.HardwareRemoved += ComputerHardwareRemoved;
}
public void Update() {
foreach (IWmiClass instance in _activeInstances)
instance.Update();
}
#region Eventhandlers
private void ComputerHardwareAdded(IHardware hardware) {
if (!Exists(hardware.Identifier.ToString())) {
foreach (ISensor sensor in hardware.Sensors)
HardwareSensorAdded(sensor);
hardware.SensorAdded += HardwareSensorAdded;
hardware.SensorRemoved += HardwareSensorRemoved;
Hardware hw = new Hardware(hardware);
_activeInstances.Add(hw);
Instrumentation.Publish(hw);
}
foreach (IHardware subHardware in hardware.SubHardware)
ComputerHardwareAdded(subHardware);
}
private void HardwareSensorAdded(ISensor data) {
Sensor sensor = new Sensor(data);
_activeInstances.Add(sensor);
Instrumentation.Publish(sensor);
}
private void ComputerHardwareRemoved(IHardware hardware) {
hardware.SensorAdded -= HardwareSensorAdded;
hardware.SensorRemoved -= HardwareSensorRemoved;
foreach (ISensor sensor in hardware.Sensors)
HardwareSensorRemoved(sensor);
foreach (IHardware subHardware in hardware.SubHardware)
ComputerHardwareRemoved(subHardware);
RevokeInstance(hardware.Identifier.ToString());
}
private void HardwareSensorRemoved(ISensor sensor) {
RevokeInstance(sensor.Identifier.ToString());
}
#endregion
#region Helpers
private bool Exists(string identifier) {
return _activeInstances.Exists(h => h.Identifier == identifier);
}
private void RevokeInstance(string identifier) {
int instanceIndex = _activeInstances.FindIndex(
item => item.Identifier == identifier.ToString()
);
Instrumentation.Revoke(_activeInstances[instanceIndex]);
_activeInstances.RemoveAt(instanceIndex);
}
#endregion
public void Dispose() {
foreach (IWmiClass instance in _activeInstances)
Instrumentation.Revoke(instance);
_activeInstances = null;
}
}
}