tbf/TBF/Rig/Output/Printers/Label/Printer.cs
2021-10-26 19:38:09 +02:00

171 lines
5.2 KiB
C#

///
/// 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.Rig.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("{0}({1})", ClassName, Cfg.ToString(-1)); }
readonly PrinterCfg printerCfg;
public bool SupressPrinting { get { return (printerCfg.NrOfCopies == 0); } }
/// <summary> Data to print </summary>
Results.Entities.Batch batch;
///
/// Items to print
///
IList<Results.WMeterRsltItemSpec> commonItems;
public Printer() { }
public Printer(Generic.IComponentCfg cfg)
: base(cfg)
{
printerCfg = cfg as PrinterCfg;
}
public override void Initialize()
{
ApplyConfig();
log.FatalFormat("{0} initialized: {1}", Name, this);
}
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<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.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
/// <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 ProcessResultsOp(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 (wm != null && (!printerCfg.GoodOnly || wm.Passed))
{
PrintResults(batch, wm, string.Format("{0}-{1}", batch.BatchNr, wm.WMPosition));
}
}
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)
{
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();
}
}
}
}