// UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.
THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using System;
using System.Text;
using NLog;
using Pickering.Pipx40.Interop;
using Raytheon.Common;
namespace Raytheon.Instruments
{
///
/// A Pickering implementation of the IRelaySwitch interface
///
public class SwitchPickeringPipx40 : ISwitch
{
#region PrivateMemberVariables
private readonly int _handle;
private string _name;
private readonly int _subUnit;
private State _state;
private SelfTestResult _selfTestResult;
private readonly ILogger _logger;
private readonly IConfigurationManager _configurationManager;
private readonly IConfiguration _configuration;
#endregion
#region PrivateFunctions
///
/// Dispose of the resources contained by this object
///
///
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_state == State.Ready)
{
RelayOpenAll();
Pickering.Pipx40.Interop.Pipx40Module.Reset(_handle);
Pickering.Pipx40.Interop.Pipx40Module.Close(_handle);
_state = State.Uninitialized;
}
}
}
///
/// Close a single relay
/// The relay string should be the string form of an integer that represents the bit to close
///
/// The relay to close
private void RelayClose(string relay)
{
//VI_ON energizes
int ret = Pickering.Pipx40.Interop.Pipx40Module.SetChannelState(_handle, _subUnit, Convert.ToInt32(relay), 1);
//pipx40_operateSwitch
if (ret != 0)
{
throw new Exception("Pickering.Pipx40.Interop.Pipx40Module.SetChannelState() return: " + ret.ToString());
}
}
///
/// Opens a single relay
/// The relay strings should be the string form of an integer that represents the bit to close
///
/// The relay to open
private void RelayOpen(string relay)
{
//VI_OFF energizes
int ret = Pickering.Pipx40.Interop.Pipx40Module.SetChannelState(_handle, _subUnit, Convert.ToInt32(relay), 0);
//pipx40_operateSwitch
if (ret != 0)
{
throw new Exception("Pickering.Pipx40.Interop.Pipx40Module.SetChannelState() return: " + ret.ToString());
}
}
///
/// Opens all relays on the card
///
private void RelayOpenAll()
{
Pickering.Pipx40.Interop.Pipx40Module.ClearCard(_handle);
//Pickering.Pipx40.Interop.Pipx40Module.ClearSub(_handle, _subUnit);
}
private void SelfTest()
{
SelftestFault stFault = new SelftestFault();
StringBuilder message = new StringBuilder();
int ret = Pickering.Pipx40.Interop.Pipx40Module.SelfTest(_handle, ref stFault, message);
if (ret != 0)
{
throw new Exception("Pickering.Pipx40.Interop.Pipx40Module.SelfTest() return: " + ret.ToString() + ", " + message.ToString());
}
}
#endregion
#region PublicFunctions
///
/// SwitchPickeringPipx40 factory constructor
///
///
///
public SwitchPickeringPipx40(string deviceName, IConfigurationManager configurationManager)
{
Name = deviceName;
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
_configurationManager = configurationManager;
_configuration = _configurationManager.GetConfiguration(Name);
_subUnit = _configuration.GetConfigurationValue("SwitchPickeringPipx40", "SubUnit", 0);
int ret = Pipx40Module.Init(Name, 0, 1, ref _handle);
if (ret != 0)
{
throw new Exception("Pickering.Pipx40.Interop.Pipx40Module.Init() return: " + ret.ToString());
}
_state = State.Uninitialized;
_selfTestResult = SelfTestResult.Unknown;
}
///
///
///
///
///
///
public SwitchPickeringPipx40(string deviceName, string address, int subUnit)
{
_name = deviceName;
_logger = LogManager.GetLogger($"{this.GetType().Name} - {deviceName}");
_subUnit = subUnit;
int ret = Pipx40Module.Init(deviceName, 0, 1, ref _handle);
if (ret != 0)
{
throw new Exception("Pickering.Pipx40.Interop.Pipx40Module.Init() return: " + ret.ToString());
}
_state = State.Uninitialized;
_selfTestResult = SelfTestResult.Unknown;
}
///
/// The Finalizer
///
~SwitchPickeringPipx40()
{
Dispose(false);
}
///
///
///
///
public bool ClearErrors()
{
throw new NotImplementedException();
}
///
///
///
///
public void Connect(string path)
{
RelayClose(path);
}
///
///
///
///
public void Disconnect(string path)
{
RelayOpen(path);
}
///
///
///
public void DisconnectAll()
{
RelayOpenAll();
}
///
/// Dispose of the resources contained by this object
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
///
///
public string DetailedStatus
{
get
{
return "This is a Pickering Switch";
}
}
///
///
///
public bool DisplayEnabled
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
///
///
///
public bool FrontPanelEnabled
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
///
///
///
public InstrumentMetadata Info
{
get
{
throw new NotImplementedException();
}
}
///
///
///
public void Initialize()
{
if (_state == State.Uninitialized)
{
_state = State.Ready;
}
else
{
throw new Exception("expected the state to be Uninitialized, state was: " + _state.ToString());
}
}
///
///
///
public bool IsDebounced
{
get
{
throw new NotImplementedException();
}
}
///
///
///
public string Name
{
get { return _name; }
set { _name = value; }
}
///
///
///
///
public SelfTestResult PerformSelfTest()
{
throw new NotImplementedException();
}
///
///
///
public void Reset()
{
throw new NotImplementedException();
}
///
///
///
public SelfTestResult SelfTestResult
{
get
{
return _selfTestResult;
}
}
///
///
///
public State Status
{
get
{
return _state;
}
}
///
///
///
public void Shutdown()
{
if (_state == State.Ready)
{
RelayOpenAll();
Pickering.Pipx40.Interop.Pipx40Module.Reset(_handle);
Pickering.Pipx40.Interop.Pipx40Module.Close(_handle);
_state = State.Uninitialized;
}
}
#endregion
}
}