From 15d0eac3c5c7baa79b0bf009138d9479a032753a Mon Sep 17 00:00:00 2001 From: atef Date: Thu, 2 Nov 2023 16:03:08 +0100 Subject: [PATCH] Add a communication to the middelware --- csharp/App/SaliMax/src/Program.cs | 58 ++++++++++++++++++------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/csharp/App/SaliMax/src/Program.cs b/csharp/App/SaliMax/src/Program.cs index 4d4fd2740..2fbbb3e45 100644 --- a/csharp/App/SaliMax/src/Program.cs +++ b/csharp/App/SaliMax/src/Program.cs @@ -36,6 +36,7 @@ namespace InnovEnergy.App.SaliMax; internal static class Program { private static readonly TimeSpan UpdateInterval = TimeSpan.FromSeconds(2); + private static readonly IReadOnlyList BatteryNodes; private static readonly Channel TruConvertAcChannel ; @@ -48,14 +49,14 @@ internal static class Program private static readonly Channel RelaysChannel ; private static readonly Channel BatteriesChannel ; - private static IPAddress _controllerIpAddress; - private static UdpClient _udpListener; - private const string VpnServerIp = "10.2.0.11"; - private static ConnectionFactory? _factory ; - private static IConnection ?_connection; - private static IModel? _channel; - private static bool _subscribedToQueue = false; - private static SalimaxAlarmState _prevSalimaxState = SalimaxAlarmState.Orange; + private static IPAddress _controllerIpAddress; + private static UdpClient _udpListener; + private const String VpnServerIp = "10.2.0.11"; + private static ConnectionFactory? _factory ; + private static IConnection ? _connection; + private static IModel? _channel; + private static Boolean _subscribedToQueue = false; + private static SalimaxAlarmState _prevSalimaxState = SalimaxAlarmState.Orange; static Program() { @@ -86,7 +87,7 @@ internal static class Program public static async Task Main(String[] args) { - _controllerIpAddress=FindVpnIp(); + _controllerIpAddress = FindVpnIp(); if (_controllerIpAddress == null) { Console.WriteLine("There is no VPN interface, exiting..."); @@ -180,6 +181,7 @@ internal static class Program LoadOnAcGrid = gridBusLoad, LoadOnAcIsland = loadOnAcIsland, LoadOnDc = dcLoad, + StateMachine = StateMachine.Default, EssControl = EssControl.Default, Log = new SystemLog { SalimaxAlarmState = SalimaxAlarmState.Green, Message = null }, //TODO: Put real stuff @@ -216,22 +218,8 @@ internal static class Program var record = ReadStatus(); var currentSalimaxState = GetSalimaxStateAlarm(record); - //-------------------------------------------------------------------------------------------- - //If already subscribed to the queue and the status has been changed, update the queue - if (_subscribedToQueue && currentSalimaxState != _prevSalimaxState) - { - _prevSalimaxState = currentSalimaxState; - InformMiddleware(Config.Load().S3.Bucket, (int)currentSalimaxState); - } - - //If there is an available message, subscribe to the queue - if (_udpListener.Available > 0) - { - subscribedToQueue(currentSalimaxState); - } - //-------------------------------------------------------------------------------------------- + SendSalimaxStateAlarm(currentSalimaxState); - record.ControlConstants(); record.ControlSystemState(); @@ -264,7 +252,27 @@ internal static class Program // ReSharper disable once FunctionNeverReturns } - private static void subscribedToQueue(SalimaxAlarmState currentSalimaxState) + private static void SendSalimaxStateAlarm(SalimaxAlarmState currentSalimaxState) + { + //-------------------------------------------------------------------------------------------- + //If already subscribed to the queue and the status has been changed, update the queue + if (_subscribedToQueue && currentSalimaxState != _prevSalimaxState) + { + _prevSalimaxState = currentSalimaxState; + var s3Bucket = Config.Load().S3?.Bucket; + if (s3Bucket != null) + InformMiddleware(s3Bucket, (Int32)currentSalimaxState); + } + + //If there is an available message, subscribe to the queue + if (_udpListener.Available > 0) + { + SubscribeToQueue(currentSalimaxState); + } + //-------------------------------------------------------------------------------------------- + } + + private static void SubscribeToQueue(SalimaxAlarmState currentSalimaxState) { IPEndPoint serverEndpoint = null; byte[] udpMessage = _udpListener.Receive(ref serverEndpoint);