423 lines
14 KiB
C#
423 lines
14 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.BenchControl.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 bool[] disabled;
|
|
|
|
// To be retrieved after the form closes
|
|
public float[] MainStartState;
|
|
public float[] AuxStartState;
|
|
|
|
// To be retrieved after the form closes
|
|
public readonly string[] MainStartStateStr;
|
|
public readonly string[] AuxStartStateStr;
|
|
|
|
// To be retrieved after the form closes
|
|
public float[] MainEndState;
|
|
public float[] AuxEndState;
|
|
|
|
bool endStates; /// false=start states, true=end states
|
|
float refVolume;
|
|
float warningLimLo; /// limit to display exclamation mark, typically 2x errLimLo
|
|
float 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();
|
|
|
|
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, bool[] disabled)
|
|
: this()
|
|
{
|
|
endStates = false;
|
|
|
|
this.WaterMetersCount = waterMetersCount;
|
|
this.disabled = disabled;
|
|
|
|
ShuffleTextBoxes();
|
|
|
|
MainStartState = new float[waterMetersCount];
|
|
AuxStartState = new float[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, string[] mainStartStateStr, string[] auxStartStateStr,
|
|
bool[] disabled, float refVolume, float errLimLo, float errLimHi)
|
|
: this()
|
|
{
|
|
endStates = true;
|
|
|
|
this.WaterMetersCount = waterMetersCount;
|
|
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 float[waterMetersCount];
|
|
AuxEndState = new float[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()
|
|
{
|
|
if (Config.Data.CompoundWMsCount < TextBoxesCount)
|
|
{
|
|
TextBoxesCount = Config.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.ParseUFloat(mainEndTextBoxes[i].Text);
|
|
}
|
|
if (auxEndTextBoxes[i].Visible && auxEndTextBoxes[i].Enabled)
|
|
{
|
|
AuxEndState[i] = Utils.ParseUFloat(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.ParseUFloat(mainStartTextBoxes[i].Text);
|
|
}
|
|
if (auxStartTextBoxes[i].Visible && auxStartTextBoxes[i].Enabled)
|
|
{
|
|
AuxStartStateStr[i] = auxStartTextBoxes[i].Text;
|
|
AuxStartState[i] = Utils.ParseUFloat(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)
|
|
{
|
|
//float err = 0;
|
|
//try
|
|
//{
|
|
// float endState = Utils.ParseUFloat(main1EndTextBox.Text);
|
|
// float startState = Utils.ParseUFloat(main1StartTextBox.Text);
|
|
// err = 100.0f * (endState - startState - refVolume) / refVolume;
|
|
//}
|
|
//catch { };
|
|
|
|
//main1Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
|
|
}
|
|
|
|
private void aux1EndTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
//float err = 0;
|
|
//try
|
|
//{
|
|
// float endState = Utils.ParseUFloat(aux1EndTextBox.Text);
|
|
// float startState = Utils.ParseUFloat(aux1StartTextBox.Text);
|
|
// err = 100.0f * (endState - startState - refVolume) / refVolume;
|
|
//}
|
|
//catch { err = 1000.0f; };
|
|
|
|
//aux1Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
|
|
}
|
|
|
|
private void main2EndTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
//float err = 0;
|
|
//try
|
|
//{
|
|
// float endState = Utils.ParseUFloat(main2EndTextBox.Text);
|
|
// float startState = Utils.ParseUFloat(main2StartTextBox.Text);
|
|
// err = 100.0f * (endState - startState - refVolume) / refVolume;
|
|
//}
|
|
//catch { err = 1000.0f; };
|
|
|
|
//main2Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
|
|
}
|
|
|
|
private void aux2EndTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
//float err = 0;
|
|
//try
|
|
//{
|
|
// float endState = Utils.ParseUFloat(aux2EndTextBox.Text);
|
|
// float startState = Utils.ParseUFloat(aux2StartTextBox.Text);
|
|
// err = 100.0f * (endState - startState - refVolume) / refVolume;
|
|
//}
|
|
//catch { err = 1000.0f; };
|
|
|
|
//aux2Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
|
|
}
|
|
|
|
private void main3EndTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
//float err = 0;
|
|
//try
|
|
//{
|
|
// float endState = Utils.ParseUFloat(main3EndTextBox.Text);
|
|
// float startState = Utils.ParseUFloat(main3StartTextBox.Text);
|
|
// err = 100.0f * (endState - startState - refVolume) / refVolume;
|
|
//}
|
|
//catch { err = 1000.0f; };
|
|
|
|
//main3Exclamation.Visible = (err < warningLimLo) || (warningLimHi < err);
|
|
}
|
|
|
|
private void aux3EndTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
//float err = 0;
|
|
//try
|
|
//{
|
|
// float endState = Utils.ParseUFloat(aux3EndTextBox.Text);
|
|
// float startState = Utils.ParseUFloat(aux3StartTextBox.Text);
|
|
// err = 100.0f * (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); }
|
|
}
|
|
}
|