This repository has been archived by the owner on Dec 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
220 lines (196 loc) · 7.36 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
namespace FSMosquitoClient
{
using FSMosquitoClient.Extensions;
using FSMosquitoClient.Forms;
using FSMosquitoClient.Properties;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tmds.Utils;
[System.ComponentModel.DesignerCategory("")]
public class Program : ApplicationContext
{
private static ServiceProvider _serviceProvider;
private static ILogger<Program> _logger;
private readonly NotifyIcon _trayIcon;
private static readonly FunctionExecutor FSMosquitoExecutor = new FunctionExecutor(
o =>
{
o.StartInfo.RedirectStandardError = true;
o.OnExit = p =>
{
if (p.ExitCode != 0)
{
string message = $"FSMosquito execution failed with exit code: {p.ExitCode}" + Environment.NewLine +
p.StandardError.ReadToEnd();
FSMosquitoExecutor.Run(() =>
{
Launch(false);
});
}
};
});
#region Tray Application Initialization
public Program(bool showWindowOnStartup = true)
{
// Initialize Tray Icon
_trayIcon = new NotifyIcon()
{
Icon = Icon.FromHandle(Resources.mosquito.GetHicon()),
ContextMenuStrip = new ContextMenuStrip(),
Visible = true,
Text = "FSMosquito Client",
};
_trayIcon.ContextMenuStrip.Items.Add("Exit", null, (sender, e) => Application.Exit());
_trayIcon.Click += (sender, e) =>
{
if (MainForm == null)
{
MainForm = _serviceProvider.GetService<MainForm>();
}
if (!MainForm.Visible)
{
MainForm.Show();
}
MainForm.Activate();
};
_trayIcon.BalloonTipClosed += (sender, e) =>
{
_trayIcon.Visible = false;
_trayIcon.Dispose();
};
// Associate the form with the adapter
var form = _serviceProvider.GetService<MainForm>();
var adapter = _serviceProvider.GetService<ISimConnectMqttAdapter>();
var handle = form.Handle; // Use a separate variable as to not pass the thread context
form.SimConnectMessageReceived += (object sender, EventArgs e) =>
{
adapter.SignalReceiveSimConnectMessage();
};
// Fire and forget as to not block the UI thread.
Task.Run(() => { Task.Delay(2500); adapter.Start(handle); }).Forget();
if (showWindowOnStartup)
{
MainForm = form;
MainForm.Show();
MainForm.Activate();
}
}
#endregion
/// <summary>
/// Entry point for the application
/// </summary>
/// <param name="args"></param>
[STAThread]
static void Main(string[] args)
{
if (args.Length < 1 || !bool.TryParse(args[0], out bool showWindowOnStartup))
{
showWindowOnStartup = true;
}
#if DEBUG
Launch(showWindowOnStartup);
#else
if (ExecFunction.IsExecFunctionCommand(args))
{
ExecFunction.Program.Main(args);
}
else
{
FSMosquitoExecutor.Run((string[] args) =>
{
if (args.Length < 1 || !bool.TryParse(args[0], out bool showWindowOnStartup))
{
showWindowOnStartup = true;
}
Launch(showWindowOnStartup);
}, new string[] { showWindowOnStartup.ToString() });
}
#endif
}
/// <summary>
/// Launch the application
/// </summary>
private static void Launch(bool showWindowOnStartup = true)
{
#if !DEBUG
// Associate with all unhandled exceptions
AppDomain.CurrentDomain.UnhandledException += GlobalExceptionHandler;
#endif
string appGuid =
((GuidAttribute)Assembly.GetExecutingAssembly().
GetCustomAttributes(typeof(GuidAttribute), false).
GetValue(0)).Value.ToString();
// Stand up DI
var services = new ServiceCollection();
var builder = new ConfigurationBuilder()
.SetBasePath(GetBasePath())
.AddJsonFile("appsettings.json", false)
#if DEBUG
.AddJsonFile("appsettings.dev.json", true)
#endif
.AddJsonFile("appsettings.local.json", true)
.AddEnvironmentVariables();
var configuration = builder.Build();
var startup = new Startup(configuration);
startup.ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
_logger = _serviceProvider.GetService<ILogger<Program>>();
using Mutex mutex = new Mutex(false, $"Global\\{appGuid}");
if (!mutex.WaitOne(0, false))
{
_logger.LogInformation("Prevented a second instance of the FSMosquito client from opening.");
MessageBox.Show("Another instance of FSMosquito client is already running");
return;
}
_logger.LogInformation("Starting FSMosquitoClient...");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#if DEBUG
var ctx = new Program(showWindowOnStartup);
Application.Run(ctx);
#else
try
{
var ctx = new Program(showWindowOnStartup);
Application.Run(ctx);
_logger.LogInformation("FSMosquitoClient is shutting down.");
}
catch (Exception ex)
{
_logger.LogError($"FSMosquitoClient shut down unexpectedly: {ex.Message}", ex);
if (Debugger.IsAttached)
{
throw;
}
}
#endif
_logger.LogInformation("FSMosquitoClient Stopped.");
}
private static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
if (_logger != null)
{
_logger.LogError($"An unhandled exception occurred: {e.ExceptionObject}", e);
}
Console.WriteLine(e.ExceptionObject.ToString());
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
Environment.Exit(1);
}
private static string GetBasePath()
{
using var processModule = Process.GetCurrentProcess().MainModule;
return Path.GetDirectoryName(processModule?.FileName);
}
}
}