From 403882f40fb8ea677f928749714422c1d908a8b7 Mon Sep 17 00:00:00 2001 From: LilleBRG Date: Thu, 20 Mar 2025 14:10:29 +0100 Subject: [PATCH] chart at startup, mqtt receiver receives --- backend/Api/Api.csproj | 2 + backend/Api/Api.sln | 6 +++ backend/Api/MQTTReciever/MQTTReciever.cs | 57 ++++++++++++++++++++++++ backend/Api/Program.cs | 7 ++- backend/test/Program.cs | 36 +++++++++++++++ backend/test/test.csproj | 14 ++++++ frontend/home/index.html | 12 ++--- frontend/styles/home.css | 38 +--------------- 8 files changed, 126 insertions(+), 46 deletions(-) create mode 100644 backend/Api/MQTTReciever/MQTTReciever.cs create mode 100644 backend/test/Program.cs create mode 100644 backend/test/test.csproj diff --git a/backend/Api/Api.csproj b/backend/Api/Api.csproj index 71bca14..486cff1 100644 --- a/backend/Api/Api.csproj +++ b/backend/Api/Api.csproj @@ -22,6 +22,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/backend/Api/Api.sln b/backend/Api/Api.sln index 409cce6..ebc8b3a 100644 --- a/backend/Api/Api.sln +++ b/backend/Api/Api.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.9.34607.119 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Api", "Api.csproj", "{9CCF78E1-969C-420F-BE31-F8AFCE0C6827}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "..\test\test.csproj", "{5ACD3275-AE0C-458C-AACD-12FE1E165621}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {9CCF78E1-969C-420F-BE31-F8AFCE0C6827}.Debug|Any CPU.Build.0 = Debug|Any CPU {9CCF78E1-969C-420F-BE31-F8AFCE0C6827}.Release|Any CPU.ActiveCfg = Release|Any CPU {9CCF78E1-969C-420F-BE31-F8AFCE0C6827}.Release|Any CPU.Build.0 = Release|Any CPU + {5ACD3275-AE0C-458C-AACD-12FE1E165621}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5ACD3275-AE0C-458C-AACD-12FE1E165621}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5ACD3275-AE0C-458C-AACD-12FE1E165621}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5ACD3275-AE0C-458C-AACD-12FE1E165621}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/backend/Api/MQTTReciever/MQTTReciever.cs b/backend/Api/MQTTReciever/MQTTReciever.cs new file mode 100644 index 0000000..aec5de5 --- /dev/null +++ b/backend/Api/MQTTReciever/MQTTReciever.cs @@ -0,0 +1,57 @@ +using Api.DBAccess; +using MQTTnet; +using MQTTnet.Extensions.TopicTemplate; +using System.Text; + + +namespace Api.MQTTReciever +{ + public class MQTTReciever + { + IMqttClient mqttClient; + private readonly IConfiguration _configuration; + + public MQTTReciever(IConfiguration configuration) + { + _configuration = configuration; + } + + + public async Task Handle_Received_Application_Message() + { + var mqttFactory = new MqttClientFactory(); + + using (mqttClient = mqttFactory.CreateMqttClient()) + { + var mqttClientOptions = new MqttClientOptionsBuilder() + .WithTcpServer($"{_configuration["MQTT:host"]}", 1883) + .WithCredentials($"{_configuration["MQTT:username"]}", $"{_configuration["MQTT:password"]}") + .WithCleanSession() + .Build(); + + // Setup message handling before connecting so that queued messages + // are also handled properly. When there is no event handler attached all + // received messages get lost. + mqttClient.ApplicationMessageReceivedAsync += e => + { + Console.WriteLine("Received application message."); + + return Task.CompletedTask; + }; + + + await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None); + Console.WriteLine("mqttClient"); + + //var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(topic).Build(); + + await mqttClient.SubscribeAsync("temperature"); + + Console.WriteLine("MQTT client subscribed to topic."); + + Console.WriteLine("Press enter to exit."); + Console.ReadLine(); + } + } + } +} diff --git a/backend/Api/Program.cs b/backend/Api/Program.cs index 5802836..34262fb 100644 --- a/backend/Api/Program.cs +++ b/backend/Api/Program.cs @@ -1,13 +1,18 @@ using Api; +using Api.MQTTReciever; using Microsoft.AspNetCore; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; class Program { + public static void Main(string[] args) { var app = CreateWebHostBuilder(args).Build(); - + var configuration = app.Services.GetRequiredService(); + MQTTReciever mqtt = new MQTTReciever(configuration); + mqtt.Handle_Received_Application_Message(); RunMigrations(app); app.Run(); diff --git a/backend/test/Program.cs b/backend/test/Program.cs new file mode 100644 index 0000000..cc6d76a --- /dev/null +++ b/backend/test/Program.cs @@ -0,0 +1,36 @@ +using MQTTnet; + +var mqttFactory = new MqttClientFactory(); +IMqttClient mqttClient; + +using (mqttClient = mqttFactory.CreateMqttClient()) + { + var mqttClientOptions = new MqttClientOptionsBuilder() + .WithTcpServer($"10.135.51.116", 1883) + .WithCredentials($"h5", $"Merc1234") + .WithCleanSession() + .Build(); + + // Setup message handling before connecting so that queued messages + // are also handled properly. When there is no event handler attached all + // received messages get lost. + mqttClient.ApplicationMessageReceivedAsync += e => + { + Console.WriteLine("Received application message."); + + return Task.CompletedTask; + }; + + + await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None); + Console.WriteLine("mqttClient"); + + //var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(topic).Build(); + + await mqttClient.SubscribeAsync("temperature"); + + Console.WriteLine("MQTT client subscribed to topic."); + + Console.WriteLine("Press enter to exit."); + Console.ReadLine(); +} diff --git a/backend/test/test.csproj b/backend/test/test.csproj new file mode 100644 index 0000000..f105f42 --- /dev/null +++ b/backend/test/test.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/frontend/home/index.html b/frontend/home/index.html index ab234ca..2f7d6a8 100644 --- a/frontend/home/index.html +++ b/frontend/home/index.html @@ -10,17 +10,11 @@
- -