Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions SteelSeriesAPI/Exceptions/SteelSeriesNotRunningException.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
8 changes: 8 additions & 0 deletions SteelSeriesAPI/Sonar/Exceptions/ConfigNotFound.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
8 changes: 8 additions & 0 deletions SteelSeriesAPI/Sonar/Exceptions/MasterChannelNotSupported.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
8 changes: 8 additions & 0 deletions SteelSeriesAPI/Sonar/Exceptions/MicChannelSupportOnly.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
8 changes: 8 additions & 0 deletions SteelSeriesAPI/Sonar/Exceptions/SonarListenerNotConnected.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
8 changes: 8 additions & 0 deletions SteelSeriesAPI/Sonar/Exceptions/SonarNotRunningException.cs
Original file line number Diff line number Diff line change
@@ -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) { }
}
44 changes: 37 additions & 7 deletions SteelSeriesAPI/Sonar/Http/HttpFetcher.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion SteelSeriesAPI/Sonar/Http/SonarHttpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.Json;
using SteelSeriesAPI.Sonar.Interfaces;
using SteelSeriesAPI.Sonar.Enums;
using SteelSeriesAPI.Sonar.Exceptions;

namespace SteelSeriesAPI.Sonar.Http;

Expand Down Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions SteelSeriesAPI/Sonar/Http/SonarHttpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -88,7 +89,7 @@ public IEnumerable<RoutedProcess> 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");
Expand Down
7 changes: 4 additions & 3 deletions SteelSeriesAPI/Sonar/Managers/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SteelSeriesAPI.Sonar.Models;

using System.Text.Json;
using SteelSeriesAPI.Sonar.Exceptions;

namespace SteelSeriesAPI.Sonar.Managers;

Expand All @@ -27,7 +28,7 @@ public IEnumerable<SonarAudioConfiguration> GetAudioConfigurations(Channel chann
{
if (channel == Channel.MASTER)
{
throw new Exception("Can't get audio configurations for master");
throw new MasterChannelNotSupported();
}

IEnumerable<SonarAudioConfiguration> configs = GetAllAudioConfigurations();
Expand Down Expand Up @@ -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");
Expand All @@ -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");
}
Expand Down
9 changes: 5 additions & 4 deletions SteelSeriesAPI/Sonar/Managers/PlaybackDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SteelSeriesAPI.Sonar.Models;

using System.Text.Json;
using SteelSeriesAPI.Sonar.Exceptions;

namespace SteelSeriesAPI.Sonar.Managers;

Expand Down Expand Up @@ -57,15 +58,15 @@ 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.");
}
}

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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions SteelSeriesAPI/Sonar/SonarRetriever.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics;
using System.Text.Json;
using SteelSeriesAPI.Exceptions;
using SteelSeriesAPI.Interfaces;
using SteelSeriesAPI.Sonar.Exceptions;

namespace SteelSeriesAPI.Sonar;

Expand Down Expand Up @@ -36,7 +38,7 @@ public bool[] GetMetaDatas()
{
if (!SteelSeriesRetriever.Instance.Running)
{
throw new Exception("SteelSeries Sonar is not running.");
throw new SteelSeriesNotRunningException();
}

try
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions SteelSeriesAPI/Sonar/SonarSocket.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -46,7 +48,7 @@ public bool Listen()
{
if (!IsConnected)
{
throw new Exception("Listener need to be connected before listening");
throw new SonarListenerNotConnected();
}

_listenerThread.Start();
Expand Down
7 changes: 4 additions & 3 deletions SteelSeriesAPI/SteelSeriesRetriever.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Text.Json;
using SteelSeriesAPI.Exceptions;
using SteelSeriesAPI.Interfaces;

namespace SteelSeriesAPI;
Expand All @@ -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
Expand Down Expand Up @@ -53,7 +54,7 @@ public void WaitUntilSteelSeriesStarted()

private bool SteelSeriesProcessesChecker()
{
_steelSeriesProcesses = Process.GetProcessesByName("SteelSeriesSonar");
_steelSeriesProcesses = Process.GetProcessesByName("SteelSeriesGG");
return _steelSeriesProcesses.Length > 0;
}
}