Add a communication to the middelware

This commit is contained in:
atef 2023-11-02 16:03:08 +01:00
parent 22ad7b3612
commit 15d0eac3c5
1 changed files with 33 additions and 25 deletions

View File

@ -36,6 +36,7 @@ namespace InnovEnergy.App.SaliMax;
internal static class Program
{
private static readonly TimeSpan UpdateInterval = TimeSpan.FromSeconds(2);
private static readonly IReadOnlyList<Byte> 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);