Files
tbf/TBF/Rig/Various/CoverTest/CoverTest.cs
T

147 lines
4.4 KiB
C#

///
/// Copyright (c) 2017-2021 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using Dirichlet.Numerics;
using log4net;
using TBF.Rig.ControlBoard.Legacy;
using TBF.Rig.GenericDevices;
using TBF.Resources;
namespace TBF.Rig.Various.CoverTest
{
public class CoverTest : ComponentBase, IOperation, ISequenceCondition
{
private static readonly ILog log = LogManager.GetLogger(typeof(CoverTest));
private static readonly ILog bypassLog = LogManager.GetLogger("BypassLog");
public override string ToString() { return string.Format("CoverTest({0})", Cfg.ToString(-1)); }
readonly CoverTestCfg coverTestCfg;
readonly CBoard controlBoard;
int bitPosition; /// 0 .. 127
UInt128 mask; /// derived from bitPosition in the constructor
UInt128 target;
string message;
Users.Entities.GID[] bypassLevel;
public CoverTest() { }
public CoverTest(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
coverTestCfg = cfg as CoverTestCfg;
controlBoard = (CBoard)TbfComponents.FindComponent(cfg.ParentName, components);
if (controlBoard == null) throw new Exception("Cannot find " + Name + " parent");
log.Warn(this.ToString());
}
public override void Initialize()
{
bitPosition = coverTestCfg.BitNr;
mask = (((UInt128)1) << coverTestCfg.BitNr);
target = coverTestCfg.Inverted ? 0 : mask;
message = coverTestCfg.Message;
var groups = new List<Users.Entities.GID>();
if (coverTestCfg.BypassGroup1 != Users.Entities.GID.None) groups.Add(coverTestCfg.BypassGroup1);
if (coverTestCfg.BypassGroup2 != Users.Entities.GID.None) groups.Add(coverTestCfg.BypassGroup2);
if (coverTestCfg.BypassGroup3 != Users.Entities.GID.None) groups.Add(coverTestCfg.BypassGroup3);
if (coverTestCfg.BypassGroup4 != Users.Entities.GID.None) groups.Add(coverTestCfg.BypassGroup4);
bypassLevel = groups.ToArray();
}
public int ConditionsCount { get { return 1; } }
/// Strings are added to the combo-box for transition sequence condition selection
public string ConditionName(int i)
{
if (i < 1 && i >= 0)
return Name;
else
return string.Empty;
}
/// Operation to be executed as a part of a transition sequence
public IOperation ConditionOp(int i)
{
if (i < 1 && i >= 0)
return this;
else
return null;
}
CoverTestForm coverTestForm;
bool completed = false;
delegate void CoverTestFormDlgt(CoverTest myRef);
///
void OpenFormDlg(CoverTest myRef)
{
myRef.coverTestForm = new CoverTestForm(message, bypassLevel, bypassLog);
coverTestForm.Show();
}
public IOperation CoverTestOp()
{
return this;
}
/// <summary>Start this operation</summary>
public void Start()
{
log.DebugFormat("Start()");
completed = false;
Program.MainWnd.Invoke(new CoverTestFormDlgt(OpenFormDlg), this);
}
/// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns>
public Event Run()
{
bool conditionMet = ((controlBoard.Route & mask) == target);
if (completed || conditionMet)
{
completed = true;
}
if (completed || coverTestForm.Completed)
{
completed = true;
CloseModelessDlg();
log.DebugFormat("Run() ... conditionMet={0} completed={1} ... Event.ConditionMet", conditionMet, completed);
return Event.ConditionMet;
}
log.DebugFormat("Run() ... conditionMet={0} completed={1} ... Event.ConditionNotMet", conditionMet, completed);
return Event.ConditionNotMet;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
log.DebugFormat("Stop()");
CloseModelessDlg();
}
void CloseModelessDlg()
{
if (coverTestForm != null)
{
coverTestForm.OnCloseThisForm(this, null);
coverTestForm = null;
}
}
}
}