Skip to content

Commit

Permalink
Refactorisation
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasConstant committed Nov 9, 2015
1 parent ab6c4d7 commit 34de5e8
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 64 deletions.
14 changes: 7 additions & 7 deletions ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Text;
using System.Threading.Tasks;
using SpotifyTools.Domain;
using SpotifyTools.Domain.AudioManagement;
using SpotifyTools.Domain.MessageManagement;
using SpotifyTools.Domain.PowerManagement;
using SpotifyTools.Tools;

namespace PreventSleep
Expand All @@ -12,14 +15,11 @@ class Program
{
static void Main(string[] args)
{
var spotifyAnalyser = new SpotifySaveModeStopper(new MessageDisplayer(), new PreventSleepScreen(), new SoundAnalyser());
var spotifyAnalyser = new SpotifySaveModeStopper(new MessageDisplayer(), new PowerRequestContextHandler(), new CsCoreSoundAnalyser());
spotifyAnalyser.StartListening();

while (true)
Task.Delay(3000).Wait();




Console.ReadKey();

//var preventer = new PreventSleepScreen();
//while (true)
//{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using CSCore.CoreAudioAPI;
using SpotifyTools.Contracts;

namespace SpotifyTools.Tools
namespace SpotifyTools.Domain.AudioManagement
{
public class SoundAnalyser : ISoundAnalyser
public class CsCoreSoundAnalyser : ISoundAnalyser
{
public bool IsWindowsOutputingSound()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NAudio.CoreAudioApi;
using SpotifyTools.Contracts;

namespace SpotifyTools.Tools
namespace SpotifyTools.Domain.AudioManagement
{
public class NAudioSoundAnalyser : ISoundAnalyser
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using SpotifyTools.Contracts;

namespace SpotifyTools.Domain.MessageManagement
{
public class DummyMessageDisplayer : IMessageDisplayer
{
public void OutputMessage(string mess)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using SpotifyTools.Contracts;

namespace SpotifyTools.Tools
namespace SpotifyTools.Domain.MessageManagement
{
public class MessageDisplayer : IMessageDisplayer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Runtime.InteropServices;
using SpotifyTools.Contracts;

namespace SpotifyTools.Tools
namespace SpotifyTools.Domain.PowerManagement
{
public class PreventSleepScreen : IPreventSleepScreen
public class PowerRequestContextHandler : IPreventSleepScreen
{
#region prevent screensaver, display dimming and automatically sleeping
PowerRequestContext _powerRequestContext;
Expand Down Expand Up @@ -74,6 +74,9 @@ public struct PowerRequestContextDetailed
}
#endregion

[DllImport("kernel32.dll")]
static extern uint GetLastError();

/// <summary>
/// Prevent screensaver, display dimming and power saving. This function wraps PInvokes on Win32 API.
/// </summary>
Expand All @@ -92,8 +95,8 @@ public void EnableConstantDisplayAndPower(bool enableConstantDisplayAndPower)
_powerRequest = PowerCreateRequest(ref _powerRequestContext);

// Set the request
PowerSetRequest(_powerRequest, PowerRequestType.PowerRequestSystemRequired);
PowerSetRequest(_powerRequest, PowerRequestType.PowerRequestDisplayRequired);
var s = PowerSetRequest(_powerRequest, PowerRequestType.PowerRequestSystemRequired);
var s2 = PowerSetRequest(_powerRequest, PowerRequestType.PowerRequestDisplayRequired);
}
else
{
Expand All @@ -104,5 +107,28 @@ public void EnableConstantDisplayAndPower(bool enableConstantDisplayAndPower)
CloseHandle(_powerRequest);
}
}

//[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
//internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

//[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
//internal static extern IntPtr LoadLibrary(string dllToLoad);

//private static bool PowerAvailabilityRequestsSupported()
//{
// var ptr = LoadLibrary("kernel32.dll");
// var ptr2 = GetProcAddress(ptr, "PowerSetRequest");

// if (ptr2 == IntPtr.Zero)
// {
// // Power availability requests NOT suppoted.
// return false;
// }
// else
// {
// // Power availability requests ARE suppoted.
// return true;
// }
//}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using SpotifyTools.Contracts;
using SpotifyTools.Tools;

namespace SpotifyTools.Domain.PowerManagement
{
public class SetThreadExecutionStateHandler : IPreventSleepScreen
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);

[Flags]
private enum ExecutionState : uint
{
EsAwaymodeRequired = 0x00000040,
EsContinuous = 0x80000000,
EsDisplayRequired = 0x00000002,
EsSystemRequired = 0x00000001
}

public static void PreventSleep()
{
SetThreadExecutionState(ExecutionState.EsDisplayRequired | ExecutionState.EsContinuous | ExecutionState.EsSystemRequired);
}

public static void AllowSleep()
{
SetThreadExecutionState(ExecutionState.EsContinuous);
}

private Task _keepScreenUp;
private CancellationTokenSource _cToken;

public void EnableConstantDisplayAndPower(bool enableConstantDisplayAndPower)
{
if (enableConstantDisplayAndPower)
{
_cToken?.Cancel();
_cToken = new CancellationTokenSource();

Repeat.Interval(TimeSpan.FromSeconds(10), PreventSleep, _cToken.Token);
}
else
{
_cToken?.Cancel();
AllowSleep();
}
}
}
}
26 changes: 2 additions & 24 deletions SpotifySleepModeStopper/Domain/SpotifySaveModeStopper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using SpotifyTools.Contracts;
using SpotifyTools.Tools;

namespace SpotifyTools.Domain
{
Expand Down Expand Up @@ -34,7 +35,7 @@ public void StartListening()
_cancellationTokenSource?.Cancel();
_cancellationTokenSource = new CancellationTokenSource();
_analyst = Repeat.Interval(
TimeSpan.FromSeconds(60),
TimeSpan.FromSeconds(10),
AnalyseSpotifyStatus, _cancellationTokenSource.Token);
}

Expand Down Expand Up @@ -98,29 +99,6 @@ private bool IsSoundStreaming()
return isSpotifyPlaying;
}
}

internal static class Repeat
{
public static Task Interval(
TimeSpan pollInterval,
Action action,
CancellationToken token)
{
// We don't use Observable.Interval:
// If we block, the values start bunching up behind each other.
return Task.Factory.StartNew(
() =>
{
for (;;)
{
if (token.WaitHandle.WaitOne(pollInterval))
break;

action();
}
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
}
}


13 changes: 8 additions & 5 deletions SpotifySleepModeStopper/SpotifySleepModeStopper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@
<Compile Include="Contracts\IMessageDisplayer.cs" />
<Compile Include="Contracts\IPreventSleepScreen.cs" />
<Compile Include="Contracts\ISoundAnalyser.cs" />
<Compile Include="Tools\Repeat.cs" />
<Compile Include="Domain\SpotifySaveModeStopper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tools\ConstantDisplayAndPowerHandler.cs" />
<Compile Include="Tools\DummyMessageDisplayer.cs" />
<Compile Include="Tools\MessageDisplayer.cs" />
<Compile Include="Tools\NAudioSoundAnalyser.cs" />
<Compile Include="Tools\SoundAnalyser.cs" />
<Compile Include="Domain\PowerManagement\PowerRequestContextHandler.cs" />
<Compile Include="Domain\MessageManagement\DummyMessageDisplayer.cs" />
<Compile Include="Domain\MessageManagement\MessageDisplayer.cs" />
<Compile Include="Domain\AudioManagement\NAudioSoundAnalyser.cs" />
<Compile Include="Domain\PowerManagement\SetThreadExecutionStateHandler.cs" />
<Compile Include="Domain\AudioManagement\CsCoreSoundAnalyser.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
16 changes: 0 additions & 16 deletions SpotifySleepModeStopper/Tools/DummyMessageDisplayer.cs

This file was deleted.

29 changes: 29 additions & 0 deletions SpotifySleepModeStopper/Tools/Repeat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace SpotifyTools.Tools
{
internal static class Repeat
{
public static Task Interval(
TimeSpan pollInterval,
Action action,
CancellationToken token)
{
// We don't use Observable.Interval:
// If we block, the values start bunching up behind each other.
return Task.Factory.StartNew(
() =>
{
for (;;)
{
if (token.WaitHandle.WaitOne(pollInterval))
break;

action();
}
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
}
}
5 changes: 4 additions & 1 deletion WindowsService/Service1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using System.Text;
using System.Threading.Tasks;
using SpotifyTools.Domain;
using SpotifyTools.Domain.AudioManagement;
using SpotifyTools.Domain.MessageManagement;
using SpotifyTools.Domain.PowerManagement;
using SpotifyTools.Tools;

namespace PreventSpotifyInterruptionService
Expand All @@ -19,7 +22,7 @@ public partial class Service1 : ServiceBase
public Service1()
{
InitializeComponent();
_spotifyAnalyser = new SpotifySaveModeStopper(new DummyMessageDisplayer(), new PreventSleepScreen(), new NAudioSoundAnalyser());
_spotifyAnalyser = new SpotifySaveModeStopper(new DummyMessageDisplayer(), new PowerRequestContextHandler(), new NAudioSoundAnalyser());
}

protected override void OnStart(string[] args)
Expand Down

0 comments on commit 34de5e8

Please sign in to comment.