diff --git a/SteelSeriesAPI/Exceptions/SteelSeriesNotRunningException.cs b/SteelSeriesAPI/Exceptions/SteelSeriesNotRunningException.cs new file mode 100644 index 0000000..a2de28a --- /dev/null +++ b/SteelSeriesAPI/Exceptions/SteelSeriesNotRunningException.cs @@ -0,0 +1,8 @@ +namespace SteelSeriesAPI.Exceptions; + +public class SteelSeriesNotRunningException : Exception +{ + public SteelSeriesNotRunningException() : base("SteelSeries is not running.") { } + public SteelSeriesNotRunningException(string message) : base(message) { } + public SteelSeriesNotRunningException(string message, Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/SteelSeriesAPI/Sonar/Exceptions/ConfigNotFound.cs b/SteelSeriesAPI/Sonar/Exceptions/ConfigNotFound.cs new file mode 100644 index 0000000..4632d17 --- /dev/null +++ b/SteelSeriesAPI/Sonar/Exceptions/ConfigNotFound.cs @@ -0,0 +1,8 @@ +namespace SteelSeriesAPI.Sonar.Exceptions; + +public class ConfigNotFound : Exception +{ + public ConfigNotFound() : base("No audio configuration found.") { } + public ConfigNotFound(string message) : base(message) { } + public ConfigNotFound(string message, Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/SteelSeriesAPI/Sonar/Exceptions/MasterChannelNotSupported.cs b/SteelSeriesAPI/Sonar/Exceptions/MasterChannelNotSupported.cs new file mode 100644 index 0000000..f16c9a8 --- /dev/null +++ b/SteelSeriesAPI/Sonar/Exceptions/MasterChannelNotSupported.cs @@ -0,0 +1,8 @@ +namespace SteelSeriesAPI.Sonar.Exceptions; + +public class MasterChannelNotSupported : Exception +{ + public MasterChannelNotSupported() : base("Master Channel is not supported in this case.") { } + public MasterChannelNotSupported(string message) : base(message) { } + public MasterChannelNotSupported(string message, Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/SteelSeriesAPI/Sonar/Exceptions/MicChannelSupportOnly.cs b/SteelSeriesAPI/Sonar/Exceptions/MicChannelSupportOnly.cs new file mode 100644 index 0000000..32fdc11 --- /dev/null +++ b/SteelSeriesAPI/Sonar/Exceptions/MicChannelSupportOnly.cs @@ -0,0 +1,8 @@ +namespace SteelSeriesAPI.Sonar.Exceptions; + +public class MicChannelSupportOnly : Exception +{ + public MicChannelSupportOnly() : base("Only the Mic Channel is supported in this case.") { } + public MicChannelSupportOnly(string message) : base(message) { } + public MicChannelSupportOnly(string message, Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/SteelSeriesAPI/Sonar/Exceptions/SonarListenerNotConnected.cs b/SteelSeriesAPI/Sonar/Exceptions/SonarListenerNotConnected.cs new file mode 100644 index 0000000..cbce5b9 --- /dev/null +++ b/SteelSeriesAPI/Sonar/Exceptions/SonarListenerNotConnected.cs @@ -0,0 +1,8 @@ +namespace SteelSeriesAPI.Sonar.Exceptions; + +public class SonarListenerNotConnected : Exception +{ + public SonarListenerNotConnected() : base("Listener need to be connected before listening") { } + public SonarListenerNotConnected(string message) : base(message) { } + public SonarListenerNotConnected(string message, Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/SteelSeriesAPI/Sonar/Exceptions/SonarNotRunningException.cs b/SteelSeriesAPI/Sonar/Exceptions/SonarNotRunningException.cs new file mode 100644 index 0000000..96b60e8 --- /dev/null +++ b/SteelSeriesAPI/Sonar/Exceptions/SonarNotRunningException.cs @@ -0,0 +1,8 @@ +namespace SteelSeriesAPI.Sonar.Exceptions; + +public class SonarNotRunningException : Exception +{ + public SonarNotRunningException() : base("Sonar is not running.") { } + public SonarNotRunningException(string message) : base(message) { } + public SonarNotRunningException(string message, Exception innerException) : base(message, innerException) { } +} \ No newline at end of file diff --git a/SteelSeriesAPI/Sonar/Http/HttpFetcher.cs b/SteelSeriesAPI/Sonar/Http/HttpFetcher.cs index c399a60..b8de29e 100644 --- a/SteelSeriesAPI/Sonar/Http/HttpFetcher.cs +++ b/SteelSeriesAPI/Sonar/Http/HttpFetcher.cs @@ -1,6 +1,8 @@ -using System.Text.Json; +using SteelSeriesAPI.Sonar.Exceptions; using SteelSeriesAPI.Interfaces; +using System.Text.Json; + namespace SteelSeriesAPI.Sonar.Http; public class HttpFetcher @@ -19,15 +21,43 @@ public HttpFetcher() public JsonDocument Provide(string targetHttp) { - JsonDocument response = JsonDocument.Parse(_httpClient.GetStringAsync(_sonarRetriever.WebServerAddress() + targetHttp).Result); - return response; + if (_sonarRetriever is { IsEnabled: false, IsReady: false, IsRunning: false }) + { + throw new SonarNotRunningException(); + } + + try + { + JsonDocument response = JsonDocument.Parse(_httpClient.GetStringAsync(_sonarRetriever.WebServerAddress() + targetHttp).Result); + return response; + } + catch (Exception e) + { + Console.WriteLine(e); + Console.WriteLine("Sonar may not be running."); + throw; + } } public void Put(string targetHttp) { - HttpResponseMessage httpResponseMessage = _httpClient - .PutAsync(_sonarRetriever.WebServerAddress() + targetHttp, null) - .GetAwaiter().GetResult(); - httpResponseMessage.EnsureSuccessStatusCode(); + if (_sonarRetriever is { IsEnabled: false, IsReady: false, IsRunning: false }) + { + throw new SonarNotRunningException(); + } + + try + { + HttpResponseMessage httpResponseMessage = _httpClient + .PutAsync(_sonarRetriever.WebServerAddress() + targetHttp, null) + .GetAwaiter().GetResult(); + httpResponseMessage.EnsureSuccessStatusCode(); + } + catch (Exception e) + { + Console.WriteLine(e); + Console.WriteLine("Sonar may not be running."); + throw; + } } } \ No newline at end of file diff --git a/SteelSeriesAPI/Sonar/Http/SonarHttpCommand.cs b/SteelSeriesAPI/Sonar/Http/SonarHttpCommand.cs index cf84673..6dad7c4 100644 --- a/SteelSeriesAPI/Sonar/Http/SonarHttpCommand.cs +++ b/SteelSeriesAPI/Sonar/Http/SonarHttpCommand.cs @@ -2,6 +2,7 @@ using System.Text.Json; using SteelSeriesAPI.Sonar.Interfaces; using SteelSeriesAPI.Sonar.Enums; +using SteelSeriesAPI.Sonar.Exceptions; namespace SteelSeriesAPI.Sonar.Http; @@ -50,7 +51,7 @@ public void SetProcessToDeviceRouting(int pId, Channel channel) { if (channel == Channel.MASTER) { - throw new Exception("Can't set process to master routing"); + throw new MasterChannelNotSupported(); } JsonDocument audioDeviceRouting = new HttpFetcher().Provide("AudioDeviceRouting"); diff --git a/SteelSeriesAPI/Sonar/Http/SonarHttpProvider.cs b/SteelSeriesAPI/Sonar/Http/SonarHttpProvider.cs index 139df3b..e4ee8cf 100644 --- a/SteelSeriesAPI/Sonar/Http/SonarHttpProvider.cs +++ b/SteelSeriesAPI/Sonar/Http/SonarHttpProvider.cs @@ -2,6 +2,7 @@ using System.Text.Json; using SteelSeriesAPI.Sonar.Interfaces; using SteelSeriesAPI.Sonar.Enums; +using SteelSeriesAPI.Sonar.Exceptions; using SteelSeriesAPI.Sonar.Models; namespace SteelSeriesAPI.Sonar.Http; @@ -46,7 +47,7 @@ public bool GetRedirectionState(Channel channel, Mix mix) { if (channel == Channel.MASTER) { - throw new Exception("Can't get redirection state for master"); + throw new MasterChannelNotSupported(); } JsonDocument streamRedirections = new HttpFetcher().Provide("streamRedirections"); @@ -88,7 +89,7 @@ public IEnumerable GetRoutedProcess(Channel channel) { if (channel == Channel.MASTER) { - throw new Exception("Can't get routed process for Master"); + throw new MasterChannelNotSupported(); } JsonDocument audioDeviceRoutings = new HttpFetcher().Provide("AudioDeviceRouting"); diff --git a/SteelSeriesAPI/Sonar/Managers/ConfigurationManager.cs b/SteelSeriesAPI/Sonar/Managers/ConfigurationManager.cs index 10e2146..abf221c 100644 --- a/SteelSeriesAPI/Sonar/Managers/ConfigurationManager.cs +++ b/SteelSeriesAPI/Sonar/Managers/ConfigurationManager.cs @@ -4,6 +4,7 @@ using SteelSeriesAPI.Sonar.Models; using System.Text.Json; +using SteelSeriesAPI.Sonar.Exceptions; namespace SteelSeriesAPI.Sonar.Managers; @@ -27,7 +28,7 @@ public IEnumerable GetAudioConfigurations(Channel chann { if (channel == Channel.MASTER) { - throw new Exception("Can't get audio configurations for master"); + throw new MasterChannelNotSupported(); } IEnumerable configs = GetAllAudioConfigurations(); @@ -65,7 +66,7 @@ public SonarAudioConfiguration GetSelectedAudioConfiguration(Channel channel) { if (channel == Channel.MASTER) { - throw new Exception("Can't get audio configuration for master"); + throw new MasterChannelNotSupported(); } JsonDocument selectedConfigs = new HttpFetcher().Provide("configs/selected"); @@ -89,7 +90,7 @@ public SonarAudioConfiguration GetSelectedAudioConfiguration(Channel channel) public void SetConfig(string configId) { - if (string.IsNullOrEmpty(configId)) throw new Exception("Couldn't retrieve config id"); + if (string.IsNullOrEmpty(configId)) throw new ConfigNotFound($"No audio configuration found with this id: {configId}"); new HttpFetcher().Put("configs/" + configId + "/select"); } diff --git a/SteelSeriesAPI/Sonar/Managers/PlaybackDeviceManager.cs b/SteelSeriesAPI/Sonar/Managers/PlaybackDeviceManager.cs index ae65c40..10cee6b 100644 --- a/SteelSeriesAPI/Sonar/Managers/PlaybackDeviceManager.cs +++ b/SteelSeriesAPI/Sonar/Managers/PlaybackDeviceManager.cs @@ -5,6 +5,7 @@ using SteelSeriesAPI.Sonar.Models; using System.Text.Json; +using SteelSeriesAPI.Sonar.Exceptions; namespace SteelSeriesAPI.Sonar.Managers; @@ -57,7 +58,7 @@ public PlaybackDevice GetPlaybackDevice(string deviceId) catch (Exception e) { Console.WriteLine(e); - throw new Exception("Can't get any channel from this Id, maybe the channel doesn't exist or its Id changed."); + throw new Exception("Can't get any device from this Id, maybe the device doesn't exist or its Id changed."); } } @@ -65,7 +66,7 @@ public PlaybackDevice GetClassicPlaybackDevice(Channel channel) { if (channel == Channel.MASTER) { - throw new Exception("Can't get redirection channel for master"); + throw new MasterChannelNotSupported(); } JsonDocument classicRedirections = new HttpFetcher().Provide("classicRedirections"); @@ -126,7 +127,7 @@ public PlaybackDevice GetStreamerPlaybackDevice(Channel channel = Channel.MIC) { if (channel != Channel.MIC) { - throw new Exception("Can only get stream redirection channel for Mic"); + throw new MicChannelSupportOnly(); } JsonDocument streamRedirections = new HttpFetcher().Provide("streamRedirections"); @@ -198,7 +199,7 @@ public void SetStreamerPlaybackDevice(string deviceId, Channel channel = Channel { if (channel != Channel.MIC) { - throw new Exception("Can only change stream redirection channel for Mic"); + throw new MicChannelSupportOnly(); } new HttpFetcher().Put("streamRedirections/" + channel.ToDictKey(ChannelMapChoice.ChannelDict) +"/deviceId/" + deviceId); diff --git a/SteelSeriesAPI/Sonar/SonarRetriever.cs b/SteelSeriesAPI/Sonar/SonarRetriever.cs index 4f469df..14dd3a5 100644 --- a/SteelSeriesAPI/Sonar/SonarRetriever.cs +++ b/SteelSeriesAPI/Sonar/SonarRetriever.cs @@ -1,6 +1,8 @@ using System.Diagnostics; using System.Text.Json; +using SteelSeriesAPI.Exceptions; using SteelSeriesAPI.Interfaces; +using SteelSeriesAPI.Sonar.Exceptions; namespace SteelSeriesAPI.Sonar; @@ -36,7 +38,7 @@ public bool[] GetMetaDatas() { if (!SteelSeriesRetriever.Instance.Running) { - throw new Exception("SteelSeries Sonar is not running."); + throw new SteelSeriesNotRunningException(); } try @@ -72,7 +74,7 @@ public string WebServerAddress() { if (!IsEnabled || !IsReady || !IsRunning) { - throw new Exception("SteelSeries Sonar not running"); + throw new SonarNotRunningException(); } JsonDocument subApps = JsonDocument.Parse(_httpClient.GetStringAsync("https://" + SteelSeriesRetriever.Instance.GetggEncryptedAddress() + "/subApps").Result); diff --git a/SteelSeriesAPI/Sonar/SonarSocket.cs b/SteelSeriesAPI/Sonar/SonarSocket.cs index 8e5ea77..6bd5631 100644 --- a/SteelSeriesAPI/Sonar/SonarSocket.cs +++ b/SteelSeriesAPI/Sonar/SonarSocket.cs @@ -1,9 +1,11 @@ -using System.Net; -using System.Net.Sockets; -using System.Text; +using SteelSeriesAPI.Sonar.Exceptions; using SteelSeriesAPI.Sonar.Interfaces; using SteelSeriesAPI.Sonar.Managers; +using System.Net; +using System.Net.Sockets; +using System.Text; + namespace SteelSeriesAPI.Sonar; public class SonarSocket : ISonarSocket @@ -46,7 +48,7 @@ public bool Listen() { if (!IsConnected) { - throw new Exception("Listener need to be connected before listening"); + throw new SonarListenerNotConnected(); } _listenerThread.Start(); diff --git a/SteelSeriesAPI/SteelSeriesRetriever.cs b/SteelSeriesAPI/SteelSeriesRetriever.cs index fda64f9..ccc98be 100644 --- a/SteelSeriesAPI/SteelSeriesRetriever.cs +++ b/SteelSeriesAPI/SteelSeriesRetriever.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using System.Text.Json; +using SteelSeriesAPI.Exceptions; using SteelSeriesAPI.Interfaces; namespace SteelSeriesAPI; @@ -16,14 +17,14 @@ public class SteelSeriesRetriever : ISteelSeriesRetriever public SteelSeriesRetriever() { - _steelSeriesProcesses = Process.GetProcessesByName("SteelSeriesSonar"); + _steelSeriesProcesses = Process.GetProcessesByName("SteelSeriesGG"); } public string GetggEncryptedAddress() { if (!Running) { - throw new Exception("SteelSeries is not started"); + throw new SteelSeriesNotRunningException(); } try @@ -53,7 +54,7 @@ public void WaitUntilSteelSeriesStarted() private bool SteelSeriesProcessesChecker() { - _steelSeriesProcesses = Process.GetProcessesByName("SteelSeriesSonar"); + _steelSeriesProcesses = Process.GetProcessesByName("SteelSeriesGG"); return _steelSeriesProcesses.Length > 0; } } \ No newline at end of file