Skip to content

Commit

Permalink
Split automation into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszCichecki committed May 30, 2022
1 parent 7dd7886 commit f9c24ee
Show file tree
Hide file tree
Showing 38 changed files with 236 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ internal enum AutomationPipelineCriteria
{
ACAdapterConnected,
ACAdapterDisconnected,
DisplayConfigurationChanged,
}

internal static class AutomationPipelineCriteriaExtensions
Expand All @@ -17,7 +16,6 @@ public static bool IsSatisfied(this AutomationPipelineCriteria trigger)
{
AutomationPipelineCriteria.ACAdapterConnected => Power.IsPowerAdapterConnected(),
AutomationPipelineCriteria.ACAdapterDisconnected => !Power.IsPowerAdapterConnected(),
AutomationPipelineCriteria.DisplayConfigurationChanged => true,
_ => false,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ namespace LenovoLegionToolkit.Lib.Automation
public class AutomationProcessor
{
private readonly PowerAdapterListener _powerAdapterListener = new();
private readonly DisplayConfigurationListener _displayConfigurationListener = new();

private readonly List<AutomationPipeline> _pipelines = new();

private readonly AsyncLock _lock = new();

private CancellationTokenSource? _cts;

public bool Enabled { get; set; } = true;

public AutomationProcessor()
{
var criteria = new List<AutomationPipelineCriteria>
{
AutomationPipelineCriteria.ACAdapterConnected,
AutomationPipelineCriteria.DisplayConfigurationChanged,
};
var steps = new List<IAutomationStep>
{
Expand All @@ -34,7 +35,6 @@ public AutomationProcessor()
var criteria2 = new List<AutomationPipelineCriteria>
{
AutomationPipelineCriteria.ACAdapterDisconnected,
AutomationPipelineCriteria.DisplayConfigurationChanged,
};
var steps2 = new List<IAutomationStep>
{
Expand All @@ -44,19 +44,17 @@ public AutomationProcessor()
_pipelines.Add(new(criteria2, steps2));

_powerAdapterListener.Changed += PowerAdapterListener_Changed;
_displayConfigurationListener.Changed += DisplayConfigurationListener_Changed;

_powerAdapterListener.Start();
_displayConfigurationListener.Start();
}

private async void PowerAdapterListener_Changed(object? sender, EventArgs e) => await RunAsync();

private async void DisplayConfigurationListener_Changed(object? sender, EventArgs e) => await RunAsync();

private async Task RunAsync()
{
_cts?.Cancel();

if (!Enabled)
return;

_cts = new CancellationTokenSource();

var token = _cts.Token;
Expand Down
11 changes: 11 additions & 0 deletions LenovoLegionToolkit.Lib.Automation/DIContainerModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Autofac;

namespace LenovoLegionToolkit.Lib.Automation
{
public class DIContainerModule : Module
{
protected override void Load(ContainerBuilder builder)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="6.4.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LenovoLegionToolkit.Lib\LenovoLegionToolkit.Lib.csproj" />
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions LenovoLegionToolkit.Lib/DIContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Autofac;

namespace LenovoLegionToolkit.Lib
{
public class DIContainer
{
private static IContainer? _container;

public static void Initialize(params Module[] modules)
{
var cb = new ContainerBuilder();

foreach (var module in modules)
cb.RegisterModule(module);

_container = cb.Build();
}

public static T Resolve<T>() where T : notnull
{
if (_container == null)
throw new InvalidOperationException("DIContainer must be initialized first");
return _container.Resolve<T>();
}
}
}
35 changes: 35 additions & 0 deletions LenovoLegionToolkit.Lib/DIContainerModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Autofac;
using LenovoLegionToolkit.Lib.Controllers;
using LenovoLegionToolkit.Lib.Extensions;
using LenovoLegionToolkit.Lib.Features;
using LenovoLegionToolkit.Lib.Listeners;
using LenovoLegionToolkit.Lib.Utils;

namespace LenovoLegionToolkit.Lib
{
public class DIContainerModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register<AlwaysOnUSBFeature>();
builder.Register<BatteryFeature>();
builder.Register<FlipToStartFeature>();
builder.Register<FnLockFeature>();
builder.Register<HybridModeFeature>();
builder.Register<OverDriveFeature>();
builder.Register<PowerModeFeature>();
builder.Register<RefreshRateFeature>();
builder.Register<TouchpadLockFeature>();

builder.Register<PowerModeListener>(true);
builder.Register<PowerAdapterListener>(true);
builder.Register<DisplayConfigurationListener>(true);
builder.Register<SpecialKeyListener>(true);

builder.Register<GPUController>();
builder.Register<CPUBoostModeController>();

builder.Register<UpdateChecker>();
}
}
}
15 changes: 15 additions & 0 deletions LenovoLegionToolkit.Lib/Extensions/ContainerBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Autofac;

namespace LenovoLegionToolkit.Lib.Extensions
{
public static class ContainerBuilderExtensions
{
public static void Register<T>(this ContainerBuilder cb, bool autoCreate = false) where T : notnull
{
var r = cb.RegisterType<T>().AsSelf().AsImplementedInterfaces().SingleInstance();

if (autoCreate)
r.AutoActivate();
}
}
}
35 changes: 18 additions & 17 deletions LenovoLegionToolkit.Lib/LenovoLegionToolkit.Lib.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Win32.SystemEvents" Version="6.0.1" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="6.0.0" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="NvAPIWrapper.Net" Version="0.8.1.101" />
<PackageReference Include="Octokit" Version="0.51.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
<PackageReference Include="WindowsDisplayAPI" Version="1.3.0.13" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="6.4.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Win32.SystemEvents" Version="6.0.1" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="6.0.0" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="NvAPIWrapper.Net" Version="0.8.1.101" />
<PackageReference Include="Octokit" Version="0.51.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
<PackageReference Include="WindowsDisplayAPI" Version="1.3.0.13" />
</ItemGroup>
</Project>
30 changes: 17 additions & 13 deletions LenovoLegionToolkit.Lib/Listeners/AbstractWMIListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Management;
using LenovoLegionToolkit.Lib.Utils;

Expand All @@ -10,7 +11,9 @@ public abstract class AbstractWMIListener<T> : IListener<T> where T : struct, En
private readonly string _property;
private readonly int _offset;

#pragma warning disable IDE0052 // Remove unread private members
private IDisposable? _disposable;
#pragma warning restore IDE0052 // Remove unread private members

public event EventHandler<T>? Changed;

Expand All @@ -19,23 +22,24 @@ public AbstractWMIListener(string eventName, string property, int offset)
_eventName = eventName;
_property = property;
_offset = offset;
}

public void Start()
{
if (Log.Instance.IsTraceEnabled)
Log.Instance.Trace($"Starting... [listener={GetType().Name}]");

_disposable = WMI.Listen("ROOT\\WMI", $"SELECT * FROM {_eventName}", Handler);
Start();
}

public void Stop()
private void Start()
{
_disposable?.Dispose();
_disposable = null;

if (Log.Instance.IsTraceEnabled)
Log.Instance.Trace($"Stopped [listener={GetType().Name}]");
try
{
if (Log.Instance.IsTraceEnabled)
Log.Instance.Trace($"Starting... [listener={GetType().Name}]");

_disposable = WMI.Listen("ROOT\\WMI", $"SELECT * FROM {_eventName}", Handler);
}
catch (Exception ex)
{
if (Log.Instance.IsTraceEnabled)
Log.Instance.Trace($"Couldn't start listener: {ex.Demystify()} [listener={GetType().Name}]");
}
}

protected abstract void OnChanged(T value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ public class DisplayConfigurationListener : IListener<EventArgs>
{
public event EventHandler<EventArgs>? Changed;

public void Start()
public DisplayConfigurationListener()
{
Stop();

SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
}

public void Stop() => SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;

private void SystemEvents_DisplaySettingsChanged(object? sender, EventArgs e) => Changed?.Invoke(this, EventArgs.Empty);
}
}
4 changes: 0 additions & 4 deletions LenovoLegionToolkit.Lib/Listeners/IListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ namespace LenovoLegionToolkit.Lib.Listeners
public interface IListener<T>
{
event EventHandler<T>? Changed;

void Start();

void Stop();
}
}
5 changes: 1 addition & 4 deletions LenovoLegionToolkit.Lib/Listeners/PowerAdapterListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ public class PowerAdapterListener : IListener<EventArgs>
{
public event EventHandler<EventArgs>? Changed;

public void Start()
public PowerAdapterListener()
{
Stop();
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
}

public void Stop() => SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;

private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) => Changed?.Invoke(this, EventArgs.Empty);
}
}
2 changes: 1 addition & 1 deletion LenovoLegionToolkit.Lib/Utils/CMD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace LenovoLegionToolkit.Lib.Utils
{
internal static class CMD
public static class CMD
{
public static async Task<string> RunAsync(string file, string arguments)
{
Expand Down
2 changes: 1 addition & 1 deletion LenovoLegionToolkit.Lib/Utils/WMI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace LenovoLegionToolkit.Lib.Utils
{
internal static class WMI
public static class WMI
{
public static IDisposable Listen(string scope, FormattableString query, Action<PropertyDataCollection> handler)
{
Expand Down
Loading

0 comments on commit f9c24ee

Please sign in to comment.