222 lines
6.8 KiB
C#
222 lines
6.8 KiB
C#
///
|
|
/// Copyright (c) 2013-2016 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using TBF.BenchControl;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.BenchControl.Output.FileWriters.Basic
|
|
{
|
|
public class Writer : ComponentBase, IOperation, GenericDevices.IResultsWriter
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Writer));
|
|
public override string ToString() { return string.Format("Output.FileWriters.Basic({0})", Cfg.ToString(1)); }
|
|
|
|
readonly WriterCfg writerCfg;
|
|
readonly string separatorStr;
|
|
|
|
/// <summary>
|
|
/// Result items to print
|
|
/// </summary>
|
|
IList<Results.WMeterRsltItemSpec> rsltItems;
|
|
|
|
/// <summary>
|
|
/// Results to print
|
|
/// </summary>
|
|
Results.Entities.Batch batch;
|
|
|
|
StreamWriter writer;
|
|
StreamWriter writer2;
|
|
|
|
|
|
public Writer() {}
|
|
|
|
public Writer(WriterCfg cfg)
|
|
: base(cfg)
|
|
{
|
|
writerCfg = cfg;
|
|
|
|
switch (cfg.Separator)
|
|
{
|
|
default:
|
|
case Separator.None: separatorStr = string.Empty; break;
|
|
case Separator.Space: separatorStr = " "; break;
|
|
case Separator.Tabulator: separatorStr = "\t"; break;
|
|
case Separator.Comma: separatorStr = ","; break;
|
|
case Separator.Semicolon: separatorStr = ";"; break;
|
|
}
|
|
|
|
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(string destinationPath, DateTime time)
|
|
{
|
|
string directory = destinationPath; /// Ends with "\\";
|
|
|
|
switch (writerCfg.YearFolders)
|
|
{
|
|
case YearFolders.FourDigit:
|
|
directory = string.Format("{0}{1}\\", directory, time.Year.ToString("D4"));
|
|
break;
|
|
case YearFolders.TwoDigit:
|
|
directory = string.Format("{0}{1}\\", directory, (time.Year % 100).ToString("D2"));
|
|
break;
|
|
}
|
|
|
|
switch (writerCfg.MonthFolders)
|
|
{
|
|
case MonthFolders.Name:
|
|
{
|
|
string monthStr;
|
|
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);
|
|
break;
|
|
}
|
|
case MonthFolders.Digit:
|
|
directory = string.Format("{0}{1}\\", directory, time.Month.ToString());
|
|
break;
|
|
case MonthFolders.TwoDigit:
|
|
directory = string.Format("{0}{1}\\", directory, time.Month.ToString("D2"));
|
|
break;
|
|
}
|
|
|
|
switch (writerCfg.DayFolders)
|
|
{
|
|
case DayFolders.Digit:
|
|
directory = string.Format("{0}{1}\\", directory, time.Day.ToString());
|
|
break;
|
|
case DayFolders.TwoDigit:
|
|
directory = string.Format("{0}{1}\\", directory, time.Day.ToString("D2"));
|
|
break;
|
|
}
|
|
|
|
Directory.CreateDirectory(directory);
|
|
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(writerCfg.FileNameFormat)) throw new Exception();
|
|
return directory + string.Format(writerCfg.FileNameFormat, time);
|
|
}
|
|
catch
|
|
{
|
|
return string.Format("{0}{1:yyMMdd}-{1:hhss}.txt", directory, time);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes the test cycle results into a file, Events: Event.ResultsWritten
|
|
/// </summary>
|
|
/// <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(Results.Entities.Batch batchResults)
|
|
{
|
|
this.batch = batchResults;
|
|
|
|
if (batch.WaterMeters.Count <= 0)
|
|
{
|
|
writer = null;
|
|
writer2 = null;
|
|
return this;
|
|
}
|
|
|
|
rsltItems = Results.WMeterRsltItemSpec.FromStrArray(writerCfg.SelectedItems);
|
|
|
|
writer = null;
|
|
if (!string.IsNullOrEmpty(writerCfg.DestinationPath))
|
|
{
|
|
try { writer = File.AppendText(GetFilename(writerCfg.DestinationPath, batch.EndTime)); }
|
|
catch { };
|
|
}
|
|
|
|
writer2 = null;
|
|
if (!string.IsNullOrEmpty(writerCfg.DestinationPath2))
|
|
{
|
|
try { writer2 = File.AppendText(GetFilename(writerCfg.DestinationPath2, batch.EndTime)); }
|
|
catch { };
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
/// <summary>Start this operation</summary>
|
|
public void Start()
|
|
{
|
|
if (rsltItems == null || rsltItems.Count == 0) return;
|
|
|
|
foreach (var wm in batch.WaterMeters)
|
|
{
|
|
if (!writerCfg.SaveGoodOnly || wm.Passed)
|
|
{
|
|
if (writer != null) WriteWM(writer, wm);
|
|
if (writer2 != null) WriteWM(writer2, wm);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write one water meter results
|
|
/// </summary>
|
|
/// <param name="wmNr">Water meter number (0-based)</param>
|
|
void WriteWM(StreamWriter wr, Results.Entities.WaterMeter wm)
|
|
{
|
|
for (int i = 0; i < rsltItems.Count; i++)
|
|
{
|
|
wr.Write(rsltItems[i].Print(wm));
|
|
|
|
if (i < rsltItems.Count - 1)
|
|
{
|
|
wr.Write(separatorStr);
|
|
}
|
|
else
|
|
{
|
|
wr.WriteLine(string.Empty);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>Event.ResultsWritten</returns>
|
|
public Event Run()
|
|
{
|
|
return Event.ResultsWritten;
|
|
}
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
public void Stop()
|
|
{
|
|
if (writer != null) writer.Close();
|
|
writer = null;
|
|
|
|
if (writer2 != null) writer2.Close();
|
|
writer2 = null;
|
|
}
|
|
}
|
|
}
|