///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System;
using log4net;
using TBF.BenchControl.GenericDevices;
using TBF.BenchControl.Configs.NameOnly;
using TBF.Boxes;
namespace TBF.BenchControl.Various.ManualLevelMsrmnt
{
public class ManualLevelMsrmnt : ComponentBase, ILevelMeter, IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(ManualLevelMsrmnt));
public override string ToString() { return string.Format("ManualLevelMsrmnt({0})", Cfg.ToString(1)); }
readonly TestMethodCfg myCfg;
DoubleBox levelBox;
public double WaterLevelMm;
System.Windows.Forms.Form modelessDlg;
bool resultSaved; /// Set in Run() when results are saved
public enum CurrentOp
{
None,
EnterLevel,
}
CurrentOp currentOp;
public ManualLevelMsrmnt()
{
currentOp = CurrentOp.None;
}
public ManualLevelMsrmnt(Generic.IComponentCfg cfg)
: base(cfg)
{
myCfg = cfg as TestMethodCfg;
currentOp = CurrentOp.None;
log.Debug(this.ToString());
}
/// Reference to the operation
public IOperation ReadLevelOp(ref DoubleBox levelBox)
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
this.levelBox = levelBox;
currentOp = CurrentOp.EnterLevel;
return this;
}
delegate void EntryFormDlgt(ManualLevelMsrmnt myRef);
///
void OpenWaterLevelForm(ManualLevelMsrmnt myRef)
{
myRef.modelessDlg = new WaterLevelForm();
modelessDlg.Show();
}
/// Start this operation
public void Start()
{
resultSaved = false;
if (currentOp == CurrentOp.EnterLevel)
{
Program.MainWnd.Invoke(new EntryFormDlgt(OpenWaterLevelForm), this);
}
}
/// Run this operation
/// Event.ResultsPrinted
public Event Run()
{
if ((modelessDlg is IHasCompleted) && !(modelessDlg as IHasCompleted).Completed)
{
return Event.Busy; /// = Form open
}
if (!resultSaved) /// This is to save the result only once
{
if ((modelessDlg is WaterLevelForm) && (currentOp == CurrentOp.EnterLevel))
{
WaterLevelMm = (modelessDlg as WaterLevelForm).WaterLevelMm;
if (levelBox != null) levelBox.Val = WaterLevelMm;
resultSaved = true;
modelessDlg = null;
}
}
return Event.LevelDone; /// = Form closed
}
/// Stop this operation
public void Stop()
{
if (modelessDlg is IHasCompleted)
{
UiBridge.Bridge.OnCloseModelessForm(this, null);
modelessDlg = null;
}
currentOp = CurrentOp.None;
}
}
}