Major upgrade

This commit is contained in:
Duc
2025-10-24 15:18:11 -07:00
parent fd85735c93
commit ce583d1664
478 changed files with 237518 additions and 47610 deletions

View File

@@ -15,421 +15,402 @@ GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using NLog;
using Raytheon.Common;
using System;
using System.Net;
using System.Net.Sockets;
using NLog;
using Raytheon.Common;
namespace Raytheon.Instruments
{
/// <summary>
/// Class for controlling a UDP communication device
/// </summary>
public class CommDeviceUdp : ICommDevice, IDisposable
{
#region PrivateClassMembers
private const uint _DEFAULT_SEND_TIMEOUT = 5000;
/// <summary>
/// Class for controlling a UDP communication device
/// </summary>
public class CommDeviceUdp : ICommDevice, IDisposable
{
#region PrivateClassMembers
private const uint _DEFAULT_SEND_TIMEOUT = 5000;
private static readonly object _syncObj = new Object();
private UdpClient _udpClient;
private readonly int _localPort;
private readonly int _remotePort;
private readonly string _remoteAddress;
private IPEndPoint _remoteIPEndPoint;
private string _name;
private readonly SelfTestResult _selfTestResult;
private State _state;
private static readonly object _syncObj = new Object();
private UdpClient _udpClient;
private readonly int _localPort;
private readonly int _remotePort;
private readonly string _remoteAddress;
private IPEndPoint _remoteIPEndPoint;
private string _name;
private readonly SelfTestResult _selfTestResult;
private State _state;
/// <summary>
/// NLog logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Raytheon configuration
/// </summary>
private readonly IConfigurationManager _configurationManager;
private readonly IConfiguration _configuration;
private readonly ILogger _logger;
#endregion
private readonly IConfigurationManager _configurationManager;
private readonly IConfiguration _configuration;
#region PrivateFunctions
/// <summary>
/// The Finalizer
/// </summary>
~CommDeviceUdp()
{
Dispose(false);
}
#endregion
/// <summary>
/// Dispose of the resources contained by this object
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// close the socket and threads
try
{
if (_state == State.Ready)
{
_udpClient.Close();
#region PrivateFunctions
/// <summary>
/// The Finalizer
/// </summary>
~CommDeviceUdp()
{
Dispose(false);
}
_udpClient.Dispose();
/// <summary>
/// Dispose of the resources contained by this object
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// close the socket and threads
try
{
if (_state == State.Ready)
{
_udpClient.Close();
_state = State.Uninitialized;
}
}
catch (Exception)
{
try
{
}
catch (Exception)
{
//Do not rethrow. Exception from error logger that has already been garbage collected
}
}
}
}
_udpClient.Dispose();
#endregion
_state = State.Uninitialized;
}
}
catch (Exception)
{
try
{
}
catch (Exception)
{
//Do not rethrow. Exception from error logger that has already been garbage collected
}
}
}
}
#region PublicFuctions
#endregion
/// <summary>
/// CommDevice factory constructor
/// </summary>
/// <param name="name"></param>
/// <param name="configurationManager"></param>
public CommDeviceUdp(string deviceName, IConfigurationManager configurationManager, ILogger logger)
{
Name = deviceName;
#region PublicFuctions
_logger = logger;
/// <summary>
/// CommDevice factory constructor
/// </summary>
/// <param name="name"></param>
/// <param name="configurationManager"></param>
public CommDeviceUdp(string deviceName, IConfigurationManager configurationManager)
{
Name = deviceName;
_configurationManager = configurationManager;
_configuration = _configurationManager.GetConfiguration(Name);
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
_localPort = _configuration.GetConfigurationValue("CommDeviceUdp", "LocalPort", 0);
_remotePort = _configuration.GetConfigurationValue("CommDeviceUdp", "RemotePort", 0);
_remoteAddress = _configuration.GetConfigurationValue("CommDeviceUdp", "RemoteAddress", "127.0.0.1");
_configurationManager = configurationManager;
_configuration = _configurationManager.GetConfiguration(Name);
// created in Initialize()
_udpClient = null;
_localPort = _configuration.GetConfigurationValue("CommDeviceUdp", "LocalPort", 0);
_remotePort = _configuration.GetConfigurationValue("CommDeviceUdp", "RemotePort", 0);
_remoteAddress = _configuration.GetConfigurationValue("CommDeviceUdp", "RemoteAddress", "127.0.0.1");
_selfTestResult = SelfTestResult.Unknown;
_state = State.Uninitialized;
}
// created in Initialize()
_udpClient = null;
_selfTestResult = SelfTestResult.Unknown;
_state = State.Uninitialized;
}
/// <summary>
///
/// </summary>
/// <param name="name">The name of this instance</param>
/// <param name="localPort">the port on the local computer to use</param>
/// <param name="remotePort">the port on the remote computer to send to</param>
/// <param name="remoteAddress">the address to send to</param>
public CommDeviceUdp(string name, int localPort, int remotePort, string remoteAddress)
{
_name = name;
_localPort = localPort;
_remotePort = remotePort;
_remoteAddress = remoteAddress;
/// <summary>
///
/// </summary>
/// <param name="deviceName">The name of this instance</param>
/// <param name="localPort">the port on the local computer to use</param>
/// <param name="remotePort">the port on the remote computer to send to</param>
/// <param name="remoteAddress">the address to send to</param>
public CommDeviceUdp(string deviceName, int localPort, int remotePort, string remoteAddress)
{
_name = deviceName;
_localPort = localPort;
_remotePort = remotePort;
_remoteAddress = remoteAddress;
// created in Initialize()
_udpClient = null;
// created in Initialize()
_udpClient = null;
_selfTestResult = SelfTestResult.Unknown;
_state = State.Uninitialized;
_logger = LogManager.GetCurrentClassLogger();
}
_selfTestResult = SelfTestResult.Unknown;
_state = State.Uninitialized;
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool ClearErrors()
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool ClearErrors()
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
public bool DisplayEnabled
{
get
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
public bool DisplayEnabled
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public string DetailedStatus
{
get
{
return "This is a UDP Device called " + _name;
}
}
/// <summary>
///
/// </summary>
public string DetailedStatus
{
get
{
return "This is a UDP Device called " + _name;
}
}
/// <summary>
/// Dispose of the resources contained by this object
/// </summary>
public void Dispose()
{
try
{
Dispose(true);
/// <summary>
/// Dispose of the resources contained by this object
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
catch (Exception)
{
try
{
//ErrorLogger.Instance().Write(err.Message + "\r\n" + err.StackTrace);
}
catch (Exception)
{
//Do not rethrow. Exception from error logger that has already been garbage collected
}
}
}
GC.SuppressFinalize(this);
}
/// <summary>
///
/// </summary>
public bool FrontPanelEnabled
{
get
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
public bool FrontPanelEnabled
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public InstrumentMetadata Info
{
get
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public InstrumentMetadata Info
{
get
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public void Initialize()
{
lock (_syncObj)
{
if (_state == State.Uninitialized)
{
_udpClient = new UdpClient(_localPort);
/// <summary>
///
/// </summary>
public void Initialize()
{
lock (_syncObj)
{
if (_state == State.Uninitialized)
{
_udpClient = new UdpClient(_localPort);
_udpClient.Client.ReceiveBufferSize = int.MaxValue;
_udpClient.Client.SendBufferSize = int.MaxValue;
_udpClient.Client.ReceiveBufferSize = int.MaxValue;
_udpClient.Client.SendBufferSize = int.MaxValue;
_udpClient.Client.SendTimeout = (int)_DEFAULT_SEND_TIMEOUT;
_udpClient.Client.SendTimeout = (int)_DEFAULT_SEND_TIMEOUT;
// set an arbitrary short receive timeout. Don't want the read call to block
_udpClient.Client.ReceiveTimeout = 5;
// set an arbitrary short receive timeout. Don't want the read call to block
_udpClient.Client.ReceiveTimeout = 5;
IPAddress remoteAddy = IPAddress.Parse(_remoteAddress);
_remoteIPEndPoint = new IPEndPoint(remoteAddy, _remotePort);
IPAddress remoteAddy = IPAddress.Parse(_remoteAddress);
_remoteIPEndPoint = new IPEndPoint(remoteAddy, _remotePort);
_state = State.Ready;
}
else
{
throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString() + " on device " + _name);
}
}
}
_state = State.Ready;
}
else
{
throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString() + " on device " + _name);
}
}
}
/// <summary>
///
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
///
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public SelfTestResult PerformSelfTest()
{
lock (_syncObj)
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public SelfTestResult PerformSelfTest()
{
lock (_syncObj)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Read data from the device.
/// </summary>
/// <param name="dataRead">The buffer to put the data in</param>
/// <returns>The number of bytes read</returns>
public uint Read(ref byte[] dataRead)
{
lock (_syncObj)
{
try
{
dataRead = _udpClient.Receive(ref _remoteIPEndPoint);
/// <summary>
/// Read data from the device.
/// </summary>
/// <param name="dataRead">The buffer to put the data in</param>
/// <returns>The number of bytes read</returns>
public uint Read(ref byte[] dataRead)
{
lock (_syncObj)
{
try
{
dataRead = _udpClient.Receive(ref _remoteIPEndPoint);
uint numBytesRead = (uint)(dataRead.Length);
uint numBytesRead = (uint)(dataRead.Length);
return numBytesRead;
}
catch (SocketException e)
{
if (e.SocketErrorCode == SocketError.TimedOut)
{
// expected, do nothing
return 0;
}
else
{
throw;
}
}
}
}
return numBytesRead;
}
catch (SocketException e)
{
if (e.SocketErrorCode == SocketError.TimedOut)
{
// expected, do nothing
return 0;
}
else
{
throw;
}
}
}
}
/// <summary>
/// </summary>
public void Reset()
{
lock (_syncObj)
{
}
}
/// <summary>
/// </summary>
public void Reset()
{
lock (_syncObj)
{
}
}
/// <summary>
///
/// </summary>
public SelfTestResult SelfTestResult
{
get
{
return _selfTestResult;
}
}
/// <summary>
///
/// </summary>
public SelfTestResult SelfTestResult
{
get
{
return _selfTestResult;
}
}
/// <summary>
///
/// </summary>
public State Status
{
get
{
return _state;
}
}
/// <summary>
///
/// </summary>
public State Status
{
get
{
return _state;
}
}
/// <summary>
///
/// </summary>
/// <param name="timeoutMs"></param>
public void SetReadTimeout(uint timeoutMs)
{
lock (_syncObj)
{
_udpClient.Client.ReceiveTimeout = (int)timeoutMs;
}
}
/// <summary>
///
/// </summary>
/// <param name="timeoutMs"></param>
public void SetReadTimeout(uint timeoutMs)
{
lock (_syncObj)
{
_udpClient.Client.ReceiveTimeout = (int)timeoutMs;
}
}
/// <summary>
///
/// </summary>
public void Shutdown()
{
lock (_syncObj)
{
if (_state == State.Ready)
{
_udpClient.Close();
/// <summary>
///
/// </summary>
public void Shutdown()
{
lock (_syncObj)
{
if (_state == State.Ready)
{
_udpClient.Close();
_udpClient.Dispose();
_udpClient.Dispose();
_state = State.Uninitialized;
}
}
}
_state = State.Uninitialized;
}
}
}
/// <summary>
/// Write data to the device
/// </summary>
/// <param name="dataToSend">The data to write</param>
/// <param name="numBytesToWrite">The number of bytes to write</param>
/// <returns>THe number of bytes that were written</returns>
public uint Write(byte[] dataToSend, uint numBytesToWrite)
{
lock (_syncObj)
{
const uint MAX_BYTES_PER_PACKET = 65400;
/// <summary>
/// Write data to the device
/// </summary>
/// <param name="dataToSend">The data to write</param>
/// <param name="numBytesToWrite">The number of bytes to write</param>
/// <returns>THe number of bytes that were written</returns>
public uint Write(byte[] dataToSend, uint numBytesToWrite)
{
lock (_syncObj)
{
const uint MAX_BYTES_PER_PACKET = 65400;
uint index = 0;
uint index = 0;
if (numBytesToWrite > MAX_BYTES_PER_PACKET)
{
uint numPacketsToSend = numBytesToWrite / MAX_BYTES_PER_PACKET;
int packetsSent = 0;
if (numBytesToWrite > MAX_BYTES_PER_PACKET)
{
uint numPacketsToSend = numBytesToWrite / MAX_BYTES_PER_PACKET;
int packetsSent = 0;
while (packetsSent < numPacketsToSend)
{
Byte[] segment1 = new Byte[MAX_BYTES_PER_PACKET];
Array.Copy(dataToSend, index, segment1, 0, MAX_BYTES_PER_PACKET);
uint count = (uint)(_udpClient.Send(segment1, (int)MAX_BYTES_PER_PACKET, _remoteIPEndPoint));
index += count;
packetsSent++;
}
while (packetsSent < numPacketsToSend)
{
Byte[] segment1 = new Byte[MAX_BYTES_PER_PACKET];
Array.Copy(dataToSend, index, segment1, 0, MAX_BYTES_PER_PACKET);
uint count = (uint)(_udpClient.Send(segment1, (int)MAX_BYTES_PER_PACKET, _remoteIPEndPoint));
index += count;
packetsSent++;
}
Byte[] segment = new Byte[MAX_BYTES_PER_PACKET];
uint numBytesRemaining = numBytesToWrite - index;
Array.Copy(dataToSend, index, segment, 0, numBytesRemaining);
index += (uint)(_udpClient.Send(segment, (int)numBytesRemaining, _remoteIPEndPoint));
}
else
{
index = (uint)(_udpClient.Send(dataToSend, (int)numBytesToWrite, _remoteIPEndPoint));
}
Byte[] segment = new Byte[MAX_BYTES_PER_PACKET];
uint numBytesRemaining = numBytesToWrite - index;
Array.Copy(dataToSend, index, segment, 0, numBytesRemaining);
index += (uint)(_udpClient.Send(segment, (int)numBytesRemaining, _remoteIPEndPoint));
}
else
{
index = (uint)(_udpClient.Send(dataToSend, (int)numBytesToWrite, _remoteIPEndPoint));
}
return index;
}
}
return index;
}
}
public void Close()
{
//throw new NotImplementedException();
}
public void Close()
{
//throw new NotImplementedException();
}
public void Open()
{
//throw new NotImplementedException();
}
#endregion
}
public void Open()
{
//throw new NotImplementedException();
}
#endregion
}
}

View File

@@ -1,68 +1,61 @@
using NLog;
using Raytheon.Common;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;
using NLog;
using Raytheon.Common;
namespace Raytheon.Instruments
{
[ExportInstrumentFactory(ModelNumber = "CommDeviceUdpFactory")]
public class CommDeviceUdpFactory : IInstrumentFactory
{
/// <summary>
/// The supported interfaces
/// </summary>
private readonly List<Type> _supportedInterfaces = new List<Type>();
private ILogger _logger;
private readonly IConfigurationManager _configurationManager;
private const string DefaultConfigPath = @"C:\ProgramData\Raytheon\InstrumentManagerService";
private static string DefaultPath;
[ExportInstrumentFactory(ModelNumber = "CommDeviceUdpFactory")]
public class CommDeviceUdpFactory : IInstrumentFactory
{
private readonly List<Type> _supportedInterfaces = new List<Type>();
public CommDeviceUdpFactory(string defaultConfigPath = DefaultConfigPath)
: this(null, defaultConfigPath)
{
}
private readonly IConfigurationManager _configurationManager;
private const string DefaultConfigPath = @"C:\ProgramData\Raytheon\InstrumentManagerService";
private static string DefaultPath;
/// <summary>
/// COECommDeviceInstrumentFactory injection constructor
/// </summary>
/// <param name="configManager"></param>
/// <param name="simEngine"></param>
/// <param name="logger"></param>
[ImportingConstructor]
public CommDeviceUdpFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
public CommDeviceUdpFactory(string defaultConfigPath = DefaultConfigPath)
: this(null, defaultConfigPath)
{
}
if (LogManager.Configuration == null)
{
var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
}
/// <summary>
/// CommDeviceUdpFactory injection constructor
/// </summary>
[ImportingConstructor]
public CommDeviceUdpFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(ICommDevice));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
_logger = LogManager.GetLogger(name);
return new CommDeviceUdp(name, _configurationManager, _logger);
}
catch (Exception)
{
throw;
}
}
if (LogManager.Configuration == null)
{
var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
}
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(ICommDevice));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
return new CommDeviceUdp(name, _configurationManager);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Gets the instrument
@@ -73,9 +66,7 @@ namespace Raytheon.Instruments
{
try
{
_logger = LogManager.GetLogger(name);
return new CommDeviceUdp(name, _configurationManager, _logger);
return new CommDeviceUdp(name, _configurationManager);
}
catch (Exception)
{
@@ -88,17 +79,17 @@ namespace Raytheon.Instruments
/// </summary>
/// <returns></returns>
public ICollection<Type> GetSupportedInterfaces()
{
return _supportedInterfaces.ToArray();
}
{
return _supportedInterfaces.ToArray();
}
/// <summary>
/// returns configuration based on the predefined path or default path c:/ProgramData/Raytheon/InstrumentManagerService
/// </summary>
/// <returns></returns>
private static IConfigurationManager GetConfigurationManager()
{
return string.IsNullOrEmpty(DefaultPath) ? new RaytheonConfigurationManager() : new RaytheonConfigurationManager(DefaultPath);
}
}
/// <summary>
/// returns configuration based on the predefined path or default path c:/ProgramData/Raytheon/InstrumentManagerService
/// </summary>
/// <returns></returns>
private static IConfigurationManager GetConfigurationManager()
{
return string.IsNullOrEmpty(DefaultPath) ? new RaytheonConfigurationManager() : new RaytheonConfigurationManager(DefaultPath);
}
}
}