DB.SensusOracle: saving to disk re-implemented, other minor changes and fixes. TODO: Oracle DB.

This commit is contained in:
Milan Hanajik
2016-01-06 22:37:19 +01:00
parent 1ee5724082
commit 1585066b57
5 changed files with 180 additions and 280 deletions
@@ -27,30 +27,26 @@ namespace TBF.BenchControl.DB.SensusOracle
const int Anbid_StaraTura = 4;
readonly DatabaseCfg dbCfg;
OracleConnection conn; /// Oracle database connection
string separatorStr;
OracleConnection conn; /// Oracle database connection
/// <summary>
/// Result items to print
/// </summary>
IList<Config.ResultItemSpec> resultItems;
bool combined;
IList<Results.ItemSpec> rsltItems;
/// <summary>
/// Results to print
/// </summary>
Procedure procedure;
IList<GenericDevices.IWaterMeter> waterMeters;
IList<TestResult> unsortedResults;
DateTime dateTime;
Results.Entities.Batch batch;
StreamWriter writer;
bool batchDataSaved; /// This is to save batch data (VT_PRUEFREIHE_PD) only once
string separatorStr;
public Database()
{
}
public Database() {}
public Database(DatabaseCfg cfg)
: base(cfg)
@@ -187,31 +183,22 @@ namespace TBF.BenchControl.DB.SensusOracle
/// <param name="procedure">Procedure to print the results of</param>
/// <param name="unsortedResults">Results to write into the file</param>
/// <returns>Reference to the operation</returns>
public IOperation WriteResultsOp(string TestBenchId, Procedure procedure, IList<GenericDevices.IWaterMeter> waterMeters, IList<TestResult> unsortedResults)
public IOperation WriteResultsOp(Results.Entities.Batch batch)
{
if ((procedure == null) || (unsortedResults == null) || (unsortedResults.Count < 1))
{
return null;
}
this.batch = batch;
this.procedure = procedure;
this.waterMeters = waterMeters;
this.unsortedResults = unsortedResults;
combined = (procedure.MetersKind == MetersKind.Combined);
if (combined)
if (batch.WaterMeters.Count > 0 && !batch.WaterMeters[0].Compound())
{
resultItems = Config.ResultItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Disk_CombinedWM);
rsltItems = Results.ItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Printer_SingleWM);
}
else
{
resultItems = Config.ResultItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Disk_SingleWM);
rsltItems = Results.ItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Printer_CombinedWM);
}
try
{
dateTime = DateTime.Now;
writer = new StreamWriter(System.IO.File.Create(GetFilename(dateTime)));
writer = new StreamWriter(System.IO.File.Create(GetFilename(batch.EndTime)));
}
catch
{
@@ -259,7 +246,7 @@ namespace TBF.BenchControl.DB.SensusOracle
///----------
/// Header
///----------
writer.WriteLine(unsortedResults[0].ProtocolTitle);
writer.WriteLine(batch.ProtocolTitle);
writer.WriteLine(string.Empty);
string[] leftColumn = new string[]
@@ -275,18 +262,18 @@ namespace TBF.BenchControl.DB.SensusOracle
string[] rightColumn = new string[]
{
unsortedResults[0].BatchNr.ToString(),
//dateTime.ToShortDateString() + " " + dateTime.ToShortTimeString(),
string.Format("{0}.{1}.{2} {3}:{4}", dateTime.Year.ToString("D4"),
dateTime.Month.ToString("D2"),
dateTime.Day.ToString("D2"),
dateTime.Hour.ToString("D2"),
dateTime.Minute.ToString("D2")),
procedure.Name,
User.CurrentUser.Name,
unsortedResults[0].AmbientTempAve.ToString("F1") + " °C",
unsortedResults[0].AmbientPressAve.ToString("F0") + " mbar",
unsortedResults[0].AmbientHumiAve.ToString("F0") + " %",
batch.BatchNr.ToString(),
//batch.EndTime.ToShortDateString() + " " + batch.EndTime.ToShortTimeString(),
string.Format("{0}.{1}.{2} {3}:{4}", batch.EndTime.Year.ToString("D4"),
batch.EndTime.Month.ToString("D2"),
batch.EndTime.Day.ToString("D2"),
batch.EndTime.Hour.ToString("D2"),
batch.EndTime.Minute.ToString("D2")),
batch.ProcedureName,
batch.UserName,
batch.AmbientTempAve().ToString("F1") + " °C",
batch.AmbientPressAve().ToString("F0") + " mbar",
batch.AmbientHumiAve().ToString("F0") + " %",
};
/// Determine max. left column width in characters
@@ -302,22 +289,11 @@ namespace TBF.BenchControl.DB.SensusOracle
}
writer.WriteLine(string.Empty);
/// Write text body to a disk file
if (combined)
{
for (int wmNr = 0; wmNr < Config.Data.CompoundWMsCount; wmNr++) WriteCombinedWM(wmNr + 1); /// wmNr is 0-based
}
else
{
for (int wmNr = 0; wmNr < Config.Data.WMsCount; wmNr++) /// wmNr is 0-based
{
if (!unsortedResults[0].Meters[wmNr].Disabled)
{
IList<TestResult> results = Utils.GetSortedTestResults(procedure, unsortedResults, Utils.PartNr(wmNr + 1, combined));
WriteSingleWM(wmNr, wmNr + 1, results);
}
}
}
///--------
/// Body
///--------
foreach (var wm in batch.WaterMeters) WriteWM(wm);
/// Close the file
writer.Close();
@@ -333,6 +309,141 @@ namespace TBF.BenchControl.DB.SensusOracle
}
/// <summary>
/// Write one water meter results
/// </summary>
/// <param name="wmNr">Water meter number (0-based)</param>
void WriteWM(Results.Entities.WaterMeter wm)
{
/// Determine column widths
int[] columnWidths = new int[rsltItems.Count];
int totalWidth = 0;
for (int i = 0; i < rsltItems.Count; i++)
{
columnWidths[i] = ElSpaces(rsltItems[i].ClmnHeaderText).Length;
foreach (var mtr in wm.MeterTestRslts)
{
string itemText;
if (!wm.Compound())
{
/// Single water meter
itemText = rsltItems[i].Print(mtr);
}
else if (mtr.CompoundMeterId == (byte)CompoundMeterId.Compound)
{
Results.Entities.MeterTestRslt mainMtr = wm.GetMeterTestRslt(mtr.Name(), CompoundMeterId.CompoundMain);
Results.Entities.MeterTestRslt auxMtr = wm.GetMeterTestRslt(mtr.Name(), CompoundMeterId.CompoundAux);
itemText = rsltItems[i].PrintCombined(mainMtr, auxMtr, mtr);
}
else
{
continue;
}
/// Strip color information
string[] texts = itemText.Split(new char[] { '|' });
if (texts.Length == 2) { itemText = texts[0]; }
int len = ElSpaces(itemText).Length;
if (len > columnWidths[i]) columnWidths[i] = len;
}
totalWidth += columnWidths[i];
}
totalWidth += 3 * (rsltItems.Count - 1);
if (totalWidth < 0) totalWidth = 0;
/// Write water meter number and s/n
writer.Write(string.Format("Water meter {0}", wm.WMPosition));
if (!string.IsNullOrEmpty(wm.SerialNr)) writer.Write(string.Format(" s/n: {0}", wm.SerialNr));
writer.WriteLine(string.Empty);
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line above the header
/// Write column headers
for (int i = 0; i < rsltItems.Count; i++)
{
writer.Write(ElSpaces(rsltItems[i].ClmnHeaderText));
if (i < rsltItems.Count - 1)
{
if (!dbCfg.EliminateSpaces)
{
writer.Write(new string(' ', columnWidths[i] - rsltItems[i].ClmnHeaderText.Length + 3));
}
writer.Write(separatorStr);
}
else
{
writer.WriteLine(string.Empty);
}
}
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line between the header and the body
/// Write table data
foreach (var mtr in wm.MeterTestRslts)
{
if (mtr != null)
{
for (int i = 0; i < rsltItems.Count; i++)
{
string itemText;
///
/// Fetch an item
///
if (!wm.Compound())
{
/// Single water meter
itemText = rsltItems[i].Print(mtr);
}
else if (mtr.CompoundMeterId == (byte)CompoundMeterId.Compound)
{
Results.Entities.MeterTestRslt mainMtr = wm.GetMeterTestRslt(mtr.Name(), CompoundMeterId.CompoundMain);
Results.Entities.MeterTestRslt auxMtr = wm.GetMeterTestRslt(mtr.Name(), CompoundMeterId.CompoundAux);
itemText = rsltItems[i].PrintCombined(mainMtr, auxMtr, mtr);
}
else
{
continue;
}
///
/// Print the item
///
string[] texts = itemText.Split(new char[] { '|' });
if (texts.Length == 2) { itemText = texts[0]; } /// Strip color information
writer.Write(ElSpaces(itemText));
if (i < rsltItems.Count - 1)
{
if (!dbCfg.EliminateSpaces)
{
writer.Write(new string(' ', columnWidths[i] - itemText.Length + 3));
}
writer.Write(separatorStr);
}
else
{
writer.WriteLine(string.Empty);
}
}
}
}
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line below the body
writer.WriteLine(string.Empty);
}
Retv WriteResultsToDatabase()
{
batchDataSaved = false;
@@ -637,210 +748,5 @@ namespace TBF.BenchControl.DB.SensusOracle
transaction.Rollback();
}
}
/// <summary>
/// Write one water meter results into a file
/// </summary>
/// <param name="wmNr">Water meter number (0-based)</param>
void WriteSingleWM(int wmNr, int printedWMNr, IList<TestResult> results)
{
/// Determine column widths
int[] columnWidths = new int[resultItems.Count];
int totalWidth = 0;
for (int i = 0; i < resultItems.Count; i++)
{
columnWidths[i] = ElSpaces(resultItems[i].ClmnHeaderText).Length;
foreach (var tr in results)
{
if (tr != null)
{
string text = resultItems[i].Print(tr.Meters[wmNr]);
/// Get rid of the color information
string[] texts = text.Split(new char[] { '|' });
if (texts.Length == 2) { text = texts[0]; }
int len = ElSpaces(text).Length;
if (len > columnWidths[i]) columnWidths[i] = len;
}
}
totalWidth += columnWidths[i];
}
totalWidth += 3 * (resultItems.Count - 1);
/// Write water meter number and s/n
writer.Write(string.Format("Water meter {0}", printedWMNr));
if (!string.IsNullOrEmpty(unsortedResults[0].Meters[wmNr].SerialNr))
{
writer.Write(string.Format(" s/n: {0}", unsortedResults[0].Meters[wmNr].SerialNr));
}
writer.WriteLine(string.Empty);
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line above the header
/// Write column headers
for (int i = 0; i < resultItems.Count; i++)
{
writer.Write(ElSpaces(resultItems[i].ClmnHeaderText));
if (i < resultItems.Count - 1)
{
if (!dbCfg.EliminateSpaces)
{
writer.Write(new string(' ', columnWidths[i] - resultItems[i].ClmnHeaderText.Length + 3));
}
writer.Write(separatorStr);
}
else
{
writer.WriteLine(string.Empty);
}
}
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line between the header and the body
/// Write table data
foreach (var tr in results)
{
if (tr != null)
{
for (int i = 0; i < resultItems.Count; i++)
{
string text = resultItems[i].Print(tr.Meters[wmNr]);
/// Get rid of the color information
string[] texts = text.Split(new char[] { '|' });
if (texts.Length == 2) { text = texts[0]; }
writer.Write(ElSpaces(text));
if (i < resultItems.Count - 1)
{
if (!dbCfg.EliminateSpaces)
{
writer.Write(new string(' ', columnWidths[i] - text.Length + 3));
}
writer.Write(separatorStr);
}
else
{
writer.WriteLine(string.Empty);
}
}
}
}
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line below the body
writer.WriteLine(string.Empty);
}
/// <summary>
/// Write one combined water meter results
/// </summary>
/// <param name="wmNr1">Combined water meter number (1-based)</param>
void WriteCombinedWM(int wmNr1)
{
IList<TestResult> results = Utils.GetSortedTestResults(procedure, unsortedResults, Utils.PartNr(wmNr1, combined));
/// Determin column widths
int[] columnWidths = new int[resultItems.Count];
int totalWidth = 0;
for (int i = 0; i < resultItems.Count; i++)
{
columnWidths[i] = ElSpaces(resultItems[i].ClmnHeaderText).Length;
foreach (var tr in results)
{
if (tr != null)
{
string text = resultItems[i].PrintCombined(tr.Meters[2 * wmNr1 - 2], tr.Meters[2 * wmNr1 - 1], tr.CombinedMeters[wmNr1 - 1]);
int len = ElSpaces(text).Length;
if (len > columnWidths[i]) columnWidths[i] = len;
}
}
totalWidth += columnWidths[i];
}
totalWidth += 3 * (resultItems.Count - 1);
/// Write water meter number and two s/n-s
writer.Write(string.Format("Water meter {0}", wmNr1));
if (!string.IsNullOrEmpty(unsortedResults[0].Meters[2 * wmNr1 - 2].SerialNr))
{
writer.Write(string.Format(", Main s/n: {0}", unsortedResults[0].Meters[2 * wmNr1 - 2].SerialNr));
}
if (!string.IsNullOrEmpty(unsortedResults[0].Meters[2 * wmNr1 - 1].SerialNr))
{
writer.Write(string.Format(", Aux. s/n:{0}", unsortedResults[0].Meters[2 * wmNr1 - 1].SerialNr));
}
writer.WriteLine(string.Empty);
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line above the header
for (int i = 0; i < resultItems.Count; i++)
{
writer.Write(ElSpaces(resultItems[i].ClmnHeaderText));
if (i < resultItems.Count - 1)
{
if (!dbCfg.EliminateSpaces)
{
writer.Write(new string(' ', columnWidths[i] - resultItems[i].ClmnHeaderText.Length + 3));
}
writer.Write(separatorStr);
}
else
{
writer.WriteLine(string.Empty);
}
}
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line between the header and the body
foreach (var tr in results)
{
if (tr != null)
{
for (int i = 0; i < resultItems.Count; i++)
{
string text = resultItems[i].PrintCombined(tr.Meters[2 * wmNr1 - 2], tr.Meters[2 * wmNr1 - 1], tr.CombinedMeters[wmNr1 - 1]);
/// Get rid of the color information
string[] texts = text.Split(new char[] { '|' });
if (texts.Length == 2) { text = texts[0]; }
writer.Write(ElSpaces(text));
if (i < resultItems.Count - 1)
{
if (!dbCfg.EliminateSpaces)
{
writer.Write(new string(' ', columnWidths[i] - text.Length + 3));
}
writer.Write(separatorStr);
}
else
{
writer.WriteLine(string.Empty);
}
}
}
}
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line below the body
writer.WriteLine(string.Empty);
}
}
}
@@ -188,7 +188,7 @@ namespace TBF.BenchControl.DataEntry.Standard24
{
purchaseOrder = dlg.PurchaseOrder;
for (int i = 0; i < Math.Min(dlg.WaterMetersCount, waterMeters.Count); i++)
for (int i = 0; i < Math.Min(dlg.WaterMetersCount, waterMeters.Length); i++)
{
if (waterMeters[i] != null)
{
@@ -221,13 +221,11 @@ namespace TBF.BenchControl.DataEntry.iPerl
/// </summary>
/// <param name="waterMeters">Watermetes with information entered in the forms</param>
/// <param name="testResults">Test results to be updated with the information</param>
public void UpdateTestResults(Results.BatchResults results)
public void UpdateTestResults(Results.BatchResults batchResults)
{
foreach (var testRslt in testResults)
foreach (var wm in batchResults.WaterMeters)
{
if (testRslt.MetersKind != MetersKind.Single) continue;
testRslt.PurchaseOrder = PurchaseOrder;
if (wm != null) wm.PurchaseOrder = PurchaseOrder;
}
}
}
@@ -29,13 +29,10 @@ namespace TBF.BenchControl.ResultsWriters.Basic
/// </summary>
Results.Entities.Batch batch;
DateTime dateTime;
StreamWriter writer;
public Writer()
{
}
public Writer() {}
public Writer(WriterCfg cfg)
: base(cfg)
@@ -151,8 +148,7 @@ namespace TBF.BenchControl.ResultsWriters.Basic
try
{
dateTime = batch.EndTime;
writer = new StreamWriter(System.IO.File.Create(GetFilename(dateTime)));
writer = new StreamWriter(System.IO.File.Create(GetFilename(batch.EndTime)));
}
catch
{
@@ -187,12 +183,12 @@ namespace TBF.BenchControl.ResultsWriters.Basic
string[] rightColumn = new string[]
{
batch.BatchNr.ToString(),
//dateTime.ToShortDateString() + " " + dateTime.ToShortTimeString(),
string.Format("{0}.{1}.{2} {3}:{4}", dateTime.Year.ToString("D4"),
dateTime.Month.ToString("D2"),
dateTime.Day.ToString("D2"),
dateTime.Hour.ToString("D2"),
dateTime.Minute.ToString("D2")),
//batch.EndTime.ToShortDateString() + " " + batch.EndTime.ToShortTimeString(),
string.Format("{0}.{1}.{2} {3}:{4}", batch.EndTime.Year.ToString("D4"),
batch.EndTime.Month.ToString("D2"),
batch.EndTime.Day.ToString("D2"),
batch.EndTime.Hour.ToString("D2"),
batch.EndTime.Minute.ToString("D2")),
batch.ProcedureName,
batch.UserName,
batch.AmbientTempAve().ToString("F1") + " °C",
@@ -349,7 +345,6 @@ namespace TBF.BenchControl.ResultsWriters.Basic
}
}
writer.WriteLine(new String('-', totalWidth)); /// Horizontal line below the body
writer.WriteLine(string.Empty);
@@ -620,7 +620,7 @@ namespace TBF.BenchControl.Sequences
{
/// Open the modeless form for the end of the cycle
State.Create("MainSeq : Enter end data")
.AddOperation((dataEntryCmpnt as GenericDevices.IHasCycleEndForm).ShowCycleEndFormOp(WaterMeters))
.AddOperation((dataEntryCmpnt as GenericDevices.IHasCycleEndForm).ShowCycleEndFormOp())
.AddOperation(checkUiOp)
.EnterState();
do
@@ -639,6 +639,7 @@ namespace TBF.BenchControl.Sequences
dataEntryCmpnt.UpdateTestResults(BatchRslts);
}
ProcessData.BatchRslts.Batch.EndTime = DateTime.Now;
ProcessData.BatchRslts.AddWaterMetersToBatch();
//------------------------------------------------------