tbf/TBF/BenchControl/Output/Printers/OnePerMeter/Printer.cs

217 lines
7.6 KiB
C#

///
/// Copyright (c) 2017 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using Results.Output.Printers.OnePerMeter;
using log4net;
namespace TBF.BenchControl.Output.Printers.OnePerMeter
{
public class Printer : ComponentBase, IOperation, GenericDevices.IResultsPrinter
{
private static readonly ILog log = LogManager.GetLogger(typeof(Printer));
public override string ToString() { return string.Format("Output.Printers.OnePerMeter({0})", Cfg.ToString(1)); }
readonly PrinterCfg printerCfg;
public bool SupressPrinting { get { return printerCfg.SupressPrinting; } }
/// <summary> Data to print </summary>
Results.Entities.Batch batch;
///
/// Items to print
///
string header;
IList<Results.WMeterRsltItemSpec> commonItems;
IList<Results.WMeterRsltItemSpec> testItems;
string footer;
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.Template = newCfg.Template;
printerCfg.TopMargin = newCfg.TopMargin;
printerCfg.BottomMargin = newCfg.BottomMargin;
printerCfg.LeftMargin = newCfg.LeftMargin;
printerCfg.TitFntFml = newCfg.TitFntFml;
printerCfg.TitFntSz = newCfg.TitFntSz;
printerCfg.TitFntSty = newCfg.TitFntSty;
printerCfg.HdrFntFml = newCfg.HdrFntFml;
printerCfg.HdrFntSz = newCfg.HdrFntSz;
printerCfg.HdrFntSty = newCfg.HdrFntSty;
printerCfg.BodyFntFml = newCfg.BodyFntFml;
printerCfg.BodyFntSz = newCfg.BodyFntSz;
printerCfg.BodyFntSty = newCfg.BodyFntSty;
printerCfg.FooterFntFml = newCfg.FooterFntFml;
printerCfg.FooterFntSz = newCfg.FooterFntSz;
printerCfg.FooterFntSty = newCfg.FooterFntSty;
printerCfg.GoodOnly = newCfg.GoodOnly;
printerCfg.SupressPrinting = newCfg.SupressPrinting;
printerCfg.Culture = newCfg.Culture;
printerCfg.Header = newCfg.Header;
printerCfg.CommonItems = newCfg.CommonItems;
printerCfg.SelectedItems = newCfg.SelectedItems;
printerCfg.SelectedItems2 = newCfg.SelectedItems2;
printerCfg.Footer = newCfg.Footer;
printerCfg.TestsAreRows = newCfg.TestsAreRows;
printerCfg.DocName = newCfg.DocName;
printerCfg.ImgName = newCfg.ImgName;
printerCfg.ImgLeft = newCfg.ImgLeft;
printerCfg.ImgTop = newCfg.ImgTop;
printerCfg.ImgWid = newCfg.ImgWid;
printerCfg.ImgHgh = newCfg.ImgHgh;
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)
{
this.batch = batch;
return this;
}
/// <summary>Start this operation</summary>
public void Start()
{
CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture;
if (printerCfg.Culture != Culture.system)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString());
}
foreach (var wm in batch.WaterMeters)
{
if (!printerCfg.GoodOnly || wm.Passed)
{
string documentName;
try
{
if (string.IsNullOrEmpty(printerCfg.DocName)) throw (new Exception());
documentName = string.Format(printerCfg.DocName, wm.StartTime(), wm.EndTime(), wm.BatchNr(), wm.WMPosition, wm.SerialNr);
}
catch
{
documentName = string.Format("{0}-{1}", batch.BatchNr, wm.WMPosition);
}
PrintResults(batch, wm, documentName);
}
}
Thread.CurrentThread.CurrentCulture = oriCulture;
}
/// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns>
public Event Run()
{
return Event.ResultsPrinted;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
}
void PrintResults(Results.Entities.Batch batch, Results.Entities.WaterMeter wm, string documentName)
{
if (batch.WaterMeters.Count > 0)
{
OnePerMeterPrinterCfg cfg = new OnePerMeterPrinterCfg {
PageOrientation = printerCfg.PageOrientation,
Template = printerCfg.Template,
TopMargin = printerCfg.TopMargin,
BottomMargin = printerCfg.BottomMargin,
LeftMargin = printerCfg.LeftMargin,
TitFntFml = printerCfg.TitFntFml,
TitFntSz = printerCfg.TitFntSz,
TitFntSty = printerCfg.TitFntSty,
HdrFntFml = printerCfg.HdrFntFml,
HdrFntSz = printerCfg.HdrFntSz,
HdrFntSty = printerCfg.HdrFntSty,
BodyFntFml = printerCfg.BodyFntFml,
BodyFntSz = printerCfg.BodyFntSz,
BodyFntSty = printerCfg.BodyFntSty,
FooterFntFml = printerCfg.FooterFntFml,
FooterFntSz = printerCfg.FooterFntSz,
FooterFntSty = printerCfg.FooterFntSty,
GoodOnly = printerCfg.GoodOnly,
SupressPrinting = printerCfg.SupressPrinting,
CultureInfo = (printerCfg.Culture == Culture.system) ? Thread.CurrentThread.CurrentCulture : new CultureInfo(printerCfg.Culture.ToString()),
Header = printerCfg.Header,
CommonItems = printerCfg.CommonItems,
TestItems = printerCfg.SelectedItems,
TestItems2 = printerCfg.SelectedItems2,
Footer = printerCfg.Footer,
TestsAreRows = printerCfg.TestsAreRows,
ImgFullPathName = (wm.ArchivePath != null) ? System.IO.Path.Combine(wm.ArchivePath, printerCfg.ImgName) : null,
ImgLeft = printerCfg.ImgLeft,
ImgTop = printerCfg.ImgTop,
ImgWid = printerCfg.ImgWid,
ImgHgh = printerCfg.ImgHgh,
};
new Results.Output.Printers.OnePerMeter.OnePerMeterPrintDocument(batch, cfg, wm, documentName).Print();
}
}
}
}