tbf/TestBenchFramework/BenchControl/Output/Printers/Enhanced/Printer.cs

162 lines
4.4 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using log4net;
namespace TBF.BenchControl.Output.Printers.Enhanced
{
public class Printer : ComponentBase, IOperation, GenericDevices.IResultsPrinter
{
private static readonly ILog log = LogManager.GetLogger(typeof(Printer));
public override string ToString() { return string.Format("ResultsPrinters.Basic({0})", Cfg.ToString(1)); }
readonly PrinterCfg printerCfg;
public bool SupressPrinting { get { return printerCfg.SupressPrinting; } }
///
/// Items to print
///
string header;
IList<Results.WMeterRsltItemSpec> commonItems;
IList<Results.WMeterRsltItemSpec> testItems;
string footer;
Thread thread; /// Thread where results are printed
bool completed; /// Set to 'true' by the thread when printing results is completed
bool abort; /// Set to 'true' by Stop() operation to abort printing results
public Printer() { }
public Printer(Generic.IComponentCfg cfg)
: base(cfg)
{
printerCfg = cfg as PrinterCfg;
ApplyConfig();
log.Debug(this.ToString());
}
void ApplyConfig()
{
header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine);
commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems);
testItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.SelectedItems); /// TestID info will be overwritten later on
footer = string.IsNullOrEmpty(printerCfg.Footer) ? string.Empty : printerCfg.Footer.Replace("~", Environment.NewLine);
}
#region Configuration Change Handling
public static void OnCfgChange(object sender, CfgChangeArgs args)
{
if (CfgChangeHandler == null) return;
try { CfgChangeHandler(sender, args); }
catch (Exception e) { log.Error("CfgChangeHandler(...) failed", e); }
}
public static event EventHandler<CfgChangeArgs> CfgChangeHandler;
public override void StartChangeHandler()
{
CfgChangeHandler += delegate(object sender, CfgChangeArgs args)
{
PrinterCfg newCfg = args.Cfg as PrinterCfg;
if (newCfg != null && newCfg.Name.Equals(Name))
{
if (args.Command == CfgChangeCmd.CfgChange)
{
printerCfg.PageOrientation = newCfg.PageOrientation;
printerCfg.TopMargin = newCfg.TopMargin;
printerCfg.BottomMargin = newCfg.BottomMargin;
printerCfg.LeftMargin = newCfg.LeftMargin;
printerCfg.SupressPrinting = newCfg.SupressPrinting;
printerCfg.Culture = newCfg.Culture;
printerCfg.CommonItems = newCfg.CommonItems;
printerCfg.SelectedItems = newCfg.SelectedItems;
printerCfg.Header = newCfg.Header;
printerCfg.Footer = newCfg.Footer;
ApplyConfig();
}
}
};
}
#endregion Configuration Change Handling
/// <summary>
/// Prints the test cycle results, Events: Event.ResultsPrinted
/// </summary>
/// <param name="batch">Batch results to print</param>
/// <returns>Reference to the operation</returns>
public IOperation PrintResultsOp(Results.Entities.Batch batch)
{
thread = new Thread(() =>
{
PrintResults(batch);
completed = true;
});
thread.CurrentCulture = Thread.CurrentThread.CurrentCulture;
thread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture;
if (printerCfg.Culture > Culture.system && printerCfg.Culture < Culture.Count)
{
thread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString());
}
return this;
}
/// <summary>Start this operation</summary>
public void Start()
{
completed = false;
abort = false;
thread.Start();
}
/// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns>
public Event Run()
{
if (completed)
return Event.ResultsPrinted;
else
return Event.Busy;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
if (!completed) abort = true;
}
void PrintResults(Results.Entities.Batch batch)
{
if (batch.WaterMeters.Count > 0)
{
new Results.Output.Printers.Enhanced.EnhancedPrintDocument(batch,
printerCfg.PageOrientation,
printerCfg.TopMargin,
printerCfg.BottomMargin,
printerCfg.LeftMargin,
header,
commonItems,
testItems,
footer).Print();
}
}
}
}