Skip to content

Commit

Permalink
Charge limit will be applied every time powe mode changes (incl hiber…
Browse files Browse the repository at this point in the history
…nate)
  • Loading branch information
seerge committed Feb 23, 2023
1 parent 27f5ed5 commit 1c17c70
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 76 deletions.
2 changes: 1 addition & 1 deletion GHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<ItemGroup>
<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="WinForms.DataVisualization" Version="1.7.0" />
</ItemGroup>
Expand Down
98 changes: 23 additions & 75 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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;


Expand Down Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -240,13 +184,18 @@ public static void Main()
settingsForm.AutoGPUMode(isPlugged ? 1 : 0);
settingsForm.AutoScreen(isPlugged ? 1 : 0);

SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;

IntPtr dummy = settingsForm.Handle;

Application.Run();

}

private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
settingsForm.SetBatteryChargeLimit(config.getConfig("charge_limit"));
}

static void WatcherEventArrived(object sender, EventArrivedEventArgs e)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1c17c70

Please sign in to comment.