405 lines
14 KiB
C#
405 lines
14 KiB
C#
///
|
|
/// Copyright (c) 2013-2020 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using Common.Forms;
|
|
using TBF.Resources;
|
|
using TBF.UI.Shared;
|
|
|
|
namespace TBF.UI.Bench.Metrology
|
|
{
|
|
public partial class MetrologyDlgBalanceTab : UserControl, IMetrologyDlgTab
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(MetrologyDlgBalanceTab));
|
|
|
|
/// <summary>
|
|
/// Component entity that has a list of 'measurement-correction' pairs
|
|
/// to be updated in the database on OK.
|
|
/// </summary>
|
|
public Component MeterEntity
|
|
{
|
|
get { return meterEntity; }
|
|
set { meterEntity = value; }
|
|
}
|
|
Component meterEntity;
|
|
|
|
/// <summary>
|
|
/// Balance configuration (with buoyancy parameters)
|
|
/// </summary>
|
|
public Rig.GenericDevices.IScaleCfg BalanceCfg;
|
|
|
|
/// <summary>
|
|
/// A list of 'measurement-correction' pairs to be deleted from the database on OK.
|
|
/// </summary>
|
|
public IList<MeasurementCorrection> ToBeRemoved
|
|
{
|
|
get { return toBeRemoved; }
|
|
set { toBeRemoved = value; }
|
|
}
|
|
IList<MeasurementCorrection> toBeRemoved;
|
|
|
|
MetrologyDlg parent;
|
|
bool unlocked;
|
|
Control measurementTextBox;
|
|
Control correctionTextBox;
|
|
Control errorTextBox;
|
|
|
|
public MetrologyDlgBalanceTab()
|
|
{
|
|
InitializeComponent();
|
|
calibDateTimePicker.CustomFormat = Constants.DateFormat;
|
|
calibDateTimePicker.MinDate = Constants.MinDate;
|
|
calibExpirationDateTimePicker.CustomFormat = Constants.DateFormat;
|
|
calibExpirationDateTimePicker.MinDate = Constants.MinDate;
|
|
toBeRemoved = new List<MeasurementCorrection>();
|
|
unlocked = false;
|
|
}
|
|
|
|
private void MetrologyDlgBalanceTab_Load(object sender, System.EventArgs e)
|
|
{
|
|
if (meterEntity == null) return;
|
|
|
|
parent = ParentForm as MetrologyDlg;
|
|
|
|
Localize();
|
|
|
|
/// Panel1
|
|
cmpntNameLabel.Text = meterEntity.Name;
|
|
measuredTextBox.Text = string.Empty;
|
|
correctedTextBox.Text = string.Empty;
|
|
|
|
/// Panel2
|
|
this.Controls.Add(measurementTextBox = new TextBox());
|
|
this.Controls.Add(correctionTextBox = new TextBox());
|
|
this.Controls.Add(errorTextBox = new TextBox());
|
|
|
|
listViewEx.SubItemClicked += new SubItemEventHandler(listViewEx_SubItemClicked);
|
|
listViewEx.SubItemEndEditing += new SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing);
|
|
|
|
listViewEx.Columns.Add(new ColumnHeader() { Text = Strings.Nr, Width = 60 });
|
|
listViewEx.Columns.Add(new ColumnHeader() { Text = string.Format("{0} [kg]", Strings.Mass), Width = 100 });
|
|
listViewEx.Columns.Add(new ColumnHeader() { Text = string.Format("{0} [g]", Strings.Correction), Width = 100 });
|
|
listViewEx.Columns.Add(new ColumnHeader() { Text = string.Format("{0} [%]", Strings.Error), Width = 100 });
|
|
|
|
RefreshAll();
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
nameGroupBox.Text = Strings.Component;
|
|
|
|
calibrationGroupBox.Text = Strings.Calibration;
|
|
calibCertificateNrLabel.Text = Strings.Certificate;
|
|
calibDateLabel.Text = Strings.Date;
|
|
calibValidDateLabel.Text = Strings.Valid_until;
|
|
|
|
buoyancyTempLabel.Text = string.Format("{0} [°C]", Strings.Ambient_temperature);
|
|
buoyancyPressLabel.Text = string.Format("{0} [mbar]", Strings.Ambient_pressure);
|
|
buoyancyHumiLabel.Text = string.Format("{0} [%]", Strings.Ambient_humidity);
|
|
airDensityLabel.Text = Strings.Air_density_kg_m3;
|
|
weightStandardDensityLabel.Text = Strings.Ref_mass_dens_kg_m3;
|
|
|
|
testGroupBox.Text = Strings.Test_measured_corrected;
|
|
measuredLabel.Text = Strings.Measured;
|
|
correctedLabel.Text = Strings.Corrected;
|
|
}
|
|
|
|
void RefreshAll()
|
|
{
|
|
if (BalanceCfg != null)
|
|
{
|
|
calibCertificateNrTextBox.Text = BalanceCfg.CalibCertificateNr;
|
|
calibDateTimePicker.Value = (BalanceCfg.CalibDate < Constants.MinDate) ? Constants.MinDate : BalanceCfg.CalibDate;
|
|
calibExpirationDateTimePicker.Value = (BalanceCfg.CalibValidDate < Constants.MinDate) ? Constants.MinDate : BalanceCfg.CalibValidDate;
|
|
|
|
buoyancyTempTextBox.Text = BalanceCfg.BuoyancyTemp.ToString("F2");
|
|
buoyancyPressTextBox.Text = BalanceCfg.BuoyancyPress.ToString("F4");
|
|
buoyancyHumiTextBox.Text = BalanceCfg.BuoyancyHumi.ToString("F1");
|
|
/// airDensityTextBox.Text = //calculate air density
|
|
weightStandardDensityTextBox.Text = BalanceCfg.WeightStandardDensity.ToString("F1");
|
|
}
|
|
|
|
listViewEx.Items.Clear();
|
|
foreach (var corr in MeterEntity.Corrections) AddOneLVI(corr);
|
|
}
|
|
|
|
void AddOneLVI(MeasurementCorrection corr)
|
|
{
|
|
int nr = listViewEx.Items.Count + 1;
|
|
ListViewItem lvi = new ListViewItem(nr.ToString());
|
|
lvi.Tag = corr;
|
|
lvi.SubItems.Add(corr.Measurement.ToString());
|
|
lvi.SubItems.Add((1000 * corr.Correction).ToString());
|
|
|
|
if (corr.Measurement == 0)
|
|
{
|
|
lvi.SubItems.Add(Strings.NaN);
|
|
}
|
|
else
|
|
{
|
|
double error = Config.Formulas.ErrorFromVolumes(corr.Measurement, corr.Measurement + corr.Correction);
|
|
lvi.SubItems.Add(error.ToString(Config.Utils.SignificantDigitsToFmt(error, 5)));
|
|
}
|
|
|
|
listViewEx.Items.Add(lvi);
|
|
}
|
|
|
|
void listViewEx_SubItemClicked(object sender, SubItemEventArgs e)
|
|
{
|
|
if (unlocked && e.SubItem == 1)
|
|
{
|
|
listViewEx.StartEditing(measurementTextBox, e.Item, e.SubItem);
|
|
}
|
|
else if (unlocked && e.SubItem == 2)
|
|
{
|
|
listViewEx.StartEditing(correctionTextBox, e.Item, e.SubItem);
|
|
}
|
|
else if (unlocked && e.SubItem == 3)
|
|
{
|
|
listViewEx.StartEditing(correctionTextBox, e.Item, e.SubItem);
|
|
}
|
|
}
|
|
|
|
void listViewEx_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e)
|
|
{
|
|
if (ParseItem(e.Item, e.SubItem, e.DisplayText)) return;
|
|
|
|
/// New value is NOK, revert the original text
|
|
e.DisplayText = e.Item.Text;
|
|
e.Cancel = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse text of a ListViewItem
|
|
/// </summary>
|
|
/// <returns>true = value parsed OK</returns>
|
|
bool ParseItem(ListViewItem item, int subItem, string strValue)
|
|
{
|
|
double dblValue = 0;
|
|
if (subItem == 1 && Utils.TryParseEDouble(strValue, out dblValue) && dblValue >= 0)
|
|
{
|
|
(item.Tag as MeasurementCorrection).Measurement = (float)dblValue;
|
|
|
|
/// Update error (if possible)
|
|
double correction1000;
|
|
if (Utils.TryParseEDouble(item.SubItems[2].Text, out correction1000) && (dblValue != 0))
|
|
{
|
|
double correction = correction1000 / 1000;
|
|
double error = Config.Formulas.ErrorFromVolumes(dblValue, dblValue + correction);
|
|
item.SubItems[3].Text = error.ToString(Config.Utils.SignificantDigitsToFmt(error, 5));
|
|
}
|
|
else
|
|
{
|
|
item.SubItems[3].Text = Strings.NaN;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else if (subItem == 2 && Utils.TryParseEDouble(strValue, out dblValue))
|
|
{
|
|
double correctionInDefaultUnits = dblValue / 1000;
|
|
(item.Tag as MeasurementCorrection).Correction = (float)correctionInDefaultUnits;
|
|
|
|
/// Update error (if possible)
|
|
double measurement;
|
|
if (Utils.TryParseEDouble(item.SubItems[1].Text, out measurement) && (measurement != 0))
|
|
{
|
|
double error = Config.Formulas.ErrorFromVolumes(measurement, measurement + correctionInDefaultUnits);
|
|
item.SubItems[3].Text = error.ToString(Config.Utils.SignificantDigitsToFmt(error, 5));
|
|
}
|
|
else
|
|
{
|
|
item.SubItems[3].Text = Strings.NaN;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else if (subItem == 3 && ((strValue == Strings.NaN) || Utils.TryParseEDouble(strValue, out dblValue)))
|
|
{
|
|
if (strValue == Strings.NaN)
|
|
{
|
|
}
|
|
else
|
|
{
|
|
double measurement;
|
|
if (Utils.TryParseEDouble(item.SubItems[1].Text, out measurement) && measurement != 0)
|
|
{
|
|
double correction = Config.Formulas.CorrectionFromError(measurement, dblValue);
|
|
double correction1000 = 1000 * correction;
|
|
item.SubItems[2].Text = correction1000.ToString(Config.Utils.SignificantDigitsToFmt(correction1000, 5));
|
|
(item.Tag as MeasurementCorrection).Correction = (float)correction;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void Unlock()
|
|
{
|
|
unlocked = true;
|
|
|
|
calibCertificateNrTextBox.Enabled = true;
|
|
calibDateTimePicker.Enabled = true;
|
|
calibExpirationDateTimePicker.Enabled = true;
|
|
|
|
buoyancyTempTextBox.Enabled = true;
|
|
buoyancyPressTextBox.Enabled = true;
|
|
buoyancyHumiTextBox.Enabled = true;
|
|
weightStandardDensityTextBox.Enabled = true;
|
|
|
|
if (listViewEx.SelectedItems.Count == 1)
|
|
{
|
|
int ix = listViewEx.SelectedIndices[0];
|
|
listViewEx.Focus();
|
|
listViewEx.Items[ix].Selected = true;
|
|
listViewEx.Items[ix].EnsureVisible();
|
|
}
|
|
}
|
|
|
|
public void OkBtnClicked()
|
|
{
|
|
if (BalanceCfg != null)
|
|
{
|
|
try
|
|
{
|
|
BalanceCfg.CalibCertificateNr = calibCertificateNrTextBox.Text;
|
|
BalanceCfg.CalibDate = calibDateTimePicker.Value.Date;
|
|
BalanceCfg.CalibValidDate = calibExpirationDateTimePicker.Value.Date;
|
|
|
|
BalanceCfg.BuoyancyTemp = Utils.ParseSFloat(buoyancyTempTextBox.Text);
|
|
BalanceCfg.BuoyancyPress = Utils.ParseUFloat(buoyancyPressTextBox.Text);
|
|
BalanceCfg.BuoyancyHumi = Utils.ParseUFloat(buoyancyHumiTextBox.Text);
|
|
BalanceCfg.WeightStandardDensity = Utils.ParseUFloat(weightStandardDensityTextBox.Text);
|
|
|
|
Component modified = BalanceCfg.CreateDbEntity();
|
|
MeterEntity.Parameters = modified.Parameters;
|
|
}
|
|
catch
|
|
{
|
|
MessageBox.Show(Strings.Invalid_buoyancy_parameter_Correct_pls,
|
|
Strings.Error,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Exclamation);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void CancelBtnClicked()
|
|
{
|
|
}
|
|
|
|
public void AddOne()
|
|
{
|
|
MeasurementCorrection corr = new MeasurementCorrection();
|
|
MeterEntity.Corrections.Add(corr);
|
|
AddOneLVI(corr);
|
|
|
|
listViewEx.Focus();
|
|
listViewEx.SelectedItems.Clear();
|
|
listViewEx.Items[listViewEx.Items.Count - 1].Selected = true;
|
|
listViewEx.Items[listViewEx.Items.Count - 1].EnsureVisible();
|
|
}
|
|
|
|
public void RemoveSelected()
|
|
{
|
|
if (listViewEx.SelectedItems.Count != 1) return;
|
|
|
|
int removeItemNr = listViewEx.SelectedIndices[0];
|
|
|
|
MeasurementCorrection selected =
|
|
(MeasurementCorrection)listViewEx.SelectedItems[0].Tag;
|
|
if (selected.Id != 0) toBeRemoved.Add(selected);
|
|
MeterEntity.Corrections.RemoveAt(removeItemNr);
|
|
RefreshAll();
|
|
|
|
if (removeItemNr < listViewEx.Items.Count)
|
|
{
|
|
listViewEx.Focus();
|
|
listViewEx.Items[removeItemNr].Selected = true;
|
|
listViewEx.Items[removeItemNr].EnsureVisible();
|
|
}
|
|
else
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None);
|
|
}
|
|
}
|
|
|
|
public void MoveUpSelected()
|
|
{
|
|
}
|
|
|
|
public void MoveDownSelected()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event handler that indicates the selected item position
|
|
/// and updates 'Remove', 'Up' and 'Down' buttons in the parent dialog.
|
|
/// </summary>
|
|
private void listViewEx_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (listViewEx.SelectedItems.Count != 1)
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.None);
|
|
}
|
|
else if (listViewEx.Items.Count == 1)
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.FirstAndLast);
|
|
}
|
|
else if (listViewEx.SelectedIndices[0] == 0)
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.First);
|
|
}
|
|
else if (listViewEx.SelectedIndices[0] == (listViewEx.Items.Count - 1))
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.Last);
|
|
}
|
|
else
|
|
{
|
|
parent.UpdateButtonStates(SharedButtons.SelectedItemPos.Middle);
|
|
}
|
|
}
|
|
|
|
private void measuredTextBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
float measured;
|
|
if (float.TryParse(measuredTextBox.Text,
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent,
|
|
CultureInfo.CurrentCulture,
|
|
out measured) ||
|
|
float.TryParse(measuredTextBox.Text,
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowExponent,
|
|
CultureInfo.InvariantCulture,
|
|
out measured))
|
|
{
|
|
double corrected = Config.Formulas.CorrectedValue(measured, meterEntity.Corrections);
|
|
correctedTextBox.Text = corrected.ToString();
|
|
}
|
|
}
|
|
|
|
public string GetWarningsBeforeSaving(ref Dictionary<string, string> renmInfo)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
public void UpdateRelatedItems(Dictionary<string, string> renameInfo)
|
|
{
|
|
}
|
|
|
|
public void SaveEntityToDB(NHibernate.ISession session)
|
|
{
|
|
session.SaveOrUpdate(MeterEntity);
|
|
}
|
|
}
|
|
}
|