/// /// Copyright (c) 2020 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using log4net; using System.Globalization; using System.Threading; using Results.Output.Printers.MultiLabel; namespace TBF.BenchControl.Output.Printers.MultiLabel { public class Printer : ComponentBase, IOperation, GenericDevices.IResultsPrinter { private static readonly ILog log = LogManager.GetLogger(typeof(Printer)); public override string ToString() { return string.Format("MultiLabel.Printer({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.PageOrientation = newCfg.PageOrientation; printerCfg.LabelColumnsCount = newCfg.LabelColumnsCount; printerCfg.LeftTopLabelLeft = newCfg.LeftTopLabelLeft; printerCfg.LeftTopLabelTop = newCfg.LeftTopLabelTop; printerCfg.NrOfCopies = newCfg.NrOfCopies; printerCfg.GoodOnly = newCfg.GoodOnly; printerCfg.ItemsToPrint = newCfg.ItemsToPrint; printerCfg.LabelWidth = newCfg.LabelWidth; printerCfg.LabelHeight = newCfg.LabelHeight; printerCfg.Template = newCfg.Template; printerCfg.FontFamily = newCfg.FontFamily; printerCfg.FontSize = newCfg.FontSize; printerCfg.FontStyle = newCfg.FontStyle; printerCfg.Culture = newCfg.Culture; 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()); } PrintResults(batch, string.Format("{0}", batch.BatchNr)); 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) { MultiLabelPrinterCfg cfg = new MultiLabelPrinterCfg { PageOrientation = printerCfg.PageOrientation, LabelColumnsCount = printerCfg.LabelColumnsCount, LeftTopLabelLeft = printerCfg.LeftTopLabelLeft, LeftTopLabelTop = printerCfg.LeftTopLabelTop, GoodOnly = printerCfg.GoodOnly, ItemsToPrint = printerCfg.ItemsToPrint, LabelWidth = printerCfg.LabelWidth, LabelHeight = printerCfg.LabelHeight, Template = printerCfg.Template, FontFamily = printerCfg.FontFamily, FontSize = printerCfg.FontSize, FontStyle = printerCfg.FontStyle, CultureInfo = (printerCfg.Culture == Culture.system) ? Thread.CurrentThread.CurrentCulture : new CultureInfo(printerCfg.Culture.ToString()), 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.MultiLabel.MultiLabelPrintDocument(batch, cfg, docNameEx).Print(); } } } }