///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.BenchControl;
using TBF.BenchControl.GenericDevices;
namespace TBF.BenchControl.DataEntry.Munich
{
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.Munich({0})", Cfg.ToString(1)); }
readonly EntryFormCfg entryFormCfg;
///
/// Properties set by the Begin and the End form
///
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 WMEndState(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;
IList wMeters;
public enum CurrentOp
{
None,
ShowFormAtCycleBeginning,
ShowFormAtCycleEnd,
}
CurrentOp currentOp;
public EntryForm(EntryFormCfg cfg)
: base(cfg)
{
serialNr = new string[Program.WMsCount];
wmEndState = new string[Program.WMsCount];
entryFormCfg = cfg;
currentOp = CurrentOp.None;
log.Debug(this.ToString());
}
/// Reference to the operation
public IOperation ShowCycleBeginFormOp(IList waterMeters)
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.ShowFormAtCycleBeginning;
this.wMeters = waterMeters;
return this;
}
/// Reference to the operation
public IOperation ShowCycleEndFormOp(IList waterMeters)
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.ShowFormAtCycleEnd;
this.wMeters = waterMeters;
return this;
}
/// Reference to the operation
public IOperation ShowWMSNsAndStatesFormOp()
{
return null;
}
/// Reference to the operation
public IOperation ShowWMStatesFormOp()
{
return null;
}
delegate void EntryFormDlgt(EntryForm myRef);
///
void OpenBeginningDlg(EntryForm myRef)
{
myRef.modelessDlg = new CycleBeginningForm();
modelessDlg.Show();
}
///
void OpenEndDlg(EntryForm myRef)
{
CycleEndForm endForm = new CycleEndForm();
endForm.ProtocolName1 = entryFormCfg.ProtocolTitle1;
endForm.ProtocolName2 = entryFormCfg.ProtocolTitle2;
endForm.ProtocolName3 = entryFormCfg.ProtocolTitle3;
myRef.modelessDlg = endForm;
modelessDlg.Show();
}
/// Start this operation
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;
}
}
/// Run this operation
/// Event.ResultsPrinted
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)
{
if (wMeters.Count >= 2 && wMeters[0] != null) wMeters[0].SerialNr = (modelessDlg as CycleBeginningForm).MainSNText[0];
if (wMeters.Count >= 2 && wMeters[1] != null) wMeters[1].SerialNr = (modelessDlg as CycleBeginningForm).AuxSNText[0];
if (wMeters.Count >= 4 && wMeters[2] != null) wMeters[2].SerialNr = (modelessDlg as CycleBeginningForm).MainSNText[1];
if (wMeters.Count >= 4 && wMeters[3] != null) wMeters[3].SerialNr = (modelessDlg as CycleBeginningForm).AuxSNText[1];
if (wMeters.Count >= 6 && wMeters[4] != null) wMeters[4].SerialNr = (modelessDlg as CycleBeginningForm).MainSNText[2];
if (wMeters.Count >= 6 && wMeters[5] != null) wMeters[5].SerialNr = (modelessDlg as CycleBeginningForm).AuxSNText[2];
}
else if (modelessDlg is CycleEndForm)
{
if (wMeters.Count >= 2 && wMeters[0] != null) wMeters[0].EndState = (modelessDlg as CycleEndForm).MainStateText[0];
if (wMeters.Count >= 2 && wMeters[1] != null) wMeters[1].EndState = (modelessDlg as CycleEndForm).AuxStateText[0];
if (wMeters.Count >= 4 && wMeters[2] != null) wMeters[2].EndState = (modelessDlg as CycleEndForm).MainStateText[1];
if (wMeters.Count >= 4 && wMeters[3] != null) wMeters[3].EndState = (modelessDlg as CycleEndForm).AuxStateText[1];
if (wMeters.Count >= 6 && wMeters[4] != null) wMeters[4].EndState = (modelessDlg as CycleEndForm).MainStateText[2];
if (wMeters.Count >= 6 && wMeters[5] != null) wMeters[5].EndState = (modelessDlg as CycleEndForm).AuxStateText[2];
protocolTitle = (modelessDlg as CycleEndForm).ProtocolTitle;
}
resultSaved = true;
modelessDlg = null;
}
return Event.ModelessFormClosed; /// Form closed
}
/// Stop this operation
public void Stop()
{
if (modelessDlg is IHasCompleted)
{
UiBridge.Bridge.OnCloseModelessForm(this, null);
modelessDlg = null;
}
currentOp = CurrentOp.None;
}
///
/// Updates test results with the information collected in watermeters.
///
/// Watermetes with information entered in the forms
/// Test results to be updated with the information
public void UpdateTestResults(IList waterMeters, IList results)
{
foreach (var test in results)
{
if (test.MetersKind != Entities.MetersKind.Combined) continue;
/// Todo: Generalize !!! Now implemented for Munich = one combined meter.
test.Meters[0].SerialNr = waterMeters[0].SerialNr;
test.Meters[0].EndState = waterMeters[0].EndState;
test.Meters[1].SerialNr = waterMeters[1].SerialNr;
test.Meters[1].EndState = waterMeters[1].EndState;
}
}
}
}