From 1c17c705dea036f882d69025a971ba95238b47cc Mon Sep 17 00:00:00 2001 From: seerge Date: Thu, 23 Feb 2023 19:50:06 +0100 Subject: [PATCH] Charge limit will be applied every time powe mode changes (incl hibernate) --- GHelper.csproj | 2 +- Program.cs | 98 ++++++++++++-------------------------------------- 2 files changed, 24 insertions(+), 76 deletions(-) diff --git a/GHelper.csproj b/GHelper.csproj index 721d74abc..ff181f6c8 100644 --- a/GHelper.csproj +++ b/GHelper.csproj @@ -35,7 +35,7 @@ - + diff --git a/Program.cs b/Program.cs index c495db32c..00e19d220 100644 --- a/Program.cs +++ b/Program.cs @@ -1,7 +1,8 @@ -using LibreHardwareMonitor.Hardware; +using Microsoft.Win32; using System.Diagnostics; using System.Management; using System.Security.Principal; +using System.Text; using System.Text.Json; @@ -80,27 +81,9 @@ public void setConfig(string name, string value) } - -public class UpdateVisitor : IVisitor -{ - public void VisitComputer(IComputer computer) - { - computer.Traverse(this); - } - public void VisitHardware(IHardware hardware) - { - hardware.Update(); - foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this); - } - public void VisitSensor(ISensor sensor) { } - public void VisitParameter(IParameter parameter) { } -} - - public class HardwareMonitor { - Computer computer; public float? cpuTemp = -1; public float? gpuTemp = -1; @@ -121,73 +104,34 @@ public HardwareMonitor() public void ReadSensors() { - try - { - if (computer is not Computer) - { - computer = new Computer - { - IsGpuEnabled = true, - IsBatteryEnabled = true, - }; - - if (IsAdministrator()) computer.IsCpuEnabled = true; - } - - computer.Open(); - computer.Accept(new UpdateVisitor()); - } catch - { - Debug.WriteLine("Failed to read sensors"); - } - - cpuTemp = -1; gpuTemp = -1; batteryDischarge = -1; - batteryCharge = -1; - foreach (IHardware hardware in computer.Hardware) + try { - //Debug.WriteLine("Hardware: {0}", hardware.Name); - //Debug.WriteLine("Hardware: {0}", hardware.HardwareType); - foreach (ISensor sensor in hardware.Sensors) + if (cpuTemp < 0) { - if (sensor.SensorType == SensorType.Temperature) - { - if (hardware.HardwareType.ToString().Contains("Cpu") && sensor.Name.Contains("Core")) - { - cpuTemp = sensor.Value; - //Debug.WriteLine("\tSensor: {0}, value: {1}", sensor.Name, sensor.Value); - } - - if (hardware.HardwareType.ToString().Contains("Gpu") && sensor.Name.Contains("Core")) - { - gpuTemp = sensor.Value; - } - - //Debug.WriteLine("\tSensor: {0}, value: {1}", sensor.Name, sensor.Value); - - } - else if (sensor.SensorType == SensorType.Power) - { - if (sensor.Name.Contains("Discharge")) - { - batteryDischarge = sensor.Value; - } - - if (sensor.Name.Contains("Charge")) - { - batteryCharge = sensor.Value; - } - } + var ct = new PerformanceCounter("Thermal Zone Information", "Temperature", @"\_TZ.THRM", true); + cpuTemp = ct.NextValue() - 273; + ct.Dispose(); + } + if (batteryDischarge < 0) + { + var ct = new PerformanceCounter("Power Meter", "Power", "Power Meter (0)", true); + batteryDischarge = ct.NextValue() / 1000; + ct.Dispose(); + } - } + } catch + { + Debug.WriteLine("Failed reading sensors"); } + } public void StopReading() @@ -240,6 +184,7 @@ public static void Main() settingsForm.AutoGPUMode(isPlugged ? 1 : 0); settingsForm.AutoScreen(isPlugged ? 1 : 0); + SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; IntPtr dummy = settingsForm.Handle; @@ -247,6 +192,10 @@ public static void Main() } + private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) + { + settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit")); + } static void WatcherEventArrived(object sender, EventArrivedEventArgs e) { @@ -283,7 +232,6 @@ static void WatcherEventArrived(object sender, EventArrivedEventArgs e) }); return; case 88: // Plugged - settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit")); settingsForm.BeginInvoke(delegate { settingsForm.AutoGPUMode(1);