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

215 lines
8.4 KiB
C#
Raw Normal View History

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()
{
Restore Genesis communication and Q3 calibration from special branches A – Registration and execution - Register GenesisCommunication Factory in the component list. - Separate the Genesis form and sequence from iPerl communication. B – Communication activities - Restore initialization, connection, PCB reading, password and login. - Include grouped login, mode switching and disconnection. - Support processing up to 10 slots. C1 – Input calibration factors - Read three factors from Water meters / Text1–Text3. - Validate integer values in the range 1–65535. - Preserve the default of 15625 when all three fields are empty. D – Q3 calibration - Connect the Prepare Q3 → measurement → Write Q3 workflow. - Add channel processing to FlyingStart and FlyingStartMassCollection. - Reset previous measurement data and validate calculated factors. - Mark factors as stored only after StoreCalibration succeeds. E – Results and database - Store calibration factors separately for each meter and channel. - Add result entities, mappings and Q3 data. - Extend DB.cs / EnsureSchema to create and update the schema. - Preserve compatibility with the existing binary format. Validation: - Debug build and 18 tests passed. - Simulated communication runs follow the same activity sequence. - The complete Q3 workflow has not yet been verified on hardware. Known limitation: - An inherited mismatch in simulated responses and error propagation can produce an incorrect OK result; this change does not fix it.
2026-09-09 13:04:49 +00:00
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)
{
}
}
}
}