483 lines
18 KiB
C#
483 lines
18 KiB
C#
///
|
|
/// Copyright (c) 2016-2023 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using NHibernate;
|
|
using log4net;
|
|
#if MANAGED
|
|
using Oracle.ManagedDataAccess.Client;
|
|
#else
|
|
using Oracle.DataAccess.Client;
|
|
#endif
|
|
using Common;
|
|
using Results;
|
|
using Results.Entities;
|
|
using TBF.Resources;
|
|
using TBF.Rig.Generic;
|
|
using TBF.Rig.GenericDevices;
|
|
using TBF.Rig.Output.DB.SensusOracle;
|
|
using TBF.Rig.Sequences;
|
|
|
|
namespace TBF.UI.ResultsMI
|
|
{
|
|
public enum PreviousResultsMode
|
|
{
|
|
Show,
|
|
Reload,
|
|
Fix,
|
|
}
|
|
|
|
public partial class PreviousResultsDlg : Form
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(PreviousResultsDlg));
|
|
|
|
readonly PreviousResultsMode mode;
|
|
readonly int currentBatchNr;
|
|
|
|
ISession session;
|
|
IList<Batch> batches; /// A list of batches selected from all batches using criteria entered in UI
|
|
IList<string> serialNrs; /// Display only watermeters with these serial numbers (null = display all)
|
|
int lastDisplayedIx; /// Index of the last displayed batch from the list 'batches'
|
|
const int LinesCount = 25; /// Number of batches displayed on one screen
|
|
|
|
|
|
public PreviousResultsDlg(PreviousResultsMode mode)
|
|
{
|
|
InitializeComponent();
|
|
|
|
this.mode = mode;
|
|
currentBatchNr = Program.LocalSettings.BatchNr;
|
|
|
|
PrintHandler += delegate(object sndr, PreviousResultIdEventArgs args)
|
|
{
|
|
if (InvokeRequired) { Invoke(new EventHandler<PreviousResultIdEventArgs>(DoPrint), sndr, args); }
|
|
else DoPrint(sndr, args);
|
|
};
|
|
|
|
SendAgainHandler += delegate(object sndr, PreviousResultIdEventArgs args)
|
|
{
|
|
if (InvokeRequired) { Invoke(new EventHandler<PreviousResultIdEventArgs>(DoSendAgain), sndr, args); }
|
|
else DoSendAgain(sndr, args);
|
|
};
|
|
|
|
ShowResultsHandler += delegate(object sndr, PreviousResultIdEventArgs args)
|
|
{
|
|
if (InvokeRequired) { Invoke(new EventHandler<PreviousResultIdEventArgs>(DoShowResults), sndr, args); }
|
|
else DoShowResults(sndr, args);
|
|
};
|
|
}
|
|
|
|
public PreviousResultsDlg()
|
|
: this(PreviousResultsMode.Show)
|
|
{
|
|
}
|
|
|
|
private void PreviousResultsDlg_Load(object sender, EventArgs args)
|
|
{
|
|
Localize();
|
|
|
|
fromDateTimePicker.MinDate = Constants.MinDate;
|
|
fromDateTimePicker.MaxDate = DateTime.Today;
|
|
fromDateTimePicker.Value = Constants.MinDate;
|
|
fromDateTimePicker.CustomFormat = Constants.DateFormat;
|
|
fromDateTimePicker.ShowUpDown = true;
|
|
|
|
toDateTimePicker.MinDate = Constants.MinDate;
|
|
toDateTimePicker.MaxDate = DateTime.Today;
|
|
toDateTimePicker.Value = DateTime.Today;
|
|
toDateTimePicker.CustomFormat = Constants.DateFormat;
|
|
toDateTimePicker.ShowUpDown = true;
|
|
|
|
try { session = TBF.DB.ResultsDBSessionFactory.OpenSession(); }
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show(Strings.Cannot_connect_to_the_database + Environment.NewLine + e.Message,
|
|
Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
}
|
|
|
|
batches = GetFilteredBatches(out lastDisplayedIx, out serialNrs);
|
|
UpdateButtons();
|
|
Redraw();
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
Text = Strings.Previous_results;
|
|
closeButton.Text = TBF.Resources.Strings.CloseBtnText;
|
|
previousButton.Text = TBF.Resources.Strings.Previous;
|
|
nextButton.Text = TBF.Resources.Strings.Next;
|
|
fromLabel.Text = Strings.from;
|
|
toLabel.Text = Strings.to;
|
|
procedureLabel.Text = Strings.Procedure;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a list of batches selected using criteria entered in UI
|
|
/// </summary>
|
|
/// <returns>List of batches</returns>
|
|
IList<Batch> GetFilteredBatches(out int lastIx, out IList<string> serialNrs)
|
|
{
|
|
IList<Batch> rslt;
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(procedureTextBox.Text) && string.IsNullOrEmpty(snTextBox.Text))
|
|
{
|
|
rslt = session.QueryOver<Batch>()
|
|
.Where(x => (x.EndTime >= fromDateTimePicker.Value))
|
|
.And(x => (x.StartTime < toDateTimePicker.Value.AddDays(1)))
|
|
.OrderBy(x => x.BatchNr).Asc
|
|
.List();
|
|
}
|
|
else if (string.IsNullOrEmpty(snTextBox.Text))
|
|
{
|
|
rslt = session.QueryOver<Batch>()
|
|
.Where(x => (x.EndTime >= fromDateTimePicker.Value))
|
|
.And(x => (x.StartTime < toDateTimePicker.Value.AddDays(1)))
|
|
.And(x => (x.ProcedureName == procedureTextBox.Text))
|
|
.OrderBy(x => x.BatchNr).Asc
|
|
.List();
|
|
}
|
|
else if (string.IsNullOrEmpty(procedureTextBox.Text))
|
|
{
|
|
rslt = session.QueryOver<Batch>()
|
|
.Where(x => (x.EndTime >= fromDateTimePicker.Value))
|
|
.And(x => (x.StartTime < toDateTimePicker.Value.AddDays(1)))
|
|
.OrderBy(x => x.BatchNr).Asc
|
|
.JoinQueryOver<WaterMeter>(b => b.WaterMeters)
|
|
.Where(wm => (wm.SerialNr == snTextBox.Text))
|
|
.List<Batch>();
|
|
}
|
|
else
|
|
{
|
|
rslt = session.QueryOver<Batch>()
|
|
.Where(x => (x.EndTime >= fromDateTimePicker.Value))
|
|
.And(x => (x.StartTime < toDateTimePicker.Value.AddDays(1)))
|
|
.And(x => (x.ProcedureName == procedureTextBox.Text))
|
|
.OrderBy(x => x.BatchNr).Asc
|
|
.JoinQueryOver<WaterMeter>(b => b.WaterMeters)
|
|
.Where(wm => (wm.SerialNr == snTextBox.Text))
|
|
.List<Batch>();
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
rslt = new List<Batch>();
|
|
log.ErrorFormat("Reading batches from Results DB failed: {0}", exc.Message);
|
|
}
|
|
|
|
serialNrs = string.IsNullOrEmpty(snTextBox.Text) ? null : new List<string> { snTextBox.Text };
|
|
lastIx = rslt.Count - 1;
|
|
|
|
return rslt;
|
|
}
|
|
|
|
public void OnReload(object sender, PreviousResultIdEventArgs data)
|
|
{
|
|
foreach (var batch in batches)
|
|
{
|
|
if (batch.BatchNr == data.BatchNr)
|
|
{
|
|
TBF.UiBridge.Bridge.BatchNr = batch.BatchNr;
|
|
|
|
Program.MainWnd.ReloadProcedureComboBoxItems(batch.ProcedureName);
|
|
TBF.UiBridge.Bridge.Ui2Bench(TBF.UiBridge.UI2BenchCmd.ReloadBatch);
|
|
break;
|
|
}
|
|
}
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
|
|
public void OnReloadAndFix(object sender, PreviousResultIdEventArgs data)
|
|
{
|
|
foreach (var batch in batches)
|
|
{
|
|
if (batch.BatchNr == data.BatchNr)
|
|
{
|
|
TBF.UiBridge.Bridge.BatchNr = batch.BatchNr;
|
|
|
|
Program.MainWnd.ReloadProcedureComboBoxItems(batch.ProcedureName);
|
|
TBF.UiBridge.Bridge.Ui2Bench(TBF.UiBridge.UI2BenchCmd.ReloadAndFixBatch);
|
|
break;
|
|
}
|
|
}
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
|
|
|
|
public event EventHandler<PreviousResultIdEventArgs> PrintHandler;
|
|
|
|
/// <summary>
|
|
/// Called from PreviousResultCtrl when 'Print' button pressed.
|
|
/// </summary>
|
|
public void OnPrint(object sender, PreviousResultIdEventArgs data)
|
|
{
|
|
if (PrintHandler == null) return;
|
|
try
|
|
{
|
|
PrintHandler(sender, data);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.Error("PrintHandler(...) failed", e);
|
|
}
|
|
}
|
|
|
|
public void DoPrint(object sender, PreviousResultIdEventArgs data)
|
|
{
|
|
foreach (var b in batches)
|
|
{
|
|
if (b.BatchNr == data.BatchNr)
|
|
{
|
|
IList<IResultsPrinter> printers = new List<IResultsPrinter>();
|
|
foreach (var c in TBF.Rig.StateMachine.Components)
|
|
{
|
|
if (c is IResultsPrinter) printers.Add(c as IResultsPrinter);
|
|
}
|
|
var dlg = new PrintResultsSelectionDlg(printers);
|
|
if (dlg.ShowDialog() != DialogResult.OK) return;
|
|
|
|
TBF.Rig.IOperation printResultsOp = dlg.SelectedPrinter.ProcessResultsOp(b);
|
|
printResultsOp.Start();
|
|
TBF.Rig.Event e;
|
|
do { e = printResultsOp.Run(); }
|
|
while (e != TBF.Rig.Event.ResultsPrinted && e != TBF.Rig.Event.ErrorProcessingResults);
|
|
printResultsOp.Stop();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public event EventHandler<PreviousResultIdEventArgs> SendAgainHandler;
|
|
|
|
/// <summary>
|
|
/// Called from PreviousResultCtrl when 'Send again' button pressed.
|
|
/// </summary>
|
|
public void OnSendAgain(object sender, PreviousResultIdEventArgs data)
|
|
{
|
|
if (SendAgainHandler == null) return;
|
|
try
|
|
{
|
|
SendAgainHandler(sender, data);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.Error("SendAgainHandler(...) failed", e);
|
|
}
|
|
}
|
|
|
|
public void DoSendAgain(object sender, PreviousResultIdEventArgs data)
|
|
{
|
|
foreach (var b in batches)
|
|
{
|
|
if (b.BatchNr == data.BatchNr)
|
|
{
|
|
var dlg = new SendAgainDBAndWMSelectionForm();
|
|
if (dlg.ShowDialog() != DialogResult.OK) return;
|
|
|
|
bool isSuccessful = dlg.SaveToOracle || dlg.SaveToProductionTracing; /// At least one must be selected to be successful
|
|
#if ORACLE_DB
|
|
if (dlg.SaveToOracle)
|
|
{
|
|
if (ProcessData.OracleDB != null)
|
|
{
|
|
OracleConnection conn;
|
|
switch (dlg.SelectedDB)
|
|
{
|
|
default:
|
|
case DBUsed.Production: conn = ProcessData.OracleDB.ProductionDB; break;
|
|
case DBUsed.Quality: conn = ProcessData.OracleDB.QualityDB; break;
|
|
case DBUsed.Test: conn = ProcessData.OracleDB.TestDB; break;
|
|
}
|
|
|
|
isSuccessful = isSuccessful && (ProcessData.OracleDB.WriteResultsToDB(conn, b, dlg.SelectedWM, true) == Retv.OK);
|
|
}
|
|
else
|
|
{
|
|
isSuccessful = false;
|
|
MessageBox.Show("No Oracle database");
|
|
}
|
|
}
|
|
#endif
|
|
|
|
if (dlg.SaveToProductionTracing)
|
|
{
|
|
if (ProcessData.TracingDB != null)
|
|
{
|
|
isSuccessful = isSuccessful && (ProcessData.TracingDB.SaveTracingRecords(b) == Rig.Output.DB.ProductionTracing.Retv.OK);
|
|
}
|
|
else
|
|
{
|
|
isSuccessful = false;
|
|
MessageBox.Show("No production tracing database");
|
|
}
|
|
}
|
|
|
|
if (isSuccessful)
|
|
{
|
|
b.RsltsSent = true; /// Result was successfully sent
|
|
session.SaveOrUpdate(b); /// Update MySQL DB
|
|
session.Flush();
|
|
(sender as PreviousResultCtrl).Sent = true; /// Update UI
|
|
Redraw();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public event EventHandler<PreviousResultIdEventArgs> ShowResultsHandler;
|
|
|
|
/// <summary>
|
|
/// Called from PreviousResultCtrl when 'Show' button pressed.
|
|
/// </summary>
|
|
public void OnShowResults(object sender, PreviousResultIdEventArgs data)
|
|
{
|
|
if (ShowResultsHandler == null) return;
|
|
try { ShowResultsHandler(sender, data); }
|
|
catch (Exception e) { log.Error("ShowResultsHandler(...) failed", e); }
|
|
}
|
|
|
|
public void DoShowResults(object sender, PreviousResultIdEventArgs data)
|
|
{
|
|
foreach (var b in batches)
|
|
{
|
|
if (b.BatchNr == data.BatchNr)
|
|
{
|
|
if (b.WaterMeters == null || b.WaterMeters.Count == 0)
|
|
{
|
|
return; /// No water meters in the selected batch
|
|
}
|
|
|
|
/// Prepare result items
|
|
IList<WMeterRsltItemSpec> items = null;
|
|
foreach (var wm in b.WaterMeters)
|
|
{
|
|
if (!wm.Disabled && wm.WaterMeterData != null)
|
|
{
|
|
if (wm.WaterMeterData.Compound)
|
|
{
|
|
items = WMeterRsltItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Screen_CombinedWM);
|
|
}
|
|
else if (wm.WaterMeterData.HeatMeter)
|
|
{
|
|
items = WMeterRsltItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Screen_HeatM);
|
|
}
|
|
else
|
|
{
|
|
items = WMeterRsltItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Screen_SingleWM);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
if (items == null) items = new List<WMeterRsltItemSpec>();
|
|
|
|
/// Show dialog with results
|
|
BatchResultsDlg dlg = new BatchResultsDlg
|
|
{
|
|
Text = string.Format(Strings.Results_of_batch_0, b.BatchNr),
|
|
Results = BatchResults.FromBatch(b),
|
|
Items = items,
|
|
PopupResultsLeft = Program.LocalSettings.PopupResultsLeft,
|
|
PopupResultsTop = Program.LocalSettings.PopupResultsTop,
|
|
PopupResultsWidth = Program.LocalSettings.PopupResultsWidth,
|
|
PopupResultsHeight = Program.LocalSettings.PopupResultsHeight,
|
|
MetersArrangement = Program.LocalSettings.ResultsConfigMeters,
|
|
TestsArrangement = Program.LocalSettings.ResultsConfigTests,
|
|
NrMetersInOneGroup = Math.Max(1, Program.LocalSettings.ResultsConfigMetersInOneGroup),
|
|
ShowDisabledPositions = Program.LocalSettings.ShowDisabledPositions,
|
|
MinWidth = Program.LocalSettings.ResultsConfigMinWidth,
|
|
MinHeight = Program.LocalSettings.ResultsConfigMinHeight,
|
|
RsltsClmnWidths = Program.LocalSettings.RsltsClmnWidths,
|
|
SerialNrs = data.SerialNrs,
|
|
};
|
|
|
|
dlg.Show();
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Redraws list of batches.
|
|
/// Uses member variables 'batches' and 'lastDisplayedIx'
|
|
/// </summary>
|
|
void Redraw()
|
|
{
|
|
SuspendLayout();
|
|
|
|
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
|
|
flowLayoutPanel1.Controls.Clear();
|
|
|
|
if (batches == null || batches.Count == 0) return;
|
|
|
|
for (int i = Math.Max(0, lastDisplayedIx - LinesCount + 1); i <= lastDisplayedIx; i++)
|
|
{
|
|
var b = batches[i];
|
|
int passedCount = 0;
|
|
int failedCount = 0;
|
|
foreach (var wm in b.WaterMeters)
|
|
{
|
|
if ((wm != null) && wm.Passed) { passedCount++; } else { failedCount++; }
|
|
}
|
|
|
|
if (passedCount + failedCount == 0) continue;
|
|
|
|
flowLayoutPanel1.Controls.Add(new PreviousResultCtrl(this, b.BatchNr, b.StartTime, b.EndTime, b.ProgramVersion,
|
|
b.ProcedureName, b.WaterMeters[0].PurchaseOrder,
|
|
passedCount, failedCount, b.RsltsSent, mode, serialNrs));
|
|
}
|
|
|
|
ResumeLayout();
|
|
}
|
|
|
|
private void closeButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
private void previousButton_Click(object sender, EventArgs e)
|
|
{
|
|
lastDisplayedIx = Math.Max(lastDisplayedIx - LinesCount, LinesCount - 1);
|
|
Redraw();
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void nextButton_Click(object sender, EventArgs e)
|
|
{
|
|
lastDisplayedIx = Math.Min(lastDisplayedIx + LinesCount, batches.Count - 1);
|
|
Redraw();
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void UpdateButtons()
|
|
{
|
|
nextButton.Enabled = (lastDisplayedIx < batches.Count - 1);
|
|
previousButton.Enabled = (lastDisplayedIx > LinesCount - 1);
|
|
}
|
|
|
|
private void reloadButton_Click(object sender, EventArgs e)
|
|
{
|
|
batches = GetFilteredBatches(out lastDisplayedIx, out serialNrs);
|
|
UpdateButtons();
|
|
Redraw();
|
|
}
|
|
|
|
private void PreviousResultsDlg_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
if (Users.CurrentUser.Restore(this)) Program.MainWnd.UpdateUser();
|
|
if (session != null && session.IsOpen) session.Close();
|
|
}
|
|
}
|
|
}
|