/// /// Copyright (c) 2015-2020 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using log4net; using System.Globalization; using System.Threading; using Results.Output.Printers.Label; namespace TBF.BenchControl.Output.Printers.Label { public class Printer : ComponentBase, IOperation, GenericDevices.IResultsPrinter { private static readonly ILog log = LogManager.GetLogger(typeof(Printer)); public override string ToString() { return string.Format("LabelPrinter({0})", Cfg.ToString(1)); } readonly PrinterCfg printerCfg; public bool SupressPrinting { get { return (printerCfg.NrOfCopies == 0); } } /// Data to print Results.Entities.Batch batch; /// /// Items to print /// IList commonItems; public Printer() { } public Printer(Generic.IComponentCfg cfg) : base(cfg) { printerCfg = cfg as PrinterCfg; ApplyConfig(); log.Warn(this.ToString()); } void ApplyConfig() { commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.ItemsToPrint); } #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.Template = newCfg.Template; printerCfg.PageOrientation = newCfg.PageOrientation; printerCfg.PaperWidth = newCfg.PaperWidth; printerCfg.PaperHeight = newCfg.PaperHeight; printerCfg.ItemsToPrint = newCfg.ItemsToPrint; printerCfg.FontFamily = newCfg.FontFamily; printerCfg.FontSize = newCfg.FontSize; printerCfg.FontStyle = newCfg.FontStyle; printerCfg.Culture = newCfg.Culture; printerCfg.NrOfCopies = newCfg.NrOfCopies; printerCfg.GoodOnly = newCfg.GoodOnly; printerCfg.BarcodeType = newCfg.BarcodeType; printerCfg.BarcodeLeft = newCfg.BarcodeLeft; printerCfg.BarcodeTop = newCfg.BarcodeTop; printerCfg.BarcodeWidth = newCfg.BarcodeWidth; printerCfg.BarcodeHeight = newCfg.BarcodeHeight; ApplyConfig(); } } }; } #endregion Configuration Change Handling /// /// Prints the test cycle results, Events: Event.ResultsPrinted /// /// Batch results to print /// Reference to the operation public IOperation ProcessResultsOp(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()); } foreach (var wm in batch.WaterMeters) { if (wm != null && (!printerCfg.GoodOnly || wm.Passed)) { PrintResults(batch, wm, string.Format("{0}-{1}", batch.BatchNr, wm.WMPosition)); } } 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, Results.Entities.WaterMeter wm, string documentName) { LabelPrinterCfg cfg = new LabelPrinterCfg { Template = printerCfg.Template, PageOrientation = printerCfg.PageOrientation, PaperWidth = printerCfg.PaperWidth, PaperHeight = printerCfg.PaperHeight, ItemsToPrint = printerCfg.ItemsToPrint, FontFamily = printerCfg.FontFamily, FontSize = printerCfg.FontSize, FontStyle = printerCfg.FontStyle, CultureInfo = (printerCfg.Culture == Culture.system) ? Thread.CurrentThread.CurrentCulture : new CultureInfo(printerCfg.Culture.ToString()), GoodOnly = printerCfg.GoodOnly, BarcodeType = printerCfg.BarcodeType, BarcodeLeft = printerCfg.BarcodeLeft, BarcodeTop = printerCfg.BarcodeTop, BarcodeWidth = printerCfg.BarcodeWidth, BarcodeHeight = printerCfg.BarcodeHeight, }; for (int i = 1; i <= printerCfg.NrOfCopies; i++) { string docNameEx = string.Format(((printerCfg.NrOfCopies == 1) ? "{0}" : "{0}_{1}"), documentName, i); new Results.Output.Printers.Label.LabelPrintDocument(batch, cfg, wm, docNameEx).Print(); } } } }