210 lines
6.8 KiB
C#
210 lines
6.8 KiB
C#
///
|
|
/// Copyright (c) 2015-2023 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using System.Globalization;
|
|
using System.Threading;
|
|
using Common;
|
|
using Common.Forms;
|
|
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); } }
|
|
|
|
Results.Entities.Batch batch; /// Data to print
|
|
IList<Results.WMeterRsltItemSpec> commonItems; /// Items to print
|
|
bool printingCompleted = false;
|
|
bool statusDlgShown = false;
|
|
ModelessForm modelessForm;
|
|
|
|
public Printer() { }
|
|
|
|
public Printer(Generic.IComponentCfg cfg)
|
|
: base(cfg)
|
|
{
|
|
printerCfg = cfg as PrinterCfg;
|
|
}
|
|
|
|
public override void Initialize()
|
|
{
|
|
ApplyConfig();
|
|
if (printerCfg.DebugLevel == DebugMode.Normal)
|
|
{
|
|
string message;
|
|
if (!PrinterStatus.IsOnline(printerCfg.PrinterName, out message)) throw new ApplicationException(message);
|
|
log.FatalFormat("{0} initialized: {1}", Name, this);
|
|
}
|
|
else
|
|
{
|
|
log.FatalFormat("{0} simulated: {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()
|
|
{
|
|
printingCompleted = false;
|
|
statusDlgShown = false;
|
|
}
|
|
|
|
/// <summary>Run this operation</summary>
|
|
/// <returns>Event.ResultsPrinted</returns>
|
|
public Event Run()
|
|
{
|
|
string message;
|
|
if (printingCompleted || printerCfg.DebugLevel != DebugMode.Normal)
|
|
{
|
|
return Event.ResultsPrinted;
|
|
}
|
|
else if (PrinterStatus.IsOnline(printerCfg.PrinterName, out message))
|
|
{
|
|
if (statusDlgShown)
|
|
{
|
|
statusDlgShown = false;
|
|
if (modelessForm != null) modelessForm.Close();
|
|
}
|
|
|
|
/// Printer is online => Print results now
|
|
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 && !wm.Disabled && wm.PrintLabel && (!printerCfg.GoodOnly || wm.Passed))
|
|
{
|
|
PrintResults(batch, wm, string.Format("{0}-{1}", batch.BatchNr, wm.WMPosition));
|
|
}
|
|
}
|
|
|
|
Thread.CurrentThread.CurrentCulture = oriCulture;
|
|
|
|
printingCompleted = true;
|
|
return Event.ResultsPrinted;
|
|
}
|
|
else
|
|
{
|
|
if (!statusDlgShown)
|
|
{
|
|
statusDlgShown = true;
|
|
modelessForm = new ModelessForm(message);
|
|
new Thread(() => System.Windows.Forms.Application.Run(modelessForm)).Start();
|
|
}
|
|
|
|
return Event.Busy;
|
|
}
|
|
}
|
|
|
|
/// <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);
|
|
var pd = new Results.Output.Printers.Label.LabelPrintDocument(batch, cfg, wm, docNameEx);
|
|
if (!string.IsNullOrEmpty(printerCfg.PrinterName)) pd.PrinterSettings.PrinterName = printerCfg.PrinterName;
|
|
pd.Print();
|
|
}
|
|
}
|
|
}
|
|
}
|