-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
196 lines (159 loc) · 8.87 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Runtime.InteropServices;
using XpadControl.Common.Services.LoggerService;
using XpadControl.Common.Services.WebSocketService;
using XpadControl.Interfaces;
using System.Configuration;
using System.Reflection;
using XpadControl.Interfaces.WebSocketClientsService.Dependencies;
using XpadControl.Interfaces.Common.Dependencies.SettingsCollection;
using LinuxGamepadService = XpadControl.Linux.Services.GamepadService.GamepadService;
using WindowsGamepadService = XpadControl.Windows.Services.GamepadService.GamepadService;
using WindowsGamepadHostedService = XpadControl.Windows.Services.GamepadService.GamepadHostedService;
using LinuxGamepadHostedService = XpadControl.Linux.Services.GamepadService.GamepadHostedService;
using XpadControl.Common.Services.BindingButtonsService;
using XpadControl.Common.Services.AdamActionsMethods;
namespace XpadControl
{
public class Program
{
private static readonly ProgramArguments mProgramArguments = new();
static void Main(string[] args)
{
try
{
if (!ParseArguments(args))
return;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return;
}
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile(mProgramArguments.ConfigPath)
.Build();
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
builder.Logging.ClearProviders();
builder.Configuration.AddConfiguration(configuration);
try
{
builder.Services.AddSingleton<ILoggerService>(new LoggerService(configuration));
var appSettingsSection = configuration.GetRequiredSection("AppSettings");
UriCollection uri = ReadUriFromSettings(appSettingsSection);
UpdateIntervalCollection intervalCollection = ReadUpdateIntervalFromSettings(appSettingsSection);
builder.Services.AddSingleton<IWebSocketClientsService>(serviceProvider
=> new WebSocketClientsService(serviceProvider.GetService<ILoggerService>(), uri));
// The background service reconnects when the connection is disconnected
builder.Services.AddHostedService(serviceProvider =>
new WebSocketClientsHostedService(serviceProvider.GetService<IWebSocketClientsService>(), intervalCollection));
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
// The background service monitors the connection disconnection of the game controller
builder.Services.AddSingleton<IGamepadService>(serviceProvider =>
new LinuxGamepadService(serviceProvider.GetService<ILoggerService>()));
builder.Services.AddHostedService(serviceProvider =>
new LinuxGamepadHostedService(serviceProvider.GetService<IGamepadService>(), intervalCollection));
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// The Gamepad library for Windows requires background updates
builder.Services.AddSingleton<IGamepadService>(serviceProvider =>
new WindowsGamepadService(serviceProvider.GetService<ILoggerService>()));
builder.Services.AddHostedService(serviceProvider =>
new WindowsGamepadHostedService(serviceProvider.GetService<IGamepadService>(), intervalCollection));
}
PathCollection pathCollection = ReadPathCollectionFromSettings(appSettingsSection);
builder.Services.AddSingleton<IBindingButtonsService>(serviceProvider =>
new BindingButtonsService(serviceProvider.GetService<ILoggerService>(), serviceProvider.GetService<IGamepadService>(), pathCollection.ButtonBindingsConfigPath));
builder.Services.AddSingleton<IAdamActionsMethods>(serviceProvider =>
new AdamActionsMethods(serviceProvider.GetService<ILoggerService>(), serviceProvider.GetService<IWebSocketClientsService>(), pathCollection.ZeroPositionConfigPath));
builder.Services.AddHostedService(serviceProvider =>
new App(serviceProvider.GetService<IWebSocketClientsService>(),
serviceProvider.GetService<ILoggerService>(),
serviceProvider.GetService<IGamepadService>(),
serviceProvider.GetService<IBindingButtonsService>(),
serviceProvider.GetService<IHostApplicationLifetime>(),
serviceProvider.GetService<IAdamActionsMethods>()));
}
catch (ConfigurationErrorsException ex)
{
Console.Error.WriteLine("Configuraton read error");
Console.Error.WriteLine(ex.ToString());
}
catch (Exception ex)
{
Console.Error.WriteLine("Service initialization error");
Console.Error.WriteLine(ex.ToString());
}
using IHost host = builder.Build();
host.RunAsync().Wait();
host.WaitForShutdownAsync();
}
#region Read settings methods
private static UriCollection ReadUriFromSettings(IConfigurationSection appSettings)
{
IConfigurationSection socketClientOptions = appSettings.GetRequiredSection("WebSocketClientOptions");
string webSocketHost = socketClientOptions.GetValue<string>("WebSocketHost");
int webSocketPort = socketClientOptions.GetValue<int>("WebSocketPort");
string wheelWebSocketPath = socketClientOptions.GetValue<string>("WheelWebSocketPath");
string servosWebSocketPath = socketClientOptions.GetValue<string>("ServosWebSocketPath");
UriCollection uriCollection = new(webSocketHost, webSocketPort, wheelWebSocketPath, servosWebSocketPath);
return uriCollection;
}
private static UpdateIntervalCollection ReadUpdateIntervalFromSettings(IConfigurationSection appSettings)
{
IConfigurationSection pollingOptions = appSettings.GetSection("GamepadPollingOptions");
IConfigurationSection socketClientOptions = appSettings.GetRequiredSection("WebSocketClientOptions");
double windowGamepadUpdatePolling = pollingOptions.GetValue<double>("WindowGamepadUpdatePolling");
int linuxGamepadUpdatePolling = pollingOptions.GetValue<int>("LinuxGamepadUpdatePolling");
int websocketReconnectInterval = socketClientOptions.GetValue<int>("WebsocketReconnectInterval");
UpdateIntervalCollection updateIntervalCollection = new(linuxGamepadUpdatePolling, windowGamepadUpdatePolling, websocketReconnectInterval);
return updateIntervalCollection;
}
private static PathCollection ReadPathCollectionFromSettings(IConfigurationSection appSettings)
{
IConfigurationSection optionsSection = appSettings.GetSection("PathOptions");
string zeroConfigPath = optionsSection.GetValue<string>("AdamZeroPositionConfig");
string buttonBindingConfigPath = optionsSection.GetValue<string>("ButtonBindingsConfig");
PathCollection appArguments = new(zeroConfigPath, buttonBindingConfigPath);
return appArguments;
}
#endregion
#region Read program arguments
private static bool ParseArguments(string[] args)
{
CommandLineParser.CommandLineParser parser = new()
{
IgnoreCase = true,
};
parser.ExtractArgumentAttributes(mProgramArguments);
parser.ParseCommandLine(args);
if (!Path.Exists(mProgramArguments.ConfigPath))
throw new FileNotFoundException($"Cannot find app settings file {mProgramArguments.ConfigPath}");
if (mProgramArguments.ShowConfigPath)
{
Console.WriteLine($"Setting loaded from {mProgramArguments.ConfigPath}");
return false;
}
if (mProgramArguments.ShowVersion)
{
Version v = Assembly.GetExecutingAssembly().GetName().Version;
Console.WriteLine($"{v}");
return false;
}
if (mProgramArguments.ShowHelp)
{
parser.ShowUsage();
return false;
}
return true;
}
#endregion
}
}