Skip to content

Commit 8bd2483

Browse files
General cleanup
1 parent 557f7e3 commit 8bd2483

6 files changed

Lines changed: 31 additions & 44 deletions

File tree

AlpacaDriverDemo/AlpacaDriverDemo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="ASCOM.Alpaca.Razor" Version="0.4.1-rc.1" />
10+
<PackageReference Include="ASCOM.Alpaca.Razor" Version="0.5.0-alpha02" />
1111
</ItemGroup>
1212

1313
</Project>

AlpacaDriverDemo/Data/UserService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using ASCOM.Alpaca;
22

3-
namespace AlpacaDriverDemo
3+
namespace AlpacaDriverDemo.Data
44
{
55
internal class UserService : ASCOM.Alpaca.IUserService
66
{

AlpacaDriverDemo/Drivers/BasicMonitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public bool IsSafe
2828

2929
public string Name => "Safety Monitor";
3030

31-
public IList<string> SupportedActions => new List<string>();
31+
public IList<string> SupportedActions => [];
3232

3333
public bool Connecting => throw new NotImplementedException();
3434

AlpacaDriverDemo/Pages/Setup.razor

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<br />
3737

3838
<button @onclick="SaveServerSettings" style="min-width:12ch; vertical-align:middle;">Save</button>
39-
39+
4040
</fieldset>
4141

4242
<fieldset>
@@ -168,7 +168,7 @@
168168
<br />
169169
<span>Shutdown:</span>
170170
<input type="checkbox"
171-
@bind="EnableShutdown">
171+
@bind="EnableShutdown">
172172
<button @onclick="Shutdown" style="min-width:12ch; vertical-align:middle;" disabled="@AllowShutdown">Shutdown</button>
173173
</fieldset>
174174

@@ -261,13 +261,13 @@
261261
{
262262
get;
263263
set;
264-
}
264+
} = string.Empty;
265265

266266
string Password2
267267
{
268268
get;
269269
set;
270-
}
270+
} = string.Empty;
271271

272272
bool RunSwagger
273273
{
@@ -319,7 +319,7 @@
319319

320320
if (SettingsChanged) //If any core settings change call a restart.
321321
{
322-
Program.Main(null);
322+
Program.Main(Array.Empty<string>());
323323
}
324324

325325
}
@@ -417,7 +417,7 @@
417417
{
418418
try
419419
{
420-
Program.Lifetime.StopApplication();
420+
Program.Lifetime?.StopApplication();
421421
}
422422
catch (Exception ex)
423423
{

AlpacaDriverDemo/Program.cs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using ASCOM.Alpaca;
22
using ASCOM.Common;
3-
using ASCOM.Common.Interfaces;
4-
using Microsoft.AspNetCore.Components;
5-
using Microsoft.AspNetCore.Components.Web;
63
using System.Diagnostics;
74
using System.Net.NetworkInformation;
85
using System.Reflection;
@@ -19,17 +16,17 @@ public class Program
1916
//Change this to a unique value
2017
//You should offer a way for the end user to customize this via the command line so it can be changed in the case of a collision.
2118
//This supports --urls=http://*:port by default.
22-
internal const int DefaultPort = 1234;
19+
internal const int DefaultPort = 31234;
2320

2421
//Fill these out
2522
internal const string Manufacturer = "Your name here";
23+
2624
internal const string ServerName = "A friendly name for the server";
2725
internal const string ServerVersion = "1.0";
2826

29-
internal static ASCOM.Common.Interfaces.ILogger Logger;
30-
31-
internal static IHostApplicationLifetime Lifetime;
27+
internal static ASCOM.Common.Interfaces.ILogger? Logger;
3228

29+
internal static IHostApplicationLifetime? Lifetime;
3330

3431
public static void Main(string[] args)
3532
{
@@ -40,12 +37,11 @@ public static void Main(string[] args)
4037
//For Debug ConsoleLogger is very nice. For production TraceLogger is recommended.
4138
Logger = new ASCOM.Tools.ConsoleLogger();
4239

43-
4440
#region Startup and Logging
41+
4542
Logger.LogInformation($"{ServerName} version {ServerVersion}");
4643
Logger.LogInformation($"Running on: {RuntimeInformation.OSDescription}.");
4744

48-
4945
//If already running start browser
5046
try
5147
{
@@ -62,12 +58,15 @@ public static void Main(string[] args)
6258
}
6359
else
6460
{
65-
//This may need to be changed to a global lock of some sort.
66-
if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1)
61+
Assembly? entryAssembly = Assembly.GetEntryAssembly();
62+
if (entryAssembly != null)
6763
{
68-
Logger.LogInformation("Detected driver already running, starting web browser on IP and Port");
69-
StartBrowser(ServerSettings.ServerPort);
70-
return;
64+
if(Process.GetProcessesByName(entryAssembly.Location).Length > 1)
65+
{
66+
Logger.LogInformation("Detected driver already running, starting web browser on IP and Port");
67+
StartBrowser(ServerSettings.ServerPort);
68+
return;
69+
}
7170
}
7271
}
7372
}
@@ -88,7 +87,6 @@ public static void Main(string[] args)
8887
return;
8988
}
9089

91-
9290
//Turn off Authentication. Once off the user can change the password and re-enable authentication
9391
if (args?.Any(str => str.Contains("--reset-auth")) ?? false)
9492
{
@@ -104,10 +102,7 @@ public static void Main(string[] args)
104102

105103
if (!args?.Any(str => str.Contains("--urls")) ?? true)
106104
{
107-
if (args == null)
108-
{
109-
args = new string[0];
110-
}
105+
args ??= [];
111106

112107
Logger.LogInformation("No startup url args detected, binding to saved server settings.");
113108

@@ -136,9 +131,9 @@ public static void Main(string[] args)
136131
args = temparray;
137132
}
138133

139-
var builder = WebApplication.CreateBuilder(args);
134+
var builder = WebApplication.CreateBuilder(args ?? []);
140135

141-
#endregion
136+
#endregion Startup and Logging
142137

143138
//ToDo you can add devices here
144139

@@ -148,7 +143,7 @@ public static void Main(string[] args)
148143
//Load the configuration
149144
ASCOM.Alpaca.DeviceManager.LoadConfiguration(new AlpacaConfiguration());
150145

151-
//Add a safety monitor with device id 0. You can load any number of the same device with different ids or load other devices with Load* functions.
146+
//Add a safety monitor with device id 0. You can load any number of the same device with different ids or load other devices with Load* functions.
152147
//You may want to inject settings and logging here to the Driver Instance.
153148
//For each device you add you should add a setting page to the settings folder and an entry in the Shared NavMenu
154149
ASCOM.Alpaca.DeviceManager.LoadSafetyMonitor(0, new Drivers.BasicMonitor(), "Really Basic Safety Monitor", ServerSettings.GetDeviceUniqueId("SafetyMonitor", 0));
@@ -170,7 +165,7 @@ public static void Main(string[] args)
170165
//Use Authentication
171166
ASCOM.Alpaca.Razor.StartupHelpers.ConfigureAuthentication(builder.Services);
172167
//Add User Service
173-
builder.Services.AddScoped<IUserService, UserService>();
168+
builder.Services.AddScoped<IUserService, Data.UserService>();
174169

175170
var app = builder.Build();
176171

@@ -199,19 +194,19 @@ public static void Main(string[] args)
199194

200195
app.MapFallbackToPage("/_Host");
201196

202-
if(ServerSettings.AutoStartBrowser)
197+
if (ServerSettings.AutoStartBrowser)
203198
{
204199
try
205200
{
206201
StartBrowser(ServerSettings.ServerPort);
207202
}
208-
catch(Exception ex)
203+
catch (Exception ex)
209204
{
210205
Logger.LogWarning(ex.Message);
211206
}
212207
}
213208

214-
#endregion
209+
#endregion Finish Building and Start server
215210

216211
Lifetime = app.Lifetime;
217212

@@ -223,8 +218,6 @@ public static void Main(string[] args)
223218

224219
//Start the Alpaca Server
225220
app.Run();
226-
227-
228221
}
229222

230223
/// <summary>

AlpacaDriverDemo/ServerSettings.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
using ASCOM.Alpaca;
2-
using ASCOM.Common.Interfaces;
3-
using ASCOM.Tools;
4-
using Microsoft.Extensions.Logging;
5-
using System;
6-
using System.Runtime.InteropServices;
7-
using System.Threading.Tasks;
82

93
namespace AlpacaDriverDemo
104
{
@@ -226,7 +220,7 @@ internal static ASCOM.Common.Interfaces.LogLevel LoggingLevel
226220
}
227221
set
228222
{
229-
Program.Logger.SetMinimumLoggingLevel(value);
223+
Program.Logger?.SetMinimumLoggingLevel(value);
230224
Profile.WriteValue("LoggingLevel", value.ToString());
231225
}
232226
}

0 commit comments

Comments
 (0)