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,12 +15,12 @@ GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using NLog;
using Raytheon.Common;
using Raytheon.Units;
using System;
using System.Net.Sockets;
using System.Text;
using NLog;
using Raytheon.Common;
using Raytheon.Units;
namespace Raytheon.Instruments
{
@@ -28,466 +28,452 @@ namespace Raytheon.Instruments
/// A class that provides an interface for controlling power supply modules via SCPI commands.
/// </summary>
public class PowerSupplyKeysight : IDCPwr
{
#region PublicClassMembers
{
#region PublicClassMembers
#pragma warning disable CS0067
public event EventHandler<OverCurrentEventArgs> OverCurrent;
public event EventHandler<OverVoltageEventArgs> OverVoltage;
public event EventHandler<OverCurrentEventArgs> OverCurrent;
public event EventHandler<OverVoltageEventArgs> OverVoltage;
#pragma warning restore
#endregion
#endregion
#region PrivateClassMembers
#region PrivateClassMembers
private string _name;
private readonly PowerScpiCommands _scpiCommands;
private double _overCurrentProtection;
private double _overVoltageProtection;
private double _voltageSetpoint;
private readonly double _voltageSetpointInitial;
private readonly double _maxVoltageSetpoint;
private readonly double _minVoltageSetpoint;
private readonly double _inRushDelay;
private readonly double _slewRateVoltsPerSecond;
private readonly int _moduleNumber;
private byte[] _readBuffer;
private NetworkStream _tcpStream;
private State _state;
private const int _READ_TIMEOUT = 5000;
private string _name;
private readonly PowerScpiCommands _scpiCommands;
private double _overCurrentProtection;
private double _overVoltageProtection;
private double _voltageSetpoint;
private readonly double _voltageSetpointInitial;
private readonly double _maxVoltageSetpoint;
private readonly double _minVoltageSetpoint;
private readonly double _inRushDelay;
private readonly double _slewRateVoltsPerSecond;
private readonly int _moduleNumber;
private byte[] _readBuffer;
private NetworkStream _tcpStream;
private State _state;
private const int _READ_TIMEOUT = 5000;
/// <summary>
/// NLog logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// NLog logger
/// </summary>
private readonly ILogger _logger;
#endregion
#endregion
#region PublicFuctions
#region PublicFuctions
/// <summary>
/// The constructor which sets the power supply to constant voltage mode, set the OCP, OVP and setpoint voltage
/// </summary>
/// <param name="name">The logical name of the supply.</param>
/// <param name="scpiCommands">Scpi commands to use.</param>
/// <param name="overCurrentProtection">The overcurrent protection setting (Amps).</param>
/// <param name="overVoltageProtection">The overvoltage protection setting (Volts).</param>
/// <param name="voltageSetpoint">The voltage setpoint (Volts).</param>
/// <param name="maxVoltageSetpoint">The voltage setpoint max (Volts) soft limit.</param>
/// <param name="minVoltageSetpoint">The voltage setpoint min (Volts) soft limit.</param>
/// <param name="inRushDelayInSeconds">In rush delay in seconds.-1 to leave it as default</param>
/// <param name="slewRateVoltsPerSecond">The ramp up rate.-1 to leave it as default</param>
/// <param name="tcpStream">TCP Stream (Ethernet).</param>
/// <param name="moduleNumber">The module number (multiple modules in a system).</param>
public PowerSupplyKeysight(string name, PowerScpiCommands scpiCommands, double overCurrentProtection, double overVoltageProtection, double voltageSetpoint, double maxVoltageSetpoint, double minVoltageSetpoint, double inRushDelayInSeconds, double slewRateVoltsPerSecond, NetworkStream tcpStream, int moduleNumber = -1)
{
const int READ_BUFFER_SIZE = 512;
/// <summary>
/// The constructor which sets the power supply to constant voltage mode, set the OCP, OVP and setpoint voltage
/// </summary>
/// <param name="name">The logical name of the supply.</param>
/// <param name="scpiCommands">Scpi commands to use.</param>
/// <param name="overCurrentProtection">The overcurrent protection setting (Amps).</param>
/// <param name="overVoltageProtection">The overvoltage protection setting (Volts).</param>
/// <param name="voltageSetpoint">The voltage setpoint (Volts).</param>
/// <param name="maxVoltageSetpoint">The voltage setpoint max (Volts) soft limit.</param>
/// <param name="minVoltageSetpoint">The voltage setpoint min (Volts) soft limit.</param>
/// <param name="inRushDelayInSeconds">In rush delay in seconds.-1 to leave it as default</param>
/// <param name="slewRateVoltsPerSecond">The ramp up rate.-1 to leave it as default</param>
/// <param name="tcpStream">TCP Stream (Ethernet).</param>
/// <param name="moduleNumber">The module number (multiple modules in a system).</param>
public PowerSupplyKeysight(string name, PowerScpiCommands scpiCommands, double overCurrentProtection, double overVoltageProtection, double voltageSetpoint, double maxVoltageSetpoint, double minVoltageSetpoint, double inRushDelayInSeconds, double slewRateVoltsPerSecond, NetworkStream tcpStream, int moduleNumber = -1)
{
const int READ_BUFFER_SIZE = 512;
_name = name;
_logger = LogManager.GetCurrentClassLogger();
_scpiCommands = scpiCommands;
_overCurrentProtection = overCurrentProtection;
_overVoltageProtection = overVoltageProtection;
_voltageSetpoint = voltageSetpoint;
_voltageSetpointInitial = voltageSetpoint;
_maxVoltageSetpoint = maxVoltageSetpoint;
_minVoltageSetpoint = minVoltageSetpoint;
_inRushDelay = inRushDelayInSeconds;
_slewRateVoltsPerSecond = slewRateVoltsPerSecond;
_moduleNumber = moduleNumber;
_readBuffer = new byte[READ_BUFFER_SIZE];
_name = name;
_logger = LogManager.GetCurrentClassLogger();
_scpiCommands = scpiCommands;
_overCurrentProtection = overCurrentProtection;
_overVoltageProtection = overVoltageProtection;
_voltageSetpoint = voltageSetpoint;
_voltageSetpointInitial = voltageSetpoint;
_maxVoltageSetpoint = maxVoltageSetpoint;
_minVoltageSetpoint = minVoltageSetpoint;
_inRushDelay = inRushDelayInSeconds;
_slewRateVoltsPerSecond = slewRateVoltsPerSecond;
_moduleNumber = moduleNumber;
_readBuffer = new byte[READ_BUFFER_SIZE];
_tcpStream = tcpStream;
_tcpStream = tcpStream;
_state = State.Uninitialized;
}
_state = State.Uninitialized;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool ClearErrors()
{
return _state == State.Uninitialized;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool ClearErrors()
{
return _state == State.Uninitialized;
}
/// <summary>
/// Dispose of this object.
/// </summary>
public void Dispose()
{
try
{
Dispose(true);
/// <summary>
/// Dispose of 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 Current CurrentLimit
{
get
{
return Current.FromAmps(ReadOcpSetpoint());
}
/// <summary>
///
/// </summary>
public Current CurrentLimit
{
get
{
return Current.FromAmps(ReadOcpSetpoint());
}
set
{
_overCurrentProtection = value.Amps;
set
{
_overCurrentProtection = value.Amps;
SetAndConfirmOCP();
}
}
SetAndConfirmOCP();
}
}
/// <summary>
///
/// </summary>
public string DetailedStatus
{
get
{
return "This is a Keysight Scpi Power Supply";
}
}
/// <summary>
///
/// </summary>
public string DetailedStatus
{
get
{
return "This is a Keysight Scpi Power Supply";
}
}
/// <summary>
///
/// </summary>
public bool DisplayEnabled
{
get
{
return false;
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public bool DisplayEnabled
{
get
{
return false;
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public bool Enabled
{
get
{
return IsOutputOn();
}
/// <summary>
///
/// </summary>
public bool Enabled
{
get
{
return IsOutputOn();
}
set
{
if (value == false)
{
Off();
}
else
{
On();
}
}
}
set
{
if (value == false)
{
Off();
}
else
{
On();
}
}
}
/// <summary>
///
/// </summary>
public bool FrontPanelEnabled
{
get
{
return false;
}
/// <summary>
///
/// </summary>
public bool FrontPanelEnabled
{
get
{
return false;
}
set
{
throw new NotImplementedException();
}
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
/// Returns this object's module number.
/// </summary>
/// <returns>The module number.</returns>
/*public int GetModuleNumber()
/// <summary>
/// Returns this object's module number.
/// </summary>
/// <returns>The module number.</returns>
/*public int GetModuleNumber()
{
return _moduleNumber;
}*/
/// <summary>
///
/// </summary>
public bool InhibitEnabled
{
get
{
return false;
}
/// <summary>
///
/// </summary>
public bool InhibitEnabled
{
get
{
return false;
}
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()
{
if (_state == State.Uninitialized)
{
// make sure it is off
Off();
/// <summary>
///
/// </summary>
public void Initialize()
{
if (_state == State.Uninitialized)
{
// make sure it is off
Off();
// set up power supply
// for 6702 look at page 36
SetConstantVoltageMode();
SetAndConfirmOVP();
SetAndConfirmOCP();
SetVoltageSetpoint(_voltageSetpoint);
// set up power supply
// for 6702 look at page 36
SetConstantVoltageMode();
SetAndConfirmOVP();
SetAndConfirmOCP();
SetVoltageSetpoint(_voltageSetpoint);
// -1.0 means leave as default
if (_inRushDelay != -1.0)
{
SetAndConfirmInRushDelay();
}
// -1.0 means leave as default
if (_inRushDelay != -1.0)
{
SetAndConfirmInRushDelay();
}
// -1.0 means leave as default
if (_slewRateVoltsPerSecond != -1.0)
{
SetAndConfirmSlewRate();
}
// -1.0 means leave as default
if (_slewRateVoltsPerSecond != -1.0)
{
SetAndConfirmSlewRate();
}
_state = State.Ready;
}
else
{
throw new Exception("PowerSupplyKeysight::Initialize() - " + _name + " expected the state to be Uninitialized, state was: " + _state.ToString());
}
}
_state = State.Ready;
}
else
{
throw new Exception(_name + " expected the state to be Uninitialized, state was: " + _state.ToString());
}
}
/// <summary>
/// Control the power supply internal mechanical relay state
/// </summary>
/// <param name="shallWeConnect">True to connect, false to disconnect</param>
public void MechanicalRelayOutputControl(bool shallWeConnect)
{
throw new Exception("Not yet implemented");
}
/// <summary>
/// Control the power supply internal mechanical relay state
/// </summary>
/// <param name="shallWeConnect">True to connect, false to disconnect</param>
public void MechanicalRelayOutputControl(bool shallWeConnect)
{
throw new Exception("Not yet implemented");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public Current MeasureCurrent()
{
return Current.FromAmps(ReadCurrent());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public Current MeasureCurrent()
{
return Current.FromAmps(ReadCurrent());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public Voltage MeasureVoltage()
{
return Voltage.FromVolts(ReadVoltage());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public Voltage MeasureVoltage()
{
return Voltage.FromVolts(ReadVoltage());
}
/// <summary>
///
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
///
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
///
/// </summary>
public Voltage OutputVoltage
{
get
{
return Voltage.FromVolts(ReadVoltageSetpoint());
}
/// <summary>
///
/// </summary>
public Voltage OutputVoltage
{
get
{
return Voltage.FromVolts(ReadVoltageSetpoint());
}
set
{
double volts = value.Volts;
set
{
double volts = value.Volts;
// do not let host set the voltage out of range, unless it is being set to 0
if (volts != 0.0)
{
if (volts > _maxVoltageSetpoint || volts < _minVoltageSetpoint)
{
throw new Exception("PowerSupplyKeysight::OutputVoltage() - Desired voltage setpoint out of range for supply " + _name + ". Commanded setpoint: " + value.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString());
}
}
// do not let host set the voltage out of range, unless it is being set to 0
if (volts != 0.0)
{
if (volts > _maxVoltageSetpoint || volts < _minVoltageSetpoint)
{
throw new Exception("Desired voltage setpoint out of range for supply " + _name + ". Commanded setpoint: " + value.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString());
}
}
SetVoltageSetpoint(volts);
}
}
SetVoltageSetpoint(volts);
}
}
/// <summary>
///
/// </summary>
public Voltage OverVoltageProtection
{
get
{
return Voltage.FromVolts(ReadOvpSetpoint());
}
/// <summary>
///
/// </summary>
public Voltage OverVoltageProtection
{
get
{
return Voltage.FromVolts(ReadOvpSetpoint());
}
set
{
_overVoltageProtection = value.Volts;
set
{
_overVoltageProtection = value.Volts;
SetAndConfirmOVP();
}
}
SetAndConfirmOVP();
}
}
/// <summary>
///
/// </summary>
public bool OverVoltageProtectionEnabled
{
get
{
return true;
}
/// <summary>
///
/// </summary>
public bool OverVoltageProtectionEnabled
{
get
{
return true;
}
set
{
throw new NotImplementedException();
}
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public SelfTestResult PerformSelfTest()
{
throw new NotImplementedException("PowerSupplyKeysight::PerformSelfTest() - self test is not implemented in the " + _name + " supply");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public SelfTestResult PerformSelfTest()
{
throw new NotImplementedException("Self test is not implemented in the " + _name + " supply");
}
/// <summary>
/// Read the overvoltage and overcurrent protection status.
/// </summary>
/// <returns>The binary sum of all bits (decimal value) set in the Questionable Status Condition register.</returns>
public int ReadProtectionStatus()
{
// send the command
string command = _scpiCommands._READ_PROTECTION_STATUS_CMD + " " + GetModuleCmd() + "\n";
string rspStr = IOQuery(command);
/// <summary>
/// Read the overvoltage and overcurrent protection status.
/// </summary>
/// <returns>The binary sum of all bits (decimal value) set in the Questionable Status Condition register.</returns>
public int ReadProtectionStatus()
{
// send the command
string command = _scpiCommands._READ_PROTECTION_STATUS_CMD + " " + GetModuleCmd() + "\n";
string rspStr = IOQuery(command);
// parse the response
string[] tokens = rspStr.Split('\n');
// parse the response
string[] tokens = rspStr.Split('\n');
int ret = Util.ConvertStringToInt32(tokens[0]);
int ret = Util.ConvertStringToInt32(tokens[0]);
return ret;
}
return ret;
}
/// <summary>
///
/// </summary>
public void Reset()
{
throw new NotImplementedException("PowerSupplyKeysight::Reset() - cant reset a module, only the system");
}
/// <summary>
///
/// </summary>
public void Reset()
{
throw new NotImplementedException("Cant reset a module, only the system");
}
/// <summary>
///
/// </summary>
public SelfTestResult SelfTestResult
{
get
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public SelfTestResult SelfTestResult
{
get
{
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
public State Status
{
get
{
return _state;
}
}
/// <summary>
///
/// </summary>
public State Status
{
get
{
return _state;
}
}
/// <summary>
///
/// </summary>
/// <summary>
///
/// </summary>
public void Shutdown()
{
if (_state == State.Ready)
{
string errorMsg = "";
public void Shutdown()
{
if (_state == State.Ready)
{
string errorMsg = "";
try
{
Off();
}
catch (Exception err)
{
errorMsg += err.Message + " ";
}
try
{
Off();
}
catch (Exception err)
{
errorMsg += err.Message + " ";
}
_state = State.Uninitialized;
_state = State.Uninitialized;
if (errorMsg != "")
{
throw new Exception("PowerSupplyKeysight::ShutdDown() - Power Supply " + _name + " had an error: " + errorMsg);
}
}
}
if (errorMsg != "")
{
throw new Exception("Power Supply " + _name + " had an error: " + errorMsg);
}
}
}
/// <summary>
///
/// </summary>
public Voltage VoltageSoftLimit
{
get
{
return Voltage.FromVolts(_maxVoltageSetpoint);
}
/// <summary>
///
/// </summary>
public Voltage VoltageSoftLimit
{
get
{
return Voltage.FromVolts(_maxVoltageSetpoint);
}
set
{
throw new NotImplementedException();
}
}
set
{
throw new NotImplementedException();
}
}
#endregion
@@ -506,33 +492,19 @@ namespace Raytheon.Instruments
/// <param name="disposing">True = currently disposing, False = not disposing.</param>
protected virtual void Dispose(bool disposing)
{
try
if (disposing)
{
if (disposing)
if (_state == State.Ready)
{
if (_state == State.Ready)
try
{
try
{
Off();
}
catch (Exception)
{
}
_state = State.Uninitialized;
Off();
}
}
}
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
catch (Exception)
{
}
_state = State.Uninitialized;
}
}
}
@@ -651,7 +623,7 @@ namespace Raytheon.Instruments
if (programmedSlewValue != _slewRateVoltsPerSecond)
{
string errMsg = "PowerSupplyKeysight:SetAndConfirmSlewRate() - power supply " + _name + " reported a setpoint slew rate that was not cmmanded. Trying to set it to: " + _slewRateVoltsPerSecond + ", the power supply returned: " + programmedSlewValue;
string errMsg = "Power supply " + _name + " reported a setpoint slew rate that was not commanded. Trying to set it to: " + _slewRateVoltsPerSecond + ", the power supply returned: " + programmedSlewValue;
throw new Exception(errMsg);
}
}
@@ -669,12 +641,12 @@ namespace Raytheon.Instruments
{
if (voltage > _maxVoltageSetpoint || voltage < _minVoltageSetpoint)
{
throw new Exception("PowerSupplyKeysight:SetVoltageSetpoint() - Desired voltage setpoint for supply " + _name + " out of range. Commanded setpoint: " + voltage.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString());
throw new Exception("Desired voltage setpoint for supply " + _name + " out of range. Commanded setpoint: " + voltage.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString());
}
if (voltage > _overVoltageProtection)
{
throw new Exception("PowerSupplyKeysight:SetVoltageSetpoint() - Desired voltage setpoint for supply " + _name + " out of range. Commanded setpoint: " + voltage.ToString() + ", OVP is: " + _overVoltageProtection.ToString());
throw new Exception("Desired voltage setpoint for supply " + _name + " out of range. Commanded setpoint: " + voltage.ToString() + ", OVP is: " + _overVoltageProtection.ToString());
}
}
@@ -687,12 +659,12 @@ namespace Raytheon.Instruments
if (voltageSetpointReturned < (voltage - TOLERANCE))
{
string errMsg = "PowerSupplyKeysight:SetVoltageSetpoint() - power supply " + _name + " reported setpoint voltage lower than expected. Trying to set it to: " + voltage + ", the power supply returned: " + voltageSetpointReturned;
string errMsg = "Power supply " + _name + " reported setpoint voltage lower than expected. Trying to set it to: " + voltage + ", the power supply returned: " + voltageSetpointReturned;
throw new Exception(errMsg);
}
else if (voltageSetpointReturned > (voltage + TOLERANCE))
{
string errMsg = "PowerSupplyKeysight:SetVoltageSetpoint() - power supply " + _name + " reported setpoint voltage higher than expected. Trying to set it to: " + voltage + ", the power supply returned: " + voltageSetpointReturned;
string errMsg = "Power supply " + _name + " reported setpoint voltage higher than expected. Trying to set it to: " + voltage + ", the power supply returned: " + voltageSetpointReturned;
throw new Exception(errMsg);
}
@@ -713,7 +685,7 @@ namespace Raytheon.Instruments
{
response = Encoding.ASCII.GetString(_readBuffer, 0, bytesRead);
}
return response;
}

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 = "PowerSupplySystemKeysightFactory")]
public class PowerSupplySystemKeysightFactory : 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 = "PowerSupplySystemKeysightFactory")]
public class PowerSupplySystemKeysightFactory : IInstrumentFactory
{
private readonly List<Type> _supportedInterfaces = new List<Type>();
public PowerSupplySystemKeysightFactory(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 PowerSupplySystemKeysightFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
public PowerSupplySystemKeysightFactory(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>
/// PowerSupplySystemKeysightFactory injection constructor
/// </summary>
[ImportingConstructor]
public PowerSupplySystemKeysightFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(IPowerSupplySystem));
}
if (LogManager.Configuration == null)
{
var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
_logger = LogManager.GetLogger(name);
return new PowerSupplySystemKeysight(name, _configurationManager, _logger);
}
catch (Exception)
{
throw;
}
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(IPowerSupplySystem));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
return new PowerSupplySystemKeysight(name, _configurationManager);
}
catch (Exception)
{
throw;
}
}
/// <summary>
@@ -106,16 +99,14 @@ namespace Raytheon.Instruments
{
try
{
_logger = LogManager.GetLogger(name);
if (simulateHw)
return new PowerSupplySystemSim(name, _configurationManager, _logger);
else
return new PowerSupplySystemKeysight(name, _configurationManager, _logger);
if (simulateHw)
return new PowerSupplySystemSim(name, _configurationManager);
else
return new PowerSupplySystemKeysight(name, _configurationManager);
}
catch (Exception)
{
throw;
throw;
}
}
@@ -124,17 +115,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);
}
}
}

View File

@@ -15,24 +15,22 @@ GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using NLog;
using Raytheon.Common;
using Raytheon.Units;
using System;
using System.Net.Sockets;
using System.Threading;
using NLog;
using Raytheon.Units;
namespace Raytheon.Instruments
{
/// <summary>
/// A class that provides an interface for controlling simulated power supply systems and their modules.
/// </summary>
public class PowerSupplySim : IDCPwr
{
#region PublicClassMembers
/// <summary>
/// A class that provides an interface for controlling simulated power supply systems and their modules.
/// </summary>
public class PowerSupplySim : IDCPwr
{
#region PublicClassMembers
#pragma warning disable CS0067
public event EventHandler<OverCurrentEventArgs> OverCurrent;
public event EventHandler<OverVoltageEventArgs> OverVoltage;
public event EventHandler<OverCurrentEventArgs> OverCurrent;
public event EventHandler<OverVoltageEventArgs> OverVoltage;
#pragma warning restore
#endregion
@@ -131,23 +129,9 @@ namespace Raytheon.Instruments
/// </summary>
public void Dispose()
{
try
{
Dispose(true);
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>
@@ -233,7 +217,7 @@ namespace Raytheon.Instruments
}
else
{
throw new Exception("PowerSupplySim::Initialize() - expected the supply " + _name + " to be Uninitialized, state was: " + _state.ToString());
throw new Exception("Expected the supply " + _name + " to be Uninitialized, state was: " + _state.ToString());
}
}
@@ -312,7 +296,7 @@ namespace Raytheon.Instruments
{
if (volts > _maxVoltageSetpoint || volts < _minVoltageSetpoint)
{
throw new Exception("PowerSupplySim::OutputVoltage() - Desired voltage setpoint out of range for supply " + _name + ". Commanded setpoint: " + value.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString());
throw new Exception("Desired voltage setpoint out of range for supply " + _name + ". Commanded setpoint: " + value.ToString() + ", Max: " + _maxVoltageSetpoint.ToString() + ", Min: " + _minVoltageSetpoint.ToString());
}
}
@@ -452,149 +436,135 @@ namespace Raytheon.Instruments
#region PrivateClassMembers
private string _name;
private double _overCurrentProtection;
private double _overVoltageProtection;
private double _voltageSetpoint;
private double _voltageSetpointInitial;
private readonly double _maxVoltageSetpoint;
private readonly double _minVoltageSetpoint;
private readonly int _moduleNumber;
private bool _isPowerOn;
private double _slewRateVoltsPerSecond;
private State _state;
private double _overCurrentProtection;
private double _overVoltageProtection;
private double _voltageSetpoint;
private double _voltageSetpointInitial;
private readonly double _maxVoltageSetpoint;
private readonly double _minVoltageSetpoint;
private readonly int _moduleNumber;
private bool _isPowerOn;
private double _slewRateVoltsPerSecond;
private State _state;
/// <summary>
/// NLog logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// NLog logger
/// </summary>
private readonly ILogger _logger;
#endregion
#endregion
#region PrivateFuctions
#region PrivateFuctions
/// <summary>
/// The finalizer.
/// </summary>
~PowerSupplySim()
{
Dispose(false);
}
/// <summary>
/// The finalizer.
/// </summary>
~PowerSupplySim()
{
Dispose(false);
}
/// <summary>
/// Dispose of this object.
/// </summary>
/// <param name="disposing">True = currently disposing, False = not disposing.</param>
protected virtual void Dispose(bool disposing)
{
try
{
if (disposing)
{
if (_state == State.Ready)
{
Off();
/// <summary>
/// Dispose of this object.
/// </summary>
/// <param name="disposing">True = currently disposing, False = not disposing.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_state == State.Ready)
{
Off();
_state = State.Uninitialized;
}
}
}
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
}
}
}
_state = State.Uninitialized;
}
}
}
/// <summary>
/// Returns this object module number.
/// </summary>
/// <returns>The module number.</returns>
/*public int GetModuleNumber()
/// <summary>
/// Returns this object module number.
/// </summary>
/// <returns>The module number.</returns>
/*public int GetModuleNumber()
{
return _moduleNumber;
}*/
/// <summary>
/// Turn the output off (simulated).
/// </summary>
private void Off()
{
// a small 10 ms sleep for simulation
Thread.Sleep(10);
/// <summary>
/// Turn the output off (simulated).
/// </summary>
private void Off()
{
// a small 10 ms sleep for simulation
Thread.Sleep(10);
_isPowerOn = false;
}
_isPowerOn = false;
}
/// <summary>
/// Turn the output on (simulated).
/// </summary>
private void On()
{
// a small 10 ms sleep for simulation
Thread.Sleep(10);
/// <summary>
/// Turn the output on (simulated).
/// </summary>
private void On()
{
// a small 10 ms sleep for simulation
Thread.Sleep(10);
_isPowerOn = true;
}
_isPowerOn = true;
}
/// <summary>
/// Read the current (simulated).
/// </summary>
/// <returns>The current (simulated).</returns>
private double ReadCurrent()
{
// a small 10 ms sleep for simulation
Thread.Sleep(100);
/// <summary>
/// Read the current (simulated).
/// </summary>
/// <returns>The current (simulated).</returns>
private double ReadCurrent()
{
// a small 10 ms sleep for simulation
Thread.Sleep(100);
double currentToReturn = 0.0;
double currentToReturn = 0.0;
if (_isPowerOn)
{
double maxCurrent = _overCurrentProtection;
double minCurrent = _overCurrentProtection - .5;
if (_isPowerOn)
{
double maxCurrent = _overCurrentProtection;
double minCurrent = _overCurrentProtection - .5;
Random rnd = new Random();
Random rnd = new Random();
double seed = rnd.NextDouble();
double seed = rnd.NextDouble();
currentToReturn = (seed * (maxCurrent - minCurrent)) + minCurrent;
}
currentToReturn = (seed * (maxCurrent - minCurrent)) + minCurrent;
}
return currentToReturn;
}
return currentToReturn;
}
/// <summary>
/// Read the voltage.
/// </summary>
/// <returns>The voltage (simulated).</returns>
private double ReadVoltage()
{
// a small 10 ms sleep for simulation
Thread.Sleep(100);
/// <summary>
/// Read the voltage.
/// </summary>
/// <returns>The voltage (simulated).</returns>
private double ReadVoltage()
{
// a small 10 ms sleep for simulation
Thread.Sleep(100);
double voltageToReturn = 0.0;
double voltageToReturn = 0.0;
if (_isPowerOn)
{
double maxVoltage = _voltageSetpoint + 1;
double minVoltage = _voltageSetpoint - 1;
if (_isPowerOn)
{
double maxVoltage = _voltageSetpoint + 1;
double minVoltage = _voltageSetpoint - 1;
Random rnd = new Random();
Random rnd = new Random();
double seed = rnd.NextDouble();
double seed = rnd.NextDouble();
voltageToReturn = (seed * (maxVoltage - minVoltage)) + minVoltage;
}
voltageToReturn = (seed * (maxVoltage - minVoltage)) + minVoltage;
}
return voltageToReturn;
}
return voltageToReturn;
}
#endregion
#endregion
}
}
}

View File

@@ -30,72 +30,65 @@
// 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 = "PowerSupplySystemSimFactory")]
public class PowerSupplySystemSimFactory : 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 = "PowerSupplySystemSimFactory")]
public class PowerSupplySystemSimFactory : IInstrumentFactory
{
private readonly List<Type> _supportedInterfaces = new List<Type>();
public PowerSupplySystemSimFactory(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 PowerSupplySystemSimFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
public PowerSupplySystemSimFactory(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>
/// PowerSupplySystemSimFactory injection constructor
/// </summary>
[ImportingConstructor]
public PowerSupplySystemSimFactory([Import(AllowDefault = false)] IConfigurationManager configManager,
[Import(AllowDefault = true)] string defaultConfigPath = null)
{
DefaultPath = defaultConfigPath;
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(IPowerSupplySystem));
}
if (LogManager.Configuration == null)
{
var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
_logger = LogManager.GetLogger(name);
return new PowerSupplySystemSim(name, _configurationManager, _logger);
}
catch (Exception)
{
throw;
}
}
_configurationManager = configManager ?? GetConfigurationManager();
_supportedInterfaces.Add(typeof(IPowerSupplySystem));
}
/// <summary>
/// Gets the instrument
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IInstrument GetInstrument(string name)
{
try
{
return new PowerSupplySystemSim(name, _configurationManager);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Gets the instrument
@@ -106,12 +99,11 @@ namespace Raytheon.Instruments
{
try
{
_logger = LogManager.GetLogger(name);
return new PowerSupplySystemSim(name, _configurationManager, _logger);
return new PowerSupplySystemSim(name, _configurationManager);
}
catch (Exception)
{
throw;
throw;
}
}
@@ -120,17 +112,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);
}
}
}