/// /// Copyright (c) 2017 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Globalization; using System.Threading; using Results.Output.Printers.Zapiska; using log4net; namespace TBF.BenchControl.Output.Printers.Zapiska { 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.Zapiska({0})", Cfg.ToString(1)); } readonly PrinterCfg printerCfg; public bool SupressPrinting { get { return printerCfg.SupressPrinting; } } /// Data to print Results.Entities.Batch batch; /// /// Items to print /// string header; IList commonItems; IList 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.CommonItems1); 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 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.SupressPrinting = newCfg.SupressPrinting; printerCfg.GoodOnly = newCfg.GoodOnly; printerCfg.Culture = newCfg.Culture; printerCfg.Header = newCfg.Header; printerCfg.ColW0Pct = newCfg.ColW0Pct; printerCfg.ColW1Pct = newCfg.ColW1Pct; printerCfg.ColW2Pct = newCfg.ColW2Pct; printerCfg.CommonItems1 = newCfg.CommonItems1; printerCfg.CommonItems2 = newCfg.CommonItems2; printerCfg.CommonItems3 = newCfg.CommonItems3; printerCfg.SelectedItems = newCfg.SelectedItems; printerCfg.BelowW0Pct = newCfg.BelowW0Pct; printerCfg.BelowW1Pct = newCfg.BelowW1Pct; printerCfg.BelowW2Pct = newCfg.BelowW2Pct; printerCfg.BelowItems1 = newCfg.BelowItems1; printerCfg.BelowItems2 = newCfg.BelowItems2; printerCfg.BelowItems3 = newCfg.BelowItems3; printerCfg.Footer = newCfg.Footer; printerCfg.DocName = newCfg.DocName; ApplyConfig(); } } }; } #endregion Configuration Change Handling /// /// Prints the test cycle results, Events: Event.ResultsPrinted /// /// Batch results to print /// Reference to the operation public IOperation PrintResultsOp(Results.Entities.Batch batch) { this.batch = batch; return this; } /// Start this operation public void Start() { CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; if (printerCfg.Culture != Culture.system) { Thread.CurrentThread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString()); } string documentName; try { if (string.IsNullOrEmpty(printerCfg.DocName)) throw (new Exception()); documentName = string.Format(printerCfg.DocName, batch.StartTime, batch.EndTime, batch.BatchNr); } catch { documentName = string.Format("{0:yyyyMMdd-HHmm}", batch.EndTime); } PrintResults(batch, documentName); Thread.CurrentThread.CurrentCulture = oriCulture; } /// Run this operation /// Event.ResultsPrinted public Event Run() { return Event.ResultsPrinted; } /// Stop this operation public void Stop() { } void PrintResults(Results.Entities.Batch batch, string documentName) { if (batch.WaterMeters.Count > 0) { ZapiskaPrinterCfg cfg = new ZapiskaPrinterCfg { 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, SupressPrinting = printerCfg.SupressPrinting, GoodOnly = printerCfg.GoodOnly, CultureInfo = (printerCfg.Culture == Culture.system) ? Thread.CurrentThread.CurrentCulture : new CultureInfo(printerCfg.Culture.ToString()), Header = printerCfg.Header, ColW0Pct = printerCfg.ColW0Pct, ColW1Pct = printerCfg.ColW1Pct, ColW2Pct = printerCfg.ColW2Pct, CommonItems1 = printerCfg.CommonItems1, CommonItems2 = printerCfg.CommonItems2, CommonItems3 = printerCfg.CommonItems3, SelectedItems = printerCfg.SelectedItems, BelowW0Pct = printerCfg.BelowW0Pct, BelowW1Pct = printerCfg.BelowW1Pct, BelowW2Pct = printerCfg.BelowW2Pct, BelowItems1 = printerCfg.BelowItems1, BelowItems2 = printerCfg.BelowItems2, BelowItems3 = printerCfg.BelowItems3, Footer = printerCfg.Footer, }; new Results.Output.Printers.Zapiska.ZapiskaPrintDocument(batch, cfg, documentName).Print(); } } } }