Compare commits

...

2 Commits

Author SHA1 Message Date
seerge
1c17c705de Charge limit will be applied every time powe mode changes (incl hibernate) 2023-02-23 19:50:06 +01:00
seerge
27f5ed50d5 Fixed possible crash 2023-02-23 16:05:31 +01:00
3 changed files with 69 additions and 68 deletions

View File

@@ -13,6 +13,7 @@
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
<SupportedOSPlatformVersion>8.0</SupportedOSPlatformVersion> <SupportedOSPlatformVersion>8.0</SupportedOSPlatformVersion>
<AssemblyName>GHelper</AssemblyName> <AssemblyName>GHelper</AssemblyName>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -34,7 +35,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="hidlibrary" Version="3.3.40" /> <PackageReference Include="hidlibrary" Version="3.3.40" />
<PackageReference Include="LibreHardwareMonitorLib" Version="0.9.1" /> <PackageReference Include="System.Management" Version="7.0.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" /> <PackageReference Include="TaskScheduler" Version="2.10.1" />
<PackageReference Include="WinForms.DataVisualization" Version="1.7.0" /> <PackageReference Include="WinForms.DataVisualization" Version="1.7.0" />
</ItemGroup> </ItemGroup>

View File

@@ -1,8 +1,41 @@
using System.Runtime.InteropServices; using System.Diagnostics;
using System.Runtime.InteropServices;
public class NativeMethods public class NativeMethods
{ {
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
public const int SW_RESTORE = 9;
public static bool SwitchToCurrent()
{
IntPtr hWnd = IntPtr.Zero;
Process process = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(process.ProcessName);
foreach (Process _process in processes)
{
if (_process.Id != process.Id)
{
if (_process.MainWindowHandle != IntPtr.Zero)
{
Debug.WriteLine(_process.Id);
Debug.WriteLine(process.Id);
hWnd = _process.MainWindowHandle;
ShowWindowAsync(hWnd, SW_RESTORE);
}
return true;
break;
}
}
return false;
}
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)] [DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerWriteDCValueIndex(IntPtr RootPowerKey, static extern UInt32 PowerWriteDCValueIndex(IntPtr RootPowerKey,
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid, [MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,

View File

@@ -1,6 +1,8 @@
using LibreHardwareMonitor.Hardware; using Microsoft.Win32;
using System.Diagnostics; using System.Diagnostics;
using System.Management; using System.Management;
using System.Security.Principal;
using System.Text;
using System.Text.Json; using System.Text.Json;
@@ -79,101 +81,62 @@ public class AppConfig
} }
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 public class HardwareMonitor
{ {
Computer computer;
public float? cpuTemp = -1; public float? cpuTemp = -1;
public float? gpuTemp = -1; public float? gpuTemp = -1;
public float? batteryDischarge = -1; public float? batteryDischarge = -1;
public float? batteryCharge = -1; public float? batteryCharge = -1;
public static bool IsAdministrator()
{
return (new WindowsPrincipal(WindowsIdentity.GetCurrent()))
.IsInRole(WindowsBuiltInRole.Administrator);
}
public HardwareMonitor() public HardwareMonitor()
{ {
computer = new Computer
{
IsCpuEnabled = true,
IsGpuEnabled = true,
IsBatteryEnabled = true,
};
} }
public void ReadSensors() public void ReadSensors()
{ {
computer.Open();
computer.Accept(new UpdateVisitor());
cpuTemp = -1; cpuTemp = -1;
gpuTemp = -1; gpuTemp = -1;
batteryDischarge = -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) var ct = new PerformanceCounter("Thermal Zone Information", "Temperature", @"\_TZ.THRM", true);
{ cpuTemp = ct.NextValue() - 273;
if (hardware.HardwareType.ToString().Contains("Cpu") && sensor.Name.Contains("Core")) ct.Dispose();
{
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;
}
}
} }
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() public void StopReading()
{ {
computer.Close(); //computer.Close();
} }
} }
@@ -221,6 +184,7 @@ namespace GHelper
settingsForm.AutoGPUMode(isPlugged ? 1 : 0); settingsForm.AutoGPUMode(isPlugged ? 1 : 0);
settingsForm.AutoScreen(isPlugged ? 1 : 0); settingsForm.AutoScreen(isPlugged ? 1 : 0);
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
IntPtr dummy = settingsForm.Handle; IntPtr dummy = settingsForm.Handle;
@@ -228,6 +192,10 @@ namespace GHelper
} }
private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit"));
}
static void WatcherEventArrived(object sender, EventArrivedEventArgs e) static void WatcherEventArrived(object sender, EventArrivedEventArgs e)
{ {
@@ -264,7 +232,6 @@ namespace GHelper
}); });
return; return;
case 88: // Plugged case 88: // Plugged
settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit"));
settingsForm.BeginInvoke(delegate settingsForm.BeginInvoke(delegate
{ {
settingsForm.AutoGPUMode(1); settingsForm.AutoGPUMode(1);