tbf/TBF/Rig/DataEntry/Combined/TestStartEndForm.cs

430 lines
14 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using log4net;
2021-03-05 13:47:21 +00:00
using TBF.Rig.GenericDevices;
using TBF.Resources;
2021-03-05 13:47:21 +00:00
namespace TBF.Rig.DataEntry.Combined
{
public partial class TestStartEndForm : Form, GenericDevices.IHasCompleted
{
private static readonly ILog log = LogManager.GetLogger(typeof(TestStartEndForm));
// Set to 'true' when the form closes
public bool Completed { get { return completed; } }
bool completed;
/// <summary> Number of water meters </summary>
public readonly int WaterMetersCount; /// Main + Aux water meter count as one (combined) water meter
readonly IRegReader[] regReaders;
readonly bool[] disabled;
// To be retrieved after the form closes
public double[] MainStartState;
public double[] AuxStartState;
// To be retrieved after the form closes
public readonly string[] MainStartStateStr;
public readonly string[] AuxStartStateStr;
// To be retrieved after the form closes
public double[] MainEndState;
public double[] AuxEndState;
bool endStates; /// false=start states, true=end states
double refVolume;
double warningLimLo; /// limit to display exclamation mark, typically 2x errLimLo
double warningLimHi; /// limit to display exclamation mark, typically 2x errLimHi
int TextBoxesCount;
///
Label[] mainLabels;
Label[] auxLabels;
TextBox[] mainStartTextBoxes;
TextBox[] auxStartTextBoxes;
TextBox[] mainEndTextBoxes;
TextBox[] auxEndTextBoxes;
IList<TextBox> enabledTextBoxes;
/// <summary>
/// Parameterless constructor initializing common part for start and endstate form
/// </summary>
public TestStartEndForm()
{
InitializeComponent();
2026-03-04 07:37:27 +00:00
this.Icon = Properties.Resources.TBF_icon;
ControlBox = false;
TextBoxesCount = 3;
endStates = false;
mainLabels = new Label[] { main1Label, main2Label, main3Label, };
auxLabels = new Label[] { aux1Label, aux2Label, aux3Label, };
mainStartTextBoxes = new TextBox[] { main1StartTextBox, main2StartTextBox, main3StartTextBox, };
auxStartTextBoxes = new TextBox[] { aux1StartTextBox, aux2StartTextBox, aux3StartTextBox, };
mainEndTextBoxes = new TextBox[] { main1EndTextBox, main2EndTextBox, main3EndTextBox, };
auxEndTextBoxes = new TextBox[] { aux1EndTextBox, aux2EndTextBox, aux3EndTextBox, };
enabledTextBoxes = new List<TextBox>();
completed = false;
StartForceCloseHandler();
}
/// <summary>
/// Constructor to fill in start states
/// </summary>
/// <param name="waterMetersCount">Number of water meters</param>
/// <param name="disabled">Information from CycleBeginningForm about availability of water meters</param>
public TestStartEndForm(int waterMetersCount, IRegReader[] regReaders, bool[] disabled)
: this()
{
endStates = false;
this.WaterMetersCount = waterMetersCount;
this.regReaders = regReaders;
this.disabled = disabled;
ShuffleTextBoxes();
MainStartState = new double[waterMetersCount];
AuxStartState = new double[waterMetersCount];
MainStartStateStr = new string[waterMetersCount];
AuxStartStateStr = new string[waterMetersCount];
}
/// <summary>
/// Constructor to fill in end states
/// </summary>
/// <param name="waterMetersCount">Number of water meters</param>
/// <param name="mainStartStateStr">Information entered on test start (main)</param>
/// <param name="auxStartStateStr">Information entered on test start (aux)</param>
/// <param name="disabled">Information from CycleBeginningForm about availability of water meters</param>
/// <param name="refVolume">Reference volume, it is used to verify the end state, show/hide exclamation if necessary</param>
/// <param name="errLimLo">Error limit low, it is used to verify the end state, show/hide exclamation if necessary</param>
/// <param name="errLimHi">Error limit high, it is used to verify the end state, show/hide exclamation if necessary</param>
public TestStartEndForm(int waterMetersCount, IRegReader[] regReaders, string[] mainStartStateStr, string[] auxStartStateStr,
bool[] disabled, double refVolume, double errLimLo, double errLimHi)
: this()
{
endStates = true;
this.WaterMetersCount = waterMetersCount;
this.regReaders = regReaders;
if (mainStartStateStr == null || mainStartStateStr.Length != waterMetersCount)
throw (new Exception("Invalid 'mainStartStateStr' argument"));
this.MainStartStateStr = mainStartStateStr;
if (auxStartStateStr == null || auxStartStateStr.Length != waterMetersCount)
throw (new Exception("Invalid 'auxStartStateStr' argument"));
this.AuxStartStateStr = auxStartStateStr;
this.disabled = disabled;
this.refVolume = refVolume;
this.warningLimLo = 2 * errLimLo;
this.warningLimHi = 2 * errLimHi;
ShuffleTextBoxes();
MainEndState = new double[waterMetersCount];
AuxEndState = new double[waterMetersCount];
}
/// <summary>
/// Make sure the layout of labels/text boxes on the screen
/// corresponds to the layout of watermeters of the test bench.
/// </summary>
void ShuffleTextBoxes()
{
2021-09-19 11:59:58 +00:00
if (TBF.Data.CompoundWMsCount < TextBoxesCount)
{
2021-09-19 11:59:58 +00:00
TextBoxesCount = TBF.Data.CompoundWMsCount;
}
}
private void CycleEndForm_Load(object sender, EventArgs e)
{
Localize();
//Height = 115 + 90 * WaterMetersCount;
for (int i = 0; i < TextBoxesCount; i++)
{
if (i < WaterMetersCount)
{
mainLabels[i].Visible = mainStartTextBoxes[i].Visible = mainEndTextBoxes[i].Visible = true;
auxLabels[i].Visible = auxStartTextBoxes[i].Visible = auxEndTextBoxes[i].Visible = true;
}
}
if (TextBoxesCount >= 1) groupBox1.Visible = true;
if (TextBoxesCount >= 2) groupBox2.Visible = true;
if (TextBoxesCount >= 3) groupBox3.Visible = true;
if (endStates)
{
for (int i = 0; i < TextBoxesCount; i++)
{
if (MainStartStateStr != null && i < MainStartStateStr.Length)
{
mainStartTextBoxes[i].Text = MainStartStateStr[i];
}
if (AuxStartStateStr != null && i < AuxStartStateStr.Length)
{
auxStartTextBoxes[i].Text = AuxStartStateStr[i];
}
if (disabled != null && i < disabled.Length && disabled[i])
{
mainEndTextBoxes[i].Enabled = false;
auxEndTextBoxes[i].Enabled = false;
}
else
{
mainEndTextBoxes[i].Enabled = true;
auxEndTextBoxes[i].Enabled = true;
enabledTextBoxes.Add(mainEndTextBoxes[i]);
enabledTextBoxes.Add(auxEndTextBoxes[i]);
}
}
}
else
{
for (int i = 0; i < TextBoxesCount; i++)
{
if (disabled != null && i < disabled.Length && disabled[i])
{
mainStartTextBoxes[i].Enabled = false;
auxStartTextBoxes[i].Enabled = false;
}
else
{
mainStartTextBoxes[i].Enabled = true;
auxStartTextBoxes[i].Enabled = true;
enabledTextBoxes.Add(mainStartTextBoxes[i]);
enabledTextBoxes.Add(auxStartTextBoxes[i]);
}
}
}
}
void Localize()
{
Text = Strings.Water_Meter_States;
groupBox1.Text = Strings.Water_Meter + " 1";
groupBox2.Text = Strings.Water_Meter + " 2";
groupBox3.Text = Strings.Water_Meter + " 3";
for (int i = 0; i < TextBoxesCount; i++)
{
mainLabels[i].Text = Strings.Main_;
auxLabels[i].Text = Strings.Aux_;
}
startLabel.Text = Strings.Start;
endLabel.Text = Strings.End;
okButton.Text = Strings.OkBtnText;
}
private void okButton_Click(object sender, EventArgs eArgs)
{
try
{
if (endStates)
{
for (int i = 0; i < Math.Min(WaterMetersCount, TextBoxesCount); i++)
{
if (mainEndTextBoxes[i].Visible && mainEndTextBoxes[i].Enabled)
{
MainEndState[i] = Utils.ParseUDouble(mainEndTextBoxes[i].Text);
}
if (auxEndTextBoxes[i].Visible && auxEndTextBoxes[i].Enabled)
{
AuxEndState[i] = Utils.ParseUDouble(auxEndTextBoxes[i].Text);
}
}
}
else
{
for (int i = 0; i < Math.Min(WaterMetersCount, TextBoxesCount); i++)
{
if (mainStartTextBoxes[i].Visible && mainStartTextBoxes[i].Enabled)
{
MainStartStateStr[i] = mainStartTextBoxes[i].Text;
MainStartState[i] = Utils.ParseUDouble(mainStartTextBoxes[i].Text);
}
if (auxStartTextBoxes[i].Visible && auxStartTextBoxes[i].Enabled)
{
AuxStartStateStr[i] = auxStartTextBoxes[i].Text;
AuxStartState[i] = Utils.ParseUDouble(auxStartTextBoxes[i].Text);
}
}
}
}
catch (Exception e)
{
log.ErrorFormat("Exception: {0}", e.Message);
return;
}
completed = true;
Close();
}
#region Forced close handling
public void StartForceCloseHandler()
{
UiBridge.Bridge.CloseModelessFormHandler += delegate(object sender, EventArgs args)
{
if (InvokeRequired) { Invoke(new EventHandler<EventArgs>(OnForceClose), sender, args); }
else OnForceClose(sender, args);
};
}
void OnForceClose(object sender, EventArgs args)
{
DialogResult = DialogResult.Cancel;
Close();
}
#endregion
/// <summary>
/// Calculate the approximate error and verify whether it is within limits.
/// Make an exclamation mark visible if out of range.
/// Similar event handler is implemented for all main/auxEndTextBoxes (1..3).
/// </summary>
private void main1EndTextBox_TextChanged(object sender, EventArgs e)
{
//double err = 0;
//try
//{
// double endState = Utils.ParseUDouble(main1EndTextBox.Text);
// double startState = Utils.ParseUDouble(main1StartTextBox.Text);
// err = 100.0 * (endState - startState - refVolume) / refVolume;
//}
//catch { };
//main1Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void aux1EndTextBox_TextChanged(object sender, EventArgs e)
{
//double err = 0;
//try
//{
// double endState = Utils.ParseUDouble(aux1EndTextBox.Text);
// double startState = Utils.ParseUDouble(aux1StartTextBox.Text);
// err = 100.0 * (endState - startState - refVolume) / refVolume;
//}
//catch { err = 1000.0f; };
//aux1Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void main2EndTextBox_TextChanged(object sender, EventArgs e)
{
//double err = 0;
//try
//{
// double endState = Utils.ParseUDouble(main2EndTextBox.Text);
// double startState = Utils.ParseUDouble(main2StartTextBox.Text);
// err = 100.0 * (endState - startState - refVolume) / refVolume;
//}
//catch { err = 1000.0f; };
//main2Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void aux2EndTextBox_TextChanged(object sender, EventArgs e)
{
//double err = 0;
//try
//{
// double endState = Utils.ParseUDouble(aux2EndTextBox.Text);
// double startState = Utils.ParseUDouble(aux2StartTextBox.Text);
// err = 100.0 * (endState - startState - refVolume) / refVolume;
//}
//catch { err = 1000.0f; };
//aux2Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void main3EndTextBox_TextChanged(object sender, EventArgs e)
{
//double err = 0;
//try
//{
// double endState = Utils.ParseUDouble(main3EndTextBox.Text);
// double startState = Utils.ParseUDouble(main3StartTextBox.Text);
// err = 100.0 * (endState - startState - refVolume) / refVolume;
//}
//catch { err = 1000.0f; };
//main3Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
}
private void aux3EndTextBox_TextChanged(object sender, EventArgs e)
{
//double err = 0;
//try
//{
// double endState = Utils.ParseUDouble(aux3EndTextBox.Text);
// double startState = Utils.ParseUDouble(aux3StartTextBox.Text);
// err = 100.0 * (endState - startState - refVolume) / refVolume;
//}
//catch { err = 1000.0f; };
//aux3Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
}
/// <summary>
/// Process a key press within any enabled text boxe
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <param name="currentFocusOwner">TextBox control which received the event</param>
private void ProcKeyPress(object sender, KeyPressEventArgs e, TextBox currentFocusOwner)
{
if (e.KeyChar == '+')
{
/// Process '+' key ..... Form completed
okButton_Click(sender, e);
}
if (e.KeyChar == '\r')
{
/// Process <enter> key ..... Next text field
if (enabledTextBoxes.Count < 2) return; /// There is just one enabled textbox
for (int i = 0; i < enabledTextBoxes.Count; i++)
{
if (enabledTextBoxes[i] == currentFocusOwner)
{
/// Change focus to the next enabled text box
enabledTextBoxes[(i + 1) % enabledTextBoxes.Count].Focus();
return;
}
}
}
}
private void main1StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main1StartTextBox); }
private void main2StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main2StartTextBox); }
private void main3StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main3StartTextBox); }
private void aux1StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux1StartTextBox); }
private void aux2StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux2StartTextBox); }
private void aux3StartTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux3StartTextBox); }
private void main1EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main1EndTextBox); }
private void main2EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main2EndTextBox); }
private void main3EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, main3EndTextBox); }
private void aux1EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux1EndTextBox); }
private void aux2EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux2EndTextBox); }
private void aux3EndTextBox_KeyPress(object sndr, KeyPressEventArgs e) { ProcKeyPress(sndr, e, aux3EndTextBox); }
}
}