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,380 +15,347 @@ GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using NLog;
using Raytheon.Common;
using System;
using System.IO.Ports;
using System.Net.Sockets;
using System.Text;
using NLog;
using Raytheon.Common;
namespace Raytheon.Instruments
{
/// <summary>
/// Chiller class used to interact with the FTS chiller.
/// </summary>
public class ChillerFTS : IChiller, IDisposable
{
#region PrivateMembers
private enum DEGREES
{
Celsius = 0,
Fahrenheit = 1,
Kevin = 2
}
/// <summary>
/// Chiller class used to interact with the FTS chiller.
/// </summary>
public class ChillerFTS : IChiller, IDisposable
{
#region PrivateMembers
private enum DEGREES
{
Celsius = 0,
Fahrenheit = 1,
Kevin = 2
}
private static object m_sync = new object();
private static object m_sync = new object();
private const string _CARRIAGE_RETURN = "\r";
private const string _FLOWDISABLE = "STOP";
private const string _FLOWENABLE = "START";
private const string _TEMPREAD = "PT?";
private const string _TEMPREADSETPOINT = "SP?";
private const string _TEMPSET = "SP=";
private const string _DEGREES = "DEGREES=";
private const string _SUCCESS = "OK";
private const string _EXC_POINT = "!";
private const string _CARRIAGE_RETURN = "\r";
private const string _FLOWDISABLE = "STOP";
private const string _FLOWENABLE = "START";
private const string _TEMPREAD = "PT?";
private const string _TEMPREADSETPOINT = "SP?";
private const string _TEMPSET = "SP=";
private const string _DEGREES = "DEGREES=";
private const string _SUCCESS = "OK";
private const string _EXC_POINT = "!";
private readonly string _ipAddr;
private readonly int _port;
private const int _READ_BUFFER_SIZE = 128;
private const int _READ_TIMEOUT = 5000;
private byte[] _readBuffer;
private NetworkStream _tcpStream;
private readonly string _ipAddr;
private readonly int _port;
private const int _READ_BUFFER_SIZE = 128;
private const int _READ_TIMEOUT = 5000;
private byte[] _readBuffer;
private NetworkStream _tcpStream;
public string DetailedStatus { get; protected set; }
public string DetailedStatus { get; protected set; }
public bool DisplayEnabled { get; set; }
public bool FrontPanelEnabled { get; set; }
public bool DisplayEnabled { get; set; }
public bool FrontPanelEnabled { get; set; }
public InstrumentMetadata Info { get; set; }
public InstrumentMetadata Info { get; set; }
public string Name { get; protected set; }
public string Name { get; protected set; }
public SelfTestResult SelfTestResult => PerformSelfTest();
public SelfTestResult SelfTestResult => PerformSelfTest();
public State Status { get; set; }
public State Status { get; set; }
/// <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>
/// Destructor
/// </summary>
~ChillerFTS()
{
Dispose(false);
}
#endregion
/// <summary>
/// Open socket to the chiller
/// </summary>
private void ConnectEthernet()
{
Initialize();
}
#region PrivateFunctions
/// <summary>
/// Destructor
/// </summary>
~ChillerFTS()
{
Dispose(false);
}
public void Initialize()
{
//Create and open a socket to chiller cart server
TcpClient chillerSocket = new TcpClient(_ipAddr, _port);
_tcpStream = chillerSocket.GetStream();
_tcpStream.ReadTimeout = _READ_TIMEOUT;
}
/// <summary>
/// Open socket to the chiller
/// </summary>
private void ConnectEthernet()
{
Initialize();
}
/// <summary>
/// Dispose of the object's resources.
/// </summary>
/// <param name="disposing">Currently disposing.</param>
protected virtual void Dispose(bool disposing)
{
try
{
if (disposing)
{
//Close Connection (if available)
_tcpStream?.Close();
}
}
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
}
}
}
public void Initialize()
{
//Create and open a socket to chiller cart server
TcpClient chillerSocket = new TcpClient(_ipAddr, _port);
_tcpStream = chillerSocket.GetStream();
_tcpStream.ReadTimeout = _READ_TIMEOUT;
}
/// <summary>
/// Send a command to the chiller and request a response
/// </summary>
/// <param name="cmd">Command to send.</param>
/// <returns>Response from the chiller.</returns>
private string SendMessageGetResponse(string cmd)
{
lock (m_sync)
{
//Format the command before sending
string commandString = cmd + _CARRIAGE_RETURN;
/// <summary>
/// Dispose of the object's resources.
/// </summary>
/// <param name="disposing">Currently disposing.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
//Close Connection (if available)
_tcpStream?.Close();
}
}
//convert to byte array for sending
byte[] commandBuffer = Encoding.ASCII.GetBytes(commandString);
/// <summary>
/// Send a command to the chiller and request a response
/// </summary>
/// <param name="cmd">Command to send.</param>
/// <returns>Response from the chiller.</returns>
private string SendMessageGetResponse(string cmd)
{
lock (m_sync)
{
//Format the command before sending
string commandString = cmd + _CARRIAGE_RETURN;
//send the data
_tcpStream.Write(commandBuffer, 0, commandBuffer.Length);
//convert to byte array for sending
byte[] commandBuffer = Encoding.ASCII.GetBytes(commandString);
//clear the buffer
Array.Clear(_readBuffer, 0, _readBuffer.Length);
//send the data
_tcpStream.Write(commandBuffer, 0, commandBuffer.Length);
//read from the response buffer
int numBytesRead = _tcpStream.Read(_readBuffer, 0, _readBuffer.Length);
//clear the buffer
Array.Clear(_readBuffer, 0, _readBuffer.Length);
//convert response to a string
string rspStr = Encoding.ASCII.GetString(_readBuffer);
//read from the response buffer
int numBytesRead = _tcpStream.Read(_readBuffer, 0, _readBuffer.Length);
//Check Response
if (rspStr.Contains(_SUCCESS))
{
//Remove !
rspStr = rspStr.Replace(_EXC_POINT, "");
//convert response to a string
string rspStr = Encoding.ASCII.GetString(_readBuffer);
//Parse string ("/r")
char[] delimit = { '\r' };
string[] parsed = rspStr.Split(delimit, StringSplitOptions.RemoveEmptyEntries);
//Check Response
if (rspStr.Contains(_SUCCESS))
{
//Remove !
rspStr = rspStr.Replace(_EXC_POINT, "");
//Return parsed message
return parsed[1];
}
else
{
throw new Exception("SendMessageGetResponse::SendMessageGetResponse() - Command message not successful");
}
}
}
/// <summary>
/// Change the units of measurement for the chiller.
/// </summary>
/// <param name="units">Celsius/Fahrenheit</param>
private void SetDegreesUnits(DEGREES units)
{
lock (m_sync)
{
//Set the units of measurement to Celsius
string rsp = SendMessageGetResponse(_DEGREES + units.ToString());
}
}
//Parse string ("/r")
char[] delimit = { '\r' };
string[] parsed = rspStr.Split(delimit, StringSplitOptions.RemoveEmptyEntries);
#endregion
//Return parsed message
return parsed[1];
}
else
{
throw new Exception("SendMessageGetResponse::SendMessageGetResponse() - Command message not successful");
}
}
}
/// <summary>
/// Change the units of measurement for the chiller.
/// </summary>
/// <param name="units">Celsius/Fahrenheit</param>
private void SetDegreesUnits(DEGREES units)
{
lock (m_sync)
{
//Set the units of measurement to Celsius
string rsp = SendMessageGetResponse(_DEGREES + units.ToString());
}
}
#endregion
/// <summary>
/// ChillerFTS factory constructor
/// </summary>
/// <param name="deviceName"></param>
/// <param name="configurationManager"></param>
public ChillerFTS(string deviceName, IConfigurationManager configurationManager, ILogger logger)
{
Name = deviceName;
/// <summary>
/// ChillerFTS factory constructor
/// </summary>
/// <param name="deviceName"></param>
/// <param name="configurationManager"></param>
public ChillerFTS(string deviceName, IConfigurationManager configurationManager)
{
Name = deviceName;
_logger = logger;
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
_configurationManager = configurationManager;
_configuration = _configurationManager.GetConfiguration(Name);
_configurationManager = configurationManager;
_configuration = _configurationManager.GetConfiguration(Name);
_ipAddr = _configuration.GetConfigurationValue("ChillerFTS", "IpAddr", "");
_port = _configuration.GetConfigurationValue("ChillerFTS", "Port", 0);
_readBuffer = new byte[_READ_BUFFER_SIZE];
_ipAddr = _configuration.GetConfigurationValue("ChillerFTS", "IpAddr", "");
_port = _configuration.GetConfigurationValue("ChillerFTS", "Port", 0);
//Connect to device
ConnectEthernet();
}
_readBuffer = new byte[_READ_BUFFER_SIZE];
/// <summary>
/// Constructor for the chiller. It makes a socket connection to the chiller and sets the degrees to Celsius
/// </summary>
/// <param name="name">The name</param>
/// <param name="ipAddress">IP Address of the equipment</param>
/// <param name="port">Port of the equipment</param>
public ChillerFTS(string name, string ipAddress, int port)
{
Name = name;
_ipAddr = ipAddress;
_port = port;
_logger = LogManager.GetCurrentClassLogger();
//Connect to device
ConnectEthernet();
}
_readBuffer = new byte[_READ_BUFFER_SIZE];
/// <summary>
/// Constructor for the chiller. It makes a socket connection to the chiller and sets the degrees to Celsius
/// </summary>
/// <param name="deviceName">The name</param>
/// <param name="ipAddress">IP Address of the equipment</param>
/// <param name="port">Port of the equipment</param>
public ChillerFTS(string deviceName, string ipAddress, int port)
{
Name = deviceName;
_ipAddr = ipAddress;
_port = port;
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
//Connect to device
ConnectEthernet();
}
_readBuffer = new byte[_READ_BUFFER_SIZE];
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="comPortName"></param>
/// <param name="delayBeforeReadMs"></param>
/// <param name="baudRate"></param>
/// <param name="parity"></param>
/// <param name="dataBits"></param>
/// <param name="stopBits"></param>
public ChillerFTS(string name, string comPortName, uint delayBeforeReadMs, int baudRate = 115200, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
{
throw new NotImplementedException();
}
//Connect to device
ConnectEthernet();
}
/// <summary>
/// Stop the chiller pump.
/// </summary>
public void DisableFlow()
{
lock (m_sync)
{
//Send the command to stop coolant flow
string rsp = SendMessageGetResponse(_FLOWDISABLE);
}
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="comPortName"></param>
/// <param name="delayBeforeReadMs"></param>
/// <param name="baudRate"></param>
/// <param name="parity"></param>
/// <param name="dataBits"></param>
/// <param name="stopBits"></param>
public ChillerFTS(string name, string comPortName, uint delayBeforeReadMs, int baudRate = 115200, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
{
throw new NotImplementedException();
}
/// <summary>
/// Dispose of this objects resources
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
public void Dispose()
{
try
{
Dispose(true);
/// <summary>
/// Stop the chiller pump.
/// </summary>
public void DisableFlow()
{
lock (m_sync)
{
//Send the command to stop coolant flow
string rsp = SendMessageGetResponse(_FLOWDISABLE);
}
}
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
}
}
}
/// <summary>
/// Dispose of this objects resources
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
public void Dispose()
{
Dispose(true);
/// <summary>
/// Start the chiller pump.
/// </summary>
public void EnableFlow()
{
lock (m_sync)
{
//Send the command to start coolant flow
string rsp = SendMessageGetResponse(_FLOWENABLE);
}
}
GC.SuppressFinalize(this);
}
/// <summary>
/// Query the current setting for the coolant.
/// </summary>
/// <returns>The current coolant setting.</returns>
public double GetCoolantSetpoint()
{
lock (m_sync)
{
//Name of the function
const string SETPOINT_RESPONSE = "F057=";
/// <summary>
/// Start the chiller pump.
/// </summary>
public void EnableFlow()
{
lock (m_sync)
{
//Send the command to start coolant flow
string rsp = SendMessageGetResponse(_FLOWENABLE);
}
}
//Request the setpoint
string results = SendMessageGetResponse(_TEMPREADSETPOINT);
/// <summary>
/// Query the current setting for the coolant.
/// </summary>
/// <returns>The current coolant setting.</returns>
public double GetCoolantSetpoint()
{
lock (m_sync)
{
//Name of the function
const string SETPOINT_RESPONSE = "F057=";
//Not connected. No results
if (results == "")
{
return double.MaxValue;
}
//Request the setpoint
string results = SendMessageGetResponse(_TEMPREADSETPOINT);
//Remove function header
results = results.Replace(SETPOINT_RESPONSE, "");
//Not connected. No results
if (results == "")
{
return double.MaxValue;
}
return double.Parse(results);
}
}
//Remove function header
results = results.Replace(SETPOINT_RESPONSE, "");
/// <summary>
/// Query the temperature of the coolant reservoir.
/// </summary>
/// <returns>The temperature of the coolant.</returns>
public double GetCoolantTemperature()
{
lock (m_sync)
{
//Name of the function
const string TEMP_RESPONSE = "F043=";
return double.Parse(results);
}
}
//Request the temperature
string results = SendMessageGetResponse(_TEMPREAD);
/// <summary>
/// Query the temperature of the coolant reservoir.
/// </summary>
/// <returns>The temperature of the coolant.</returns>
public double GetCoolantTemperature()
{
lock (m_sync)
{
//Name of the function
const string TEMP_RESPONSE = "F043=";
//Not connected. No results
if (results == "")
{
return double.MaxValue;
}
//Request the temperature
string results = SendMessageGetResponse(_TEMPREAD);
//Remove function header
results = results.Replace(TEMP_RESPONSE, "");
//Not connected. No results
if (results == "")
{
return double.MaxValue;
}
return double.Parse(results);
}
}
//Remove function header
results = results.Replace(TEMP_RESPONSE, "");
/// <summary>
/// Set the coolant temperature to a desired setpoint.
/// </summary>
/// <param name="temp">The desired coolant temperature.</param>
public void SetCoolantTemperature(double temp)
{
lock (m_sync)
{
//Set the coolant temperature
string rsp = SendMessageGetResponse(_TEMPSET + temp.ToString());
}
}
return double.Parse(results);
}
}
public bool ClearErrors()
{
throw new NotImplementedException();
}
/// <summary>
/// Set the coolant temperature to a desired setpoint.
/// </summary>
/// <param name="temp">The desired coolant temperature.</param>
public void SetCoolantTemperature(double temp)
{
lock (m_sync)
{
//Set the coolant temperature
string rsp = SendMessageGetResponse(_TEMPSET + temp.ToString());
}
}
public SelfTestResult PerformSelfTest()
{
return SelfTestResult.Unknown;
}
public bool ClearErrors()
{
throw new NotImplementedException();
}
public void Reset()
{
Shutdown();
public SelfTestResult PerformSelfTest()
{
return SelfTestResult.Unknown;
}
Initialize();
}
public void Reset()
{
Shutdown();
public void Shutdown()
{
Dispose();
}
}
Initialize();
}
public void Shutdown()
{
Dispose();
}
}
}

View File

@@ -30,71 +30,64 @@
// DISTRIBUTION/DISSEMINATION CONTROL: F
// POC: Alex Kravchenko (1118268)
// **********************************************************************************************************
using NLog;
using Raytheon.Common;
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 = "ChillerFTSFactory")]
public class ChillerFTSFactory : 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 = "ChillerFTSFactory")]
public class ChillerFTSFactory : IInstrumentFactory
{
private readonly List<Type> _supportedInterfaces = new List<Type>();
public ChillerFTSFactory(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 ChillerFTSFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
public ChillerFTSFactory(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>
/// ChillerFTSFactory injection constructor
/// </summary>
[ImportingConstructor]
public ChillerFTSFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(IChiller));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
_logger = LogManager.GetLogger(name);
return new ChillerFTS(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(IChiller));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
return new ChillerFTS(name, _configurationManager);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Gets the instrument
@@ -105,12 +98,10 @@ namespace Raytheon.Instruments
{
try
{
_logger = LogManager.GetLogger(name);
if (simulateHw)
return new ChillerSim(name, _configurationManager, _logger);
return new ChillerSim(name, _configurationManager);
else
return new ChillerFTS(name, _configurationManager, _logger);
return new ChillerFTS(name, _configurationManager);
}
catch (Exception)
{
@@ -123,17 +114,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);
}
}
}