first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// Copyright (C) LibreHardwareMonitor and Contributors.
|
||||
// Partial Copyright (C) Michael Möller <mmoeller@openhardwaremonitor.org> and Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
using System.Management.Instrumentation;
|
||||
using LibreHardwareMonitor.Hardware;
|
||||
|
||||
namespace LibreHardwareMonitor.Wmi;
|
||||
|
||||
[InstrumentationClass(InstrumentationType.Instance)]
|
||||
public class Hardware : IWmiObject
|
||||
{
|
||||
#region WMI Exposed
|
||||
|
||||
public string HardwareType { get; }
|
||||
public string Identifier { get; }
|
||||
public string Name { get; }
|
||||
public string Parent { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
public Hardware(IHardware hardware)
|
||||
{
|
||||
Name = hardware.Name;
|
||||
Identifier = hardware.Identifier.ToString();
|
||||
HardwareType = hardware.HardwareType.ToString();
|
||||
Parent = (hardware.Parent != null)
|
||||
? hardware.Parent.Identifier.ToString()
|
||||
: "";
|
||||
}
|
||||
|
||||
public void Update() { }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// Copyright (C) LibreHardwareMonitor and Contributors.
|
||||
// Partial Copyright (C) Michael Möller <mmoeller@openhardwaremonitor.org> and Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
namespace LibreHardwareMonitor.Wmi;
|
||||
|
||||
interface IWmiObject
|
||||
{
|
||||
// Both of these get exposed to WMI
|
||||
string Name { get; }
|
||||
string Identifier { get; }
|
||||
|
||||
// Not exposed.
|
||||
void Update();
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// Copyright (C) LibreHardwareMonitor and Contributors.
|
||||
// Partial Copyright (C) Michael Möller <mmoeller@openhardwaremonitor.org> and Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
using System.Management.Instrumentation;
|
||||
using LibreHardwareMonitor.Hardware;
|
||||
|
||||
namespace LibreHardwareMonitor.Wmi;
|
||||
|
||||
[InstrumentationClass(InstrumentationType.Instance)]
|
||||
public class Sensor : IWmiObject
|
||||
{
|
||||
private readonly ISensor _sensor;
|
||||
|
||||
#region WMI Exposed
|
||||
|
||||
public string SensorType { get; }
|
||||
public string Identifier { get; }
|
||||
public string Parent { get; }
|
||||
public string Name { get; }
|
||||
public float Value { get; private set; }
|
||||
public float Min { get; private set; }
|
||||
public float Max { get; private set; }
|
||||
public int Index { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
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 ?? 0;
|
||||
|
||||
if (_sensor.Min != null)
|
||||
Min = (float)_sensor.Min;
|
||||
|
||||
if (_sensor.Max != null)
|
||||
Max = (float)_sensor.Max;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// Copyright (C) LibreHardwareMonitor and Contributors.
|
||||
// Partial Copyright (C) Michael Möller <mmoeller@openhardwaremonitor.org> and Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Management.Instrumentation;
|
||||
using LibreHardwareMonitor.Hardware;
|
||||
|
||||
[assembly: Instrumented("root/LibreHardwareMonitor")]
|
||||
|
||||
[System.ComponentModel.RunInstaller(true)]
|
||||
public class InstanceInstaller : DefaultManagementProjectInstaller
|
||||
{ }
|
||||
|
||||
namespace LibreHardwareMonitor.Wmi
|
||||
{
|
||||
/// <summary>
|
||||
/// The WMI Provider.
|
||||
/// This class is not exposed to WMI itself.
|
||||
/// </summary>
|
||||
public class WmiProvider : IDisposable
|
||||
{
|
||||
private readonly object _activeInstancesLock = new();
|
||||
private readonly List<IWmiObject> _activeInstances;
|
||||
|
||||
public WmiProvider(IComputer computer)
|
||||
{
|
||||
_activeInstances = new List<IWmiObject>();
|
||||
foreach (IHardware hardware in computer.Hardware)
|
||||
OnHardwareAdded(hardware);
|
||||
|
||||
computer.HardwareAdded += OnHardwareAdded;
|
||||
computer.HardwareRemoved += OnHardwareRemoved;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
lock (_activeInstancesLock)
|
||||
{
|
||||
foreach (IWmiObject instance in _activeInstances)
|
||||
instance.Update();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHardwareAdded(IHardware hardware)
|
||||
{
|
||||
lock (_activeInstancesLock)
|
||||
{
|
||||
if (!_activeInstances.Exists(h => h.Identifier == hardware.Identifier.ToString()))
|
||||
{
|
||||
foreach (ISensor sensor in hardware.Sensors)
|
||||
OnSensorAdded(sensor);
|
||||
|
||||
hardware.SensorAdded += OnSensorAdded;
|
||||
hardware.SensorRemoved += HardwareSensorRemoved;
|
||||
|
||||
Hardware hw = new(hardware);
|
||||
_activeInstances.Add(hw);
|
||||
|
||||
try
|
||||
{
|
||||
Instrumentation.Publish(hw);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
||||
foreach (IHardware subHardware in hardware.SubHardware)
|
||||
OnHardwareAdded(subHardware);
|
||||
}
|
||||
|
||||
private void OnSensorAdded(ISensor data)
|
||||
{
|
||||
Sensor sensor = new(data);
|
||||
|
||||
lock (_activeInstancesLock)
|
||||
_activeInstances.Add(sensor);
|
||||
|
||||
try
|
||||
{
|
||||
Instrumentation.Publish(sensor);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
|
||||
private void OnHardwareRemoved(IHardware hardware)
|
||||
{
|
||||
hardware.SensorAdded -= OnSensorAdded;
|
||||
hardware.SensorRemoved -= HardwareSensorRemoved;
|
||||
|
||||
foreach (ISensor sensor in hardware.Sensors)
|
||||
HardwareSensorRemoved(sensor);
|
||||
|
||||
foreach (IHardware subHardware in hardware.SubHardware)
|
||||
OnHardwareRemoved(subHardware);
|
||||
|
||||
RevokeInstance(hardware.Identifier.ToString());
|
||||
}
|
||||
|
||||
private void HardwareSensorRemoved(ISensor sensor)
|
||||
{
|
||||
RevokeInstance(sensor.Identifier.ToString());
|
||||
}
|
||||
|
||||
private void RevokeInstance(string identifier)
|
||||
{
|
||||
lock (_activeInstancesLock)
|
||||
{
|
||||
int instanceIndex = _activeInstances.FindIndex(item => item.Identifier == identifier);
|
||||
if (instanceIndex == -1)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
Instrumentation.Revoke(_activeInstances[instanceIndex]);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
|
||||
_activeInstances.RemoveAt(instanceIndex);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (_activeInstancesLock)
|
||||
{
|
||||
foreach (IWmiObject instance in _activeInstances)
|
||||
{
|
||||
try
|
||||
{
|
||||
Instrumentation.Revoke(instance);
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user