Update few code

This commit is contained in:
atef 2023-11-02 16:38:01 +01:00
parent 15d0eac3c5
commit 1d5578ad9a
1 changed files with 56 additions and 50 deletions

View File

@ -49,14 +49,15 @@ internal static class Program
private static readonly Channel RelaysChannel ;
private static readonly Channel BatteriesChannel ;
private static IPAddress _controllerIpAddress;
private const String VpnServerIp = "10.2.0.11";
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;
private static SalimaxAlarmState _prevSalimaxState = SalimaxAlarmState.Green;
static Program()
{
@ -86,27 +87,12 @@ internal static class Program
public static async Task Main(String[] args)
{
_controllerIpAddress = FindVpnIp();
if (_controllerIpAddress == null)
{
Console.WriteLine("There is no VPN interface, exiting...");
return;
}
int udpPort = 9000;
IPEndPoint endPoint = new IPEndPoint(_controllerIpAddress, udpPort);
_udpListener = new UdpClient();
_udpListener.Client.Blocking = false;
_udpListener.Client.Bind(endPoint);
while (true)
{
//CreateAverage();
try
{
InitializeCommunicationToMiddleware();
await Run();
}
catch (Exception e)
@ -117,6 +103,22 @@ internal static class Program
// ReSharper disable once FunctionNeverReturns
}
private static void InitializeCommunicationToMiddleware()
{
_controllerIpAddress = FindVpnIp();
if (Equals(IPAddress.None, _controllerIpAddress))
{
Console.WriteLine("There is no VPN interface, exiting...");
}
const Int32 udpPort = 9000;
var endPoint = new IPEndPoint(_controllerIpAddress, udpPort);
_udpListener = new UdpClient();
_udpListener.Client.Blocking = false;
_udpListener.Client.Bind(endPoint);
}
private static async Task Run()
{
"Starting SaliMax".LogInfo();
@ -254,12 +256,12 @@ internal static class Program
private static void SendSalimaxStateAlarm(SalimaxAlarmState currentSalimaxState)
{
//--------------------------------------------------------------------------------------------
var s3Bucket = Config.Load().S3?.Bucket;
//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);
}
@ -267,58 +269,62 @@ internal static class Program
//If there is an available message, subscribe to the queue
if (_udpListener.Available > 0)
{
SubscribeToQueue(currentSalimaxState);
SubscribeToQueue(currentSalimaxState, s3Bucket);
}
//--------------------------------------------------------------------------------------------
}
private static void SubscribeToQueue(SalimaxAlarmState currentSalimaxState)
private static void SubscribeToQueue(SalimaxAlarmState currentSalimaxState, String? s3Bucket)
{
IPEndPoint serverEndpoint = null;
byte[] udpMessage = _udpListener.Receive(ref serverEndpoint);
IPEndPoint? serverEndpoint = null;
var replyMessage = "ACK";
var replyData = Encoding.UTF8.GetBytes(replyMessage);
var udpMessage = _udpListener.Receive(ref serverEndpoint);
var message = Encoding.UTF8.GetString(udpMessage);
string message = Encoding.UTF8.GetString(udpMessage);
Console.WriteLine($"Received a message: {message}");
string replyMessage = "ACK";
byte[] replyData = Encoding.UTF8.GetBytes(replyMessage);
// Send the reply to the sender's endpoint
_udpListener.Send(replyData, replyData.Length, serverEndpoint);
Console.WriteLine($"Replied to {serverEndpoint}: {replyMessage}");
_factory = new ConnectionFactory { HostName = VpnServerIp };
_factory = new ConnectionFactory { HostName = VpnServerIp };
_connection = _factory.CreateConnection();
_channel = _connection.CreateModel();
_channel = _connection.CreateModel();
_channel.QueueDeclare(queue: "statusQueue", durable: false, exclusive: false, autoDelete: false, arguments: null);
Console.WriteLine("The controller sends its status to the middleware for the first time");
InformMiddleware(Config.Load().S3.Bucket, (int)currentSalimaxState);
if (s3Bucket != null) InformMiddleware(s3Bucket, (Int32)currentSalimaxState);
_subscribedToQueue = true;
}
private static IPAddress FindVpnIp()
{
string interfaceName = "innovenergy";
IPAddress controllerIpAddress = null;
const String interfaceName = "innovenergy";
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var networkInterface in networkInterfaces)
{
if (networkInterface.Name == interfaceName)
{
IPInterfaceProperties ipProps = networkInterface.GetIPProperties();
UnicastIPAddressInformationCollection unicastIPs = ipProps.UnicastAddresses;
Console.WriteLine("VPN IP is: "+unicastIPs[0].Address);
controllerIpAddress = unicastIPs[0].Address;
break;
var ipProps = networkInterface.GetIPProperties();
var uniCastIPs = ipProps.UnicastAddresses;
var controllerIpAddress = uniCastIPs[0].Address;
Console.WriteLine("VPN IP is: "+ uniCastIPs[0].Address);
return controllerIpAddress;
}
}
return (controllerIpAddress);
return IPAddress.None;
}
private static void InformMiddleware(string bucket, int status)
private static void InformMiddleware(String? bucket, int status)
{
int.TryParse(bucket[0].ToString(), out var installationId);
@ -332,11 +338,11 @@ internal static class Program
var body = Encoding.UTF8.GetBytes(message);
_channel.BasicPublish(exchange: string.Empty,
routingKey: "statusQueue",
basicProperties: null,
body: body);
Console.WriteLine($"Producer sent message: {message}");
routingKey: "statusQueue",
basicProperties: null,
body: body);
Console.WriteLine($"Producer sent message: {message}");
}
private static SalimaxAlarmState GetSalimaxStateAlarm(StatusRecord record)