Skip to content

Commit

Permalink
Makes the connection data used to connect to SimHub configurable.
Browse files Browse the repository at this point in the history
Issue #157
  • Loading branch information
pre-martin committed Nov 14, 2024
1 parent 9707e69 commit cd00ea0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 3 additions & 1 deletion StreamDeckSimHub.Plugin/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
host.Services.GetRequiredService<SimHubConnection>().Run();

host.Run();
return;


void ConfigureServices(IServiceCollection serviceCollection)
void ConfigureServices(HostBuilderContext context, IServiceCollection serviceCollection)
{
serviceCollection.Configure<ConnectionSettings>(context.Configuration.GetSection("SimHubConnection"));
serviceCollection.AddSingleton<PropertyParser>();
serviceCollection.AddSingleton<SimHubConnection>();
serviceCollection.AddSingleton<ShakeItStructureFetcher>();
Expand Down
13 changes: 13 additions & 0 deletions StreamDeckSimHub.Plugin/SimHub/ConnectionSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (C) 2024 Martin Renner
// LGPL-3.0-or-later (see file COPYING and COPYING.LESSER)

namespace StreamDeckSimHub.Plugin.SimHub;

/// <summary>
/// Settings used to connect to SimHub.
/// </summary>
public class ConnectionSettings
{
public string Host { get; set; } = "127.0.0.1";
public int Port { get; set; } = 18082;
}
10 changes: 7 additions & 3 deletions StreamDeckSimHub.Plugin/SimHub/SimHubConnection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (C) 2023 Martin Renner
// Copyright (C) 2024 Martin Renner
// LGPL-3.0-or-later (see file COPYING and COPYING.LESSER)

using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
using Microsoft.Extensions.Options;
using NLog;

namespace StreamDeckSimHub.Plugin.SimHub;
Expand Down Expand Up @@ -89,6 +90,7 @@ public static PropertyInformation WithReceiver(IPropertyChangedReceiver receiver
/// </remarks>
public class SimHubConnection : ISimHubConnection
{
private readonly ConnectionSettings _connectionSettings;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly PropertyParser _propertyParser;
private TcpClient? _tcpClient;
Expand All @@ -98,8 +100,9 @@ public class SimHubConnection : ISimHubConnection
private readonly Dictionary<string, PropertyInformation> _subscriptions = new();
private readonly HttpClient _apiClient = new() { Timeout = TimeSpan.FromSeconds(2) };

public SimHubConnection(PropertyParser propertyParser)
public SimHubConnection(IOptions<ConnectionSettings> connectionSettings, PropertyParser propertyParser)
{
_connectionSettings = connectionSettings.Value;
_propertyParser = propertyParser;
}

Expand All @@ -117,14 +120,15 @@ public async void Run()
private async Task ConnectAsync()
{
Logger.Info("Connecting to SimHub (Property Server plugin has to be installed in SimHub)...");
Logger.Info($"ConnectionSettings: {_connectionSettings.Host}:{_connectionSettings.Port}");
Connected = false;

while (!Connected)
{
_tcpClient = new TcpClient();
try
{
await _tcpClient.ConnectAsync("127.0.0.1", 18082).WaitAsync(TimeSpan.FromSeconds(4));
await _tcpClient.ConnectAsync(_connectionSettings.Host, _connectionSettings.Port).WaitAsync(TimeSpan.FromSeconds(4));
}
catch (Exception e)
{
Expand Down
4 changes: 4 additions & 0 deletions StreamDeckSimHub.Plugin/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
"writeTo": "file"
}
]
},
"SimHubConnection": {
"Host": "127.0.0.1",
"Port": "18082"
}
}

0 comments on commit cd00ea0

Please sign in to comment.