diff --git a/TBF/Rig/Output/Printers/Enhanced/Printer.cs b/TBF/Rig/Output/Printers/Enhanced/Printer.cs index cf90517f1..367bc1066 100644 --- a/TBF/Rig/Output/Printers/Enhanced/Printer.cs +++ b/TBF/Rig/Output/Printers/Enhanced/Printer.cs @@ -3,9 +3,10 @@ /// using System; using System.Collections.Generic; -using System.Drawing.Printing; using System.Globalization; using System.Threading; +using Common; +using Common.Forms; using Results.Output.Printers.Enhanced; using log4net; @@ -31,8 +32,12 @@ namespace TBF.Rig.Output.Printers.Enhanced IList testItems; string footer; + bool printingCompleted = false; + bool statusDlgShown = false; + ModelessForm modelessForm; - public Printer() { } + + public Printer() { } public Printer(Generic.IComponentCfg cfg) : base(cfg) @@ -43,10 +48,19 @@ namespace TBF.Rig.Output.Printers.Enhanced public override void Initialize() { ApplyConfig(); - log.FatalFormat("{0} initialized: {1}", Name, this); + 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() + void ApplyConfig() { header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems); @@ -132,38 +146,67 @@ namespace TBF.Rig.Output.Printers.Enhanced /// Start this operation public void Start() { - CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; - - if (printerCfg.Culture != Culture.system) - { - Thread.CurrentThread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString()); - } - - string documentName; - try - { - if (string.IsNullOrEmpty(printerCfg.DocName)) throw (new Exception()); - documentName = string.Format(printerCfg.DocName, batch.StartTime, batch.EndTime, batch.BatchNr); - } - catch - { - documentName = string.Format("{0:yyyyMMdd-HHmm}", batch.EndTime); - } - - PrintResults(batch, documentName); - - Thread.CurrentThread.CurrentCulture = oriCulture; + printingCompleted = false; + statusDlgShown = false; } /// Run this operation /// Event.ResultsPrinted public Event Run() { - return Event.ResultsPrinted; - } + 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(); + } - /// Stop this operation - public void Stop() + /// Printer is online => Print results now + CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; + if (printerCfg.Culture != Culture.system) + { + Thread.CurrentThread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString()); + } + + string documentName; + try + { + if (string.IsNullOrEmpty(printerCfg.DocName)) throw (new Exception()); + documentName = string.Format(printerCfg.DocName, batch.StartTime, batch.EndTime, batch.BatchNr); + } + catch + { + documentName = string.Format("{0:yyyyMMdd-HHmm}", batch.EndTime); + } + + PrintResults(batch, documentName); + + 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; + } + } + + /// Stop this operation + public void Stop() { } @@ -206,7 +249,7 @@ namespace TBF.Rig.Output.Printers.Enhanced for (int i = 1; i <= printerCfg.NrOfCopies; i++) { string docName = string.Format(((printerCfg.NrOfCopies == 1) ? "{0}" : "{0}-{1}"), documentName, i); - var pd = new Results.Output.Printers.Enhanced.EnhancedPrintDocument(batch, cfg, docName); + var pd = new EnhancedPrintDocument(batch, cfg, docName); if (!string.IsNullOrEmpty(printerCfg.PrinterName)) pd.PrinterSettings.PrinterName = printerCfg.PrinterName; pd.Print(); } diff --git a/TBF/Rig/Output/Printers/Label/Printer.cs b/TBF/Rig/Output/Printers/Label/Printer.cs index 4738211a1..e906bbcd0 100644 --- a/TBF/Rig/Output/Printers/Label/Printer.cs +++ b/TBF/Rig/Output/Printers/Label/Printer.cs @@ -6,6 +6,8 @@ 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 @@ -19,16 +21,13 @@ namespace TBF.Rig.Output.Printers.Label public bool SupressPrinting { get { return (printerCfg.NrOfCopies == 0); } } - /// Data to print - Results.Entities.Batch batch; + Results.Entities.Batch batch; /// Data to print + IList commonItems; /// Items to print + bool printingCompleted = false; + bool statusDlgShown = false; + ModelessForm modelessForm; - /// - /// Items to print - /// - IList commonItems; - - - public Printer() { } + public Printer() { } public Printer(Generic.IComponentCfg cfg) : base(cfg) @@ -39,10 +38,19 @@ namespace TBF.Rig.Output.Printers.Label public override void Initialize() { ApplyConfig(); - log.FatalFormat("{0} initialized: {1}", Name, this); + 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() + void ApplyConfig() { commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.ItemsToPrint); } @@ -108,29 +116,58 @@ namespace TBF.Rig.Output.Printers.Label /// Start this operation public void Start() { - CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; + printingCompleted = false; + statusDlgShown = false; + } - 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; - } - - /// Run this operation - /// Event.ResultsPrinted - public Event Run() + /// Run this operation + /// Event.ResultsPrinted + public Event Run() { - return Event.ResultsPrinted; + 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; + } } /// Stop this operation diff --git a/TBF/Rig/Output/Printers/MultiLabel/Printer.cs b/TBF/Rig/Output/Printers/MultiLabel/Printer.cs index 605311c10..bfef0d6be 100644 --- a/TBF/Rig/Output/Printers/MultiLabel/Printer.cs +++ b/TBF/Rig/Output/Printers/MultiLabel/Printer.cs @@ -3,9 +3,11 @@ /// using System; using System.Collections.Generic; -using log4net; using System.Globalization; using System.Threading; +using log4net; +using Common; +using Common.Forms; using Results.Output.Printers.MultiLabel; namespace TBF.Rig.Output.Printers.MultiLabel @@ -27,8 +29,12 @@ namespace TBF.Rig.Output.Printers.MultiLabel /// IList commonItems; + bool printingCompleted = false; + bool statusDlgShown = false; + ModelessForm modelessForm; - public Printer() { } + + public Printer() { } public Printer(Generic.IComponentCfg cfg) : base(cfg) @@ -39,10 +45,19 @@ namespace TBF.Rig.Output.Printers.MultiLabel public override void Initialize() { ApplyConfig(); - log.FatalFormat("{0} initialized: {1}", Name, this); + 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() + void ApplyConfig() { commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.ItemsToPrint); } @@ -112,27 +127,56 @@ namespace TBF.Rig.Output.Printers.MultiLabel /// Start this operation public void Start() { - CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; + printingCompleted = false; + statusDlgShown = false; + } - 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() + /// Run this operation + /// Event.ResultsPrinted + public Event Run() { - return Event.ResultsPrinted; - } + 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(); + } - /// Stop this operation - public void Stop() + /// Printer is online => Print results now + 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; + + 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; + } + } + + /// Stop this operation + public void Stop() { } diff --git a/TBF/Rig/Output/Printers/Munich/Printer.cs b/TBF/Rig/Output/Printers/Munich/Printer.cs index b1566c565..9de587d89 100644 --- a/TBF/Rig/Output/Printers/Munich/Printer.cs +++ b/TBF/Rig/Output/Printers/Munich/Printer.cs @@ -2,12 +2,12 @@ /// Copyright (c) 2013-2023 Sensus Slovensko a.s. /// using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Printing; using log4net; -using Config.Entities; -using TBF.Rig; +using Common; +using Common.Forms; +using Results.Output.Printers.Munich; +using System.Globalization; +using System.Threading; namespace TBF.Rig.Output.Printers.Munich { @@ -24,6 +24,10 @@ namespace TBF.Rig.Output.Printers.Munich /// Data to print Results.Entities.Batch batch; + bool printingCompleted = false; + bool statusDlgShown = false; + ModelessForm modelessForm; + public string Version { get { return printerCfg.Version; } } @@ -37,14 +41,23 @@ namespace TBF.Rig.Output.Printers.Munich public override void Initialize() { - log.FatalFormat("{0} initialized: {1}", Name, this); + 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); + } } - /// Events: ResultsPrinted - /// Procedure to print the results of - /// Results to print - /// Reference to the operation + /// Events: ResultsPrinted + /// Procedure to print the results of + /// Results to print + /// Reference to the operation public IOperation ProcessResultsOp(Results.Entities.Batch batch) { this.batch = batch; @@ -54,23 +67,53 @@ namespace TBF.Rig.Output.Printers.Munich /// Start this operation public void Start() { - foreach (var wm in batch.WaterMeters) - { - var pd = new Results.Output.Printers.Munich.MunichPrintDocument(printerCfg.Version, wm, printerCfg.PageOrientation); - if (!string.IsNullOrEmpty(printerCfg.PrinterName)) pd.PrinterSettings.PrinterName = printerCfg.PrinterName; - pd.Print(); - } - } + printingCompleted = false; + statusDlgShown = false; + } - /// Run this operation - /// Event.ResultsPrinted - public Event Run() + /// Run this operation + /// Event.ResultsPrinted + public Event Run() { - return Event.ResultsPrinted; - } + 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(); + } - /// Stop this operation - public void Stop() + /// 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; + } + } + + /// Stop this operation + public void Stop() { } } diff --git a/TBF/Rig/Output/Printers/OnePerBatch/Printer.cs b/TBF/Rig/Output/Printers/OnePerBatch/Printer.cs index ea1df70dc..8ebb718be 100644 --- a/TBF/Rig/Output/Printers/OnePerBatch/Printer.cs +++ b/TBF/Rig/Output/Printers/OnePerBatch/Printer.cs @@ -5,9 +5,11 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Threading; +using log4net; +using Common; +using Common.Forms; using Results.Entities; using Results.Output.Printers.OnePerBatch; -using log4net; namespace TBF.Rig.Output.Printers.OnePerBatch { @@ -31,8 +33,12 @@ namespace TBF.Rig.Output.Printers.OnePerBatch IList testItems; string footer; + bool printingCompleted = false; + bool statusDlgShown = false; + ModelessForm modelessForm; - public Printer() { } + + public Printer() { } public Printer(Generic.IComponentCfg cfg) : base(cfg) @@ -43,10 +49,19 @@ namespace TBF.Rig.Output.Printers.OnePerBatch public override void Initialize() { ApplyConfig(); - log.FatalFormat("{0} initialized: {1}", Name, this); + 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() + void ApplyConfig() { header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1); @@ -150,49 +165,78 @@ namespace TBF.Rig.Output.Printers.OnePerBatch /// Start this operation 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; + printingCompleted = false; + statusDlgShown = false; } - /// Run this operation - /// Event.ResultsPrinted - public Event Run() + /// Run this operation + /// Event.ResultsPrinted + public Event Run() { - return Event.ResultsPrinted; - } + 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(); + } - /// Stop this operation - public void Stop() + /// 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 && (!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; + + 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; + } + } + + /// Stop this operation + public void Stop() { } diff --git a/TBF/Rig/Output/Printers/OnePerMeter/Printer.cs b/TBF/Rig/Output/Printers/OnePerMeter/Printer.cs index 3ecb55608..ad9822d55 100644 --- a/TBF/Rig/Output/Printers/OnePerMeter/Printer.cs +++ b/TBF/Rig/Output/Printers/OnePerMeter/Printer.cs @@ -5,9 +5,11 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Threading; +using log4net; +using Common; +using Common.Forms; using Results.Entities; using Results.Output.Printers.OnePerMeter; -using log4net; namespace TBF.Rig.Output.Printers.OnePerMeter { @@ -31,8 +33,12 @@ namespace TBF.Rig.Output.Printers.OnePerMeter IList testItems; string footer; + bool printingCompleted = false; + bool statusDlgShown = false; + ModelessForm modelessForm; - public Printer() { } + + public Printer() { } public Printer(Generic.IComponentCfg cfg) : base(cfg) @@ -43,10 +49,19 @@ namespace TBF.Rig.Output.Printers.OnePerMeter public override void Initialize() { ApplyConfig(); - log.FatalFormat("{0} initialized: {1}", Name, this); + 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() + void ApplyConfig() { header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1); @@ -154,45 +169,74 @@ namespace TBF.Rig.Output.Printers.OnePerMeter /// Start this operation 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, wm.SerialNrAux); - /// {0} {1} {2} {3} {4} {5} - } - catch - { - documentName = string.Format("{0}-{1}", batch.BatchNr, wm.WMPosition); - } - - PrintResults(batch, wm, documentName); - } - } - - Thread.CurrentThread.CurrentCulture = oriCulture; + printingCompleted = false; + statusDlgShown = false; } - /// Run this operation - /// Event.ResultsPrinted - public Event Run() + /// Run this operation + /// Event.ResultsPrinted + public Event Run() { - return Event.ResultsPrinted; - } + 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(); + } - /// Stop this operation - public void Stop() + /// 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 && (!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, wm.SerialNrAux); + /// {0} {1} {2} {3} {4} {5} + } + catch + { + documentName = string.Format("{0}-{1}", batch.BatchNr, wm.WMPosition); + } + + PrintResults(batch, wm, documentName); + } + } + + 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; + } + } + + /// Stop this operation + public void Stop() { } diff --git a/TBF/Rig/Output/Printers/PrinterStatus.cs b/TBF/Rig/Output/Printers/PrinterStatus.cs new file mode 100644 index 000000000..27ec68a1b --- /dev/null +++ b/TBF/Rig/Output/Printers/PrinterStatus.cs @@ -0,0 +1,136 @@ +/// +/// Copyright (c) 2023 Sensus Slovensko a.s. +/// +using System; +using System.Management; + +namespace TBF.Rig.Output.Printers +{ + public static class PrinterStatus + { + /// + /// See also https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printer + /// + + public enum DetectedErrorState : UInt16 + { + Unknown = 0, + Other = 1, + No_Error = 2, + Low_Paper = 3, + No_Paper = 4, + Low_Toner = 5, + No_Toner = 6, + Door_Open = 7, + Jammed = 8, + Offline = 9, + Service_Requested = 10, + Output_Bin_Full = 11, + } + + public enum ExtendedPrinterStatus : UInt16 + { + Other = 1, + Unknown = 2, + Idle = 3, + Printing = 4, + Warming_Up = 5, + Stopped_Printing = 6, + Offline = 7, + Paused = 8, + Error = 9, + Busy = 10, + Not_Aavailable = 11, + Waiting = 12, + Processing = 13, + Initialization = 14, + Power_Save = 15, + Pending_Deletion = 16, + IO_Active = 17, + Manual_Feed = 18, + } + + public static void GetStatus(string printerName) + { + /// Perform the search for printers and return the listing as a collection + var mgmtSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"); + var mgmtCollection = mgmtSearcher.Get(); + + foreach (ManagementObject wmiObj in mgmtCollection) + { + if (wmiObj["Name"].ToString().ToLower() == printerName.ToLower()) + { + /// Management object name fits the printer name + + var state = (ExtendedPrinterStatus)int.Parse(wmiObj["ExtendedPrinterStatus"].ToString()); + if (state == ExtendedPrinterStatus.Other || + state == ExtendedPrinterStatus.Unknown || + state == ExtendedPrinterStatus.Offline || + state == ExtendedPrinterStatus.Error || + state == ExtendedPrinterStatus.Not_Aavailable) + { + var message = string.Format("Printer '{0}' state is '{1}'", printerName, state.ToString().Replace('_', ' ')); + throw new ApplicationException(message); + } + + var error = (DetectedErrorState)int.Parse(wmiObj["DetectedErrorState"].ToString()); + if (error != DetectedErrorState.No_Error) + { + var message = string.Format("Printer '{0}' error state is '{1}'", printerName, error.ToString().Replace('_', ' ')); + throw new ApplicationException(message); + } + } + } + } + + /// + /// Returns true when printer is online, without errors. + /// + /// Printer name + /// Error message when printer is not online + /// true when online, false when there are any errors or printer is not online + public static bool IsOnline(string printerName, out string message) + { + // TODO: Uncomment the following line and resolve problems : Unknown status/error always returned + // if (string.IsNullOrEmpty(printerName)) + { + /// Default printer + message = string.Empty; + return true; + } + + /// Perform the search for printers and return the listing as a collection + var mgmtSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"); + var mgmtCollection = mgmtSearcher.Get(); + + foreach (ManagementObject wmiObj in mgmtCollection) + { + if (wmiObj["Name"].ToString().ToLower() == printerName.ToLower()) + { + /// Management object name fits the printer name + + var state = (ExtendedPrinterStatus)int.Parse(wmiObj["ExtendedPrinterStatus"].ToString()); + if (state == ExtendedPrinterStatus.Other || + state == ExtendedPrinterStatus.Unknown || + state == ExtendedPrinterStatus.Offline || + state == ExtendedPrinterStatus.Error || + state == ExtendedPrinterStatus.Not_Aavailable) + { + message = string.Format("Printer '{0}' state is '{1}'", printerName, state.ToString().Replace('_', ' ')); + return false; + } + + var error = (DetectedErrorState)int.Parse(wmiObj["DetectedErrorState"].ToString()); + if (error != DetectedErrorState.No_Error) + { + message = string.Format("Printer '{0}' error state is '{1}'", printerName, error.ToString().Replace('_', ' ')); + return false; + } + } + } + + message = string.Empty; + return true; + } + } +} diff --git a/TBF/Rig/Output/Printers/Zapiska/Printer.cs b/TBF/Rig/Output/Printers/Zapiska/Printer.cs index 749c70290..8289c9ae2 100644 --- a/TBF/Rig/Output/Printers/Zapiska/Printer.cs +++ b/TBF/Rig/Output/Printers/Zapiska/Printer.cs @@ -5,8 +5,10 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Threading; -using Results.Output.Printers.Zapiska; using log4net; +using Common; +using Common.Forms; +using Results.Output.Printers.Zapiska; namespace TBF.Rig.Output.Printers.Zapiska { @@ -30,8 +32,12 @@ namespace TBF.Rig.Output.Printers.Zapiska IList testItems; string footer; + bool printingCompleted = false; + bool statusDlgShown = false; + ModelessForm modelessForm; - public Printer() { } + + public Printer() { } public Printer(Generic.IComponentCfg cfg) : base(cfg) @@ -42,10 +48,19 @@ namespace TBF.Rig.Output.Printers.Zapiska public override void Initialize() { ApplyConfig(); - log.FatalFormat("{0} initialized: {1}", Name, this); + 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() + void ApplyConfig() { header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1); @@ -137,38 +152,67 @@ namespace TBF.Rig.Output.Printers.Zapiska /// Start this operation public void Start() { - CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; - - if (printerCfg.Culture != Culture.system) - { - Thread.CurrentThread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString()); - } - - string documentName; - try - { - if (string.IsNullOrEmpty(printerCfg.DocName)) throw (new Exception()); - documentName = string.Format(printerCfg.DocName, batch.StartTime, batch.EndTime, batch.BatchNr); - } - catch - { - documentName = string.Format("{0:yyyyMMdd-HHmm}", batch.EndTime); - } - - PrintResults(batch, documentName); - - Thread.CurrentThread.CurrentCulture = oriCulture; + printingCompleted = false; + statusDlgShown = false; } - /// Run this operation - /// Event.ResultsPrinted - public Event Run() + /// Run this operation + /// Event.ResultsPrinted + public Event Run() { - return Event.ResultsPrinted; - } + 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(); + } - /// Stop this operation - public void Stop() + /// Printer is online => Print results now + CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; + if (printerCfg.Culture != Culture.system) + { + Thread.CurrentThread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString()); + } + + string documentName; + try + { + if (string.IsNullOrEmpty(printerCfg.DocName)) throw (new Exception()); + documentName = string.Format(printerCfg.DocName, batch.StartTime, batch.EndTime, batch.BatchNr); + } + catch + { + documentName = string.Format("{0:yyyyMMdd-HHmm}", batch.EndTime); + } + + PrintResults(batch, documentName); + + 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; + } + } + + /// Stop this operation + public void Stop() { } @@ -215,7 +259,7 @@ namespace TBF.Rig.Output.Printers.Zapiska Footer = printerCfg.Footer, }; - var pd = new Results.Output.Printers.Zapiska.ZapiskaPrintDocument(batch, cfg, documentName); + var pd = new ZapiskaPrintDocument(batch, cfg, documentName); if (!string.IsNullOrEmpty(printerCfg.PrinterName)) pd.PrinterSettings.PrinterName = printerCfg.PrinterName; pd.Print(); } diff --git a/TBF/TBF.csproj b/TBF/TBF.csproj index f66437d1f..7552c4873 100644 --- a/TBF/TBF.csproj +++ b/TBF/TBF.csproj @@ -145,6 +145,7 @@ + @@ -1189,6 +1190,7 @@ PrinterCfgCtrl.cs +