64 lines
2.2 KiB
C#
64 lines
2.2 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using InnovEnergy.App.SinexcelCommunication.DataTypes;
|
|
using RabbitMQ.Client;
|
|
|
|
namespace InnovEnergy.App.SinexcelCommunication.MiddlewareClasses;
|
|
|
|
|
|
[SuppressMessage("Trimming", "IL2026:Members annotated with \'RequiresUnreferencedCodeAttribute\' require dynamic access otherwise can break functionality when trimming application code")]
|
|
public static class RabbitMqManager
|
|
{
|
|
public static ConnectionFactory? Factory ;
|
|
public static IConnection ? Connection;
|
|
public static IModel? Channel;
|
|
|
|
public static Boolean SubscribeToQueue(StatusMessage currentSalimaxState, String? s3Bucket,String VpnServerIp)
|
|
{
|
|
try
|
|
{
|
|
//_factory = new ConnectionFactory { HostName = VpnServerIp };
|
|
|
|
Factory = new ConnectionFactory
|
|
{
|
|
HostName = VpnServerIp,
|
|
Port = 5672,
|
|
VirtualHost = "/",
|
|
UserName = "producer",
|
|
Password = "b187ceaddb54d5485063ddc1d41af66f",
|
|
|
|
};
|
|
|
|
Connection = Factory.CreateConnection();
|
|
Channel = Connection.CreateModel();
|
|
Channel.QueueDeclare(queue: "statusQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);
|
|
|
|
Console.WriteLine("The controller sends its status to the middleware for the first time");
|
|
if (s3Bucket != null) InformMiddleware(currentSalimaxState);
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("An error occurred while connecting to the RabbitMQ queue: " + ex.Message);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static void InformMiddleware(StatusMessage status)
|
|
{
|
|
var message = JsonSerializer.Serialize(status);
|
|
var body = Encoding.UTF8.GetBytes(message);
|
|
|
|
Channel.BasicPublish(exchange: string.Empty,
|
|
routingKey: "statusQueue",
|
|
basicProperties: null,
|
|
body: body);
|
|
|
|
Console.WriteLine($"Producer sent message: {message}");
|
|
}
|
|
|
|
|
|
} |