369 lines
12 KiB
C#
369 lines
12 KiB
C#
using System;
|
|
using System.IO.Ports;
|
|
using Common;
|
|
using log4net;
|
|
using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.diagnosticLed;
|
|
using TBF.Rig.TestMethods.iPerlCommunication.communication.Utils;
|
|
using TBF.Rig.TestMethods.iPerlCommunication.iPerlHead;
|
|
|
|
namespace TBF.Rig.TestMethods.iPerlCommunication.communication
|
|
{
|
|
public class OptoHeadTest : IDisposable
|
|
{
|
|
//protected static readonly ILog rfidDataLogger = LogManager.GetLogger("RfidData");
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(OptoHeadTest));
|
|
|
|
private IperlHead iperlHead;
|
|
private SerialDriver serialDriver;
|
|
|
|
public static SerialDriver BuildConnection(IperlHead iHead)
|
|
{
|
|
return new SerialDriverBuilder()
|
|
.WithPort($"COM{iHead.RfidComPortNr}")
|
|
.WithBaudRate(2400)
|
|
.WithDataBits(8)
|
|
.WithParity(Parity.None)
|
|
.WithStopBits(StopBits.One)
|
|
.WithTimeouts(4000, 2000)
|
|
.BuildAndConnect();
|
|
|
|
}
|
|
|
|
public OptoHeadTest(IperlHead iperlHead)
|
|
{
|
|
this.iperlHead = iperlHead;
|
|
}
|
|
|
|
public void CloseConnection()
|
|
{
|
|
if (serialDriver != null)
|
|
serialDriver.CloseConnection();
|
|
serialDriver = null;
|
|
}
|
|
|
|
public bool ReadSerialNr()
|
|
{
|
|
try
|
|
{
|
|
if (iperlHead != null)
|
|
{
|
|
if (serialDriver == null)
|
|
serialDriver = BuildConnection(iperlHead);
|
|
|
|
log.Debug("ReadSerialNr called for iHead: " + iperlHead.ToString() + " serialDriver: " + serialDriver);
|
|
RadioService headService = new RadioService(serialDriver);
|
|
string serialNo = headService.ReadRequest_PCB(ref iperlHead);
|
|
if (!string.IsNullOrEmpty(serialNo))
|
|
{
|
|
log.Info($"Success Serial No: {serialNo} on COM{iperlHead.RfidComPortNr} serialDriver: {serialDriver}");
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error($"ReadSerialNr(COM{iperlHead.RfidComPortNr}) - Exception:" + ex.Message);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public string ReadRequest_PCB()
|
|
{
|
|
if (iperlHead.DebugLevel == DebugMode.Simulate)
|
|
{
|
|
return "-OK Simulated response-";
|
|
}
|
|
|
|
try
|
|
{
|
|
if (iperlHead != null)
|
|
{
|
|
if (serialDriver == null)
|
|
serialDriver = BuildConnection(iperlHead);
|
|
|
|
RadioService headService = new RadioService(serialDriver);
|
|
string serialNo = headService.ReadRequest_PCB(ref iperlHead);
|
|
log.Info($"PCB Number: {serialNo} on COM{iperlHead.RfidComPortNr} serialDriver: {serialDriver}");
|
|
return serialNo;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("ReadRequest_PCB() - Exception:" + ex.StackTrace);
|
|
return (ex.Message.ToString());
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set Test mode
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <returns></returns>
|
|
public bool SetTestMode()
|
|
{
|
|
log.Debug("SetTestMode called for iHead: " + iperlHead.ToString());
|
|
bool activityModeActive = SetActivityMode_Active();
|
|
bool optActiveMode = SetOptTestMode();
|
|
|
|
log.Debug("SetTestMode result: optoMod-> " + optActiveMode + " meterModeActive ->" + activityModeActive);
|
|
return (optActiveMode && activityModeActive);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set Active mode
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <returns></returns>
|
|
public bool SetActiveMode()
|
|
{
|
|
log.Debug("SetActiveMode called for iHead: " + iperlHead.ToString());
|
|
bool optActiveMode = SetOptActiveMode(iperlHead);
|
|
//bool activityModeIdle = SetActivityMode_Idle();
|
|
|
|
return optActiveMode;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set Idle mode - only
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <returns></returns>
|
|
public bool SetIdleMode()
|
|
{
|
|
log.Debug("SetIdleMode called for iHead: " + iperlHead.ToString());
|
|
bool activityModeIdle = SetActivityMode_Idle();
|
|
|
|
return activityModeIdle;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set Test mode - string response
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <param name="isTestModeSuccessful"></param>
|
|
/// <returns></returns>
|
|
public string SetTestMode(ref bool isTestModeSuccessful)
|
|
{
|
|
if (iperlHead.DebugLevel == DebugMode.Simulate)
|
|
{
|
|
isTestModeSuccessful = true;
|
|
return "-OK Simulated response-";
|
|
}
|
|
|
|
try
|
|
{
|
|
bool testMode = SetTestMode();
|
|
isTestModeSuccessful = testMode;
|
|
return testMode ? "Set Test Mode - OK" : "Set Test Mode - FAILED";
|
|
}catch (Exception ex)
|
|
{
|
|
log.Error("SetTestMode() - Exception:" + ex.StackTrace);
|
|
return "Set Test Mode - Exception";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Set Optical -> Test mode
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
private bool SetOptTestMode()
|
|
{
|
|
try
|
|
{
|
|
if (iperlHead != null)
|
|
{
|
|
if (serialDriver == null)
|
|
serialDriver = BuildConnection(iperlHead);
|
|
|
|
RadioService headService = new RadioService(serialDriver);
|
|
bool optTestMode = headService.SetOptTestMode(iperlHead);
|
|
if (iperlHead.ConfigStruct != null)
|
|
iperlHead.ConfigStruct.OpthoStatusMode = optTestMode ? DiagnosticLedState.State7 : DiagnosticLedState.StatusUnknown;
|
|
return optTestMode;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("SetOptTestMode() - Exception:" + ex.StackTrace);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set Active mode - string response
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <param name="isTestModeSuccessful"></param>
|
|
/// <returns></returns>
|
|
public string SetActiveMode(ref bool isTestModeSuccessful)
|
|
{
|
|
if (iperlHead.DebugLevel == DebugMode.Simulate)
|
|
{
|
|
isTestModeSuccessful = true;
|
|
return "-OK Simulated response-";
|
|
}
|
|
|
|
try
|
|
{
|
|
bool activeMode = SetActiveMode();
|
|
isTestModeSuccessful = activeMode;
|
|
return activeMode ? "Set Active Mode - OK" : "Set Active Mode - FAILED";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("SetActiveMode() - Exception:" + ex.StackTrace);
|
|
return "Set Active Mode - Exception";
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Set Optical -> Active mode
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
private bool SetOptActiveMode(IperlHead iHead)
|
|
{
|
|
try
|
|
{
|
|
if (iHead != null)
|
|
{
|
|
if (serialDriver == null)
|
|
serialDriver = BuildConnection(iHead);
|
|
|
|
RadioService headService = new RadioService(serialDriver);
|
|
return headService.SetOptActiveMode(iHead);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("SetOptActiveMode() - Exception:" + ex.StackTrace);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set activity mode to active
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
private bool SetActivityMode_Active()
|
|
{
|
|
try
|
|
{
|
|
if (iperlHead != null)
|
|
{
|
|
if (serialDriver == null)
|
|
serialDriver = BuildConnection(iperlHead);
|
|
|
|
log.Debug("SetActivityMode_Active called for iHead: " + iperlHead.ToString() + " serialDriver: " + serialDriver);
|
|
RadioService headService = new RadioService(serialDriver);
|
|
return headService.SetActivityMode_Active(iperlHead);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("SetActivityMode_Active() - Exception:" + ex.StackTrace);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set activity mode to idle
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
private bool SetActivityMode_Idle()
|
|
{
|
|
try
|
|
{
|
|
if (iperlHead != null)
|
|
{
|
|
if (serialDriver == null)
|
|
serialDriver = BuildConnection(iperlHead);
|
|
|
|
log.Debug("SetActivityMode_Idle called for iHead: " + iperlHead.ToString() + " serialDriver: " + serialDriver);
|
|
|
|
RadioService headService = new RadioService(serialDriver);
|
|
return headService.SetActivityMode_Idle(iperlHead);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("SetActivityMode_Idle() - Exception:" + ex.StackTrace);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
{
|
|
CloseConnection();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read configuration from iHead
|
|
/// DiagnosticLedState is not readable, mus only be set!
|
|
/// </summary>
|
|
/// <param name="iHead"></param>
|
|
/// <param name="ledState"></param>
|
|
/// <returns></returns>
|
|
public bool ReadConfiguration(DiagnosticLedState ledState )
|
|
{
|
|
if (iperlHead.DebugLevel == DebugMode.Simulate)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
try
|
|
{
|
|
|
|
if (iperlHead != null)
|
|
{
|
|
iperlHead.ConfigStruct = new ConfigStruct();
|
|
|
|
if (serialDriver == null)
|
|
serialDriver = BuildConnection(iperlHead);
|
|
|
|
RadioService headService = new RadioService(serialDriver);
|
|
iperlHead.ConfigStruct.PCBNumberString = headService.ReadRequest_PCB(ref iperlHead);
|
|
iperlHead.ConfigStruct.StatusMode = headService.GetActivityStatusMode(iperlHead);
|
|
iperlHead.ConfigStruct.Unit = headService.GetUnit(iperlHead);
|
|
|
|
if (ledState != DiagnosticLedState.StatusUnknown) // do set
|
|
{
|
|
iperlHead.ConfigStruct.OpthoStatusMode = headService.SetOptoStatusMode(iperlHead, ledState);
|
|
}
|
|
else
|
|
{
|
|
iperlHead.ConfigStruct.OpthoStatusMode = DiagnosticLedState.StatusUnknown;
|
|
}
|
|
|
|
iperlHead.ConfigStruct.Version = headService.GetVersion(iperlHead);
|
|
|
|
return true;
|
|
}
|
|
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |