From 1f372355d4c52344e682adf66efa0eb5a313ea9f Mon Sep 17 00:00:00 2001 From: Jeas0001 Date: Mon, 7 Apr 2025 10:00:57 +0200 Subject: [PATCH] Publish automatisk ved edit af temphigh og low --- backend/Api/AMQP/AMQPPublisher.cs | 55 ++++++------------------ backend/Api/BusinessLogic/DeviceLogic.cs | 18 ++++++-- backend/Api/Program.cs | 2 - 3 files changed, 27 insertions(+), 48 deletions(-) diff --git a/backend/Api/AMQP/AMQPPublisher.cs b/backend/Api/AMQP/AMQPPublisher.cs index be883f1..5da1861 100644 --- a/backend/Api/AMQP/AMQPPublisher.cs +++ b/backend/Api/AMQP/AMQPPublisher.cs @@ -10,15 +10,13 @@ namespace Api.AMQP public class AMQPPublisher { private readonly IConfiguration _configuration; - private readonly DbAccess _dbAccess; private IConnection _conn; private IChannel _channel; private ConnectionFactory _factory; private string _queue; - public AMQPPublisher(IConfiguration configuration, DbAccess dbAccess) + public AMQPPublisher(IConfiguration configuration) { - _dbAccess = dbAccess; _configuration = configuration; _factory = new ConnectionFactory(); _queue = "temperature-limits"; @@ -26,51 +24,22 @@ namespace Api.AMQP InitFactory(); } - public async Task Handle_Push_Device_Limits() + public async void Handle_Push_Device_Limits(DeviceLimit deviceLimit) { - while (true) - { - await Connect(); + // Connecting to rabbitMQ + await Connect(); - // Publishes all devices limits - var devices = _dbAccess.ReadDevices(); - foreach (var device in devices) - { - var deviceLimit = new DeviceLimit(); - deviceLimit.ReferenceId = device.ReferenceId; - deviceLimit.TempHigh = device.TempHigh; - deviceLimit.TempLow = device.TempLow; - string message = JsonSerializer.Serialize(deviceLimit); - var body = Encoding.UTF8.GetBytes(message); - await _channel.BasicPublishAsync(exchange: string.Empty, routingKey: _queue, body: body); - } + string message = JsonSerializer.Serialize(deviceLimit); + var body = Encoding.UTF8.GetBytes(message); + await _channel.BasicPublishAsync(exchange: string.Empty, routingKey: _queue, body: body); - // Short delay before disconnecting from rabbitMQ - await Task.Delay(1000); - // Disconnecting from rabbitMQ to save resources - await Dispose(); - // 1 hour delay - await Task.Delay(3600000); + // Short delay before disconnecting from rabbitMQ + await Task.Delay(1000); - await Connect(); + // Disconnecting from rabbitMQ to save resources + await Dispose(); - // Here all messages is consumed so the queue is empty - var consumer = new AsyncEventingBasicConsumer(_channel); - consumer.ReceivedAsync += (model, ea) => - { - Console.WriteLine("Emptying queue"); - - return Task.CompletedTask; - }; - - // Consumes the data in the queue - await _channel.BasicConsumeAsync(_queue, true, consumer); - - // Short delay before disconnecting from rabbitMQ - await Task.Delay(1000); - await Dispose(); - } } // Disconnects from rabbitMQ @@ -92,7 +61,7 @@ namespace Api.AMQP _channel = await _conn.CreateChannelAsync(); // Here we connect to the queue through the channel that got created earlier - await _channel.QueueDeclareAsync(queue: _queue, durable: false, exclusive: false, autoDelete: false); + await _channel.QueueDeclareAsync(queue: _queue, durable: true, exclusive: false, autoDelete: false); Console.WriteLine($"{_queue} connected"); return true; } diff --git a/backend/Api/BusinessLogic/DeviceLogic.cs b/backend/Api/BusinessLogic/DeviceLogic.cs index b49db03..e841509 100644 --- a/backend/Api/BusinessLogic/DeviceLogic.cs +++ b/backend/Api/BusinessLogic/DeviceLogic.cs @@ -4,6 +4,7 @@ using Api.Models.Devices; using Api.Models.Users; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Api.AMQP; namespace Api.BusinessLogic { @@ -68,11 +69,17 @@ namespace Api.BusinessLogic Device device = new Device { Name = "Undefined", - TempHigh = 0, - TempLow = 0, + TempHigh = 100, + TempLow = -40, ReferenceId = referenceId, Logs = new List(), }; + DeviceLimit deviceLimit = new DeviceLimit(); + deviceLimit.TempHigh = device.TempHigh; + deviceLimit.TempLow = device.TempLow; + deviceLimit.ReferenceId = device.ReferenceId; + AMQPPublisher publisher = new AMQPPublisher(_configuration); + publisher.Handle_Push_Device_Limits(deviceLimit); user.Devices.Add(device); @@ -96,7 +103,12 @@ namespace Api.BusinessLogic device.Name = request.Name; device.TempLow = request.TempLow; device.TempHigh = request.TempHigh; - + DeviceLimit deviceLimit = new DeviceLimit(); + deviceLimit.TempHigh = device.TempHigh; + deviceLimit.TempLow = device.TempLow; + deviceLimit.ReferenceId = device.ReferenceId; + AMQPPublisher publisher = new AMQPPublisher(_configuration); + publisher.Handle_Push_Device_Limits(deviceLimit); } return await _dbAccess.EditDevice(device); diff --git a/backend/Api/Program.cs b/backend/Api/Program.cs index fc7a5e3..3fa0a00 100644 --- a/backend/Api/Program.cs +++ b/backend/Api/Program.cs @@ -29,8 +29,6 @@ class Program { AMQPReciever amqpReciever = new AMQPReciever(configuration, dbAccess); amqpReciever.Handle_Received_Application_Message().Wait(); - AMQPPublisher aMQPPush = new AMQPPublisher(configuration, dbAccess); - aMQPPush.Handle_Push_Device_Limits().Wait(); } else if (rabbitMQ == "MQTT") {