406 lines
12 KiB
C#
406 lines
12 KiB
C#
///
|
|
/// Copyright (c) 2013-2017 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using TBF.Rig.GenericDevices;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.Rig.DataEntry.Munich3
|
|
{
|
|
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;
|
|
readonly IRegReader[] regReaders;
|
|
readonly bool[] disabled;
|
|
|
|
// To be retrieved after the form closes
|
|
public double[] WMStartState;
|
|
|
|
// To be retrieved after the form closes
|
|
public readonly string[] WMStartStateStr;
|
|
|
|
// To be retrieved after the form closes
|
|
public double[] WMEndState;
|
|
|
|
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
|
|
|
|
GroupBox[] groupBoxes;
|
|
Label[] labels;
|
|
TextBox[] startTextBoxes;
|
|
TextBox[] endTextBoxes;
|
|
Label[] exclamations;
|
|
|
|
|
|
public TestStartEndForm()
|
|
{
|
|
InitializeComponent();
|
|
ControlBox = false;
|
|
|
|
groupBoxes = new GroupBox[] { groupBox1, groupBox2, groupBox3, groupBox4, groupBox5, groupBox6 };
|
|
labels = new Label[] { label1, label2, label3, label4, label5, label6 };
|
|
startTextBoxes = new TextBox[] { startTextBox1, startTextBox2, startTextBox3, startTextBox4, startTextBox5, startTextBox6 };
|
|
endTextBoxes = new TextBox[] { endTextBox1, endTextBox2, endTextBox3, endTextBox4, endTextBox5, endTextBox6 };
|
|
exclamations = new Label[] { exclamation1, exclamation2, exclamation3, exclamation3, exclamation4, exclamation5 };
|
|
|
|
completed = false;
|
|
StartForceCloseHandler();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor to fill in start states
|
|
/// </summary>
|
|
/// <param name="waterMetersCount">Number of text boxes for serial numbers</param>
|
|
public TestStartEndForm(int waterMetersCount, IRegReader[] regReaders, bool[] disabled)
|
|
: this()
|
|
{
|
|
endStates = false;
|
|
|
|
this.regReaders = regReaders;
|
|
this.WaterMetersCount = Math.Min(waterMetersCount, regReaders.Length);
|
|
this.disabled = disabled;
|
|
|
|
WMStartState = new double[waterMetersCount];
|
|
WMStartStateStr = new string[waterMetersCount];
|
|
|
|
Height = 70 + 90 * WaterMetersCount;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor to fill in end states
|
|
/// </summary>
|
|
/// <param name="waterMetersCount">Number of text boxes for serial numbers</param>
|
|
public TestStartEndForm(int waterMetersCount, IRegReader[] regReaders, string[] wmStartStateStr, bool[] disabled,
|
|
double refVolume, double errLimLo, double errLimHi)
|
|
: this()
|
|
{
|
|
endStates = true;
|
|
|
|
this.regReaders = regReaders;
|
|
this.WaterMetersCount = Math.Min(waterMetersCount, regReaders.Length);
|
|
this.WMStartStateStr = wmStartStateStr;
|
|
if (wmStartStateStr == null || wmStartStateStr.Length != this.WaterMetersCount)
|
|
throw (new Exception("Invalid 'wmStartStateStr' argument"));
|
|
this.disabled = disabled;
|
|
this.refVolume = refVolume;
|
|
this.warningLimLo = 2 * errLimLo;
|
|
this.warningLimHi = 2 * errLimHi;
|
|
|
|
WMEndState = new double[waterMetersCount];
|
|
|
|
Height = 70 + 90 * WaterMetersCount;
|
|
}
|
|
|
|
private void TestStartEndForm_Load(object sender, EventArgs e)
|
|
{
|
|
/// First 'WaterMetersCount' positions
|
|
for (int i = 0; i < WaterMetersCount; i++)
|
|
{
|
|
if (disabled.Length > i && regReaders[i] == null) disabled[i] = true;
|
|
|
|
if (endStates)
|
|
{
|
|
startTextBoxes[i].Text = WMStartStateStr[i];
|
|
endTextBoxes[i].Enabled = (disabled.Length > i && !disabled[i]);
|
|
}
|
|
else
|
|
{
|
|
startTextBoxes[i].Enabled = (disabled.Length > i && !disabled[i]);
|
|
}
|
|
}
|
|
|
|
/// Remaining positions
|
|
for (int i = WaterMetersCount; i < 6; i++)
|
|
{
|
|
groupBoxes[i].Visible = false;
|
|
}
|
|
|
|
Localize();
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
Text = Strings.Water_Meter_States;
|
|
startLabel.Text = Strings.Start;
|
|
endLabel.Text = Strings.End;
|
|
okButton.Text = Strings.OkBtnText;
|
|
|
|
for (int i = 0; i < WaterMetersCount; i++)
|
|
{
|
|
groupBoxes[i].Text = string.Format("{0} {1}", Strings.Water_Meter, i + 1);
|
|
if (disabled.Length > i && !disabled[i])
|
|
{
|
|
labels[i].Text = string.Format("{0} [{1}]", Strings.State, GetUnitStr(regReaders[i].PulsesPerLtr));
|
|
}
|
|
else
|
|
{
|
|
labels[i].Text = string.Empty;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
string GetUnitStr(double pulsesPerLtr)
|
|
{
|
|
if (pulsesPerLtr == 1 || pulsesPerLtr <= float.Epsilon)
|
|
{
|
|
return "l";
|
|
}
|
|
else if (pulsesPerLtr == 0.001)
|
|
{
|
|
return "m3";
|
|
}
|
|
else if (pulsesPerLtr == 1000)
|
|
{
|
|
return "ml";
|
|
}
|
|
else
|
|
{
|
|
return string.Format("{0} l", 1/pulsesPerLtr);
|
|
}
|
|
}
|
|
|
|
|
|
private void okButton_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (endStates)
|
|
{
|
|
for (int i = 0; i < WaterMetersCount; i++)
|
|
{
|
|
if (endTextBoxes[i].Enabled) WMEndState[i] = Utils.ParseUDouble(endTextBoxes[i].Text) * regReaders[i].LtrsPerPulse;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < WaterMetersCount; i++)
|
|
{
|
|
if (startTextBoxes[i].Enabled)
|
|
{
|
|
WMStartStateStr[i] = startTextBoxes[i].Text;
|
|
WMStartState[i] = Utils.ParseUDouble(startTextBoxes[i].Text) * regReaders[i].LtrsPerPulse;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
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
|
|
|
|
|
|
private void endTextBox1_TextChanged(object sender, EventArgs e) { EndTextChanged(0); }
|
|
private void endTextBox2_TextChanged(object sender, EventArgs e) { EndTextChanged(1); }
|
|
private void endTextBox3_TextChanged(object sender, EventArgs e) { EndTextChanged(2); }
|
|
private void endTextBox4_TextChanged(object sender, EventArgs e) { EndTextChanged(3); }
|
|
private void endTextBox5_TextChanged(object sender, EventArgs e) { EndTextChanged(4); }
|
|
private void endTextBox6_TextChanged(object sender, EventArgs e) { EndTextChanged(5); }
|
|
|
|
void EndTextChanged(int i)
|
|
{
|
|
double err = 0;
|
|
try
|
|
{
|
|
double endState = Utils.ParseUDouble(endTextBoxes[i].Text) * regReaders[i].LtrsPerPulse; ;
|
|
double startState = Utils.ParseUDouble(startTextBoxes[i].Text) * regReaders[i].LtrsPerPulse; ;
|
|
err = 100.0 * (endState - startState - refVolume) / refVolume;
|
|
}
|
|
catch { err = 1000.0f; };
|
|
|
|
exclamations[i].Visible = (err < warningLimLo) || (warningLimHi < err);
|
|
}
|
|
|
|
|
|
private void startTextBox1_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (startTextBox2.Enabled) startTextBox2.Focus();
|
|
else if (startTextBox3.Enabled) startTextBox3.Focus();
|
|
else if (startTextBox4.Enabled) startTextBox4.Focus();
|
|
else if (startTextBox5.Enabled) startTextBox5.Focus();
|
|
else if (startTextBox6.Enabled) startTextBox6.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void startTextBox2_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (startTextBox3.Enabled) startTextBox3.Focus();
|
|
else if (startTextBox4.Enabled) startTextBox4.Focus();
|
|
else if (startTextBox5.Enabled) startTextBox5.Focus();
|
|
else if (startTextBox6.Enabled) startTextBox6.Focus();
|
|
else if (startTextBox1.Enabled) startTextBox1.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void startTextBox3_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (startTextBox4.Enabled) startTextBox4.Focus();
|
|
else if (startTextBox5.Enabled) startTextBox5.Focus();
|
|
else if (startTextBox6.Enabled) startTextBox6.Focus();
|
|
else if (startTextBox1.Enabled) startTextBox1.Focus();
|
|
else if (startTextBox2.Enabled) startTextBox2.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void startTextBox4_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (startTextBox5.Enabled) startTextBox5.Focus();
|
|
else if (startTextBox6.Enabled) startTextBox6.Focus();
|
|
else if (startTextBox1.Enabled) startTextBox1.Focus();
|
|
else if (startTextBox2.Enabled) startTextBox2.Focus();
|
|
else if (startTextBox3.Enabled) startTextBox3.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void startTextBox5_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (startTextBox6.Enabled) startTextBox6.Focus();
|
|
else if (startTextBox1.Enabled) startTextBox1.Focus();
|
|
else if (startTextBox2.Enabled) startTextBox2.Focus();
|
|
else if (startTextBox3.Enabled) startTextBox3.Focus();
|
|
else if (startTextBox4.Enabled) startTextBox4.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void startTextBox6_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (startTextBox1.Enabled) startTextBox1.Focus();
|
|
else if (startTextBox1.Enabled) startTextBox1.Focus();
|
|
else if (startTextBox3.Enabled) startTextBox3.Focus();
|
|
else if (startTextBox4.Enabled) startTextBox4.Focus();
|
|
else if (startTextBox5.Enabled) startTextBox5.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void endTextBox1_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (endTextBox2.Enabled) endTextBox2.Focus();
|
|
else if (endTextBox3.Enabled) endTextBox3.Focus();
|
|
else if (endTextBox4.Enabled) endTextBox4.Focus();
|
|
else if (endTextBox5.Enabled) endTextBox5.Focus();
|
|
else if (endTextBox6.Enabled) endTextBox6.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void endTextBox2_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (endTextBox3.Enabled) endTextBox3.Focus();
|
|
else if (endTextBox4.Enabled) endTextBox4.Focus();
|
|
else if (endTextBox5.Enabled) endTextBox5.Focus();
|
|
else if (endTextBox6.Enabled) endTextBox6.Focus();
|
|
else if (endTextBox1.Enabled) endTextBox1.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void endTextBox3_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (endTextBox4.Enabled) endTextBox4.Focus();
|
|
else if (endTextBox5.Enabled) endTextBox5.Focus();
|
|
else if (endTextBox6.Enabled) endTextBox6.Focus();
|
|
else if (endTextBox1.Enabled) endTextBox1.Focus();
|
|
else if (endTextBox2.Enabled) endTextBox2.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void endTextBox4_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (endTextBox5.Enabled) endTextBox5.Focus();
|
|
else if (endTextBox6.Enabled) endTextBox6.Focus();
|
|
else if (endTextBox1.Enabled) endTextBox1.Focus();
|
|
else if (endTextBox2.Enabled) endTextBox2.Focus();
|
|
else if (endTextBox3.Enabled) endTextBox3.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void endTextBox5_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (endTextBox6.Enabled) endTextBox6.Focus();
|
|
else if (endTextBox1.Enabled) endTextBox1.Focus();
|
|
else if (endTextBox2.Enabled) endTextBox2.Focus();
|
|
else if (endTextBox3.Enabled) endTextBox3.Focus();
|
|
else if (endTextBox4.Enabled) endTextBox4.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
|
|
private void endTextBox6_KeyPress(object sender, KeyPressEventArgs e)
|
|
{
|
|
if (e.KeyChar == '\r')
|
|
{
|
|
if (endTextBox1.Enabled) endTextBox1.Focus();
|
|
else if (endTextBox1.Enabled) endTextBox1.Focus();
|
|
else if (endTextBox3.Enabled) endTextBox3.Focus();
|
|
else if (endTextBox4.Enabled) endTextBox4.Focus();
|
|
else if (endTextBox5.Enabled) endTextBox5.Focus();
|
|
}
|
|
else if (e.KeyChar == '+') okButton_Click(sender, e);
|
|
}
|
|
}
|
|
}
|