From 998cb90acc082ebfc6958c7bdca4116f61f0590b Mon Sep 17 00:00:00 2001 From: Jeas0001 Date: Wed, 26 Mar 2025 14:28:20 +0100 Subject: [PATCH] Changed the name of the publisher --- backend/Api/AMQP/{AMQPPush.cs => AMQPPublisher.cs} | 12 ++++++++---- backend/Api/Program.cs | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) rename backend/Api/AMQP/{AMQPPush.cs => AMQPPublisher.cs} (87%) diff --git a/backend/Api/AMQP/AMQPPush.cs b/backend/Api/AMQP/AMQPPublisher.cs similarity index 87% rename from backend/Api/AMQP/AMQPPush.cs rename to backend/Api/AMQP/AMQPPublisher.cs index fe3065f..c7e08db 100644 --- a/backend/Api/AMQP/AMQPPush.cs +++ b/backend/Api/AMQP/AMQPPublisher.cs @@ -7,12 +7,12 @@ using System.Text.Json; namespace Api.AMQP { - public class AMQPPush + public class AMQPPublisher { private readonly IConfiguration _configuration; private readonly DbAccess _dbAccess; - public AMQPPush(IConfiguration configuration, DbAccess dbAccess) + public AMQPPublisher(IConfiguration configuration, DbAccess dbAccess) { _dbAccess = dbAccess; _configuration = configuration; @@ -39,7 +39,7 @@ namespace Api.AMQP while (true) { - + // Publishes all devices limits var devices = _dbAccess.ReadDevices(); foreach (var device in devices) { @@ -52,16 +52,20 @@ namespace Api.AMQP await channel.BasicPublishAsync(exchange: string.Empty, routingKey: queue, body: body); } + // Short delay before disconnecting from rabbitMQ await Task.Delay(10000); + // Disconnecting from rabbitMQ to save resources await channel.CloseAsync(); Console.WriteLine($"{queue} disconnected"); await conn.CloseAsync(); Console.WriteLine("AMQPClient disconnected"); await channel.DisposeAsync(); await conn.DisposeAsync(); + // 1 hour delay await Task.Delay(3600000); + // Creating a new connection to rabbitMQ conn = await factory.CreateConnectionAsync(); Console.WriteLine("AMQPClient connected"); channel = await conn.CreateChannelAsync(); @@ -70,7 +74,7 @@ namespace Api.AMQP await channel.QueueDeclareAsync(queue: queue, durable: false, exclusive: false, autoDelete: false); Console.WriteLine($"{queue} connected"); - // Everytime a message is recieved from the queue it goes into this consumer.ReceivedAsync + // Here all messages is consumed so the queue is empty var consumer = new AsyncEventingBasicConsumer(channel); consumer.ReceivedAsync += (model, ea) => { diff --git a/backend/Api/Program.cs b/backend/Api/Program.cs index c9a0c33..e2bce37 100644 --- a/backend/Api/Program.cs +++ b/backend/Api/Program.cs @@ -30,7 +30,7 @@ class Program { AMQPReciever amqpReciever = new AMQPReciever(configuration, dbAccess); amqpReciever.Handle_Received_Application_Message().Wait(); - AMQPPush aMQPPush = new AMQPPush(configuration, dbAccess); + AMQPPublisher aMQPPush = new AMQPPublisher(configuration, dbAccess); aMQPPush.Handle_Push_Device_Limits().Wait(); } else if (rabbitMQ == "MQTT")