From 975de1ea21642fbc5b5978efc8eb597142083419 Mon Sep 17 00:00:00 2001 From: Ted Date: Sun, 5 Jun 2016 14:59:50 -0700 Subject: [PATCH 01/12] First attempt at fix for isasue 93. 10 attempts to reconnect, 500 MS between each. --- Artemis/Artemis/Managers/DeviceManager.cs | 69 ++++++++++++++++------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index 88f9637b6..3a9cfe5a2 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using Artemis.DeviceProviders; using Artemis.Events; using Artemis.Services; @@ -93,33 +94,59 @@ namespace Artemis.Managers if (!wasNull) ReleaseActiveKeyboard(); - - // Disable everything if there's no active keyboard found - if (!keyboardProvider.CanEnable()) + bool asynchEnable = false; + if (keyboardProvider.CanEnable()) { - DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); - ActiveKeyboard = null; - General.Default.LastKeyboard = null; - General.Default.Save(); - _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); - ChangingKeyboard = false; - return; + FinishEnableKeyboard(keyboardProvider, oldKeyboard); } + else + { + for (int i = 1; i <= 10; i++) + { + var thread = new Thread( + () => + { + Thread.Sleep(500); + asynchEnable = keyboardProvider.CanEnable(); + }); + _logger.Warn("Failed enabling keyboard: {0}, re-attempt {1} of 10", keyboardProvider.Name, i); + thread.Start(); + if (asynchEnable) + break; + } - ActiveKeyboard = keyboardProvider; - ActiveKeyboard.Enable(); - - General.Default.LastKeyboard = ActiveKeyboard.Name; - General.Default.Save(); - - EnableUsableDevices(); - - ChangingKeyboard = false; - _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); - _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + if (!asynchEnable) + { + // Disable everything if there's no active keyboard found + DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); + ActiveKeyboard = null; + General.Default.LastKeyboard = null; + General.Default.Save(); + _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); + ChangingKeyboard = false; + return; + } + FinishEnableKeyboard(keyboardProvider, oldKeyboard); + } + } } + private void FinishEnableKeyboard(KeyboardProvider keyboardProvider, KeyboardProvider oldKeyboard) + { + ActiveKeyboard = keyboardProvider; + ActiveKeyboard.Enable(); + + General.Default.LastKeyboard = ActiveKeyboard.Name; + General.Default.Save(); + + EnableUsableDevices(); + + ChangingKeyboard = false; + _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); + _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + } + private void EnableUsableDevices() { foreach (var mouseProvider in MiceProviders) From 29033e942c5c601949b0cfe32e27cef49558f8b8 Mon Sep 17 00:00:00 2001 From: Ted Date: Sun, 5 Jun 2016 14:59:50 -0700 Subject: [PATCH 02/12] First attempt at fix for isasue 93. 10 attempts to reconnect, 500 MS between each. --- Artemis/Artemis/Managers/DeviceManager.cs | 69 ++++++++++++++++------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index 88f9637b6..3a9cfe5a2 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using Artemis.DeviceProviders; using Artemis.Events; using Artemis.Services; @@ -93,33 +94,59 @@ namespace Artemis.Managers if (!wasNull) ReleaseActiveKeyboard(); - - // Disable everything if there's no active keyboard found - if (!keyboardProvider.CanEnable()) + bool asynchEnable = false; + if (keyboardProvider.CanEnable()) { - DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); - ActiveKeyboard = null; - General.Default.LastKeyboard = null; - General.Default.Save(); - _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); - ChangingKeyboard = false; - return; + FinishEnableKeyboard(keyboardProvider, oldKeyboard); } + else + { + for (int i = 1; i <= 10; i++) + { + var thread = new Thread( + () => + { + Thread.Sleep(500); + asynchEnable = keyboardProvider.CanEnable(); + }); + _logger.Warn("Failed enabling keyboard: {0}, re-attempt {1} of 10", keyboardProvider.Name, i); + thread.Start(); + if (asynchEnable) + break; + } - ActiveKeyboard = keyboardProvider; - ActiveKeyboard.Enable(); - - General.Default.LastKeyboard = ActiveKeyboard.Name; - General.Default.Save(); - - EnableUsableDevices(); - - ChangingKeyboard = false; - _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); - _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + if (!asynchEnable) + { + // Disable everything if there's no active keyboard found + DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); + ActiveKeyboard = null; + General.Default.LastKeyboard = null; + General.Default.Save(); + _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); + ChangingKeyboard = false; + return; + } + FinishEnableKeyboard(keyboardProvider, oldKeyboard); + } + } } + private void FinishEnableKeyboard(KeyboardProvider keyboardProvider, KeyboardProvider oldKeyboard) + { + ActiveKeyboard = keyboardProvider; + ActiveKeyboard.Enable(); + + General.Default.LastKeyboard = ActiveKeyboard.Name; + General.Default.Save(); + + EnableUsableDevices(); + + ChangingKeyboard = false; + _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); + _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + } + private void EnableUsableDevices() { foreach (var mouseProvider in MiceProviders) From b034553bd753ffbe6c133674ea93c338ca92bb36 Mon Sep 17 00:00:00 2001 From: Ted Date: Sun, 5 Jun 2016 14:59:50 -0700 Subject: [PATCH 03/12] First attempt at fix for isasue 93. 10 attempts to reconnect, 500 MS between each. --- Artemis/Artemis/Managers/DeviceManager.cs | 69 ++++++++++++++++------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index 88f9637b6..3a9cfe5a2 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using Artemis.DeviceProviders; using Artemis.Events; using Artemis.Services; @@ -93,33 +94,59 @@ namespace Artemis.Managers if (!wasNull) ReleaseActiveKeyboard(); - - // Disable everything if there's no active keyboard found - if (!keyboardProvider.CanEnable()) + bool asynchEnable = false; + if (keyboardProvider.CanEnable()) { - DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); - ActiveKeyboard = null; - General.Default.LastKeyboard = null; - General.Default.Save(); - _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); - ChangingKeyboard = false; - return; + FinishEnableKeyboard(keyboardProvider, oldKeyboard); } + else + { + for (int i = 1; i <= 10; i++) + { + var thread = new Thread( + () => + { + Thread.Sleep(500); + asynchEnable = keyboardProvider.CanEnable(); + }); + _logger.Warn("Failed enabling keyboard: {0}, re-attempt {1} of 10", keyboardProvider.Name, i); + thread.Start(); + if (asynchEnable) + break; + } - ActiveKeyboard = keyboardProvider; - ActiveKeyboard.Enable(); - - General.Default.LastKeyboard = ActiveKeyboard.Name; - General.Default.Save(); - - EnableUsableDevices(); - - ChangingKeyboard = false; - _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); - _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + if (!asynchEnable) + { + // Disable everything if there's no active keyboard found + DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); + ActiveKeyboard = null; + General.Default.LastKeyboard = null; + General.Default.Save(); + _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); + ChangingKeyboard = false; + return; + } + FinishEnableKeyboard(keyboardProvider, oldKeyboard); + } + } } + private void FinishEnableKeyboard(KeyboardProvider keyboardProvider, KeyboardProvider oldKeyboard) + { + ActiveKeyboard = keyboardProvider; + ActiveKeyboard.Enable(); + + General.Default.LastKeyboard = ActiveKeyboard.Name; + General.Default.Save(); + + EnableUsableDevices(); + + ChangingKeyboard = false; + _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); + _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + } + private void EnableUsableDevices() { foreach (var mouseProvider in MiceProviders) From fdd17739ef289b2a3da7935ab162f286030a68bf Mon Sep 17 00:00:00 2001 From: Ted Date: Sun, 5 Jun 2016 14:59:50 -0700 Subject: [PATCH 04/12] First attempt at fix for isasue 93. 10 attempts to reconnect, 500 MS between each. --- Artemis/Artemis/Managers/DeviceManager.cs | 69 ++++++++++++++++------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index 88f9637b6..3a9cfe5a2 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using Artemis.DeviceProviders; using Artemis.Events; using Artemis.Services; @@ -93,33 +94,59 @@ namespace Artemis.Managers if (!wasNull) ReleaseActiveKeyboard(); - - // Disable everything if there's no active keyboard found - if (!keyboardProvider.CanEnable()) + bool asynchEnable = false; + if (keyboardProvider.CanEnable()) { - DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); - ActiveKeyboard = null; - General.Default.LastKeyboard = null; - General.Default.Save(); - _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); - ChangingKeyboard = false; - return; + FinishEnableKeyboard(keyboardProvider, oldKeyboard); } + else + { + for (int i = 1; i <= 10; i++) + { + var thread = new Thread( + () => + { + Thread.Sleep(500); + asynchEnable = keyboardProvider.CanEnable(); + }); + _logger.Warn("Failed enabling keyboard: {0}, re-attempt {1} of 10", keyboardProvider.Name, i); + thread.Start(); + if (asynchEnable) + break; + } - ActiveKeyboard = keyboardProvider; - ActiveKeyboard.Enable(); - - General.Default.LastKeyboard = ActiveKeyboard.Name; - General.Default.Save(); - - EnableUsableDevices(); - - ChangingKeyboard = false; - _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); - _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + if (!asynchEnable) + { + // Disable everything if there's no active keyboard found + DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); + ActiveKeyboard = null; + General.Default.LastKeyboard = null; + General.Default.Save(); + _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); + ChangingKeyboard = false; + return; + } + FinishEnableKeyboard(keyboardProvider, oldKeyboard); + } + } } + private void FinishEnableKeyboard(KeyboardProvider keyboardProvider, KeyboardProvider oldKeyboard) + { + ActiveKeyboard = keyboardProvider; + ActiveKeyboard.Enable(); + + General.Default.LastKeyboard = ActiveKeyboard.Name; + General.Default.Save(); + + EnableUsableDevices(); + + ChangingKeyboard = false; + _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); + _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + } + private void EnableUsableDevices() { foreach (var mouseProvider in MiceProviders) From cde523870a4ffcc64ed4d8a89a379435f0df7231 Mon Sep 17 00:00:00 2001 From: Ted Date: Sat, 11 Jun 2016 15:02:45 -0700 Subject: [PATCH 05/12] Added Overall CPU and RAM resource monitors for Windows Profile. --- .../WindowsProfile/WindowsProfileDataModel.cs | 8 ++ .../WindowsProfile/WindowsProfileModel.cs | 79 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs index de9bc9206..c44c5a36a 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs @@ -8,9 +8,11 @@ namespace Artemis.Modules.Effects.WindowsProfile { Spotify = new Spotify(); Cpu = new CpuDataModel(); + Performance = new PerformanceDataModel(); } public CpuDataModel Cpu { get; set; } + public PerformanceDataModel Performance { get; set; } public Spotify Spotify { get; set; } } @@ -26,6 +28,12 @@ namespace Artemis.Modules.Effects.WindowsProfile public int Core8Usage { get; set; } } + public class PerformanceDataModel + { + public int CPUUsage { get; set; } + public int RAMUsage { get; set; } + } + public class Spotify { public bool Running { get; set; } diff --git a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs index a1537c89b..b701db0cc 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs @@ -7,12 +7,68 @@ using Artemis.Managers; using Artemis.Models; using Artemis.Models.Profiles; using SpotifyAPI.Local; +using System.Runtime.InteropServices; namespace Artemis.Modules.Effects.WindowsProfile { + static class PerformanceInfo + { + [DllImport("psapi.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size); + + [StructLayout(LayoutKind.Sequential)] + public struct PerformanceInformation + { + public int Size; + public IntPtr CommitTotal; + public IntPtr CommitLimit; + public IntPtr CommitPeak; + public IntPtr PhysicalTotal; + public IntPtr PhysicalAvailable; + public IntPtr SystemCache; + public IntPtr KernelTotal; + public IntPtr KernelPaged; + public IntPtr KernelNonPaged; + public IntPtr PageSize; + public int HandlesCount; + public int ProcessCount; + public int ThreadCount; + } + + public static Int64 GetPhysicalAvailableMemoryInMiB() + { + PerformanceInformation pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64((pi.PhysicalAvailable.ToInt64() * pi.PageSize.ToInt64() / 1048576)); + } + else + { + return -1; + } + + } + + public static Int64 GetTotalMemoryInMiB() + { + PerformanceInformation pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64((pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64() / 1048576)); + } + else + { + return -1; + } + + } + } + public class WindowsProfileModel : EffectModel { private List _cores; + private PerformanceCounter _overallCPU; private int _cpuFrames; private SpotifyLocalAPI _spotify; private bool _spotifySetupBusy; @@ -57,6 +113,8 @@ namespace Artemis.Modules.Effects.WindowsProfile _cores.Add(null); coreCount++; } + + _overallCPU = GetOverallPerformanceCounter(); } private void UpdateCpu(WindowsProfileDataModel dataModel) @@ -85,6 +143,16 @@ namespace Artemis.Modules.Effects.WindowsProfile dataModel.Cpu.Core7Usage = (int) _cores[6].NextValue(); if (_cores[7] != null) dataModel.Cpu.Core8Usage = (int) _cores[7].NextValue(); + + //From Ted - Let's get overall RAM and CPU usage here + dataModel.Performance.CPUUsage = (int)_overallCPU.NextValue(); + + Int64 phav = PerformanceInfo.GetPhysicalAvailableMemoryInMiB(); + Int64 tot = PerformanceInfo.GetTotalMemoryInMiB(); + decimal percentFree = ((decimal)phav / (decimal)tot) * 100; + decimal percentOccupied = 100 - percentFree; + + dataModel.Performance.RAMUsage = (int)percentOccupied; } public override List GetRenderLayers(bool renderMice, bool renderHeadsets) @@ -92,6 +160,17 @@ namespace Artemis.Modules.Effects.WindowsProfile return Profile.GetRenderLayers(DataModel, renderMice, renderHeadsets, false); } + public static PerformanceCounter GetOverallPerformanceCounter() + { + PerformanceCounter cpuCounter = new PerformanceCounter(); + + cpuCounter.CategoryName = "Processor"; + cpuCounter.CounterName = "% Processor Time"; + cpuCounter.InstanceName = "_Total"; + + return cpuCounter; + } + public static List GetPerformanceCounters() { var performanceCounters = new List(); From b5de0bed70feaaa847083900ddd8379f041a7170 Mon Sep 17 00:00:00 2001 From: Ted Date: Sat, 11 Jun 2016 15:02:45 -0700 Subject: [PATCH 06/12] Added Overall CPU and RAM resource monitors for Windows Profile. --- .../WindowsProfile/WindowsProfileDataModel.cs | 8 ++ .../WindowsProfile/WindowsProfileModel.cs | 80 ++++++++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs index de9bc9206..c44c5a36a 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs @@ -8,9 +8,11 @@ namespace Artemis.Modules.Effects.WindowsProfile { Spotify = new Spotify(); Cpu = new CpuDataModel(); + Performance = new PerformanceDataModel(); } public CpuDataModel Cpu { get; set; } + public PerformanceDataModel Performance { get; set; } public Spotify Spotify { get; set; } } @@ -26,6 +28,12 @@ namespace Artemis.Modules.Effects.WindowsProfile public int Core8Usage { get; set; } } + public class PerformanceDataModel + { + public int CPUUsage { get; set; } + public int RAMUsage { get; set; } + } + public class Spotify { public bool Running { get; set; } diff --git a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs index b709dcde3..630f5bd01 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs @@ -8,13 +8,69 @@ using Artemis.Models; using Artemis.Models.Profiles; using Ninject.Extensions.Logging; using SpotifyAPI.Local; +using System.Runtime.InteropServices; namespace Artemis.Modules.Effects.WindowsProfile { + static class PerformanceInfo + { + [DllImport("psapi.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size); + + [StructLayout(LayoutKind.Sequential)] + public struct PerformanceInformation + { + public int Size; + public IntPtr CommitTotal; + public IntPtr CommitLimit; + public IntPtr CommitPeak; + public IntPtr PhysicalTotal; + public IntPtr PhysicalAvailable; + public IntPtr SystemCache; + public IntPtr KernelTotal; + public IntPtr KernelPaged; + public IntPtr KernelNonPaged; + public IntPtr PageSize; + public int HandlesCount; + public int ProcessCount; + public int ThreadCount; + } + + public static Int64 GetPhysicalAvailableMemoryInMiB() + { + PerformanceInformation pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64((pi.PhysicalAvailable.ToInt64() * pi.PageSize.ToInt64() / 1048576)); + } + else + { + return -1; + } + + } + + public static Int64 GetTotalMemoryInMiB() + { + PerformanceInformation pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64((pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64() / 1048576)); + } + else + { + return -1; + } + + } + } + public class WindowsProfileModel : EffectModel { private readonly ILogger _logger; private List _cores; + private PerformanceCounter _overallCPU; private int _cpuFrames; private SpotifyLocalAPI _spotify; private bool _spotifySetupBusy; @@ -63,6 +119,7 @@ namespace Artemis.Modules.Effects.WindowsProfile _cores.Add(null); coreCount++; } + _overallCPU = GetOverallPerformanceCounter(); } catch (InvalidOperationException) { @@ -73,7 +130,7 @@ namespace Artemis.Modules.Effects.WindowsProfile private void UpdateCpu(WindowsProfileDataModel dataModel) { - if (_cores == null) + if (_cores == null || _overallCPU == null) return; // CPU is only updated every 15 frames, the performance counter gives 0 if updated too often @@ -100,6 +157,16 @@ namespace Artemis.Modules.Effects.WindowsProfile dataModel.Cpu.Core7Usage = (int) _cores[6].NextValue(); if (_cores[7] != null) dataModel.Cpu.Core8Usage = (int) _cores[7].NextValue(); + + //From Ted - Let's get overall RAM and CPU usage here + dataModel.Performance.CPUUsage = (int)_overallCPU.NextValue(); + + Int64 phav = PerformanceInfo.GetPhysicalAvailableMemoryInMiB(); + Int64 tot = PerformanceInfo.GetTotalMemoryInMiB(); + decimal percentFree = ((decimal)phav / (decimal)tot) * 100; + decimal percentOccupied = 100 - percentFree; + + dataModel.Performance.RAMUsage = (int)percentOccupied; } public override List GetRenderLayers(bool renderMice, bool renderHeadsets) @@ -107,6 +174,17 @@ namespace Artemis.Modules.Effects.WindowsProfile return Profile.GetRenderLayers(DataModel, renderMice, renderHeadsets, false); } + public static PerformanceCounter GetOverallPerformanceCounter() + { + PerformanceCounter cpuCounter = new PerformanceCounter(); + + cpuCounter.CategoryName = "Processor"; + cpuCounter.CounterName = "% Processor Time"; + cpuCounter.InstanceName = "_Total"; + + return cpuCounter; + } + public static List GetPerformanceCounters() { var performanceCounters = new List(); From 3734874d54ab08fedd6c66cf9da1bd6603b3515d Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Sun, 12 Jun 2016 10:51:35 +0200 Subject: [PATCH 07/12] Disabled Bubbles for now, moved CPUUsage to Cpu class in Windows Profile --- .../InjectionModules/ArtemisModules.cs | 2 +- .../WindowsProfile/WindowsProfileDataModel.cs | 2 +- .../WindowsProfile/WindowsProfileModel.cs | 87 +++++++++---------- 3 files changed, 41 insertions(+), 50 deletions(-) diff --git a/Artemis/Artemis/InjectionModules/ArtemisModules.cs b/Artemis/Artemis/InjectionModules/ArtemisModules.cs index 2ec4602f1..531797855 100644 --- a/Artemis/Artemis/InjectionModules/ArtemisModules.cs +++ b/Artemis/Artemis/InjectionModules/ArtemisModules.cs @@ -27,7 +27,7 @@ namespace Artemis.InjectionModules // Effects Bind().To().InSingletonScope(); Bind().To().InSingletonScope(); - Bind().To().InSingletonScope(); + //Bind().To().InSingletonScope(); TODO: Performance Bind().To().InSingletonScope(); // Games diff --git a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs index c44c5a36a..be27cad22 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs @@ -18,6 +18,7 @@ namespace Artemis.Modules.Effects.WindowsProfile public class CpuDataModel { + public int TotalUsage { get; set; } public int Core1Usage { get; set; } public int Core2Usage { get; set; } public int Core3Usage { get; set; } @@ -30,7 +31,6 @@ namespace Artemis.Modules.Effects.WindowsProfile public class PerformanceDataModel { - public int CPUUsage { get; set; } public int RAMUsage { get; set; } } diff --git a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs index 630f5bd01..03d1843ee 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Artemis.Managers; @@ -8,15 +9,34 @@ using Artemis.Models; using Artemis.Models.Profiles; using Ninject.Extensions.Logging; using SpotifyAPI.Local; -using System.Runtime.InteropServices; namespace Artemis.Modules.Effects.WindowsProfile { - static class PerformanceInfo + internal static class PerformanceInfo { [DllImport("psapi.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size); + public static extern bool GetPerformanceInfo([Out] out PerformanceInformation performanceInformation, [In] int size); + + public static long GetPhysicalAvailableMemoryInMiB() + { + var pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64(pi.PhysicalAvailable.ToInt64()*pi.PageSize.ToInt64()/1048576); + } + return -1; + } + + public static long GetTotalMemoryInMiB() + { + var pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64(pi.PhysicalTotal.ToInt64()*pi.PageSize.ToInt64()/1048576); + } + return -1; + } [StructLayout(LayoutKind.Sequential)] public struct PerformanceInformation @@ -36,45 +56,16 @@ namespace Artemis.Modules.Effects.WindowsProfile public int ProcessCount; public int ThreadCount; } - - public static Int64 GetPhysicalAvailableMemoryInMiB() - { - PerformanceInformation pi = new PerformanceInformation(); - if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) - { - return Convert.ToInt64((pi.PhysicalAvailable.ToInt64() * pi.PageSize.ToInt64() / 1048576)); - } - else - { - return -1; - } - - } - - public static Int64 GetTotalMemoryInMiB() - { - PerformanceInformation pi = new PerformanceInformation(); - if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) - { - return Convert.ToInt64((pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64() / 1048576)); - } - else - { - return -1; - } - - } } public class WindowsProfileModel : EffectModel { private readonly ILogger _logger; private List _cores; - private PerformanceCounter _overallCPU; private int _cpuFrames; + private PerformanceCounter _overallCpu; private SpotifyLocalAPI _spotify; private bool _spotifySetupBusy; - private bool _triedCpuFix; public WindowsProfileModel(ILogger logger, MainManager mainManager, WindowsProfileSettings settings) : base(mainManager, new WindowsProfileDataModel()) @@ -119,20 +110,19 @@ namespace Artemis.Modules.Effects.WindowsProfile _cores.Add(null); coreCount++; } - _overallCPU = GetOverallPerformanceCounter(); + _overallCpu = GetOverallPerformanceCounter(); } catch (InvalidOperationException) { _logger.Warn("Failed to setup CPU information, try running \"lodctr /R\" as administrator."); } - } private void UpdateCpu(WindowsProfileDataModel dataModel) { - if (_cores == null || _overallCPU == null) + if (_cores == null || _overallCpu == null) return; - + // CPU is only updated every 15 frames, the performance counter gives 0 if updated too often _cpuFrames++; if (_cpuFrames < 16) @@ -159,14 +149,14 @@ namespace Artemis.Modules.Effects.WindowsProfile dataModel.Cpu.Core8Usage = (int) _cores[7].NextValue(); //From Ted - Let's get overall RAM and CPU usage here - dataModel.Performance.CPUUsage = (int)_overallCPU.NextValue(); + dataModel.Cpu.TotalUsage = (int) _overallCpu.NextValue(); - Int64 phav = PerformanceInfo.GetPhysicalAvailableMemoryInMiB(); - Int64 tot = PerformanceInfo.GetTotalMemoryInMiB(); - decimal percentFree = ((decimal)phav / (decimal)tot) * 100; - decimal percentOccupied = 100 - percentFree; + var phav = PerformanceInfo.GetPhysicalAvailableMemoryInMiB(); + var tot = PerformanceInfo.GetTotalMemoryInMiB(); + var percentFree = phav/(decimal) tot*100; + var percentOccupied = 100 - percentFree; - dataModel.Performance.RAMUsage = (int)percentOccupied; + dataModel.Performance.RAMUsage = (int) percentOccupied; } public override List GetRenderLayers(bool renderMice, bool renderHeadsets) @@ -176,11 +166,12 @@ namespace Artemis.Modules.Effects.WindowsProfile public static PerformanceCounter GetOverallPerformanceCounter() { - PerformanceCounter cpuCounter = new PerformanceCounter(); - - cpuCounter.CategoryName = "Processor"; - cpuCounter.CounterName = "% Processor Time"; - cpuCounter.InstanceName = "_Total"; + var cpuCounter = new PerformanceCounter + { + CategoryName = "Processor", + CounterName = "% Processor Time", + InstanceName = "_Total" + }; return cpuCounter; } From f1f7851e121f7312504e7ebee6d53ec9dfc2e040 Mon Sep 17 00:00:00 2001 From: Thoth2020 Date: Sun, 12 Jun 2016 15:10:02 -0700 Subject: [PATCH 08/12] Update DeviceManager.cs Attempt #2 at Issue 93 fix --- Artemis/Artemis/Managers/DeviceManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index 3a9cfe5a2..0ccf65536 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -106,7 +106,7 @@ namespace Artemis.Managers var thread = new Thread( () => { - Thread.Sleep(500); + Thread.Sleep(500 * i); asynchEnable = keyboardProvider.CanEnable(); }); _logger.Warn("Failed enabling keyboard: {0}, re-attempt {1} of 10", keyboardProvider.Name, i); @@ -184,4 +184,4 @@ namespace Artemis.Managers } } } -} \ No newline at end of file +} From fe6788d9ef0345b5531809c7c18f98528cfae855 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Mon, 13 Jun 2016 11:38:46 +0200 Subject: [PATCH 09/12] Replaced DynamicLinq with DynamicExpresso --- Artemis/Artemis/Artemis.csproj | 8 ++-- .../Models/Profiles/LayerConditionModel.cs | 41 ++++++++++++++----- .../Modules/Effects/Bubbles/BubblesModel.cs | 2 +- Artemis/Artemis/packages.config | 2 +- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index a97a823b3..13907072e 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -148,6 +148,10 @@ ..\packages\CUE.NET.1.0.3\lib\net45\CUE.NET.dll True + + ..\packages\DynamicExpresso.Core.1.3.1.0\lib\net40\DynamicExpresso.Core.dll + True + ..\packages\gong-wpf-dragdrop.0.1.4.3\lib\net40\GongSolutions.Wpf.DragDrop.dll True @@ -205,10 +209,6 @@ - - ..\packages\System.Linq.Dynamic.1.0.6\lib\net40\System.Linq.Dynamic.dll - True - diff --git a/Artemis/Artemis/Models/Profiles/LayerConditionModel.cs b/Artemis/Artemis/Models/Profiles/LayerConditionModel.cs index 7b6fc0fb1..ee659913e 100644 --- a/Artemis/Artemis/Models/Profiles/LayerConditionModel.cs +++ b/Artemis/Artemis/Models/Profiles/LayerConditionModel.cs @@ -1,12 +1,19 @@ -using System.Collections.Generic; -using System.Linq.Dynamic; +using System; using Artemis.Models.Interfaces; using Artemis.Utilities; +using DynamicExpresso; namespace Artemis.Models.Profiles { public class LayerConditionModel { + private readonly Interpreter _interpreter; + + public LayerConditionModel() + { + _interpreter = new Interpreter(); + } + public string Field { get; set; } public string Value { get; set; } public string Operator { get; set; } @@ -22,15 +29,29 @@ namespace Artemis.Models.Profiles return false; // Put the subject in a list, allowing Dynamic Linq to be used. - var subjectList = new List {(T) subject}; - bool res; if (Type == "String") - res = subjectList.Where($"{Field}.ToLower() {Operator} @0", Value.ToLower()).Any(); - else if (Type == "Enum") - res = subjectList.Where($"{Field} {Operator} \"{Value}\"").Any(); - else - res = subjectList.Where($"{Field} {Operator} {Value}").Any(); - return res; + { + return _interpreter.Eval($"subject.{Field}.ToLower() {Operator} value", + new Parameter("subject", typeof(T), subject), + new Parameter("value", Value.ToLower())); + } + + Parameter rightParam = null; + switch (Type) + { + case "Enum": + var enumType = _interpreter.Eval($"subject.{Field}.GetType()", new Parameter("subject", typeof(T), subject)); + rightParam = new Parameter("value", Enum.Parse(enumType, Value)); + break; + case "Boolean": + rightParam = new Parameter("value", bool.Parse(Value)); + break; + case "Int32": + rightParam = new Parameter("value", int.Parse(Value)); + break; + } + + return _interpreter.Eval($"subject.{Field} {Operator} value", new Parameter("subject", typeof(T), subject), rightParam); } } } \ No newline at end of file diff --git a/Artemis/Artemis/Modules/Effects/Bubbles/BubblesModel.cs b/Artemis/Artemis/Modules/Effects/Bubbles/BubblesModel.cs index 415c2cdd8..4f3c2cc86 100644 --- a/Artemis/Artemis/Modules/Effects/Bubbles/BubblesModel.cs +++ b/Artemis/Artemis/Modules/Effects/Bubbles/BubblesModel.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; -using System.Linq.Dynamic; +using System.Linq; using System.Windows; using Artemis.Managers; using Artemis.Models; diff --git a/Artemis/Artemis/packages.config b/Artemis/Artemis/packages.config index 48f0b98da..9c566c400 100644 --- a/Artemis/Artemis/packages.config +++ b/Artemis/Artemis/packages.config @@ -5,6 +5,7 @@ + @@ -20,7 +21,6 @@ - \ No newline at end of file From d92970913bf406253843f86bbd81287bf22436db Mon Sep 17 00:00:00 2001 From: Ted Date: Sun, 5 Jun 2016 14:59:50 -0700 Subject: [PATCH 10/12] First attempt at fix for isasue 93. 10 attempts to reconnect, 500 MS between each. --- Artemis/Artemis/Managers/DeviceManager.cs | 69 ++++++++++++++++------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index 88f9637b6..3a9cfe5a2 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using Artemis.DeviceProviders; using Artemis.Events; using Artemis.Services; @@ -93,33 +94,59 @@ namespace Artemis.Managers if (!wasNull) ReleaseActiveKeyboard(); - - // Disable everything if there's no active keyboard found - if (!keyboardProvider.CanEnable()) + bool asynchEnable = false; + if (keyboardProvider.CanEnable()) { - DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); - ActiveKeyboard = null; - General.Default.LastKeyboard = null; - General.Default.Save(); - _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); - ChangingKeyboard = false; - return; + FinishEnableKeyboard(keyboardProvider, oldKeyboard); } + else + { + for (int i = 1; i <= 10; i++) + { + var thread = new Thread( + () => + { + Thread.Sleep(500); + asynchEnable = keyboardProvider.CanEnable(); + }); + _logger.Warn("Failed enabling keyboard: {0}, re-attempt {1} of 10", keyboardProvider.Name, i); + thread.Start(); + if (asynchEnable) + break; + } - ActiveKeyboard = keyboardProvider; - ActiveKeyboard.Enable(); - - General.Default.LastKeyboard = ActiveKeyboard.Name; - General.Default.Save(); - - EnableUsableDevices(); - - ChangingKeyboard = false; - _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); - _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + if (!asynchEnable) + { + // Disable everything if there's no active keyboard found + DialogService.ShowErrorMessageBox(keyboardProvider.CantEnableText); + ActiveKeyboard = null; + General.Default.LastKeyboard = null; + General.Default.Save(); + _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); + ChangingKeyboard = false; + return; + } + FinishEnableKeyboard(keyboardProvider, oldKeyboard); + } + } } + private void FinishEnableKeyboard(KeyboardProvider keyboardProvider, KeyboardProvider oldKeyboard) + { + ActiveKeyboard = keyboardProvider; + ActiveKeyboard.Enable(); + + General.Default.LastKeyboard = ActiveKeyboard.Name; + General.Default.Save(); + + EnableUsableDevices(); + + ChangingKeyboard = false; + _events.PublishOnUIThread(new ActiveKeyboardChanged(oldKeyboard, ActiveKeyboard)); + _logger.Debug("Enabled keyboard: {0}", keyboardProvider.Name); + } + private void EnableUsableDevices() { foreach (var mouseProvider in MiceProviders) From 88dd1cf9d0da6393f444f6e38744840c8c930319 Mon Sep 17 00:00:00 2001 From: Thoth2020 Date: Sun, 12 Jun 2016 15:10:02 -0700 Subject: [PATCH 11/12] Update DeviceManager.cs Attempt #2 at Issue 93 fix --- Artemis/Artemis/Managers/DeviceManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index 3a9cfe5a2..0ccf65536 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -106,7 +106,7 @@ namespace Artemis.Managers var thread = new Thread( () => { - Thread.Sleep(500); + Thread.Sleep(500 * i); asynchEnable = keyboardProvider.CanEnable(); }); _logger.Warn("Failed enabling keyboard: {0}, re-attempt {1} of 10", keyboardProvider.Name, i); @@ -184,4 +184,4 @@ namespace Artemis.Managers } } } -} \ No newline at end of file +} From bfd31d4cdf2bcab84d6b6bd9020f9a4e0ee4027c Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Mon, 13 Jun 2016 11:50:46 +0200 Subject: [PATCH 12/12] Formatting --- Artemis/Artemis/Managers/DeviceManager.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index 0ccf65536..64c922764 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -94,22 +94,23 @@ namespace Artemis.Managers if (!wasNull) ReleaseActiveKeyboard(); - bool asynchEnable = false; + var asynchEnable = false; if (keyboardProvider.CanEnable()) { FinishEnableKeyboard(keyboardProvider, oldKeyboard); } else { - for (int i = 1; i <= 10; i++) + for (var i = 1; i <= 10; i++) { - var thread = new Thread( - () => + var thread = new Thread(() => { - Thread.Sleep(500 * i); + Thread.Sleep(500*i); asynchEnable = keyboardProvider.CanEnable(); }); + _logger.Warn("Failed enabling keyboard: {0}, re-attempt {1} of 10", keyboardProvider.Name, i); + thread.Start(); if (asynchEnable) break; @@ -125,10 +126,9 @@ namespace Artemis.Managers _logger.Warn("Failed enabling keyboard: {0}", keyboardProvider.Name); ChangingKeyboard = false; return; - } + } FinishEnableKeyboard(keyboardProvider, oldKeyboard); } - } } @@ -184,4 +184,4 @@ namespace Artemis.Managers } } } -} +} \ No newline at end of file