From 22c82ae521a9d8f880590a204467514cd99860f4 Mon Sep 17 00:00:00 2001 From: atef Date: Mon, 5 Aug 2024 08:51:15 +0200 Subject: [PATCH] Change UdpListener to private add null check on the config file. --- .../src/MiddlewareClasses/MiddlewareAgent.cs | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/csharp/App/SaliMax/src/MiddlewareClasses/MiddlewareAgent.cs b/csharp/App/SaliMax/src/MiddlewareClasses/MiddlewareAgent.cs index 8ae5447d5..32950ff50 100644 --- a/csharp/App/SaliMax/src/MiddlewareClasses/MiddlewareAgent.cs +++ b/csharp/App/SaliMax/src/MiddlewareClasses/MiddlewareAgent.cs @@ -4,12 +4,12 @@ using System.Net.Sockets; using System.Text; using System.Text.Json; using InnovEnergy.App.SaliMax.DataTypes; -using InnovEnergy.App.SaliMax.Ess; + namespace InnovEnergy.App.SaliMax.MiddlewareClasses; public static class MiddlewareAgent { - public static UdpClient UdpListener = null!; + private static UdpClient _udpListener = null!; private static IPAddress? _controllerIpAddress; private static EndPoint? _endPoint; @@ -24,9 +24,9 @@ public static class MiddlewareAgent const Int32 udpPort = 9000; _endPoint = new IPEndPoint(_controllerIpAddress, udpPort); - UdpListener = new UdpClient(); - UdpListener.Client.Blocking = false; - UdpListener.Client.Bind(_endPoint); + _udpListener = new UdpClient(); + _udpListener.Client.Blocking = false; + _udpListener.Client.Bind(_endPoint); } private static IPAddress FindVpnIp() @@ -53,7 +53,7 @@ public static class MiddlewareAgent public static Configuration? SetConfigurationFile() { - if (UdpListener.Available > 0) + if (_udpListener.Available > 0) { IPEndPoint? serverEndpoint = null; @@ -61,20 +61,26 @@ public static class MiddlewareAgent var replyMessage = "ACK"; var replyData = Encoding.UTF8.GetBytes(replyMessage); - var udpMessage = UdpListener.Receive(ref serverEndpoint); + var udpMessage = _udpListener.Receive(ref serverEndpoint); var message = Encoding.UTF8.GetString(udpMessage); - Configuration? config = JsonSerializer.Deserialize(message); + var config = JsonSerializer.Deserialize(message); - Console.WriteLine($"Received a configuration message: GridSetPoint is " + config.GridSetPoint + ", MinimumSoC is " + config.MinimumSoC + " and ForceCalibrationCharge is " + config.CalibrationChargeState+ " and CalibrationChargeDate is " + config.CalibrationChargeDate); + if (config != null) + { + Console.WriteLine($"Received a configuration message: GridSetPoint is " + config.GridSetPoint + + ", MinimumSoC is " + config.MinimumSoC + " and ForceCalibrationCharge is " + + config.CalibrationChargeState + " and CalibrationChargeDate is " + + config.CalibrationChargeDate); - // Send the reply to the sender's endpoint - UdpListener.Send(replyData, replyData.Length, serverEndpoint); - Console.WriteLine($"Replied to {serverEndpoint}: {replyMessage}"); - return config; + // Send the reply to the sender's endpoint + _udpListener.Send(replyData, replyData.Length, serverEndpoint); + Console.WriteLine($"Replied to {serverEndpoint}: {replyMessage}"); + return config; + } } - if (!_endPoint.Equals((IPEndPoint)UdpListener.Client.LocalEndPoint)) + if (_endPoint != null && !_endPoint.Equals(_udpListener.Client.LocalEndPoint as IPEndPoint)) { Console.WriteLine("UDP address has changed, rebinding..."); InitializeCommunicationToMiddleware();