add new function to ensure connection for sinexcel

This commit is contained in:
atef 2026-02-13 10:04:38 +01:00
parent d324de335e
commit 0b0d91f4dd
1 changed files with 36 additions and 11 deletions

View File

@ -7,12 +7,11 @@ using RabbitMQ.Client;
namespace InnovEnergy.App.SinexcelCommunication.MiddlewareClasses; 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 class RabbitMqManager
{ {
public static ConnectionFactory? Factory ; private static ConnectionFactory? _factory ;
public static IConnection ? Connection; private static IConnection ? _connection;
public static IModel? Channel; private static IModel? _channel;
public static Boolean SubscribeToQueue(StatusMessage currentSalimaxState, String? s3Bucket,String VpnServerIp) public static Boolean SubscribeToQueue(StatusMessage currentSalimaxState, String? s3Bucket,String VpnServerIp)
{ {
@ -20,19 +19,22 @@ public static class RabbitMqManager
{ {
//_factory = new ConnectionFactory { HostName = VpnServerIp }; //_factory = new ConnectionFactory { HostName = VpnServerIp };
Factory = new ConnectionFactory _factory = new ConnectionFactory
{ {
HostName = VpnServerIp, HostName = VpnServerIp,
Port = 5672, Port = 5672,
VirtualHost = "/", VirtualHost = "/",
UserName = "producer", UserName = "producer",
Password = "b187ceaddb54d5485063ddc1d41af66f", Password = "b187ceaddb54d5485063ddc1d41af66f",
AutomaticRecoveryEnabled = true,
NetworkRecoveryInterval = TimeSpan.FromSeconds(10),
RequestedHeartbeat = TimeSpan.FromSeconds(30)
}; };
Connection = Factory.CreateConnection(); _connection = _factory.CreateConnection();
Channel = Connection.CreateModel(); _channel = _connection.CreateModel();
Channel.QueueDeclare(queue: "statusQueue", durable: true, exclusive: false, autoDelete: false, arguments: null); _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"); Console.WriteLine("The controller sends its status to the middleware for the first time");
if (s3Bucket != null) InformMiddleware(currentSalimaxState); if (s3Bucket != null) InformMiddleware(currentSalimaxState);
@ -52,7 +54,7 @@ public static class RabbitMqManager
var message = JsonSerializer.Serialize(status); var message = JsonSerializer.Serialize(status);
var body = Encoding.UTF8.GetBytes(message); var body = Encoding.UTF8.GetBytes(message);
Channel.BasicPublish(exchange: string.Empty, _channel.BasicPublish(exchange: string.Empty,
routingKey: "statusQueue", routingKey: "statusQueue",
basicProperties: null, basicProperties: null,
body: body); body: body);
@ -60,5 +62,28 @@ public static class RabbitMqManager
Console.WriteLine($"Producer sent message: {message}"); Console.WriteLine($"Producer sent message: {message}");
} }
public static Boolean EnsureConnected(StatusMessage currentSalimaxState, string? s3Bucket, string vpnServerIp)
{
try
{
if (_connection == null || !_connection.IsOpen ||
_channel == null || _channel.IsClosed)
{
Console.WriteLine("⚠ RabbitMQ connection lost. Reconnecting...");
_connection?.Dispose();
_channel?.Dispose();
return SubscribeToQueue(currentSalimaxState, s3Bucket, vpnServerIp);
}
return true;
}
catch (Exception ex)
{
Console.WriteLine("❌ Error while ensuring RabbitMQ connection: " + ex);
return false;
}
}
} }