diff --git a/backend/Api/AMQPReciever/AMQPReciever.cs b/backend/Api/AMQPReciever/AMQPReciever.cs new file mode 100644 index 0000000..0b6cf56 --- /dev/null +++ b/backend/Api/AMQPReciever/AMQPReciever.cs @@ -0,0 +1,74 @@ +using Api.DBAccess; +using Api.Models; +using RabbitMQ.Client; +using RabbitMQ.Client.Events; +using System.Text; +using System.Text.Json; + +namespace Api.AMQPReciever +{ + public class AMQPReciever + { + private readonly IConfiguration _configuration; + private readonly DbAccess _dbAccess; + + public AMQPReciever(IConfiguration configuration, DbAccess dbAccess) + { + _dbAccess = dbAccess; + _configuration = configuration; + } + + public async Task Handle_Received_Application_Message() + { + var factory = new ConnectionFactory(); + var queue = "temperature-logs"; + + factory.UserName = _configuration["AMQP:username"]; + factory.Password = _configuration["AMQP:password"]; + factory.HostName = _configuration["AMQP:host"]; + factory.Port = Convert.ToInt32(_configuration["AMQP:port"]); + + using var conn = await factory.CreateConnectionAsync(); + Console.WriteLine("AMQPClien connected"); + using var channel = await conn.CreateChannelAsync(); + + await channel.QueueDeclareAsync(queue: queue, durable: false, exclusive: false, autoDelete: false); + Console.WriteLine($"{queue} connected"); + + var consumer = new AsyncEventingBasicConsumer(channel); + consumer.ReceivedAsync += (model, ea) => + { + Console.WriteLine("Received application message."); + var body = ea.Body.ToArray(); + var message = Encoding.UTF8.GetString(body); + + var messageReceive = JsonSerializer.Deserialize(message); + + if (messageReceive == null || messageReceive.temperature == 0 || messageReceive.device_id == null || messageReceive.timestamp == 0) + { + return Task.CompletedTask; + } + + TemperatureLogs newLog = new TemperatureLogs(); + string refernceId = messageReceive.device_id; + var device = _dbAccess.ReadDevice(refernceId); + + if (device == null) { return Task.CompletedTask; } + + newLog.Temperature = messageReceive.temperature; + newLog.Date = DateTimeOffset.FromUnixTimeSeconds(messageReceive.timestamp).DateTime; + newLog.TempHigh = device.TempHigh; + newLog.TempLow = device.TempLow; + + _dbAccess.CreateLog(newLog, refernceId); + + return Task.CompletedTask; + }; + + await channel.BasicConsumeAsync(queue, true, consumer); + + Console.WriteLine("Press enter to exit."); + Console.ReadLine(); + } + } +} diff --git a/backend/Api/Api.csproj b/backend/Api/Api.csproj index 1f30e12..4003040 100644 --- a/backend/Api/Api.csproj +++ b/backend/Api/Api.csproj @@ -23,6 +23,7 @@ + diff --git a/backend/Api/MQTTReciever/MQTTReciever.cs b/backend/Api/MQTTReciever/MQTTReciever.cs index 1bd56d9..aa0931d 100644 --- a/backend/Api/MQTTReciever/MQTTReciever.cs +++ b/backend/Api/MQTTReciever/MQTTReciever.cs @@ -27,7 +27,7 @@ namespace Api.MQTTReciever using (mqttClient = mqttFactory.CreateMqttClient()) { var mqttClientOptions = new MqttClientOptionsBuilder() - .WithTcpServer($"{_configuration["MQTT:host"]}", 1883) + .WithTcpServer($"{_configuration["MQTT:host"]}", Convert.ToInt32(_configuration["MQTT:port"])) .WithCredentials($"{_configuration["MQTT:username"]}", $"{_configuration["MQTT:password"]}") .WithCleanSession() .Build(); diff --git a/backend/Api/Program.cs b/backend/Api/Program.cs index d5f3518..2cd40db 100644 --- a/backend/Api/Program.cs +++ b/backend/Api/Program.cs @@ -1,4 +1,5 @@ using Api; +using Api.AMQPReciever; using Api.DBAccess; using Api.MQTTReciever; using Microsoft.AspNetCore; @@ -23,6 +24,9 @@ class Program var configuration = services.GetRequiredService(); var dbAccess = services.GetRequiredService(); + //AMQPReciever amqp = new AMQPReciever(configuration, dbAccess); + //amqp.Handle_Received_Application_Message().Wait(); + MQTTReciever mqtt = new MQTTReciever(configuration, dbAccess); mqtt.Handle_Received_Application_Message().Wait(); } diff --git a/backend/ConsoleApp1/ConsoleApp1.csproj b/backend/ConsoleApp1/ConsoleApp1.csproj new file mode 100644 index 0000000..7dfb515 --- /dev/null +++ b/backend/ConsoleApp1/ConsoleApp1.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/backend/ConsoleApp1/ConsoleApp1.sln b/backend/ConsoleApp1/ConsoleApp1.sln new file mode 100644 index 0000000..b1ff789 --- /dev/null +++ b/backend/ConsoleApp1/ConsoleApp1.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34607.119 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1.csproj", "{0BC93CF2-F92D-4DD6-83BE-A985CBB74960}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0BC93CF2-F92D-4DD6-83BE-A985CBB74960}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0BC93CF2-F92D-4DD6-83BE-A985CBB74960}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0BC93CF2-F92D-4DD6-83BE-A985CBB74960}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0BC93CF2-F92D-4DD6-83BE-A985CBB74960}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {55EE0E94-4585-4E79-AC67-4B0E809E99AB} + EndGlobalSection +EndGlobal diff --git a/backend/ConsoleApp1/Program.cs b/backend/ConsoleApp1/Program.cs new file mode 100644 index 0000000..a5e0e23 --- /dev/null +++ b/backend/ConsoleApp1/Program.cs @@ -0,0 +1,52 @@ +using RabbitMQ.Client; +using RabbitMQ.Client.Events; +using System.Text; + + +var factory = new ConnectionFactory(); +var queue = "test"; + + +factory.UserName = "h5"; +factory.Password = "Merc1234"; +factory.HostName = "10.135.51.116"; +factory.Port = 5672; + +using var conn = await factory.CreateConnectionAsync(); +Console.WriteLine("AMQPClien connected"); +using var channel = await conn.CreateChannelAsync(); + +await channel.QueueDeclareAsync(queue: queue, durable: false, exclusive: false, autoDelete: false); +Console.WriteLine($"{queue} connected"); + +var consumer = new AsyncEventingBasicConsumer(channel); +consumer.ReceivedAsync += (model, ea) => +{ + Console.WriteLine("Received application message."); + var body = ea.Body.ToArray(); + var message = Encoding.UTF8.GetString(body); + Console.WriteLine(message); + + return Task.CompletedTask; +}; + +await channel.BasicConsumeAsync(queue, true, consumer); + + +const string message = "Hello World!"; +var body = Encoding.UTF8.GetBytes(message); +await channel.BasicPublishAsync(exchange: string.Empty, routingKey: queue, body: body); +Console.WriteLine(" Press enter to continue."); +Console.ReadLine(); +await channel.BasicPublishAsync(exchange: string.Empty, routingKey: queue, body: body); +Console.WriteLine(" Press enter to continue."); +Console.ReadLine(); +await channel.BasicPublishAsync(exchange: string.Empty, routingKey: queue, body: body); +Console.WriteLine(" Press enter to continue."); +Console.ReadLine(); +await channel.BasicPublishAsync(exchange: string.Empty, routingKey: queue, body: body); +Console.WriteLine(" Press enter to continue."); +Console.ReadLine(); +await channel.BasicPublishAsync(exchange: string.Empty, routingKey: queue, body: body); +Console.WriteLine(" Press enter to exit."); +Console.ReadLine(); \ No newline at end of file