slik-dispenser/backend/Program.cs

110 lines
2.8 KiB
C#
Raw Permalink Normal View History

using MQTTnet;
using MQTTnet.Client;
using backend.Application;
using backend.Models;
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();
}
app.UseCors(builder => {
2023-12-19 12:37:13 +00:00
builder.AllowAnyHeader();
builder.WithOrigins("http://localhost:5173");
builder.AllowCredentials();
builder.AllowAnyMethod();
});
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
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);
2023-12-20 21:42:42 +00:00
await mqttClient.SubscribeAsync(
new MqttFactory()
.CreateSubscribeOptionsBuilder()
.WithTopicFilter(f => f.WithTopic("login_request"))
.WithTopicFilter(f => f.WithTopic("dispense_request"))
.Build(),
2023-12-20 21:42:42 +00:00
CancellationToken.None
);
2023-12-05 22:43:02 +00:00
ApplicationState.DbContext = new DispenserContext();
2023-12-20 21:42:42 +00:00
mqttClient.ApplicationMessageReceivedAsync += delegate(MqttApplicationMessageReceivedEventArgs args) {
var payload = System.Text.Encoding.Default.GetString(args.ApplicationMessage.PayloadSegment);
User? user;
MqttApplicationMessageBuilder message;
2023-12-20 21:42:42 +00:00
switch (args.ApplicationMessage.Topic) {
case "login_request":
user = ApplicationState.DbContext!.Users.FirstOrDefault(user => user.TouchCode == payload);
2023-12-20 21:42:42 +00:00
message = new MqttApplicationMessageBuilder()
.WithTopic("login");
2023-12-20 21:42:42 +00:00
if (user == null) {
message.WithPayload("error");
} else {
message.WithPayload(user.Id.ToString());
}
mqttClient.PublishAsync(message.Build());
break;
case "dispense_request":
user = ApplicationState.DbContext!.Users.FirstOrDefault(user => user.Id == int.Parse(payload));
if (user == null || (!user.IsParent && user.Dispenses <= 0)) {
// TODO error
break;
}
message = new MqttApplicationMessageBuilder()
.WithTopic("dispense")
.WithPayload("dispense");
mqttClient.PublishAsync(message.Build());
2023-12-20 21:42:42 +00:00
break;
}
return Task.CompletedTask;
};
ApplicationState.MqttClient = mqttClient;
Console.WriteLine("Connected");
app.Run();