tbf/TBF/Rig/Output/Printers/OnePerBatch/Printer.cs

260 lines
10 KiB
C#

///
/// Copyright (c) 2020-2023 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using Results.Entities;
using Results.Output.Printers.OnePerBatch;
using log4net;
namespace TBF.Rig.Output.Printers.OnePerBatch
{
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.SupressPrinting; } }
/// <summary> Data to print </summary>
Batch batch;
///
/// Items to print
///
string header;
IList<Results.WMeterRsltItemSpec> commonItems;
IList<Results.WMeterRsltItemSpec> testItems;
string footer;
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()
{
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<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.AutoTestParam = newCfg.AutoTestParam;
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.GoodOnly = newCfg.GoodOnly;
printerCfg.SupressPrinting = newCfg.SupressPrinting;
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.Form = newCfg.Form;
printerCfg.SelectedItems = newCfg.SelectedItems;
printerCfg.SelectedItems2 = newCfg.SelectedItems2;
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.TestsAreRows = newCfg.TestsAreRows;
printerCfg.GraphLeft = newCfg.GraphLeft;
printerCfg.GraphTop = newCfg.GraphTop;
printerCfg.GraphWid = newCfg.GraphWid;
printerCfg.GraphHgh = newCfg.GraphHgh;
printerCfg.ImgLeft = newCfg.ImgLeft;
printerCfg.ImgTop = newCfg.ImgTop;
printerCfg.ImgWid = newCfg.ImgWid;
printerCfg.ImgHgh = newCfg.ImgHgh;
printerCfg.ImgName = newCfg.ImgName;
printerCfg.DocName = newCfg.DocName;
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(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 && !wm.Disabled && (!printerCfg.GoodOnly || wm.Passed))
{
string documentName;
try
{
if (string.IsNullOrEmpty(printerCfg.DocName)) throw (new Exception());
documentName = string.Format(printerCfg.DocName, wm.StartTime(), wm.EndTime(), wm.BatchNr(), wm.WMPosition, wm.SerialNr);
}
catch
{
documentName = string.Format("{0}", batch.BatchNr, wm.WMPosition);
}
/// The following condition prevents printing empty AutoTest results protocols
if (string.IsNullOrEmpty(printerCfg.AutoTestParam) || wm.AutoMeterTestRslts(printerCfg.AutoTestParam).Count > 0)
{
PrintResults(batch, wm, documentName);
break;
}
}
}
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(Batch batch, WaterMeter wm, string documentName)
{
OnePerBatchPrinterCfg cfg = new OnePerBatchPrinterCfg
{
AutoTestParam = printerCfg.AutoTestParam,
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,
GoodOnly = printerCfg.GoodOnly,
SupressPrinting = printerCfg.SupressPrinting,
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,
Form = printerCfg.Form,
TestItems = printerCfg.SelectedItems,
TestItems2 = printerCfg.SelectedItems2,
BelowW0Pct = printerCfg.BelowW0Pct,
BelowW1Pct = printerCfg.BelowW1Pct,
BelowW2Pct = printerCfg.BelowW2Pct,
BelowItems1 = printerCfg.BelowItems1,
BelowItems2 = printerCfg.BelowItems2,
BelowItems3 = printerCfg.BelowItems3,
Footer = printerCfg.Footer,
TestsAreRows = printerCfg.TestsAreRows,
GraphLeft = printerCfg.GraphLeft,
GraphTop = printerCfg.GraphTop,
GraphWid = printerCfg.GraphWid,
GraphHgh = printerCfg.GraphHgh,
ImgLeft = printerCfg.ImgLeft,
ImgTop = printerCfg.ImgTop,
ImgWid = printerCfg.ImgWid,
ImgHgh = printerCfg.ImgHgh,
ImgFullPathName = (wm.ArchivePath != null) ? System.IO.Path.Combine(wm.ArchivePath, printerCfg.ImgName) : null,
};
var pd = new OnePerBatchPrintDocument(batch, cfg, wm, documentName);
if (!string.IsNullOrEmpty(printerCfg.PrinterName)) pd.PrinterSettings.PrinterName = printerCfg.PrinterName;
pd.Print();
}
}
}