162 lines
5.3 KiB
C#
162 lines
5.3 KiB
C#
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using InnovEnergy.App.SinexcelCommunication.DataTypes;
|
|
|
|
namespace InnovEnergy.App.SinexcelCommunication.MiddlewareClasses;
|
|
|
|
public static class MiddlewareAgent
|
|
{
|
|
private static UdpClient _udpListener = null!;
|
|
private static IPAddress? _controllerIpAddress;
|
|
private static EndPoint? _endPoint;
|
|
|
|
public static bool InitializeCommunicationToMiddleware()
|
|
{
|
|
try
|
|
{
|
|
_controllerIpAddress = FindVpnIp();
|
|
if (Equals(IPAddress.None, _controllerIpAddress))
|
|
{
|
|
Console.WriteLine("There is no VPN interface.");
|
|
_udpListener = null;
|
|
return false;
|
|
}
|
|
|
|
const int udpPort = 9000;
|
|
_endPoint = new IPEndPoint(_controllerIpAddress, udpPort);
|
|
|
|
_udpListener?.Close();
|
|
_udpListener?.Dispose();
|
|
|
|
_udpListener = new UdpClient();
|
|
_udpListener.Client.Blocking = false;
|
|
_udpListener.Client.Bind(_endPoint);
|
|
|
|
Console.WriteLine($"UDP listener bound to {_endPoint}");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Failed to initialize middleware communication: {ex}");
|
|
_udpListener = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static IPAddress FindVpnIp()
|
|
{
|
|
const String interfaceName = "innovenergy";
|
|
|
|
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
|
|
|
|
foreach (var networkInterface in networkInterfaces)
|
|
{
|
|
if (networkInterface.Name == interfaceName)
|
|
{
|
|
var ipProps = networkInterface.GetIPProperties();
|
|
var uniCastIPs = ipProps.UnicastAddresses;
|
|
var controllerIpAddress = uniCastIPs[0].Address;
|
|
|
|
Console.WriteLine("VPN IP is: "+ uniCastIPs[0].Address);
|
|
return controllerIpAddress;
|
|
}
|
|
}
|
|
|
|
return IPAddress.None;
|
|
}
|
|
public static Configuration? SetConfigurationFile()
|
|
{
|
|
try
|
|
{
|
|
// Ensure listener is initialized
|
|
if (_udpListener == null)
|
|
{
|
|
Console.WriteLine("UDP listener not initialized, trying to initialize...");
|
|
InitializeCommunicationToMiddleware();
|
|
|
|
if (_udpListener == null)
|
|
{
|
|
Console.WriteLine("Failed to initialize UDP listener.");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Check if data is available
|
|
if (_udpListener.Available <= 0)
|
|
return null;
|
|
|
|
IPEndPoint? serverEndpoint = null;
|
|
|
|
var udpMessage = _udpListener.Receive(ref serverEndpoint);
|
|
var message = Encoding.UTF8.GetString(udpMessage);
|
|
|
|
Console.WriteLine($"Received raw UDP message from {serverEndpoint}: {message}");
|
|
|
|
var config = JsonSerializer.Deserialize<Configuration>(message);
|
|
|
|
if (config != null)
|
|
{
|
|
Console.WriteLine(
|
|
$"Received a configuration message:\n" +
|
|
$"MinimumSoC: {config.MinimumSoC}\n" +
|
|
$"OperatingPriority: {config.OperatingPriority}\n" +
|
|
$"Number of batteries: {config.BatteriesCount}\n" +
|
|
$"Maximum Charging current: {config.MaximumChargingCurrent}\n" +
|
|
$"Maximum Discharging current: {config.MaximumDischargingCurrent}\n" +
|
|
$"StartTimeChargeandDischargeDayandTime: {config.StartTimeChargeandDischargeDayandTime}\n" +
|
|
$"StopTimeChargeandDischargeDayandTime: {config.StopTimeChargeandDischargeDayandTime}\n" +
|
|
$"TimeChargeandDischargePower: {config.TimeChargeandDischargePower}\n" +
|
|
$"ControlPermission: {config.ControlPermission}"
|
|
);
|
|
|
|
// Send ACK
|
|
var replyMessage = "ACK";
|
|
var replyData = Encoding.UTF8.GetBytes(replyMessage);
|
|
|
|
_udpListener.Send(replyData, replyData.Length, serverEndpoint);
|
|
Console.WriteLine($"Replied to {serverEndpoint}: {replyMessage}");
|
|
|
|
return config;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Received UDP message but failed to deserialize Configuration.");
|
|
return null;
|
|
}
|
|
}
|
|
catch (SocketException ex)
|
|
{
|
|
Console.WriteLine($"Socket error in SetConfigurationFile: {ex}");
|
|
|
|
// Recover by reinitializing
|
|
try
|
|
{
|
|
_udpListener?.Close();
|
|
_udpListener?.Dispose();
|
|
}
|
|
catch
|
|
{
|
|
// ignored
|
|
}
|
|
|
|
_udpListener = null;
|
|
InitializeCommunicationToMiddleware();
|
|
|
|
return null;
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
Console.WriteLine($"JSON deserialization error: {ex}");
|
|
return null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Unexpected error in SetConfigurationFile: {ex}");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
} |