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

121 lines
3.5 KiB
C#

///
/// Copyright (c) 2013-2023 Sensus Slovensko a.s.
///
using System;
using log4net;
using Common;
using Common.Forms;
using Results.Output.Printers.Munich;
using System.Globalization;
using System.Threading;
namespace TBF.Rig.Output.Printers.Munich
{
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 false; } }
/// <summary> Data to print </summary>
Results.Entities.Batch batch;
bool printingCompleted = false;
bool statusDlgShown = false;
ModelessForm modelessForm;
public string Version { get { return printerCfg.Version; } }
public Printer() { }
public Printer(Generic.IComponentCfg cfg)
: base(cfg)
{
printerCfg = cfg as PrinterCfg;
}
public override void Initialize()
{
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);
}
}
/// <summary>Events: ResultsPrinted</summary>
/// <param name="benchId">Procedure to print the results of</param>
/// <param name="results">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
foreach (var wm in batch.WaterMeters)
{
var pd = new MunichPrintDocument(printerCfg.Version, wm, printerCfg.PageOrientation);
if (!string.IsNullOrEmpty(printerCfg.PrinterName)) pd.PrinterSettings.PrinterName = printerCfg.PrinterName;
pd.Print();
}
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()
{
}
}
}