tbf/TestBenchFramework/BenchControl/DataEntry/Munich3/EntryForm.cs

199 lines
5.7 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
using Config.Entities;
using TBF.BenchControl;
using TBF.BenchControl.GenericDevices;
namespace TBF.BenchControl.DataEntry.Munich3
{
public class EntryForm : ComponentBase, IOperation, IDataEntry, IHasCycleBeginForm, IHasCycleEndForm, IHasProtocolTitle
{
private static readonly ILog log = LogManager.GetLogger(typeof(EntryForm));
public override string ToString() { return string.Format("DataEntry.Munich3({0})", Cfg.ToString(1)); }
readonly EntryFormCfg entryFormCfg;
/// <summary>
/// Properties set by the Begin and the End form
/// </summary>
public string ProtocolTitle { get { return protocolTitle; } } /// Only supported in Munich
string protocolTitle;
public string PurchaseOrder { get { return string.Empty; } } /// Not supported in Munich
string[] serialNr;
public string SerialNr(int wmNr) { return (wmNr >= 0 && wmNr < serialNr.Length) ? serialNr[wmNr] : string.Empty; }
public bool Disabled(int wmNr) { return false; } /// Not supported in Munich
string[] wmEndState;
public string WMCycleEndState(int wmNr) { return (wmNr >= 0 && wmNr < wmEndState.Length) ? wmEndState[wmNr] : string.Empty; }
public float WMState(int wmNr)
{
float retVal;
if (wmNr >= 0 && wmNr < wmEndState.Length && Utils.TryParseUFloat(wmEndState[wmNr], out retVal)) return retVal;
return 0;
}
System.Windows.Forms.Form modelessDlg;
bool resultSaved;
Results.Entities.WaterMeter[] waterMeters; /// Reference to ...ProcessData.BatchRslts.WaterMeters[]
public enum CurrentOp
{
None,
ShowFormAtCycleBeginning,
ShowFormAtCycleEnd,
}
CurrentOp currentOp;
public EntryForm()
{
}
public EntryForm(EntryFormCfg cfg)
: base(cfg)
{
serialNr = new string[Config.Data.WMsCount];
wmEndState = new string[Config.Data.WMsCount];
entryFormCfg = cfg;
currentOp = CurrentOp.None;
log.Debug(this.ToString());
}
/// <returns>Reference to the operation</returns>
public IOperation ShowCycleBeginFormOp()
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.ShowFormAtCycleBeginning;
this.waterMeters = TBF.BenchControl.Sequences.ProcessData.BatchRslts.WaterMeters;
return this;
}
/// <returns>Reference to the operation</returns>
public IOperation ShowCycleEndFormOp()
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.ShowFormAtCycleEnd;
this.waterMeters = TBF.BenchControl.Sequences.ProcessData.BatchRslts.WaterMeters;
return this;
}
/// <returns>Reference to the operation</returns>
public IOperation ShowWMSNsAndStatesFormOp()
{
return null;
}
delegate void EntryFormDlgt(EntryForm myRef);
///
void OpenBeginningDlg(EntryForm myRef)
{
myRef.modelessDlg = new CycleBeginningForm(Config.Data.WMsCount);
modelessDlg.Show();
}
///
void OpenEndDlg(EntryForm myRef)
{
CycleEndForm endForm = new CycleEndForm(Config.Data.WMsCount);
endForm.ProtocolName1 = entryFormCfg.ProtocolTitle1;
endForm.ProtocolName2 = entryFormCfg.ProtocolTitle2;
endForm.ProtocolName3 = entryFormCfg.ProtocolTitle3;
myRef.modelessDlg = endForm;
modelessDlg.Show();
}
/// <summary>Start this operation</summary>
public void Start()
{
resultSaved = false;
switch (currentOp)
{
case CurrentOp.ShowFormAtCycleBeginning:
Program.MainWnd.Invoke(new EntryFormDlgt(OpenBeginningDlg), this);
break;
case CurrentOp.ShowFormAtCycleEnd:
Program.MainWnd.Invoke(new EntryFormDlgt(OpenEndDlg), this);
break;
}
}
/// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns>
public Event Run()
{
if ((modelessDlg is IHasCompleted) && !(modelessDlg as IHasCompleted).Completed)
{
return Event.ModelessFormIsOpen;
}
if (!resultSaved) /// This is to save the result only once
{
if (modelessDlg is CycleBeginningForm)
{
CycleBeginningForm dlg = (modelessDlg as CycleBeginningForm);
for (int i = 0; i < Math.Min(dlg.WaterMetersCount, waterMeters.Length); i++)
{
if (waterMeters[i] != null)
{
serialNr[i] = waterMeters[i].SerialNr = dlg.SNText[i];
}
}
}
else if (modelessDlg is CycleEndForm)
{
CycleEndForm dlg = (modelessDlg as CycleEndForm);
protocolTitle = dlg.ProtocolTitle;
for (int i = 0; i < Math.Min(dlg.WaterMetersCount, waterMeters.Length); i++)
{
if (waterMeters[i] != null)
{
wmEndState[i] = waterMeters[i].EndState = dlg.EndStateText[i];
waterMeters[i].YearOfProduction = dlg.YearOfProduction[i];
}
}
}
resultSaved = true;
modelessDlg = null;
}
return Event.ModelessFormClosed; /// Form closed
}
/// <summary>Stop this operation</summary>
public void Stop()
{
if (modelessDlg is IHasCompleted)
{
UiBridge.Bridge.OnCloseModelessForm(this, null);
modelessDlg = null;
}
currentOp = CurrentOp.None;
}
/// <summary>
/// Updates test results with the information collected in watermeters.
/// </summary>
/// <param name="waterMeters">Watermetes with information entered in the forms</param>
/// <param name="results">Test results to be updated with the information</param>
public void UpdateTestResults(Results.BatchResults results)
{
/// Nothing to be done
}
}
}