-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
152 lines (127 loc) · 4.76 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
// var builder = WebApplication.CreateBuilder(args);
// // Add services to the container.
// // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
// builder.Services.AddOpenApi();
// var app = builder.Build();
// // Configure the HTTP request pipeline.
// if (app.Environment.IsDevelopment())
// {
// app.MapOpenApi();
// }
// app.UseHttpsRedirection();
// var summaries = new[]
// {
// "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
// };
// app.MapGet("/weatherforecast", () =>
// {
// var forecast = Enumerable.Range(1, 5).Select(index =>
// new WeatherForecast
// (
// DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
// Random.Shared.Next(-20, 55),
// summaries[Random.Shared.Next(summaries.Length)]
// ))
// .ToArray();
// return forecast;
// })
// .WithName("GetWeatherForecast");
// app.Run();
// record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
// {
// public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
// }
using EVFleetManagement.ChargingStations;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using WebSocketSharp.Server;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddControllers();
builder.Services.AddScoped<ServiceRegistry>();
builder.Services.AddScoped<EventBus>();
builder.Services.AddScoped<FsmService>();
builder.Services.AddScoped<VehicleNode>();
builder.Services.AddScoped<ChargingStation>();
builder.Services.AddScoped<FleetManager>();
builder.Services.AddScoped<GpsDriver>();
builder.Services.AddScoped<BatteryDriver>();
builder.Services.AddScoped<LocationSensor>();
builder.Services.AddScoped<BatterySensor>();
builder.Services.AddScoped<ChargingActuator>();
builder.Services.AddScoped<MovementActuator>();
builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<VehicleService>();
builder.Services.AddScoped<ChargingStationService>();
// Add JWT authentication
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
#pragma warning disable CS8604 // Possible null reference argument.
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])),
ValidateIssuer = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidateAudience = true,
ValidAudience = builder.Configuration["Jwt:Audience"],
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
#pragma warning restore CS8604 // Possible null reference argument.
});
builder.Services.AddAuthorization();
// Configure WebSocket for real-time communication (simulating UDP Multicast)
builder.Services.AddScoped<UdpDriver>();
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// Start the fleet manager to initialize nodes and simulate operations
using (var scope = app.Services.CreateScope())
{
var fleetManager = scope.ServiceProvider.GetRequiredService<FleetManager>();
// Simulate initial fleet operations (optional, for demo)
_ = Task.Run(async () =>
{
await Task.Delay(1000); // Delay to ensure services are ready
fleetManager.StartDriving("vehicle1"); // Hardcoded for demo
await Task.Delay(4000);
fleetManager.SendToCharge("vehicle1"); // Hardcoded for demo
});
}
// app.UseEndpoints(endpoints =>
// {
// endpoints.MapControllers();
// // Map WebSocket endpoints if needed for UdpDriver
// endpoints.Map("/udp", async context =>
// {
// var ws = await context.WebSockets.AcceptWebSocketAsync();
// // Handle the WebSocket connection here
// });
// });
app.Map("/udp", async context =>
{
if (context.WebSockets.IsWebSocketRequest)
{
using var ws = await context.WebSockets.AcceptWebSocketAsync();
// Handle the WebSocket connection here
}
else
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
}
});
app.Run();