/// /// Copyright (c) 2023 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using log4net; using Config.Entities; using TBF.Rig; using TBF.Rig.GenericDevices; namespace TBF.Rig.DataEntry.Uni { public class EntryFormNoStartEnd : ComponentBase, IOperation, IDataEntry, IHasCycleBeginForm, IHasCycleEndForm { private static readonly ILog log = LogManager.GetLogger(typeof(EntryFormNoStartEnd)); public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); } public bool UsesCameras() { return false; } readonly EntryFormCfg myCfg; IRegReader[] regReaders; /// /// Properties set by the Begin, End and WMStates form /// 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; public bool Completed { get { return (modelessDlg is IHasCompleted) ? (modelessDlg as IHasCompleted).Completed : true; } } Common.Unit volumeUnit; double refVolume; double errLimLo; double errLimHi; bool resultSaved; /// Set in Run() when results are saved IList waterMeters; /// Reference to ...ProcessData.BatchRslts.WaterMeters[] public enum CurrentOp { None, ShowFormAtCycleBeginning, ShowFormAtCycleEnd, EnterTestStartStates, EnterTestEndStates, } CurrentOp currentOp; public EntryFormNoStartEnd() { } public EntryFormNoStartEnd(Generic.IComponentCfg cfg) : base(cfg) { myCfg = cfg as EntryFormCfg; } public override void Initialize() { disabled = new bool[TBF.Data.WMsCount]; wmStartState = new double[TBF.Data.WMsCount]; wmStartStateStr = new string[TBF.Data.WMsCount]; wmEndState = new double[TBF.Data.WMsCount]; wmCycleEndState = new string[TBF.Data.WMsCount]; volumeUnit = (TBF.Rig.Sequences.ProcessData.BenchInfo != null) ? TBF.Rig.Sequences.ProcessData.BenchInfo.VolumeUnit : Common.Unit.l; currentOp = CurrentOp.None; log.FatalFormat("{0} initialized: {1}", Name, this); } /// Reference to the operation public IOperation ShowCycleBeginFormOp() { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.ShowFormAtCycleBeginning; this.waterMeters = TBF.Rig.Sequences.ProcessData.BatchRslts.Batch.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 ShowTestStartFormOp(IRegReader[] regReaders) { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.EnterTestStartStates; this.waterMeters = TBF.Rig.Sequences.ProcessData.BatchRslts.Batch.WaterMeters; this.regReaders = regReaders; return this; } /// Reference to the operation public IOperation ShowTestEndFormOp(IRegReader[] regReaders, double refVolume, double errLimLo, double errLimHi) { if (currentOp != CurrentOp.None) throw new Exception("Sequence error"); currentOp = CurrentOp.EnterTestEndStates; this.waterMeters = TBF.Rig.Sequences.ProcessData.BatchRslts.Batch.WaterMeters; this.regReaders = regReaders; this.refVolume = refVolume; this.errLimLo = errLimLo; this.errLimHi = errLimHi; return this; } public IOperation ShowAdvancedTestStartFormOp(IRegReader[] regReaders, IList waterMeters, bool isCondOp = false) { return null; } delegate void EntryFormDlgt(EntryFormNoStartEnd myRef); /// void OpenBeginningDlg(EntryFormNoStartEnd myRef) { var item1 = new DEItem(myCfg.BgItem1Content, myCfg.BgItem1Caption, myCfg.BgItem1Action, myCfg.BgItem1Width); var item2 = new DEItem(myCfg.BgItem2Content, myCfg.BgItem2Caption, myCfg.BgItem2Action, myCfg.BgItem2Width); var item3 = new DEItem(myCfg.BgItem3Content, myCfg.BgItem3Caption, myCfg.BgItem3Action, myCfg.BgItem3Width); DEItem.ClearColumns(); if (myCfg.BgColAContent != Content.None) DEItem.AddColumn(myCfg.BgColAContent, myCfg.BgColACaption, myCfg.BgColAAction, myCfg.BgColAWidth); if (myCfg.BgColBContent != Content.None) DEItem.AddColumn(myCfg.BgColBContent, myCfg.BgColBCaption, myCfg.BgColBAction, myCfg.BgColBWidth); if (myCfg.BgColCContent != Content.None) DEItem.AddColumn(myCfg.BgColCContent, myCfg.BgColCCaption, myCfg.BgColCAction, myCfg.BgColCWidth); if (myCfg.BgColDContent != Content.None) DEItem.AddColumn(myCfg.BgColDContent, myCfg.BgColDCaption, myCfg.BgColDAction, myCfg.BgColDWidth); if (myCfg.BgColEContent != Content.None) DEItem.AddColumn(myCfg.BgColEContent, myCfg.BgColECaption, myCfg.BgColEAction, myCfg.BgColEWidth); var columns = DEItem.GetColumns(); modelessDlg = new CycleBgEnForm(waterMeters, TBF.Data.LineSize, myCfg.BgTitle, myCfg.BgSize, myCfg.BgIsLrOrder, myCfg.BgFormCloseKeys, item1, item2, item3, columns, false); modelessDlg.Show(); } /// void OpenEndDlg(EntryFormNoStartEnd myRef) { var item1 = new DEItem(myCfg.EnItem1Content, myCfg.EnItem1Caption, myCfg.EnItem1Action, myCfg.EnItem1Width); var item2 = new DEItem(myCfg.EnItem2Content, myCfg.EnItem2Caption, myCfg.EnItem2Action, myCfg.EnItem2Width); var item3 = new DEItem(myCfg.EnItem3Content, myCfg.EnItem3Caption, myCfg.EnItem3Action, myCfg.EnItem3Width); DEItem.ClearColumns(); if (myCfg.EnColAContent != Content.None) DEItem.AddColumn(myCfg.EnColAContent, myCfg.EnColACaption, myCfg.EnColAAction, myCfg.EnColAWidth); if (myCfg.EnColBContent != Content.None) DEItem.AddColumn(myCfg.EnColBContent, myCfg.EnColBCaption, myCfg.EnColBAction, myCfg.EnColBWidth); if (myCfg.EnColCContent != Content.None) DEItem.AddColumn(myCfg.EnColCContent, myCfg.EnColCCaption, myCfg.EnColCAction, myCfg.EnColCWidth); if (myCfg.EnColDContent != Content.None) DEItem.AddColumn(myCfg.EnColDContent, myCfg.EnColDCaption, myCfg.EnColDAction, myCfg.EnColDWidth); if (myCfg.EnColEContent != Content.None) DEItem.AddColumn(myCfg.EnColEContent, myCfg.EnColECaption, myCfg.EnColEAction, myCfg.EnColEWidth); var columns = DEItem.GetColumns(); modelessDlg = new CycleBgEnForm(waterMeters, TBF.Data.LineSize, myCfg.EnTitle, myCfg.EnSize, myCfg.EnIsLrOrder, myCfg.EnFormCloseKeys, item1, item2, item3, columns, true); modelessDlg.Show(); } /// void OpenTestStartStatesDlg(EntryFormNoStartEnd myRef) { myRef.modelessDlg = new TestStartEndForm(waterMeters, myRef.regReaders, myRef.myCfg.TestFormCloseKeys, volumeUnit); modelessDlg.Show(); } /// void OpenTestEndStatesDlg(EntryFormNoStartEnd myRef) { modelessDlg = new TestStartEndForm(waterMeters, myRef.regReaders, wmStartStateStr, refVolume, errLimLo, errLimHi, volumeUnit); modelessDlg.Show(); } /// Start this operation public void Start() { resultSaved = false; switch (currentOp) { case CurrentOp.ShowFormAtCycleBeginning: if (myCfg.ShowCycleBeginningForm) { Program.MainWnd.Invoke(new EntryFormDlgt(OpenBeginningDlg), this); } break; case CurrentOp.ShowFormAtCycleEnd: if (myCfg.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 (modelessDlg is TestStartEndForm && currentOp == CurrentOp.EnterTestStartStates) { /// Fixed start test - start TestStartEndForm dlg = (modelessDlg as TestStartEndForm); if (dlg != null) { volumeUnit = dlg.VolumeUnit; for (int i = 0; i < dlg.WMsCount; i++) { if (!((i < waterMeters.Count && waterMeters[i].Disabled) || (i < regReaders.Length && regReaders[i] == null))) { wmStartState[i] = dlg.WMStartState[i]; wmStartStateStr[i] = dlg.WMStartStateStr[i]; } } } } else if (modelessDlg is TestStartEndForm && currentOp == CurrentOp.EnterTestEndStates) { /// Fixed start test - end TestStartEndForm dlg = (modelessDlg as TestStartEndForm); if (dlg != null) { volumeUnit = dlg.VolumeUnit; for (int i = 0; i < dlg.WMsCount; i++) { if (!((i < waterMeters.Count && waterMeters[i].Disabled) || (i < regReaders.Length && regReaders[i] == null))) { wmEndState[i] = dlg.WMEndState[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; } } }