properly dispose of managementObjects

#6-gpu-reporting
sleevezipper 4 years ago
parent cfdf803a7b
commit 4913987727

@ -34,17 +34,20 @@ namespace hass_workstation_service.Domain.Sensors
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
public override string GetState() public override string GetState()
{ {
ManagementObjectCollection collection = _searcher.Get(); using (ManagementObjectCollection collection = _searcher.Get())
List<int> processorLoadPercentages = new List<int>();
foreach (ManagementObject mo in collection)
{ {
foreach (PropertyData property in mo.Properties) List<int> processorLoadPercentages = new List<int>();
foreach (ManagementObject mo in collection)
{ {
processorLoadPercentages.Add(int.Parse(property.Value.ToString())); foreach (PropertyData property in mo.Properties)
{
processorLoadPercentages.Add(int.Parse(property.Value.ToString()));
}
} }
double average = processorLoadPercentages.Count > 0 ? processorLoadPercentages.Average() : 0.0;
return average.ToString("#.##", CultureInfo.InvariantCulture);
} }
double average = processorLoadPercentages.Count > 0 ? processorLoadPercentages.Average() : 0.0;
return average.ToString("#.##", CultureInfo.InvariantCulture);
} }
} }
} }

@ -12,9 +12,11 @@ namespace hass_workstation_service.Domain.Sensors
{ {
public class CurrentVolumeSensor : AbstractSensor public class CurrentVolumeSensor : AbstractSensor
{ {
private MMDeviceEnumerator DevEnum; private MMDeviceEnumerator deviceEnumerator;
private MMDeviceCollection devices;
public CurrentVolumeSensor(MqttPublisher publisher, int? updateInterval = null, string name = "CurrentVolume", Guid id = default(Guid)) : base(publisher, name ?? "CurrentVolume", updateInterval ?? 10, id) { public CurrentVolumeSensor(MqttPublisher publisher, int? updateInterval = null, string name = "CurrentVolume", Guid id = default(Guid)) : base(publisher, name ?? "CurrentVolume", updateInterval ?? 10, id) {
this.DevEnum = new MMDeviceEnumerator(); this.deviceEnumerator = new MMDeviceEnumerator();
this.devices = deviceEnumerator.EnumerateAudioEndPoints(EDataFlow.eRender, DEVICE_STATE.DEVICE_STATE_ACTIVE);
} }
public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig() public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig()
{ {
@ -30,17 +32,11 @@ namespace hass_workstation_service.Domain.Sensors
}); });
} }
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
public override string GetState() public override string GetState()
{ {
var collection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eRender, DEVICE_STATE.DEVICE_STATE_ACTIVE);
List<float> peaks = new List<float>(); List<float> peaks = new List<float>();
foreach (MMDevice device in devices)
foreach (MMDevice device in collection)
{ {
peaks.Add(device.AudioMeterInformation.PeakValues[0]); peaks.Add(device.AudioMeterInformation.PeakValues[0]);
} }

@ -17,22 +17,25 @@ namespace hass_workstation_service.Domain.Sensors
} }
public override string GetState() public override string GetState()
{ {
ManagementObjectCollection collection = _searcher.Get(); using (ManagementObjectCollection collection = _searcher.Get())
UInt64? totalMemory = null;
UInt64? freeMemory = null;
foreach (ManagementObject mo in collection)
{ {
totalMemory = (UInt64)mo.Properties["TotalVisibleMemorySize"]?.Value; UInt64? totalMemory = null;
freeMemory = (UInt64)mo.Properties["FreePhysicalMemory"]?.Value; UInt64? freeMemory = null;
foreach (ManagementObject mo in collection)
{
totalMemory = (UInt64)mo.Properties["TotalVisibleMemorySize"]?.Value;
freeMemory = (UInt64)mo.Properties["FreePhysicalMemory"]?.Value;
}
if (totalMemory != null && freeMemory != null)
{
decimal totalMemoryDec = totalMemory.Value;
decimal freeMemoryDec = freeMemory.Value;
decimal precentageUsed = 100 - (freeMemoryDec / totalMemoryDec) * 100;
return precentageUsed.ToString("#.##", CultureInfo.InvariantCulture);
}
return "";
} }
if (totalMemory != null && freeMemory != null)
{
decimal totalMemoryDec = totalMemory.Value;
decimal freeMemoryDec = freeMemory.Value;
decimal precentageUsed = 100 - (freeMemoryDec / totalMemoryDec) * 100;
return precentageUsed.ToString("#.##", CultureInfo.InvariantCulture);
}
return "";
} }
public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig() public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig()
{ {

@ -92,6 +92,8 @@ namespace hass_workstation_service.Domain.Sensors
} }
var numberOflogonUIProcesses = Process.GetProcessesByName("LogonUI").Length; var numberOflogonUIProcesses = Process.GetProcessesByName("LogonUI").Length;
if (numberOflogonUIProcesses >= numberOfUserDesktops) if (numberOflogonUIProcesses >= numberOfUserDesktops)

@ -34,16 +34,17 @@ namespace hass_workstation_service.Domain.Sensors
public override string GetState() public override string GetState()
{ {
ManagementObjectCollection collection = _searcher.Get(); using (ManagementObjectCollection collection = _searcher.Get())
foreach (ManagementObject mo in collection)
{ {
foreach (PropertyData property in mo.Properties) foreach (ManagementObject mo in collection)
{ {
return property.Value.ToString(); foreach (PropertyData property in mo.Properties)
{
return property.Value.ToString();
}
} }
return "";
} }
return "";
} }
} }

Loading…
Cancel
Save