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