268 lines
7.9 KiB
C#
268 lines
7.9 KiB
C#
///
|
|
/// Copyright (c) 2015-2019 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.BenchControl.TestMethods.Adjustment
|
|
{
|
|
public partial class WMErrorsForm24 : Form, GenericDevices.IHasCompleted
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(WMErrorsForm24));
|
|
|
|
const int MaxWMsCount = 24; /// Implementation limit of this form
|
|
|
|
///
|
|
/// Updated in constructor
|
|
///
|
|
readonly int waterMetersCount; /// Number of text boxes for serial numbers
|
|
readonly bool[] disabled;
|
|
int textBoxesCount;
|
|
Label[] labels;
|
|
TextBox[] errors;
|
|
Rectangle[] rects;
|
|
|
|
///
|
|
/// Updated in OnAdjustmentInProgress()
|
|
///
|
|
double[] error; /// Created in constructor
|
|
double errLimLo;
|
|
double errLimHi;
|
|
|
|
///
|
|
/// Set to 'true' when the form closes
|
|
///
|
|
public bool Completed { get { return completed; } }
|
|
bool completed;
|
|
|
|
|
|
/// <summary> Parameterless constructor for 3 watermeters </summary>
|
|
public WMErrorsForm24()
|
|
{
|
|
InitializeComponent();
|
|
ControlBox = false;
|
|
|
|
textBoxesCount = MaxWMsCount;
|
|
|
|
labels = new Label[MaxWMsCount]
|
|
{
|
|
wmErrorLabel1, wmErrorLabel2, wmErrorLabel3, wmErrorLabel4, wmErrorLabel5,
|
|
wmErrorLabel6, wmErrorLabel7, wmErrorLabel8, wmErrorLabel9, wmErrorLabel10,
|
|
wmErrorLabel11, wmErrorLabel12, wmErrorLabel13, wmErrorLabel14, wmErrorLabel15,
|
|
wmErrorLabel16, wmErrorLabel17, wmErrorLabel18, wmErrorLabel19, wmErrorLabel20,
|
|
wmErrorLabel21, wmErrorLabel22, wmErrorLabel23, wmErrorLabel24,
|
|
};
|
|
errors = new TextBox[MaxWMsCount]
|
|
{
|
|
wmErrorTextBox1, wmErrorTextBox2, wmErrorTextBox3, wmErrorTextBox4, wmErrorTextBox5,
|
|
wmErrorTextBox6, wmErrorTextBox7, wmErrorTextBox8, wmErrorTextBox9, wmErrorTextBox10,
|
|
wmErrorTextBox11, wmErrorTextBox12, wmErrorTextBox13, wmErrorTextBox14, wmErrorTextBox15,
|
|
wmErrorTextBox16, wmErrorTextBox17, wmErrorTextBox18, wmErrorTextBox19, wmErrorTextBox20,
|
|
wmErrorTextBox21, wmErrorTextBox22, wmErrorTextBox23, wmErrorTextBox24,
|
|
};
|
|
|
|
rects = new Rectangle[MaxWMsCount];
|
|
for (int i = 0; i < MaxWMsCount; i++)
|
|
{
|
|
int row = i % 12;
|
|
int col = i / 12;
|
|
rects[i] = new Rectangle(350 + col * 557, 25 + row * 47, 230, 37);
|
|
}
|
|
|
|
completed = false;
|
|
|
|
StartForceCloseHandler();
|
|
|
|
/// Attach to 'AdjustmentInProgress' handler
|
|
UiBridge.Bridge.AdjustmentInProgressHandler += delegate(object sender, UiBridge.AdjustmentInProgressEventArgs args)
|
|
{
|
|
if (InvokeRequired)
|
|
{
|
|
Invoke(new EventHandler<UiBridge.AdjustmentInProgressEventArgs>(OnAdjustmentInProgress), sender, args);
|
|
}
|
|
else OnAdjustmentInProgress(sender, args);
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="waterMetersCount">Number of text boxes for serial numbers</param>
|
|
public WMErrorsForm24(IList<GenericDevices.IWaterMeter> waterMeters)
|
|
: this()
|
|
{
|
|
waterMetersCount = Math.Min(Config.Data.WMsCount, waterMeters.Count);
|
|
|
|
ShuffleTextBoxes(waterMetersCount, Config.Data.LineSize);
|
|
|
|
disabled = new bool[waterMetersCount];
|
|
for (int i = 0; i < waterMetersCount; i++)
|
|
{
|
|
if (i < MaxWMsCount) disabled[i] = waterMeters[i].Disabled;
|
|
}
|
|
|
|
error = 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>
|
|
/// <param name="wmsCount">Number of watermeters</param>
|
|
/// <param name="lineSize">Number of watermeters in one line</param>
|
|
void ShuffleTextBoxes(int wmsCount, int lineSize)
|
|
{
|
|
if ((wmsCount < textBoxesCount) && (lineSize > 0))
|
|
{
|
|
int nrLines = (wmsCount + lineSize - 1) / lineSize;
|
|
int gap = (textBoxesCount - wmsCount) / nrLines;
|
|
|
|
int dest = 0;
|
|
for (int l = 0; l < nrLines; l++)
|
|
{
|
|
for (int i = 0; i < lineSize; i++)
|
|
{
|
|
labels[dest] = labels[l * lineSize + l * gap + i];
|
|
errors[dest] = errors[l * lineSize + l * gap + i];
|
|
rects[dest] = rects[l * lineSize + l * gap + i];
|
|
dest++;
|
|
}
|
|
}
|
|
textBoxesCount = wmsCount;
|
|
}
|
|
}
|
|
|
|
|
|
void Localize()
|
|
{
|
|
Text = Strings.Water_Meter_States;
|
|
for (int i = 0; i < textBoxesCount; i++)
|
|
{
|
|
labels[i].Text = string.Format("WM{0} {1}", i + 1, Strings.Error_pct);
|
|
}
|
|
okButton.Text = Strings.OkBtnText;
|
|
}
|
|
|
|
private void WMErrorsForm24_Load(object sender, EventArgs e)
|
|
{
|
|
Localize();
|
|
|
|
//Height = 50 + 90 * WaterMetersCount;
|
|
|
|
for (int i = 0; i < textBoxesCount; i++)
|
|
{
|
|
labels[i].Visible = errors[i].Visible = (disabled == null || i >= disabled.Length || !disabled[i]);
|
|
errors[i].Text = "---";
|
|
}
|
|
}
|
|
|
|
private void okButton_Click(object sender, EventArgs e)
|
|
{
|
|
completed = true;
|
|
Close();
|
|
}
|
|
|
|
void OnAdjustmentInProgress(object sender, UiBridge.AdjustmentInProgressEventArgs args)
|
|
{
|
|
string testName = args.TestName;
|
|
|
|
Results.Entities.TestRslt tr = TBF.BenchControl.Sequences.ProcessData.BatchRslts.GetTestRslt(testName, 0);
|
|
if (tr != null)
|
|
{
|
|
errLimLo = tr.ErrLimLo() + tr.ErrLimMargin();
|
|
errLimHi = tr.ErrLimHi() - tr.ErrLimMargin();
|
|
|
|
int count = Math.Min(waterMetersCount, TBF.BenchControl.Sequences.ProcessData.BatchRslts.WMPositionsCount);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Results.Entities.MeterTestRslt mtr = TBF.BenchControl.Sequences.ProcessData.BatchRslts.GetMeterTestRslt(testName, i, CompoundMeterId.Single);
|
|
if (mtr != null) error[i] = mtr.Error;
|
|
}
|
|
|
|
Redraw();
|
|
}
|
|
}
|
|
|
|
void Redraw()
|
|
{
|
|
for (int i = 0; i < textBoxesCount; i++)
|
|
{
|
|
errors[i].Text = error[i].ToString("F1");
|
|
Invalidate(rects[i]);
|
|
}
|
|
}
|
|
|
|
private void WMErrorsForm_Paint(object sender, PaintEventArgs e)
|
|
{
|
|
System.Drawing.Graphics graphics = this.CreateGraphics();
|
|
|
|
for (int i = 0; i < waterMetersCount; i++)
|
|
{
|
|
if ((i < MaxWMsCount) && (!disabled[i]))
|
|
{
|
|
PaintOne(graphics, rects[i], error[i], errLimLo, errLimHi);
|
|
}
|
|
}
|
|
}
|
|
|
|
void PaintOne(System.Drawing.Graphics g, Rectangle r, double value, double limLo, double limHi)
|
|
{
|
|
Rectangle rect1 = new Rectangle(r.Left, r.Top, r.Width / 3, r.Height);
|
|
Rectangle rect2 = new Rectangle(r.Left + r.Width / 3, r.Top, r.Width / 3, r.Height);
|
|
Rectangle rect3 = new Rectangle(r.Left + 2 * (r.Width / 3), r.Top, r.Width / 3, r.Height);
|
|
Rectangle rectCent = new Rectangle(r.Left + r.Width / 2 - 1, r.Top, 3, r.Height);
|
|
|
|
Brush redBrush = new SolidBrush(Color.FromName("Salmon"));
|
|
Brush greenBrush = new SolidBrush(Color.FromName("LightGreen"));
|
|
Brush centBrush = new SolidBrush(Color.FromName("Green"));
|
|
|
|
g.FillRectangle(redBrush, rect1);
|
|
g.FillRectangle(greenBrush, rect2);
|
|
g.FillRectangle(redBrush, rect3);
|
|
g.FillRectangle(centBrush, rectCent);
|
|
|
|
int x0 = r.Left + r.Width / 2;
|
|
int dx = r.Height / 3;
|
|
int x = (int)((double)r.Width * (2.0 * value - limLo - limHi) / (limHi - limLo) / 6.0f + 0.5f);
|
|
|
|
if (x0 - dx + x >= r.Left && x0 + dx + x < r.Left + r.Width)
|
|
{
|
|
Point[] triangle = new Point[3]
|
|
{
|
|
new Point(x0 - dx + x, r.Top),
|
|
new Point(x0 + dx + x, r.Top),
|
|
new Point(x0 + x, r.Top + 2 * r.Height / 3),
|
|
};
|
|
|
|
Brush blackBrush = new SolidBrush(Color.Black);
|
|
g.FillPolygon(blackBrush, triangle);
|
|
}
|
|
}
|
|
|
|
#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
|
|
}
|
|
}
|