diff --git a/backend/.gitignore b/backend/.gitignore index cdd6d4f..6b99a71 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,3 +1,4 @@ obj/ bin/ +appsettings.json diff --git a/backend/ApplicationState.cs b/backend/ApplicationState.cs new file mode 100644 index 0000000..df2c7b1 --- /dev/null +++ b/backend/ApplicationState.cs @@ -0,0 +1,9 @@ +using MQTTnet.Client; + +namespace backend.Application +{ + public static class ApplicationState + { + public static IMqttClient? MqttClient { get; set; } + } +} diff --git a/backend/Controllers/DispenserController.cs b/backend/Controllers/DispenserController.cs index 2bce8e7..bb9fe7f 100644 --- a/backend/Controllers/DispenserController.cs +++ b/backend/Controllers/DispenserController.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Mvc; +using MQTTnet; +using backend.Application; namespace backend.Controllers; @@ -8,8 +10,14 @@ public class DispenserController : ControllerBase [HttpPost("Dispense")] public void Dispense() { - // TODO send MQTT to arduino Console.WriteLine("Dispensing.."); + + var message = new MqttApplicationMessageBuilder() + .WithTopic("dispense") + .WithPayload("dispense") + .Build(); + + ApplicationState.MqttClient!.PublishAsync(message, CancellationToken.None); } } diff --git a/backend/Program.cs b/backend/Program.cs index 9cbf71e..595d7ce 100644 --- a/backend/Program.cs +++ b/backend/Program.cs @@ -1,3 +1,7 @@ +using MQTTnet; +using MQTTnet.Client; +using backend.Application; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -24,4 +28,25 @@ app.UseAuthorization(); app.MapControllers(); +var configuration = app.Services.GetRequiredService(); + +// 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; + +Console.WriteLine("Connected"); + app.Run(); + diff --git a/backend/appsettings.Development.json b/backend/appsettings.Development.json deleted file mode 100644 index 0c208ae..0000000 --- a/backend/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/backend/appsettings.json b/backend/appsettings.json deleted file mode 100644 index 10f68b8..0000000 --- a/backend/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -} diff --git a/backend/appsettings.template.json b/backend/appsettings.template.json new file mode 100644 index 0000000..2692a8e --- /dev/null +++ b/backend/appsettings.template.json @@ -0,0 +1,15 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "Mqtt": { + "Host": "example.hivemq.cloud", + "Port": 8883, + "Username": "user", + "Password": "password" + } +} diff --git a/backend/backend.csproj b/backend/backend.csproj index 33ab97a..2bf87cc 100644 --- a/backend/backend.csproj +++ b/backend/backend.csproj @@ -7,8 +7,8 @@ - +