2023-11-28 21:08:40 +00:00
|
|
|
using MQTTnet;
|
|
|
|
using MQTTnet.Client;
|
|
|
|
using backend.Application;
|
|
|
|
|
2023-11-27 00:06:07 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
|
|
|
|
2023-12-15 12:31:30 +00:00
|
|
|
app.UseCors(builder => {
|
2023-12-19 12:37:13 +00:00
|
|
|
builder.AllowAnyHeader();
|
2023-12-15 12:31:30 +00:00
|
|
|
builder.WithOrigins("http://localhost:5173");
|
|
|
|
builder.AllowCredentials();
|
2023-12-19 13:28:48 +00:00
|
|
|
builder.AllowAnyMethod();
|
2023-12-15 12:31:30 +00:00
|
|
|
});
|
2023-11-27 00:06:07 +00:00
|
|
|
|
|
|
|
//app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
2023-11-28 21:08:40 +00:00
|
|
|
var configuration = app.Services.GetRequiredService<IConfiguration>();
|
|
|
|
|
|
|
|
// Connect via MQTT
|
|
|
|
|
|
|
|
Console.WriteLine($"Connecting to MQTT server: {configuration["Mqtt:Host"]}:{configuration["Mqtt:Port"]}...");
|
|
|
|
|
|
|
|
var mqttClient = new MqttFactory().CreateMqttClient();
|
|
|
|
|
|
|
|
var options = new MqttClientOptionsBuilder()
|
|
|
|
.WithTcpServer(configuration["Mqtt:Host"], int.Parse(configuration["Mqtt:Port"]!))
|
|
|
|
.WithCredentials(configuration["Mqtt:Username"], configuration["Mqtt:Password"])
|
|
|
|
.WithTlsOptions(new MqttClientTlsOptionsBuilder().Build())
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
await mqttClient.ConnectAsync(options, CancellationToken.None);
|
|
|
|
|
|
|
|
ApplicationState.MqttClient = mqttClient;
|
2023-12-05 22:43:02 +00:00
|
|
|
ApplicationState.DbContext = new DispenserContext();
|
2023-11-28 21:08:40 +00:00
|
|
|
|
|
|
|
Console.WriteLine("Connected");
|
|
|
|
|
2023-11-27 00:06:07 +00:00
|
|
|
app.Run();
|
2023-11-28 21:08:40 +00:00
|
|
|
|