tbf/TBF/Rig/RegisterReaders/GenesisRegReader/implementations/GenesisImplHeadTestCtrl.cs

215 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using Common;
using log4net;
using TBF.Rig.Generic;
using TBF.Rig.RegisterReaders.iPerlReaderUNI.common;
using TBF.Rig.Uni.SharedDialogs.SmartMetersCommunication.common;
namespace TBF.Rig.RegisterReaders.GenesisRegReader.implementations
{
public class GenesisImplHeadTestCtrl : IUniHeadTestCtrl<OptoReceivedEventArgs>
{
private static readonly ILog log = LogManager.GetLogger(typeof(GenesisSmartReader));
private static readonly ILog logStream = LogManager.GetLogger("StreamData");
Thread optoThread;
//GenesisSmartReader _genesiHead;
public GenesisSmartReader Head
{
get { return ISmartReader as GenesisSmartReader; }
}
public ISmartReader ISmartReader { get; set; }
public bool stopWorkerThread { get; set; }
public event EventHandler<OptoReceivedEventArgs> OptoReceivedHandler;
public IComponentCfg config { get; set; }
public void Initialize()
{
stopWorkerThread = false;
}
public void Destroy()
{
if(Head != null && Head.OptoHeadTest != null)
{
Head.OptoHeadTest.CloseConnection();
}
stopWorkerThread = true;
if (optoThread != null)
{
optoThread.Abort();
}
}
private const string StrReadPcbCmd = "ReadPCB";
private const string StrSetTestModeCmd = "SetTestMode";
private const string StrSetActiveModeCmd = "SetActiveMode";
private const string StrReadOptoDataCmd = "ReadOptoData";
private const string StrStopReadOptoDataCmd = "StopReadOptoData";
private const string StrResetNfcHeadCmd = "ResetNfcHead";
private const string StrSetNfcHeadCmd = "SetNfcHead";
private const string StrSetRfidHeadCmd = "SetRfidHead";
private const string StrEmptyCmd = "";
public enum Operations
{
[Description(StrReadPcbCmd)] ReadPcbCmd,
[Description(StrSetTestModeCmd)] SetTestModeCmd,
[Description(StrSetActiveModeCmd)] SetActiveModeCmd,
[Description(StrReadOptoDataCmd)] ReadOptoDataCmd,
[Description(StrStopReadOptoDataCmd)] StopReadOptoDataCmd,
[Description(StrResetNfcHeadCmd)] ResetNfcHeadCmd,
[Description(StrSetNfcHeadCmd)] SetNfcHeadCmd,
[Description(StrSetRfidHeadCmd)] SetRfidHeadCmd,
[Description(StrEmptyCmd)] EmptyCmd
}
private static readonly Dictionary<string, Operations> ItemsForIperlOperations =
new Dictionary<string, Operations>
{
{ "Read PCB", Operations.ReadPcbCmd },
{ "Set Test Mode", Operations.SetTestModeCmd },
{ "Set Active Mode", Operations.SetActiveModeCmd },
#if TRUE
{ "Start Read Opto Data", Operations.ReadOptoDataCmd },
{ "Stop Read Opto Data", Operations.StopReadOptoDataCmd },
#endif
{ " ", Operations.EmptyCmd },
{ "Reset NFC Head", Operations.ResetNfcHeadCmd },
{ "Set NFC Head Interface", Operations.SetNfcHeadCmd },
{ "Set RFID Head interface", Operations.SetRfidHeadCmd }
};
public (string Name, string Value)[] GetComboOperationsPairs()
{
//return ItemsForIperlOperations.Select(kvp => (kvp.Key, kvp.Value)).ToArray();
return ItemsForIperlOperations.Select(kvp => (kvp.Key, kvp.Value.ToDescription())).ToArray();
}
public void CommandTestButtonClick(object sender, MouseEventArgs e, Arguments a)
{
a.RfidOutputListBox.Items.Clear();
log.Debug($"CommandTestButtonClick called: {a?.RfidCommandComboBox?.SelectedValue}");
using (Tools.LogChecker logChecker = new Tools.LogChecker("RfidData", log4net.Core.Level.Debug))
{
ListItem rfidListItem = new ListItem();
rfidListItem.Attributes.Add("style", "font-weight:bold");
Operations selectedOperation;
if (!ItemsForIperlOperations.TryGetValue((string)a.RfidCommandComboBox.SelectedValue,
out selectedOperation))
selectedOperation = Operations.EmptyCmd;
bool isTestModeSuccessful = false;
switch (selectedOperation)
{
case Operations.ReadPcbCmd:
rfidListItem.Text = $"PCB: {Head.OptoHeadTest.ReadRequest_PCB()}";
break;
case Operations.SetTestModeCmd:
rfidListItem.Text = Head.OptoHeadTest.SetTestMode(ref isTestModeSuccessful);
a.OptoListBox.Items.Clear();
stopWorkerThread = false;
optoThread = new Thread(OptoWorker);
if (!optoThread.IsAlive)
{
a.ISmartReader.StartDataStreamProcessing(); // open opto port
optoThread.Start();
}
break;
case Operations.SetActiveModeCmd:
rfidListItem.Text = Head.OptoHeadTest.SetActiveMode(ref isTestModeSuccessful);
stopWorkerThread = true;
a.ISmartReader.StopDataStreamProcessing(); // close opto port
break;
case Operations.ResetNfcHeadCmd:
a.ISmartReader.ResetNfcInterface();
break;
case Operations.SetNfcHeadCmd:
a.ISmartReader.SetNfcInterface();
break;
case Operations.SetRfidHeadCmd:
a.ISmartReader.SetRfidInterface();
break;
case Operations.ReadOptoDataCmd:
a.OptoListBox.Items.Clear();
stopWorkerThread = false;
optoThread = new Thread(OptoWorker);
if (optoThread.IsAlive)
{
stopWorkerThread = true;
a.ISmartReader.StopDataStreamProcessing(); // close opto port
//Head.OptoHeadTest.StopDataStreamProcessing(); // close opto port
}
if (!optoThread.IsAlive)
{
a.ISmartReader.StartDataStreamProcessing(); // open opto port
//Head.OptoHeadTest.StartDataStreamProcessing(); // open opto port
optoThread.Start();
}
break;
case Operations.StopReadOptoDataCmd:
stopWorkerThread = true;
a.ISmartReader.StopDataStreamProcessing(); // close opto port
break;
}
a.RfidOutputListBox.Items.Add(rfidListItem);
a.RfidOutputListBox.Items.AddRange(logChecker.Messages.ToArray());
}
}
private void OptoWorker()
{
while (!this.stopWorkerThread)
{
Thread.Sleep(250);
if (this.stopWorkerThread)
break;
try
{
string buffer = ISmartReader.ReadOptoData();
if (string.IsNullOrEmpty(buffer))
{
this.OnOptoReceived((object)this, new OptoReceivedEventArgs("."));
}
else
OnOptoReceived((object)this, new OptoReceivedEventArgs(buffer));
}
catch (Exception ex)
{
this.OnOptoReceived((object)this, new OptoReceivedEventArgs(ex.Message));
}
}
}
public void OnOptoReceived(object sender, OptoReceivedEventArgs args)
{
if (this.OptoReceivedHandler == null)
return;
try
{
this.OptoReceivedHandler(sender, args);
}
catch (Exception ex)
{
}
}
}
}