/// /// 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.Standard { 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.Standard({0})", Cfg.ToString(1)); } readonly EntryFormCfg entryFormCfg; /// /// Properties set by the Begin, End and WMStates form /// string purchaseOrder; public string PurchaseOrder { get { return purchaseOrder; } } string[] serialNr; public string SerialNr(int wmNr) { return (wmNr >= 0 && wmNr < serialNr.Length) ? serialNr[wmNr] : string.Empty; } bool[] disabled; public bool Disabled(int wmNr) { return (wmNr >= 0 && wmNr < disabled.Length) ? disabled[wmNr] : false; } string[] wmEndState; public string WMEndState(int wmNr) { return (wmNr >= 0 && wmNr < wmEndState.Length) ? wmEndState[wmNr] : string.Empty; } float[] wmState; public float WMState(int wmNr) { return (wmNr >= 0 && wmNr < wmState.Length) ? wmState[wmNr] : 0; } System.Windows.Forms.Form modelessDlg; bool resultSaved; /// Set in Run() when results are saved IList waterMeters; /// Passesd as an argument of ShowCycleBeginFormOp/ShowCycleEndFormOp constructor public enum CurrentOp { None, ShowFormAtCycleBeginning, ShowFormAtCycleEnd, EnterWMStates, } CurrentOp currentOp; public EntryForm(EntryFormCfg cfg) : base(cfg) { serialNr = new string[Program.WMsCount]; disabled = new bool[Program.WMsCount]; wmEndState = new string[Program.WMsCount]; wmState = new float[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.waterMeters = 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.waterMeters = waterMeters; return this; } /// Reference to the operation public IOperation ShowWMStatesFormOp() { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.EnterWMStates; return this; } delegate void EntryFormDlgt(EntryForm myRef); /// void OpenBeginningDlg(EntryForm myRef) { myRef.modelessDlg = new CycleBeginningForm(Program.WMsCount); modelessDlg.Show(); } /// void OpenEndDlg(EntryForm myRef) { myRef.modelessDlg = new CycleEndForm(Program.WMsCount); modelessDlg.Show(); } /// void OpenWMStatesDlg(EntryForm myRef) { myRef.modelessDlg = new WMStatesForm(Program.WMsCount); 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; case CurrentOp.EnterWMStates: Program.MainWnd.Invoke(new EntryFormDlgt(OpenWMStatesDlg), 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) { CycleBeginningForm dlg = (modelessDlg as CycleBeginningForm); purchaseOrder = dlg.PurchaseOrder; for (int i = 0; i < Math.Min(dlg.WaterMetersCount, waterMeters.Count); i++) { if (waterMeters[i] != null) { serialNr[i] = waterMeters[i].SerialNr = dlg.SNText[i]; disabled[i] = waterMeters[i].Disabled = dlg.Disabled[i]; } } } else if (modelessDlg is CycleEndForm) { CycleEndForm dlg = (modelessDlg as CycleEndForm); for (int i = 0; i < Math.Min(dlg.WaterMetersCount, waterMeters.Count); i++) { if (waterMeters[i] != null) { wmEndState[i] = waterMeters[i].EndState = dlg.EndStateText[i]; } } } else if (modelessDlg is WMStatesForm) { WMStatesForm dlg = (modelessDlg as WMStatesForm); for (int i = 0; i < dlg.WaterMetersCount; i++) { wmState[i] = dlg.WMState[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. /// /// Watermetes with information entered in the forms /// Test results to be updated with the information public void UpdateTestResults(IList waterMeters, IList testResults) { foreach (var testRslt in testResults) { if (testRslt.MetersKind != Entities.MetersKind.Single) continue; testRslt.PurchaseOrder = PurchaseOrder; for (int i = 0; i < testRslt.Meters.Count; i++) { testRslt.Meters[i].SerialNr = SerialNr(i); testRslt.Meters[i].Disabled = Disabled(i); testRslt.Meters[i].EndState = WMEndState(i); } } } } }