ProtocolTitle added to 'Procedure' and 'TestResult'. ResultsWriter-Basic rewritten

This commit is contained in:
Milan Hanajik
2015-01-25 23:43:17 +01:00
parent 5cf5fe5eba
commit c3f64215f1
13 changed files with 456 additions and 139 deletions
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO;
using log4net;
using TBF.BenchControl;
using TBF.Resources;
namespace TBF.BenchControl.ResultsWriters.Basic
{
@@ -23,7 +24,7 @@ namespace TBF.BenchControl.ResultsWriters.Basic
/// Results to print
/// </summary>
IList<Entities.TestResult> results;
DateTime now;
DateTime dateTime;
StreamWriter writer;
public Writer(WriterCfg cfg)
@@ -33,6 +34,50 @@ namespace TBF.BenchControl.ResultsWriters.Basic
log.Debug(this.ToString());
}
/// <summary>
/// Returns a file name derived from a DateTime structure.
/// Creates directories on this path as a side effect.
/// </summary>
/// <param name="time">Date and time</param>
/// <returns>File name</returns>
string GetFilename(DateTime time)
{
Directory.CreateDirectory(writerCfg.DestinationPath);
string directory = string.Format("{0}{1}\\", writerCfg.DestinationPath, time.Year.ToString());
Directory.CreateDirectory(directory);
string monthStr /* = now.Month.ToString("D2")*/;
switch (time.Month)
{
default:
case 1: monthStr = "January"; break;
case 2: monthStr = "February"; break;
case 3: monthStr = "March"; break;
case 4: monthStr = "April"; break;
case 5: monthStr = "May"; break;
case 6: monthStr = "June"; break;
case 7: monthStr = "July"; break;
case 8: monthStr = "August"; break;
case 9: monthStr = "September"; break;
case 10: monthStr = "October"; break;
case 11: monthStr = "November"; break;
case 12: monthStr = "December"; break;
}
directory = string.Format("{0}{1}\\", directory, monthStr);
Directory.CreateDirectory(directory);
if (writerCfg.DayFolders)
{
directory = string.Format("{0}{1}\\", directory, time.Day.ToString("D2"));
Directory.CreateDirectory(directory);
}
return string.Format("{0}{1}{2}{3}-{4}{5}.txt", directory, (time.Year % 100).ToString("D2"),
time.Month.ToString("D2"), time.Day.ToString("D2"), time.Hour.ToString("D2"), time.Minute.ToString("D2"));
}
/// <summary>
/// Writes the test cycle results into a file, Events: Event.ResultsWritten
/// </summary>
@@ -60,15 +105,10 @@ namespace TBF.BenchControl.ResultsWriters.Basic
}
this.results = results;
now = DateTime.Now;
try
{
string dayDirectory = string.Format("{0}Results\\{1}-{2}-{3}", Program.HomeDir,
now.Year, now.Month.ToString("D2"), now.Day.ToString("D2"));
string fileName = string.Format("{0}\\{1}-{2}.txt", dayDirectory,
now.Hour.ToString("D2"), now.Minute.ToString("D2"));
Directory.CreateDirectory(dayDirectory);
writer = new StreamWriter(System.IO.File.Create(fileName));
dateTime = DateTime.Now;
writer = new StreamWriter(System.IO.File.Create(GetFilename(dateTime)));
}
catch
{
@@ -83,54 +123,182 @@ namespace TBF.BenchControl.ResultsWriters.Basic
{
if (writer == null) return;
writer.WriteLine("Staatlich anerkante Prüfstelle für Meßgeräte für Wasser WB2");
writer.WriteLine("");
writer.WriteLine(string.Format(" PROTOKOLL Nummer {0}", 0));
writer.WriteLine("");
writer.WriteLine("Ver. 3/13");
writer.WriteLine(string.Format("Datum / Zeit : {0}.{1}.{2} {3}:{4} Seite : 1/1",
now.Day.ToString("D2"),
now.Month.ToString("D2"),
now.Year.ToString("D4"),
now.Hour.ToString("D2"),
now.Minute.ToString("D2")));
writer.WriteLine(string.Format("Wasserzähler Type : {0}", results[0].ProcedureName));
writer.WriteLine("Gerät Nr. : 1");
writer.WriteLine(string.Format("Benutzer : {0}", Entities.User.CurrentUser.Name));
writer.Write("Impulsrate : ");
for (int i = 0; i < Program.WMsCount; i++)
{
//writer.Write(results[0].
}
writer.WriteLine("Bereich : 22,9 °C 947 mbar 43 %");
writer.WriteLine("");
foreach (var item in resultItems) writer.Write(item.ClmnHeaderText);
///----------
/// Header
///----------
writer.WriteLine(results[0].ProtocolTitle);
writer.WriteLine(string.Empty);
writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------");
string[] leftColumn = new string[]
{
"Cycle number: ",
"Date and time: ",
"Procedure:",
Strings.User_,
"Ambient temperature: ",
"Ambient pressure: ",
"Ambient humidity: ",
};
string[] rightColumn = new string[]
{
results[0].CycleNr.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")),
results[0].ProcedureName,
Entities.User.CurrentUser.Name,
results[0].AmbientTempAve.ToString("F1") + " °C",
results[0].AmbientPressAve.ToString("F0") + " mbar",
results[0].AmbientHumiAve.ToString("F0") + " %",
};
/// Determine max. left column width in characters
int maxLen = 0;
foreach (var s in leftColumn) if (s.Length > maxLen) maxLen = s.Length;
/// Write aligned columns
for (int i = 0; i < Math.Min(leftColumn.Length, rightColumn.Length); i++)
{
writer.Write(leftColumn[i]);
writer.Write(new string(' ', maxLen - leftColumn[i].Length + 3));
writer.WriteLine(rightColumn[i]);
}
writer.WriteLine(string.Empty);
writer.WriteLine(string.Empty);
///--------
/// Body
///--------
int nrWMs = (combined ? Program.WMsCount / 2 : Program.WMsCount);
if (combined)
{
for (int wmNr = 0; wmNr < Program.WMsCount / 2; wmNr++) WriteCombinedWM(wmNr);
}
else
{
for (int wmNr = 0; wmNr < Program.WMsCount; wmNr++) WriteSingleWM(wmNr);
}
}
/// <summary>
/// Write one water meter results
/// </summary>
/// <param name="wmNr">Water meter number</param>
void WriteSingleWM(int wmNr)
{
/// Determin column widths
int[] columnWidths = new int[resultItems.Count];
int totalWidth = 0;
for (int i = 0; i < resultItems.Count; i++)
{
columnWidths[i] = resultItems[i].ClmnHeaderText.Length;
foreach (var tr in results)
{
int len = resultItems[i].Print(tr.Meters[wmNr]).Length;
if (len > columnWidths[i]) columnWidths[i] = len;
}
totalWidth += columnWidths[i];
}
totalWidth += 3 * (resultItems.Count - 1);
writer.WriteLine(string.Format("Water meter {0}, s/n: {1}", wmNr + 1, results[0].Meters[wmNr].SerialNr));
writer.WriteLine(new String('-', totalWidth));
for (int i = 0; i < resultItems.Count; i++)
{
writer.Write(resultItems[i].ClmnHeaderText);
if (i < resultItems.Count - 1)
writer.Write(new string(' ', columnWidths[i] - resultItems[i].ClmnHeaderText.Length + 3));
else
writer.WriteLine(string.Empty);
}
writer.WriteLine(new String('-', totalWidth));
foreach (var tr in results)
{
for (int i = 0; i < resultItems.Count; i++)
{
string text = resultItems[i].Print(tr.Meters[wmNr]);
writer.Write(text);
if (i < resultItems.Count - 1)
writer.Write(new string(' ', columnWidths[i] - text.Length + 3));
else
writer.WriteLine(string.Empty);
}
}
writer.WriteLine(new String('-', totalWidth));
writer.WriteLine(string.Empty);
writer.WriteLine(string.Empty);
}
/// <summary>
/// Write one combined water meter results
/// </summary>
/// <param name="wmNr">Water meter number</param>
void WriteCombinedWM(int wmNr)
{
/// Determin column widths
int[] columnWidths = new int[resultItems.Count];
int totalWidth = 0;
for (int i = 0; i < resultItems.Count; i++)
{
columnWidths[i] = resultItems[i].ClmnHeaderText.Length;
foreach (var tr in results)
{
int len = resultItems[i].PrintCombined(tr.Meters[2*wmNr], tr.Meters[2*wmNr+1], tr.CombinedMeters[wmNr]).Length;
if (len > columnWidths[i]) columnWidths[i] = len;
}
totalWidth += columnWidths[i];
}
totalWidth += 3 * (resultItems.Count - 1);
writer.WriteLine(string.Format("Water meter {0}, Main s/n: {1}, Aux. s/n:{2}", wmNr + 1, results[0].Meters[2 * wmNr].SerialNr, results[0].Meters[2 * wmNr + 1].SerialNr));
writer.WriteLine(new String('-', totalWidth));
for (int i = 0; i < resultItems.Count; i++)
{
writer.Write(resultItems[i].ClmnHeaderText);
if (i < resultItems.Count - 1)
writer.Write(new string(' ', columnWidths[i] - resultItems[i].ClmnHeaderText.Length + 3));
else
writer.WriteLine(string.Empty);
}
writer.WriteLine(new String('-', totalWidth));
foreach (var tr in results)
{
foreach (var item in resultItems)
{
if (combined)
{
writer.Write(item.PrintCombined(tr.CombinedMeters[0], tr.Meters[0], tr.Meters[1]));
}
else
{
writer.Write(item.Print(tr.Meters[0]));
}
}
foreach (var item in resultItems) writer.Write(item.PrintCombined(tr.Meters[2 * wmNr], tr.Meters[2 * wmNr + 1], tr.CombinedMeters[wmNr]) + " ");
writer.WriteLine(string.Empty);
}
writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------");
writer.WriteLine("No| Zähler Nr| Test. Volume [L] | Zählerabweichung [%] | Messunsicherheit k=2 |Prüfergebnis");
writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------");
writer.WriteLine(" Qmin Qt Qmax Qmin Qt Qmax Qmin Qt Qmax");
writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------");
foreach (var tr in results)
{
for (int i = 0; i < resultItems.Count; i++)
{
string text = resultItems[i].PrintCombined(tr.Meters[2 * wmNr], tr.Meters[2 * wmNr + 1], tr.CombinedMeters[wmNr]);
writer.Write(text);
if (i < resultItems.Count - 1)
writer.Write(new string(' ', columnWidths[i] - text.Length + 3));
else
writer.WriteLine(string.Empty);
}
}
writer.WriteLine(new String('-', totalWidth));
writer.WriteLine(string.Empty);
writer.WriteLine(string.Empty);
}
/// <summary>Run this operation</summary>
@@ -143,19 +311,6 @@ namespace TBF.BenchControl.ResultsWriters.Basic
/// <summary>Stop this operation</summary>
public void Stop()
{
if (writer == null) return;
writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------");
writer.WriteLine("");
writer.WriteLine("Hersteller : Bemerkung : M - Prüfung gegen Waage");
writer.WriteLine(" V - Prüfung gegen MID");
writer.WriteLine(" P - Start / Stopp");
writer.WriteLine(" F - Fliegender Start");
writer.WriteLine(" S - Impulskompensation");
writer.WriteLine(" C - Impulszählung");
writer.WriteLine("");
writer.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------");
if (writer != null) writer.Close();
writer = null;
}
@@ -11,11 +11,18 @@ namespace TBF.BenchControl.ResultsWriters.Basic
public IComponent GetComponent(IList<IComponent> components) { return new Writer(this); }
public IComponentCfgCtrl GetControl() { return new WriterCfgCtrl(); }
public string DestinationPath; /// Directory path into which the results will be saved
public bool DayFolders;
/// Private parameterless constructor invoked by all other (public) constructors
WriterCfg()
{
Name = "BasicResultsWriter";
ParentName = string.Empty;
DestinationPath = Program.HomeDir + "Results\\";
}
public WriterCfg(IComponentFactory factory)
@@ -41,11 +41,16 @@ namespace TBF.BenchControl.ResultsWriters.Basic
if (config == null) return; /// Control was not loaded, settings were not changed
classNameLabel.Text = config.Factory.ClassName;
nameTextBox.Text = config.Name;
destinationTextBox.Text = config.DestinationPath;
dayFoldersCheckBox.Checked = config.DayFolders;
}
public void Unlock()
{
nameTextBox.Enabled = true;
destinationTextBox.Enabled = true;
destinationButton.Enabled = true;
dayFoldersCheckBox.Enabled = true;
}
public CfgUpdateFlags VerifyCfg(ref string message)
@@ -56,13 +61,31 @@ namespace TBF.BenchControl.ResultsWriters.Basic
public CfgUpdateFlags UpdateCfg()
{
CfgUpdateFlags flags = CfgUpdateFlags.RestartRqrd;
CfgUpdateFlags flags = CfgUpdateFlags.None;
if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed
config.Name = nameTextBox.Text;
if (config.Name != nameTextBox.Text) { config.Name = nameTextBox.Text; flags = CfgUpdateFlags.RestartRqrd; }
if (config.DestinationPath != destinationTextBox.Text)
{
config.DestinationPath = destinationTextBox.Text;
if (!config.DestinationPath.EndsWith("\\")) config.DestinationPath += "\\";
flags = CfgUpdateFlags.RestartRqrd;
}
if (dayFoldersCheckBox.Checked != config.DayFolders)
{
config.DayFolders = dayFoldersCheckBox.Checked;
flags = CfgUpdateFlags.RestartRqrd;
}
return flags;
}
private void destinationButton_Click(object sender, EventArgs e)
{
}
}
}
@@ -31,20 +31,24 @@
this.nameTextBox = new System.Windows.Forms.TextBox();
this.nameLabel = new System.Windows.Forms.Label();
this.classNameLabel = new System.Windows.Forms.Label();
this.destinationTextBox = new System.Windows.Forms.TextBox();
this.destinationLabel = new System.Windows.Forms.Label();
this.dayFoldersCheckBox = new System.Windows.Forms.CheckBox();
this.destinationButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// nameTextBox
//
this.nameTextBox.Enabled = false;
this.nameTextBox.Location = new System.Drawing.Point(137, 57);
this.nameTextBox.Location = new System.Drawing.Point(110, 57);
this.nameTextBox.Name = "nameTextBox";
this.nameTextBox.Size = new System.Drawing.Size(130, 20);
this.nameTextBox.Size = new System.Drawing.Size(146, 20);
this.nameTextBox.TabIndex = 5;
//
// nameLabel
//
this.nameLabel.AutoSize = true;
this.nameLabel.Location = new System.Drawing.Point(27, 60);
this.nameLabel.Location = new System.Drawing.Point(19, 60);
this.nameLabel.Name = "nameLabel";
this.nameLabel.Size = new System.Drawing.Size(35, 13);
this.nameLabel.TabIndex = 4;
@@ -53,20 +57,63 @@
// classNameLabel
//
this.classNameLabel.AutoSize = true;
this.classNameLabel.Location = new System.Drawing.Point(134, 33);
this.classNameLabel.Location = new System.Drawing.Point(107, 33);
this.classNameLabel.Name = "classNameLabel";
this.classNameLabel.Size = new System.Drawing.Size(83, 13);
this.classNameLabel.TabIndex = 3;
this.classNameLabel.Text = "ComonentName";
//
// BasicPrinterCfgCtrl
// destinationTextBox
//
this.destinationTextBox.Enabled = false;
this.destinationTextBox.Location = new System.Drawing.Point(110, 83);
this.destinationTextBox.Name = "destinationTextBox";
this.destinationTextBox.Size = new System.Drawing.Size(146, 20);
this.destinationTextBox.TabIndex = 7;
//
// destinationLabel
//
this.destinationLabel.AutoSize = true;
this.destinationLabel.Location = new System.Drawing.Point(19, 86);
this.destinationLabel.Name = "destinationLabel";
this.destinationLabel.Size = new System.Drawing.Size(60, 13);
this.destinationLabel.TabIndex = 6;
this.destinationLabel.Text = "Destination";
//
// dayFoldersCheckBox
//
this.dayFoldersCheckBox.AutoSize = true;
this.dayFoldersCheckBox.Enabled = false;
this.dayFoldersCheckBox.Location = new System.Drawing.Point(110, 109);
this.dayFoldersCheckBox.Name = "dayFoldersCheckBox";
this.dayFoldersCheckBox.Size = new System.Drawing.Size(160, 17);
this.dayFoldersCheckBox.TabIndex = 15;
this.dayFoldersCheckBox.Text = "Separate folder for each day";
this.dayFoldersCheckBox.UseVisualStyleBackColor = true;
//
// destinationButton
//
this.destinationButton.Enabled = false;
this.destinationButton.Location = new System.Drawing.Point(261, 83);
this.destinationButton.Name = "destinationButton";
this.destinationButton.Size = new System.Drawing.Size(30, 20);
this.destinationButton.TabIndex = 16;
this.destinationButton.Text = "...";
this.destinationButton.UseVisualStyleBackColor = true;
this.destinationButton.Click += new System.EventHandler(this.destinationButton_Click);
//
// WriterCfgCtrl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.destinationButton);
this.Controls.Add(this.dayFoldersCheckBox);
this.Controls.Add(this.destinationTextBox);
this.Controls.Add(this.destinationLabel);
this.Controls.Add(this.nameTextBox);
this.Controls.Add(this.nameLabel);
this.Controls.Add(this.classNameLabel);
this.Name = "BasicPrinterCfgCtrl";
this.Name = "WriterCfgCtrl";
this.Size = new System.Drawing.Size(300, 200);
this.Load += new System.EventHandler(this.WriterCfgCtrl_Load);
this.ResumeLayout(false);
@@ -79,5 +126,9 @@
private System.Windows.Forms.TextBox nameTextBox;
private System.Windows.Forms.Label nameLabel;
private System.Windows.Forms.Label classNameLabel;
private System.Windows.Forms.TextBox destinationTextBox;
private System.Windows.Forms.Label destinationLabel;
private System.Windows.Forms.CheckBox dayFoldersCheckBox;
private System.Windows.Forms.Button destinationButton;
}
}