-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
107 lines (99 loc) · 4.21 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
using System.IO.Ports;
using backend24.Extensions;
using backend24.Services;
using backend24.Services.DataProcessors;
using backend24.Services.DataProcessors.DataExtractors;
using backend24.Services.DataProviders;
using backend24.Services.EventFinalizers;
using NReco.Logging.File;
using Spectre.Console;
namespace backend24
{
public class Program
{
public static void Main(string[] args)
{
// Create a builder, using the arguments passed from the command line.
var builder = WebApplication.CreateBuilder(args);
// Reset logging to the console
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddFile("Logs/log.txt");
// Get the name of the serial port where data is arriving
var serialPortName = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title(
"Please select the serial port where the [blue]APC220 module[/] is connected."
)
.PageSize(10)
.AddChoices(SerialPort.GetPortNames())
.HighlightStyle(new Style(foreground: Color.White, background: Color.Blue))
);
Console.WriteLine($"Selected port: {serialPortName}");
// Add services to the container.
// Register internal services, using keyed services
builder
.Services.AddKeyedSingleton<
IDataProvider<Dictionary<SerialProvider.DataLabel, string>>,
SerialProvider
>(
ServiceKeys.SerialProvider,
(serviceProvider, _) =>
ActivatorUtilities.CreateInstance<SerialProvider>(
serviceProvider,
serialPortName,
19200,
Parity.None
)
)
.AddKeyedSingleton<IDataProvider<float>, PressureExtractor>(
ServiceKeys.PressureExtractor
)
.AddFinalizer<PressureFinalizer>()
.AddKeyedSingleton<IDataProvider<float>, TemperatureExtractor>(
ServiceKeys.TemperatureExtractor
)
.AddFinalizer<TemperatureFinalizer>()
.AddKeyedSingleton<IDataProvider<float>, AltitudeExtractor>(
ServiceKeys.AltitudeExtractor
)
.AddFinalizer<AltitudeFinalizer>()
.AddKeyedSingleton<IDataProvider<float>, AltitudeGPSExtractor>(
ServiceKeys.AltitudeGPSExtractor
)
.AddFinalizer<AltitudeGPSFinalizer>()
.AddKeyedSingleton<IDataProvider<float>, AltitudeDeltaProcessor>(
ServiceKeys.AltitudeDeltaProcessor
)
.AddFinalizer<AltitudeDeltaFinalizer>()
.AddKeyedSingleton<IDataProvider<float>, VelocityProcessor>(
ServiceKeys.VelocityProcessor
)
.AddFinalizer<VelocityFinalizer>();
builder.Services.AddCors(options =>
options.AddDefaultPolicy(policy =>
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()
)
);
// This will register all classes annotated with ApiController
builder.Services.AddControllers();
// Set up Swagger/OpenAPI (learn more at https://aka.ms/aspnetcore/swashbuckle)
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Build an app from the configuration.
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization(); // TODO: Research this - is it necessary?
app.MapControllers();
// Start the app.
app.Run();
}
}
}