AMQP set up and port is gotten from appsettings now
This commit is contained in:
parent
be2a6c5d6f
commit
d289f28e93
74
backend/Api/AMQPReciever/AMQPReciever.cs
Normal file
74
backend/Api/AMQPReciever/AMQPReciever.cs
Normal file
@ -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<MQTTMessageReceive>(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,6 +23,7 @@
|
|||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
|
||||||
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
|
<PackageReference Include="MQTTnet" Version="5.0.1.1416" />
|
||||||
|
<PackageReference Include="RabbitMQ.Client" Version="7.1.2" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ namespace Api.MQTTReciever
|
|||||||
using (mqttClient = mqttFactory.CreateMqttClient())
|
using (mqttClient = mqttFactory.CreateMqttClient())
|
||||||
{
|
{
|
||||||
var mqttClientOptions = new MqttClientOptionsBuilder()
|
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"]}")
|
.WithCredentials($"{_configuration["MQTT:username"]}", $"{_configuration["MQTT:password"]}")
|
||||||
.WithCleanSession()
|
.WithCleanSession()
|
||||||
.Build();
|
.Build();
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Api;
|
using Api;
|
||||||
|
using Api.AMQPReciever;
|
||||||
using Api.DBAccess;
|
using Api.DBAccess;
|
||||||
using Api.MQTTReciever;
|
using Api.MQTTReciever;
|
||||||
using Microsoft.AspNetCore;
|
using Microsoft.AspNetCore;
|
||||||
@ -23,6 +24,9 @@ class Program
|
|||||||
var configuration = services.GetRequiredService<IConfiguration>();
|
var configuration = services.GetRequiredService<IConfiguration>();
|
||||||
var dbAccess = services.GetRequiredService<DbAccess>();
|
var dbAccess = services.GetRequiredService<DbAccess>();
|
||||||
|
|
||||||
|
//AMQPReciever amqp = new AMQPReciever(configuration, dbAccess);
|
||||||
|
//amqp.Handle_Received_Application_Message().Wait();
|
||||||
|
|
||||||
MQTTReciever mqtt = new MQTTReciever(configuration, dbAccess);
|
MQTTReciever mqtt = new MQTTReciever(configuration, dbAccess);
|
||||||
mqtt.Handle_Received_Application_Message().Wait();
|
mqtt.Handle_Received_Application_Message().Wait();
|
||||||
}
|
}
|
||||||
|
14
backend/ConsoleApp1/ConsoleApp1.csproj
Normal file
14
backend/ConsoleApp1/ConsoleApp1.csproj
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="RabbitMQ.Client" Version="7.1.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
25
backend/ConsoleApp1/ConsoleApp1.sln
Normal file
25
backend/ConsoleApp1/ConsoleApp1.sln
Normal file
@ -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
|
52
backend/ConsoleApp1/Program.cs
Normal file
52
backend/ConsoleApp1/Program.cs
Normal file
@ -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();
|
Loading…
Reference in New Issue
Block a user