diff --git a/StreamDeckSimHub.Plugin/Program.cs b/StreamDeckSimHub.Plugin/Program.cs index d0806bf..349475f 100644 --- a/StreamDeckSimHub.Plugin/Program.cs +++ b/StreamDeckSimHub.Plugin/Program.cs @@ -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>(); diff --git a/StreamDeckSimHub.Plugin/SimHub/ConnectionSettings.cs b/StreamDeckSimHub.Plugin/SimHub/ConnectionSettings.cs new file mode 100644 index 0000000..b9c911f --- /dev/null +++ b/StreamDeckSimHub.Plugin/SimHub/ConnectionSettings.cs @@ -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; +} diff --git a/StreamDeckSimHub.Plugin/SimHub/SimHubConnection.cs b/StreamDeckSimHub.Plugin/SimHub/SimHubConnection.cs index 7f6e06a..9390bbe 100644 --- a/StreamDeckSimHub.Plugin/SimHub/SimHubConnection.cs +++ b/StreamDeckSimHub.Plugin/SimHub/SimHubConnection.cs @@ -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; @@ -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; @@ -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; } @@ -117,6 +120,7 @@ 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) @@ -124,7 +128,7 @@ private async Task ConnectAsync() _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) { diff --git a/StreamDeckSimHub.Plugin/appsettings.json b/StreamDeckSimHub.Plugin/appsettings.json index 2cbb6d1..b006a27 100644 --- a/StreamDeckSimHub.Plugin/appsettings.json +++ b/StreamDeckSimHub.Plugin/appsettings.json @@ -22,5 +22,9 @@ "writeTo": "file" } ] + }, + "SimHubConnection": { + "Host": "127.0.0.1", + "Port": "18082" } }