Skip to content

Commit

Permalink
Preparing Backgroundog
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Sep 21, 2023
1 parent 7c283ca commit 7f47b5a
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 11 deletions.
87 changes: 83 additions & 4 deletions src/Project/Sucrose.Backgroundog/Helper/Condition.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,93 @@
namespace Sucrose.Backgroundog.Helper
using SBEL = Sucrose.Backgroundog.Extension.Lifecycle;
using SBMI = Sucrose.Backgroundog.Manage.Internal;
using SBMM = Sucrose.Backgroundog.Manage.Manager;
using SMMM = Sucrose.Manager.Manage.Manager;
using SSDECPT = Sucrose.Shared.Dependency.Enum.CategoryPerformanceType;
using SSDEPT = Sucrose.Shared.Dependency.Enum.PerformanceType;
using SSLHR = Sucrose.Shared.Live.Helper.Run;
using SSSHL = Sucrose.Shared.Space.Helper.Live;
using SSSHP = Sucrose.Shared.Space.Helper.Processor;

namespace Sucrose.Backgroundog.Helper
{
internal static class Condition
{
public static async Task Start()
{
Console.WriteLine("Condition");
//Performans şartları sağlanıp live motoru kapatıldıysa veya durdurulduysa
//Live motoruna gRPC (Live.json) ile durdur mesajı gönderilir

if (SBMI.Performance == SSDEPT.Pause && ((SBMI.App != null && SBMI.App.HasExited) || (SBMI.Live != null && SBMI.Live.HasExited)))
{
SBMI.Condition = false;
SBMI.Performance = SSDEPT.Resume;
SBMI.CategoryPerformance = SSDECPT.Not;
}
else
{
if (await CpuCondition())
{
return;
}
}

await Task.CompletedTask;
}

private static void Lifecycle()
{
if (SBMI.Performance == SSDEPT.Close)
{
SSLHR.Start();
}
else
{
SBMI.Live = SSSHL.Get();

if (SBMI.Live != null && !SBMI.Live.HasExited)
{
SBEL.Resume(SBMI.Live);
}

if (!string.IsNullOrEmpty(SMMM.App))
{
SBMI.App = SSSHP.Get(SMMM.App);

if (SBMI.App != null && !SBMI.App.HasExited)
{
SBEL.Resume(SBMI.App);
}
}
}
}

private static async Task<bool> CpuCondition()
{
if (SBMI.CategoryPerformance == SSDECPT.Cpu)
{
int Count = 0;
int MaxCount = 3;

while (SBMI.CpuData.Now < SMMM.CpuUsage || SBMM.CpuPerformance == SSDEPT.Resume)
{
if (Count >= MaxCount)
{
Lifecycle();
SBMI.Condition = false;
SBMI.Performance = SSDEPT.Resume;
SBMI.CategoryPerformance = SSDECPT.Not;

return true;
}
else
{
Count++;
}

await Task.Delay(TimeSpan.FromSeconds(1));
}
}

return false;
}
}
}
}
2 changes: 1 addition & 1 deletion src/Project/Sucrose.Backgroundog/Helper/Initialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private async void InitializeTimer_Callback(object State)
SBMI.Processing = false;
Console.WriteLine("Callback");

if (SSSHL.Run())
if (SSSHL.Run() && !SBMI.Condition)
{
await SBHP.Start();
}
Expand Down
74 changes: 73 additions & 1 deletion src/Project/Sucrose.Backgroundog/Helper/Performance.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
namespace Sucrose.Backgroundog.Helper
using SBEL = Sucrose.Backgroundog.Extension.Lifecycle;
using SBMI = Sucrose.Backgroundog.Manage.Internal;
using SBMM = Sucrose.Backgroundog.Manage.Manager;
using SMMM = Sucrose.Manager.Manage.Manager;
using SSDECPT = Sucrose.Shared.Dependency.Enum.CategoryPerformanceType;
using SSDEPT = Sucrose.Shared.Dependency.Enum.PerformanceType;
using SSLHK = Sucrose.Shared.Live.Helper.Kill;
using SSSHL = Sucrose.Shared.Space.Helper.Live;
using SSSHP = Sucrose.Shared.Space.Helper.Processor;

namespace Sucrose.Backgroundog.Helper
{
internal static class Performance
{
Expand All @@ -7,7 +17,69 @@ public static async Task Start()
Console.WriteLine("Run");
//Performans şartları kontrol edilecek...

if (await CpuPerformance())
{
return;
}

await Task.CompletedTask;
}

private static void Lifecycle()
{
if (SBMI.Performance == SSDEPT.Close)
{
SSLHK.Stop();
}
else
{
SBMI.Live = SSSHL.Get();

if (SBMI.Live != null && !SBMI.Live.HasExited)
{
SBEL.Suspend(SBMI.Live);
}

if (!string.IsNullOrEmpty(SMMM.App))
{
SBMI.App = SSSHP.Get(SMMM.App);

if (SBMI.App != null && !SBMI.App.HasExited)
{
SBEL.Suspend(SBMI.App);
}
}
}
}

private static async Task<bool> CpuPerformance()
{
if (SBMM.CpuPerformance != SSDEPT.Resume)
{
int Count = 0;
int MaxCount = 5;

while (SBMI.CpuData.State && SMMM.CpuUsage > 0 && SBMI.CpuData.Now >= SMMM.CpuUsage)
{
if (Count >= MaxCount)
{
SBMI.Performance = SBMM.CpuPerformance;
SBMI.CategoryPerformance = SSDECPT.Cpu;
SBMI.Condition = true;
Lifecycle();

return true;
}
else
{
Count++;
}

await Task.Delay(TimeSpan.FromSeconds(1));
}
}

return false;
}
}
}
10 changes: 10 additions & 0 deletions src/Project/Sucrose.Backgroundog/Manage/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using SBSDMDS = Sucrose.Backgroundog.Struct.Data.MotherboardStruct;
using SBSDMYS = Sucrose.Backgroundog.Struct.Data.MemoryStruct;
using SBSDNS = Sucrose.Backgroundog.Struct.Data.NetworkStruct;
using SSDECPT = Sucrose.Shared.Dependency.Enum.CategoryPerformanceType;
using SSDEPT = Sucrose.Shared.Dependency.Enum.PerformanceType;
using Timer = System.Threading.Timer;

namespace Sucrose.Backgroundog.Manage
Expand All @@ -20,6 +22,10 @@ internal static class Internal

public static int AppTime = 250;

public static Process App = null;

public static Process Live = null;

public static bool Condition = false;

public static bool Processing = true;
Expand All @@ -40,10 +46,14 @@ internal static class Internal

public static readonly object LockObject = new();

public static SSDEPT Performance = SSDEPT.Resume;

public static NowPlayingSession PlayingSession = null;

public static PerformanceCounter UploadCounter = null;

public static SSDECPT CategoryPerformance = SSDECPT.Not;

public static MediaPlaybackDataSource DataSource = null;

public static PerformanceCounter DownloadCounter = null;
Expand Down
15 changes: 14 additions & 1 deletion src/Project/Sucrose.Backgroundog/Manage/Manager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
using SMR = Sucrose.Memory.Readonly;
using SMC = Sucrose.Memory.Constant;
using SMMI = Sucrose.Manager.Manage.Internal;
using SMR = Sucrose.Memory.Readonly;
using SSDEPT = Sucrose.Shared.Dependency.Enum.PerformanceType;

namespace Sucrose.Backgroundog.Manage
{
internal static class Manager
{
public static SSDEPT NetworkPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMC.NetworkPerformance, SSDEPT.Resume);

public static SSDEPT BatteryPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMC.BatteryPerformance, SSDEPT.Pause);

public static SSDEPT MemoryPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMC.MemoryPerformance, SSDEPT.Pause);

public static SSDEPT SaverPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMC.SaverPerformance, SSDEPT.Pause);

public static SSDEPT CpuPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMC.CpuPerformance, SSDEPT.Pause);

public static bool Windows11_OrGreater => Environment.OSVersion.Version.Build >= 22000;

public static Mutex Mutex => new(true, SMR.BackgroundogMutex);
Expand Down
6 changes: 6 additions & 0 deletions src/Project/Sucrose.Backgroundog/Sucrose.Backgroundog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@
<PackageReference Include="LibreHardwareMonitorLib" Version="0.9.3-pre256" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net48'))">
<Reference Include="System.Web" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\gRPC\Sucrose.Grpc\Sucrose.Grpc.csproj" />
<ProjectReference Include="..\..\Library\Sucrose.Manager\Sucrose.Manager.csproj" />
</ItemGroup>

<Import Project="..\..\Shared\Sucrose.Shared.Live\Sucrose.Shared.Live.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Space\Sucrose.Shared.Space.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Theme\Sucrose.Shared.Theme.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Server\Sucrose.Shared.Server.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Watchdog\Sucrose.Shared.Watchdog.projitems" Label="Shared" />
<Import Project="..\..\Shared\Sucrose.Shared.Dependency\Sucrose.Shared.Dependency.projitems" Label="Shared" />
Expand Down
10 changes: 10 additions & 0 deletions src/Shared/Sucrose.Shared.Dependency/Enum/PerformanceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ internal enum PerformanceType
Pause,
Resume
}

internal enum CategoryPerformanceType
{
Not,
Cpu,
Saver,
Memory,
Network,
Battery
}
}
26 changes: 25 additions & 1 deletion src/Shared/Sucrose.Shared.Space/Helper/Live.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SSDEET = Sucrose.Shared.Dependency.Enum.EngineType;
using System.Diagnostics;
using SSDEET = Sucrose.Shared.Dependency.Enum.EngineType;
using SSSHP = Sucrose.Shared.Space.Helper.Processor;
using SSSMI = Sucrose.Shared.Space.Manage.Internal;

Expand Down Expand Up @@ -39,6 +40,29 @@ public static bool Run(string Live)
return false;
}

public static Process Get()
{
foreach (KeyValuePair<SSDEET, string> Pair in SSSMI.EngineLive)
{
if (SSSHP.Work(Pair.Value))
{
return SSSHP.Get(Pair.Value);
}
}

return null;
}

public static Process Get(SSDEET Live)
{
if (SSSHP.Work(SSSMI.EngineLive[Live]))
{
return SSSHP.Get(SSSMI.EngineLive[Live]);
}

return null;
}

public static void Kill()
{
foreach (KeyValuePair<SSDEET, string> Pair in SSSMI.EngineLive)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@
<Compile Include="$(MSBuildThisFileDirectory)Helper\Serialize.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helper\Various.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helper\Filter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helper\Zip.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
using SSTHV = Sucrose.Shared.Theme.Helper.Various;
using SSZHZ = Sucrose.Shared.Zip.Helper.Zip;

namespace Sucrose.Shared.Theme.Helper
namespace Sucrose.Shared.Zip.Helper
{
internal static class Zip
internal static class Archive
{
public static SSDECT Check(string Archive)
{
Expand Down
1 change: 1 addition & 0 deletions src/Shared/Sucrose.Shared.Zip/Sucrose.Shared.Zip.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Extension\Zip.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helper\Archive.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helper\Zip.cs" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions src/Sucrose.sln
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,10 @@ Global
Shared\Sucrose.Shared.Zip\Sucrose.Shared.Zip.projitems*{5e032b27-faf9-4821-aa35-7e2a49e1e90e}*SharedItemsImports = 5
Shared\Engine\Sucrose.Shared.Engine.Nebula\Sucrose.Shared.Engine.Nebula.projitems*{66354b15-cdd7-4744-bb1a-3703c08e7d3b}*SharedItemsImports = 13
Shared\Sucrose.Shared.Dependency\Sucrose.Shared.Dependency.projitems*{68fa689e-f4c8-4a71-a603-ed5773e29f9a}*SharedItemsImports = 5
Shared\Sucrose.Shared.Live\Sucrose.Shared.Live.projitems*{68fa689e-f4c8-4a71-a603-ed5773e29f9a}*SharedItemsImports = 5
Shared\Sucrose.Shared.Server\Sucrose.Shared.Server.projitems*{68fa689e-f4c8-4a71-a603-ed5773e29f9a}*SharedItemsImports = 5
Shared\Sucrose.Shared.Space\Sucrose.Shared.Space.projitems*{68fa689e-f4c8-4a71-a603-ed5773e29f9a}*SharedItemsImports = 5
Shared\Sucrose.Shared.Theme\Sucrose.Shared.Theme.projitems*{68fa689e-f4c8-4a71-a603-ed5773e29f9a}*SharedItemsImports = 5
Shared\Sucrose.Shared.Watchdog\Sucrose.Shared.Watchdog.projitems*{68fa689e-f4c8-4a71-a603-ed5773e29f9a}*SharedItemsImports = 5
Shared\Engine\Sucrose.Shared.Engine.MpvPlayer\Sucrose.Shared.Engine.MpvPlayer.projitems*{6d8a6597-da61-4972-a978-2942f076e4a4}*SharedItemsImports = 13
Shared\Sucrose.Shared.Live\Sucrose.Shared.Live.projitems*{72b01810-f520-460b-8bd3-2301aac6983b}*SharedItemsImports = 13
Expand Down

0 comments on commit 7f47b5a

Please sign in to comment.