/// /// 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.Combined { public class EntryForm : ComponentBase, IOperation, IDataEntry, IHasCycleBeginForm, IHasCycleEndForm, IHasWMStatesForm { private static readonly ILog log = LogManager.GetLogger(typeof(EntryForm)); public override string ToString() { return string.Format("DataEntry.Combined({0})", Cfg.ToString(1)); } readonly EntryFormCfg entryFormCfg; /// /// Properties set by the Begin and the End form /// string purchaseOrder; public string PurchaseOrder { get { return purchaseOrder; } } bool[] disabled; /// This array is shared between forms string[] wmCycleEndState; public string WMCycleEndState(int wmNr) { return (wmNr >= 0 && wmNr < wmCycleEndState.Length) ? wmCycleEndState[wmNr] : string.Empty; } double[] wmStartState; public double WMStartState(int wmNr) { return (wmNr >= 0 && wmNr < wmStartState.Length) ? wmStartState[wmNr] : 0; } double[] wmEndState; public double WMEndState(int wmNr) { return (wmNr >= 0 && wmNr < wmEndState.Length) ? wmEndState[wmNr] : 0; } string[] wmStartStateStr; System.Windows.Forms.Form modelessDlg; double refVolume; double errLimLo; double errLimHi; bool resultSaved; /// Set in Run() when results are saved Results.Entities.WaterMeter[] waterMeters; /// Reference to ...ProcessData.BatchRslts.WaterMeters[] public enum CurrentOp { None, ShowFormAtCycleBeginning, ShowFormAtCycleEnd, EnterTestStartStates, EnterTestEndStates, } CurrentOp currentOp; public EntryForm() { } public EntryForm(Generic.IComponentCfg cfg) : base(cfg) { entryFormCfg = cfg as EntryFormCfg; disabled = new bool[Config.Data.CompoundWMsCount]; wmStartState = new double[Config.Data.CompoundWMsCount * 2]; wmStartStateStr = new string[Config.Data.CompoundWMsCount * 2]; wmEndState = new double[Config.Data.CompoundWMsCount * 2]; wmCycleEndState = new string[Config.Data.CompoundWMsCount * 2]; currentOp = CurrentOp.None; log.Debug(this.ToString()); } /// Reference to the operation 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; } /// Reference to the operation 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; } /// Reference to the operation public IOperation ShowTestStartFormOp() { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.EnterTestStartStates; return this; } /// Reference to the operation public IOperation ShowTestEndFormOp(double refVolume, double errLimLo, double errLimHi) { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.EnterTestEndStates; this.refVolume = refVolume; this.errLimLo = errLimLo; this.errLimHi = errLimHi; return this; } delegate void EntryFormDlgt(EntryForm myRef); /// void OpenBeginningDlg(EntryForm myRef) { myRef.modelessDlg = new CycleBeginningForm(Config.Data.CompoundWMsCount); modelessDlg.Show(); } /// void OpenEndDlg(EntryForm myRef) { CycleEndForm endForm = new CycleEndForm(); myRef.modelessDlg = endForm; modelessDlg.Show(); } /// void OpenTestStartStatesDlg(EntryForm myRef) { myRef.modelessDlg = new TestStartEndForm(Config.Data.CompoundWMsCount, disabled); modelessDlg.Show(); } /// void OpenTestEndStatesDlg(EntryForm myRef) { string[] mainStartStateStr = new string[Config.Data.CompoundWMsCount]; string[] auxStartStateStr = new string[Config.Data.CompoundWMsCount]; for (int i = 0; i < Config.Data.CompoundWMsCount; i++) { mainStartStateStr[i] = wmStartStateStr[2 * i]; auxStartStateStr[i] = wmStartStateStr[2 * i + 1]; } myRef.modelessDlg = new TestStartEndForm(Config.Data.CompoundWMsCount, mainStartStateStr, auxStartStateStr, disabled, refVolume, errLimLo, errLimHi); 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: if (entryFormCfg.ShowCycleEndForm) { Program.MainWnd.Invoke(new EntryFormDlgt(OpenEndDlg), this); } break; case CurrentOp.EnterTestStartStates: Program.MainWnd.Invoke(new EntryFormDlgt(OpenTestStartStatesDlg), this); break; case CurrentOp.EnterTestEndStates: Program.MainWnd.Invoke(new EntryFormDlgt(OpenTestEndStatesDlg), 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 (currentOp == CurrentOp.ShowFormAtCycleBeginning) { CycleBeginningForm dlg = (modelessDlg as CycleBeginningForm); if (dlg != null) { purchaseOrder = dlg.PurchaseOrder; for (int i = 0; i < Math.Min(dlg.WaterMetersCount, waterMeters.Length); i++) { if (waterMeters[i] != null) { waterMeters[i].SerialNr = dlg.MainSNText[i]; waterMeters[i].SerialNrAux = dlg.AuxSNText[i]; disabled[i] = waterMeters[i].Disabled = dlg.Disabled[i]; } } } } else if (currentOp == CurrentOp.ShowFormAtCycleEnd) { //CycleEndForm dlg = (modelessDlg as CycleEndForm); //int count = waterMeters.Count; //if ((dlg != null) && (count > dlg.WaterMetersCount)) count = dlg.WaterMetersCount; //for (int i = 0; i < count; i++) //{ // if (waterMeters[i] != null) // { // /// In case entryFormCfg.ShowCycleEndForm == false dlg would be null, values would be string.Empty // wmCycleEndState[i] = waterMeters[i].EndState = (dlg != null) ? dlg.EndStateText[i] : string.Empty; // } //} } else if (modelessDlg is TestStartEndForm && currentOp == CurrentOp.EnterTestStartStates) { /// Fixed start test - start TestStartEndForm dlg = (modelessDlg as TestStartEndForm); for (int i = 0; i < dlg.WaterMetersCount; i++) { wmStartState[2 * i] = dlg.MainStartState[i]; wmStartStateStr[2 * i] = dlg.MainStartStateStr[i]; wmStartState[2 * i + 1] = dlg.AuxStartState[i]; wmStartStateStr[2 * i + 1] = dlg.AuxStartStateStr[i]; } } else if (modelessDlg is TestStartEndForm && currentOp == CurrentOp.EnterTestEndStates) { /// Fixed start test - end TestStartEndForm dlg = (modelessDlg as TestStartEndForm); for (int i = 0; i < dlg.WaterMetersCount; i++) { wmEndState[2 * i] = dlg.MainEndState[i]; wmEndState[2 * i + 1] = dlg.AuxEndState[i]; } } 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. /// /// Test results to be updated with the information public void UpdateTestResults(Results.BatchResults batchResults) { foreach (var wm in batchResults.WaterMeters) { if (wm != null && !wm.Disabled) wm.PurchaseOrder = PurchaseOrder; } } } }