tbf/TBF/Rig/TestMethods/iPerlCommunication/TestMethod.cs

160 lines
5.4 KiB
C#

///
/// Copyright (c) 2015-2022 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using Common;
using Config.Entities;
using TBF.Rig.GenericDevices;
using TBF.Rig.Sequences;
using TBF.UiBridge;
namespace TBF.Rig.TestMethods.iPerlCommunication
{
public class TestMethod : ComponentBase, ISimultTestMethod, ISequenceCondition, ISessionDataMngmnt
{
private static readonly ILog log = LogManager.GetLogger(typeof(TestMethod));
protected static readonly ILog rfidDataLogger = LogManager.GetLogger("RfidData");
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
public bool CanTest(MetersKind meters) { return meters == MetersKind.Single; }
public bool DoTransitions() { return false; }
public bool SimultWithPrevious { get { return testMethodCfg.TestParams.SimultWithPrevious; } }
public bool SimultWithNext { get { return testMethodCfg.TestParams.SimultWithNext; } }
#region Configuration Change Handling
public static void OnCfgChange(object sender, CfgChangeArgs args)
{
if (CfgChangeHandler == null) return;
try { CfgChangeHandler(sender, args); }
catch (Exception e) { log.Error("CfgChangeHandler(...) failed", e); }
}
public static event EventHandler<CfgChangeArgs> CfgChangeHandler;
public override void StartChangeHandler()
{
CfgChangeHandler += delegate(object sender, CfgChangeArgs args)
{
TestMethodCfg tmpCfg = args.Cfg as TestMethodCfg;
if (tmpCfg != null && tmpCfg.Name.Equals(Name))
{
if (args.Command == CfgChangeCmd.CfgChange)
{
testMethodCfg.CommTimeout = tmpCfg.CommTimeout;
testMethodCfg.DelayBetweenRetries = tmpCfg.DelayBetweenRetries;
testMethodCfg.MaxCommRetries = tmpCfg.MaxCommRetries;
testMethodCfg.IperlCheckErrorsToStop = tmpCfg.IperlCheckErrorsToStop;
testMethodCfg.UseWebService = tmpCfg.UseWebService;
testMethodCfg.BaseUrl = tmpCfg.BaseUrl;
testMethodCfg.RelativeUrl = tmpCfg.RelativeUrl;
}
}
};
}
#endregion Configuration Change Handling
readonly TestMethodCfg testMethodCfg;
public bool[] IperlCommMilestone;
IList<IOperation> sequenceConditionOps;
public TestMethod()
{
CreateMilestonesAndConditions();
}
public TestMethod(Generic.IComponentCfg cfg)
: base(cfg)
{
testMethodCfg = cfg as TestMethodCfg;
CreateMilestonesAndConditions();
}
void CreateMilestonesAndConditions()
{
IperlCommMilestone = new bool[(int)ConditionID.Count];
sequenceConditionOps = new List<IOperation>();
for (ConditionID id = ConditionID.A; id < ConditionID.Count; id++)
{
sequenceConditionOps.Add(new SequenceConditionOp(this, id));
}
}
/// IDevice interface - only Initialize() is used
public override void Initialize()
{
if (DebugLevel == DebugMode.Normal)
{
rfidDataLogger.Fatal("------------------------------------------------------------------------");
rfidDataLogger.FatalFormat("Test Bench Framework ver. {0}", Program.Version);
log.FatalFormat("{0} initialized: {1}", Name, this);
}
else
{
log.FatalFormat("{0} simulated: {1}", Name, this);
}
}
public IList<Event> Execute(Test test, int repetNr, bool isLastRepetition)
{
if (DebugLevel == DebugMode.Normal)
{
return (new iPerlCommunicationSeq()).Execute(test, repetNr, this, testMethodCfg.TestParams);
}
else
{
/// DebugLevel == DebugMode.Simulate
(new iPerlCommunicationSeq()).MakeSimulatedTrivial(test, repetNr, test.Part);
Bridge.OnTestProgress(this, new TestProgressEventArgs(test, 1, Progress.Completed));
Bridge.OnTestCompleted(this, new TestCompletedEventArgs(test.Name, ProcessData.BatchRslts.GetTestRslt(Common.Utils.GetTestName(test.Name, 1, 1), 0)));
return new List<Event> { Event.Done };
}
}
public int ConditionsCount { get { return (int)ConditionID.Count; } }
public string ConditionName(int i)
{
return (i >= 0 && i < (int)ConditionID.Count) ? ConditionOp(i).ToString() : string.Empty;
}
public IOperation ConditionOp(int i)
{
return (i >= 0 && i < (int)ConditionID.Count) ? sequenceConditionOps[i] : null;
}
public void StartSession()
{
/// Clear milestones
if (IperlCommMilestone != null)
{
for (int i = 0; i < IperlCommMilestone.Length; i++)
{
IperlCommMilestone[i] = false;
}
}
}
public void SaveMark(object o)
{
/// No marks
}
public void EndSession()
{
/// Nothing at the end of session
}
}
}