374 lines
14 KiB
C#
374 lines
14 KiB
C#
///
|
|
/// Copyright (c) 2016-2017 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using NHibernate;
|
|
using log4net;
|
|
using Oracle.DataAccess.Client;
|
|
using Results;
|
|
using Results.Entities;
|
|
using TBF.Resources;
|
|
using TBF.BenchControl.Output.DB.SensusOracle;
|
|
|
|
namespace TBF.UI.Results
|
|
{
|
|
public enum PreviousResultsMode
|
|
{
|
|
Show,
|
|
Reload,
|
|
Fix,
|
|
}
|
|
|
|
public partial class PreviousResultsDlg : Form
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(PreviousResultsDlg));
|
|
|
|
ISession session;
|
|
int currentBatchNr;
|
|
int lastDisplayedBatchNr;
|
|
const int LinesCount = 25;
|
|
IList<Batch> batches;
|
|
PreviousResultsMode mode;
|
|
|
|
|
|
public PreviousResultsDlg(PreviousResultsMode mode)
|
|
{
|
|
InitializeComponent();
|
|
this.mode = mode;
|
|
currentBatchNr = Program.LocalSettings.BatchNr;
|
|
|
|
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 e)
|
|
{
|
|
Localize();
|
|
|
|
session = DB.CreateSession();
|
|
|
|
lastDisplayedBatchNr = currentBatchNr - 1;
|
|
DrawBatches(lastDisplayedBatchNr - LinesCount + 1, lastDisplayedBatchNr);
|
|
UpdateButtons();
|
|
}
|
|
|
|
void Localize()
|
|
{
|
|
Text = Strings.Previous_results;
|
|
closeButton.Text = TBF.Resources.Strings.CloseBtnText;
|
|
previousButton.Text = TBF.Resources.Strings.Previous;
|
|
nextButton.Text = TBF.Resources.Strings.Next;
|
|
}
|
|
|
|
|
|
|
|
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> 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)
|
|
{
|
|
#if ORACLE_DB
|
|
//System.IO.StreamWriter logger;
|
|
//string fname = "C:\\TBF\\Logs\\test.log";
|
|
//try { logger = new System.IO.StreamWriter(System.IO.File.Create(fname)); }
|
|
//catch
|
|
//{
|
|
// logger = null;
|
|
// log.ErrorFormat("Was not able to open LU log file {0}", fname);
|
|
//}
|
|
//Database.WriteLogsToDisk(logger, b);
|
|
|
|
using (ISession configSession = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config))
|
|
{
|
|
IList<Config.Entities.Component> cmpntEntities = configSession.QueryOver<Config.Entities.Component>()
|
|
.OrderBy(x => x.ItemNr).Asc
|
|
.Where(x => (x.ClassName == "Sensus-Oracle-Database"))
|
|
.List<Config.Entities.Component>();
|
|
|
|
if (cmpntEntities.Count == 0)
|
|
{
|
|
MessageBox.Show("Nepodarilo sa uložiť, neviem načítať nastavenia Oracle databázy",
|
|
Strings.Error,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
TBF.BenchControl.Output.DB.SensusOracle.DatabaseCfg cfg = TBF.BenchControl.ComponentCfgBase.CreateFromDbEntity(DatabaseCfg.Serializer, cmpntEntities[0], null)
|
|
as TBF.BenchControl.Output.DB.SensusOracle.DatabaseCfg;
|
|
|
|
MessageBox.Show(string.Format("Výsledky budú zapísané do '{0}' databázy", cfg.DBUsed),
|
|
Strings.Information,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Information);
|
|
|
|
OracleConnection conn;
|
|
switch (cfg.DBUsed)
|
|
{
|
|
default:
|
|
case DBUsed.Production:
|
|
conn = new OracleConnection("Data Source=STARA01.WORLD;User Id=deltachef;Password=deltachef;");
|
|
break;
|
|
case DBUsed.Quality:
|
|
conn = new OracleConnection("Data Source=STARA01.WORLD;User Id=deltaqual;Password=test;");
|
|
break;
|
|
case DBUsed.Test:
|
|
conn = new OracleConnection("Data Source=STARA_TEST.WORLD;User Id=deltachef;Password=deltachef;");
|
|
break;
|
|
}
|
|
|
|
if ((TBF.BenchControl.Sequences.ProcessData.OracleDB != null) &&
|
|
(TBF.BenchControl.Sequences.ProcessData.OracleDB.WriteResultsToDB(conn, b, true) == Retv.OK))
|
|
{
|
|
b.RsltsSent = true; /// Result was successfully sent
|
|
session.SaveOrUpdate(b); /// Update MySQL DB
|
|
session.Flush();
|
|
(sender as PreviousResultCtrl).Sent = true; /// Update UI
|
|
Redraw();
|
|
}
|
|
}
|
|
#elif MUNICH
|
|
string docVersion = string.Empty;
|
|
foreach (var cmp in TBF.BenchControl.StateMachine.Components)
|
|
{
|
|
if (cmp.ClassName == "Output.Printers.Munich")
|
|
{
|
|
docVersion = (cmp as TBF.BenchControl.Output.Printers.Munich.Printer).Version;
|
|
break;
|
|
}
|
|
}
|
|
foreach (var wm in b.WaterMeters)
|
|
{
|
|
new global::Results.Output.Printers.Munich.MunichPrintDocument(docVersion, wm, Config.Entities.PageOrientation.Portrait).Print();
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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 != 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
|
|
global::Results.Forms.BatchResultsDlg dlg = new global::Results.Forms.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,
|
|
};
|
|
|
|
dlg.Show();
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void DrawBatches(int from, int to)
|
|
{
|
|
batches = session.QueryOver<Batch>()
|
|
.WhereRestrictionOn(x => x.BatchNr)
|
|
.IsBetween(Math.Max(1, from))
|
|
.And(Math.Min(to, currentBatchNr - 1))
|
|
.OrderBy(x => x.BatchNr).Asc
|
|
.List<Batch>();
|
|
Redraw();
|
|
}
|
|
|
|
|
|
void Redraw()
|
|
{
|
|
SuspendLayout();
|
|
|
|
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
|
|
flowLayoutPanel1.Controls.Clear();
|
|
|
|
if (batches == null || batches.Count == 0) return;
|
|
|
|
foreach (var b in batches)
|
|
{
|
|
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));
|
|
}
|
|
|
|
ResumeLayout();
|
|
}
|
|
|
|
private void closeButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (session != null) session.Close();
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
private void previousButton_Click(object sender, EventArgs e)
|
|
{
|
|
lastDisplayedBatchNr = Math.Max(lastDisplayedBatchNr - LinesCount, LinesCount);
|
|
DrawBatches(lastDisplayedBatchNr - LinesCount + 1, lastDisplayedBatchNr);
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void nextButton_Click(object sender, EventArgs e)
|
|
{
|
|
lastDisplayedBatchNr = Math.Min(lastDisplayedBatchNr + LinesCount, currentBatchNr - 1);
|
|
DrawBatches(lastDisplayedBatchNr - LinesCount + 1, lastDisplayedBatchNr);
|
|
UpdateButtons();
|
|
}
|
|
|
|
private void UpdateButtons()
|
|
{
|
|
nextButton.Enabled = (lastDisplayedBatchNr < currentBatchNr - 1);
|
|
previousButton.Enabled = (lastDisplayedBatchNr > LinesCount);
|
|
}
|
|
}
|
|
}
|