1. PrinterStatus implemented, used by printers, shown in ModelessDlg, 2. 'Simulated' printers, 3. Label.Printer uses wm.PrintLabel.

This commit is contained in:
Milan Hanajik 2023-11-30 17:51:49 +01:00
parent a625e2dfdb
commit a1df68b888
9 changed files with 660 additions and 223 deletions

View File

@ -3,9 +3,10 @@
/// ///
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing.Printing;
using System.Globalization; using System.Globalization;
using System.Threading; using System.Threading;
using Common;
using Common.Forms;
using Results.Output.Printers.Enhanced; using Results.Output.Printers.Enhanced;
using log4net; using log4net;
@ -31,8 +32,12 @@ namespace TBF.Rig.Output.Printers.Enhanced
IList<Results.WMeterRsltItemSpec> testItems; IList<Results.WMeterRsltItemSpec> testItems;
string footer; string footer;
bool printingCompleted = false;
bool statusDlgShown = false;
ModelessForm modelessForm;
public Printer() { }
public Printer() { }
public Printer(Generic.IComponentCfg cfg) public Printer(Generic.IComponentCfg cfg)
: base(cfg) : base(cfg)
@ -43,10 +48,19 @@ namespace TBF.Rig.Output.Printers.Enhanced
public override void Initialize() public override void Initialize()
{ {
ApplyConfig(); 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); header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine);
commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems);
@ -132,38 +146,67 @@ namespace TBF.Rig.Output.Printers.Enhanced
/// <summary>Start this operation</summary> /// <summary>Start this operation</summary>
public void Start() 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());
}
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;
} }
/// <summary>Run this operation</summary> /// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns> /// <returns>Event.ResultsPrinted</returns>
public Event Run() 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();
}
/// <summary>Stop this operation</summary> /// Printer is online => Print results now
public void Stop() 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;
}
}
/// <summary>Stop this operation</summary>
public void Stop()
{ {
} }
@ -206,7 +249,7 @@ namespace TBF.Rig.Output.Printers.Enhanced
for (int i = 1; i <= printerCfg.NrOfCopies; i++) for (int i = 1; i <= printerCfg.NrOfCopies; i++)
{ {
string docName = string.Format(((printerCfg.NrOfCopies == 1) ? "{0}" : "{0}-{1}"), documentName, 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; if (!string.IsNullOrEmpty(printerCfg.PrinterName)) pd.PrinterSettings.PrinterName = printerCfg.PrinterName;
pd.Print(); pd.Print();
} }

View File

@ -6,6 +6,8 @@ using System.Collections.Generic;
using log4net; using log4net;
using System.Globalization; using System.Globalization;
using System.Threading; using System.Threading;
using Common;
using Common.Forms;
using Results.Output.Printers.Label; using Results.Output.Printers.Label;
namespace TBF.Rig.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); } } public bool SupressPrinting { get { return (printerCfg.NrOfCopies == 0); } }
/// <summary> Data to print </summary> Results.Entities.Batch batch; /// Data to print
Results.Entities.Batch batch; IList<Results.WMeterRsltItemSpec> commonItems; /// Items to print
bool printingCompleted = false;
bool statusDlgShown = false;
ModelessForm modelessForm;
/// public Printer() { }
/// Items to print
///
IList<Results.WMeterRsltItemSpec> commonItems;
public Printer() { }
public Printer(Generic.IComponentCfg cfg) public Printer(Generic.IComponentCfg cfg)
: base(cfg) : base(cfg)
@ -39,10 +38,19 @@ namespace TBF.Rig.Output.Printers.Label
public override void Initialize() public override void Initialize()
{ {
ApplyConfig(); 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); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.ItemsToPrint);
} }
@ -108,29 +116,58 @@ namespace TBF.Rig.Output.Printers.Label
/// <summary>Start this operation</summary> /// <summary>Start this operation</summary>
public void Start() public void Start()
{ {
CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; printingCompleted = false;
statusDlgShown = false;
}
if (printerCfg.Culture != Culture.system) /// <summary>Run this operation</summary>
{ /// <returns>Event.ResultsPrinted</returns>
Thread.CurrentThread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString()); public Event Run()
}
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; 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> /// <summary>Stop this operation</summary>

View File

@ -3,9 +3,11 @@
/// ///
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using log4net;
using System.Globalization; using System.Globalization;
using System.Threading; using System.Threading;
using log4net;
using Common;
using Common.Forms;
using Results.Output.Printers.MultiLabel; using Results.Output.Printers.MultiLabel;
namespace TBF.Rig.Output.Printers.MultiLabel namespace TBF.Rig.Output.Printers.MultiLabel
@ -27,8 +29,12 @@ namespace TBF.Rig.Output.Printers.MultiLabel
/// ///
IList<Results.WMeterRsltItemSpec> commonItems; IList<Results.WMeterRsltItemSpec> commonItems;
bool printingCompleted = false;
bool statusDlgShown = false;
ModelessForm modelessForm;
public Printer() { }
public Printer() { }
public Printer(Generic.IComponentCfg cfg) public Printer(Generic.IComponentCfg cfg)
: base(cfg) : base(cfg)
@ -39,10 +45,19 @@ namespace TBF.Rig.Output.Printers.MultiLabel
public override void Initialize() public override void Initialize()
{ {
ApplyConfig(); 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); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.ItemsToPrint);
} }
@ -112,27 +127,56 @@ namespace TBF.Rig.Output.Printers.MultiLabel
/// <summary>Start this operation</summary> /// <summary>Start this operation</summary>
public void Start() public void Start()
{ {
CultureInfo oriCulture = Thread.CurrentThread.CurrentCulture; printingCompleted = false;
statusDlgShown = false;
}
if (printerCfg.Culture != Culture.system) /// <summary>Run this operation</summary>
{ /// <returns>Event.ResultsPrinted</returns>
Thread.CurrentThread.CurrentCulture = new CultureInfo(printerCfg.Culture.ToString()); public Event Run()
}
PrintResults(batch, string.Format("{0}", batch.BatchNr));
Thread.CurrentThread.CurrentCulture = oriCulture;
}
/// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns>
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();
}
/// <summary>Stop this operation</summary> /// Printer is online => Print results now
public void Stop() 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;
}
}
/// <summary>Stop this operation</summary>
public void Stop()
{ {
} }

View File

@ -2,12 +2,12 @@
/// Copyright (c) 2013-2023 Sensus Slovensko a.s. /// Copyright (c) 2013-2023 Sensus Slovensko a.s.
/// ///
using System; using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using log4net; using log4net;
using Config.Entities; using Common;
using TBF.Rig; using Common.Forms;
using Results.Output.Printers.Munich;
using System.Globalization;
using System.Threading;
namespace TBF.Rig.Output.Printers.Munich namespace TBF.Rig.Output.Printers.Munich
{ {
@ -24,6 +24,10 @@ namespace TBF.Rig.Output.Printers.Munich
/// <summary> Data to print </summary> /// <summary> Data to print </summary>
Results.Entities.Batch batch; Results.Entities.Batch batch;
bool printingCompleted = false;
bool statusDlgShown = false;
ModelessForm modelessForm;
public string Version { get { return printerCfg.Version; } } public string Version { get { return printerCfg.Version; } }
@ -37,14 +41,23 @@ namespace TBF.Rig.Output.Printers.Munich
public override void Initialize() 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);
}
} }
/// <summary>Events: ResultsPrinted</summary> /// <summary>Events: ResultsPrinted</summary>
/// <param name="benchId">Procedure to print the results of</param> /// <param name="benchId">Procedure to print the results of</param>
/// <param name="results">Results to print</param> /// <param name="results">Results to print</param>
/// <returns>Reference to the operation</returns> /// <returns>Reference to the operation</returns>
public IOperation ProcessResultsOp(Results.Entities.Batch batch) public IOperation ProcessResultsOp(Results.Entities.Batch batch)
{ {
this.batch = batch; this.batch = batch;
@ -54,23 +67,53 @@ namespace TBF.Rig.Output.Printers.Munich
/// <summary>Start this operation</summary> /// <summary>Start this operation</summary>
public void Start() public void Start()
{ {
foreach (var wm in batch.WaterMeters) printingCompleted = false;
{ statusDlgShown = false;
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();
}
}
/// <summary>Run this operation</summary> /// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns> /// <returns>Event.ResultsPrinted</returns>
public Event Run() 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();
}
/// <summary>Stop this operation</summary> /// Printer is online => Print results now
public void Stop() 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()
{ {
} }
} }

View File

@ -5,9 +5,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Threading; using System.Threading;
using log4net;
using Common;
using Common.Forms;
using Results.Entities; using Results.Entities;
using Results.Output.Printers.OnePerBatch; using Results.Output.Printers.OnePerBatch;
using log4net;
namespace TBF.Rig.Output.Printers.OnePerBatch namespace TBF.Rig.Output.Printers.OnePerBatch
{ {
@ -31,8 +33,12 @@ namespace TBF.Rig.Output.Printers.OnePerBatch
IList<Results.WMeterRsltItemSpec> testItems; IList<Results.WMeterRsltItemSpec> testItems;
string footer; string footer;
bool printingCompleted = false;
bool statusDlgShown = false;
ModelessForm modelessForm;
public Printer() { }
public Printer() { }
public Printer(Generic.IComponentCfg cfg) public Printer(Generic.IComponentCfg cfg)
: base(cfg) : base(cfg)
@ -43,10 +49,19 @@ namespace TBF.Rig.Output.Printers.OnePerBatch
public override void Initialize() public override void Initialize()
{ {
ApplyConfig(); 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); header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine);
commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1);
@ -150,49 +165,78 @@ namespace TBF.Rig.Output.Printers.OnePerBatch
/// <summary>Start this operation</summary> /// <summary>Start this operation</summary>
public void Start() 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 && !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> /// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns> /// <returns>Event.ResultsPrinted</returns>
public Event Run() 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();
}
/// <summary>Stop this operation</summary> /// Printer is online => Print results now
public void Stop() 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;
}
}
/// <summary>Stop this operation</summary>
public void Stop()
{ {
} }

View File

@ -5,9 +5,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Threading; using System.Threading;
using log4net;
using Common;
using Common.Forms;
using Results.Entities; using Results.Entities;
using Results.Output.Printers.OnePerMeter; using Results.Output.Printers.OnePerMeter;
using log4net;
namespace TBF.Rig.Output.Printers.OnePerMeter namespace TBF.Rig.Output.Printers.OnePerMeter
{ {
@ -31,8 +33,12 @@ namespace TBF.Rig.Output.Printers.OnePerMeter
IList<Results.WMeterRsltItemSpec> testItems; IList<Results.WMeterRsltItemSpec> testItems;
string footer; string footer;
bool printingCompleted = false;
bool statusDlgShown = false;
ModelessForm modelessForm;
public Printer() { }
public Printer() { }
public Printer(Generic.IComponentCfg cfg) public Printer(Generic.IComponentCfg cfg)
: base(cfg) : base(cfg)
@ -43,10 +49,19 @@ namespace TBF.Rig.Output.Printers.OnePerMeter
public override void Initialize() public override void Initialize()
{ {
ApplyConfig(); 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); header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine);
commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1);
@ -154,45 +169,74 @@ namespace TBF.Rig.Output.Printers.OnePerMeter
/// <summary>Start this operation</summary> /// <summary>Start this operation</summary>
public void Start() 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 && !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;
} }
/// <summary>Run this operation</summary> /// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns> /// <returns>Event.ResultsPrinted</returns>
public Event Run() 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();
}
/// <summary>Stop this operation</summary> /// Printer is online => Print results now
public void Stop() 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;
}
}
/// <summary>Stop this operation</summary>
public void Stop()
{ {
} }

View File

@ -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);
}
}
}
}
/// <summary>
/// Returns true when printer is online, without errors.
/// </summary>
/// <param name="printerName">Printer name</param>
/// <param name="message">Error message when printer is not online</param>
/// <returns>true when online, false when there are any errors or printer is not online</returns>
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;
}
}
}

View File

@ -5,8 +5,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Threading; using System.Threading;
using Results.Output.Printers.Zapiska;
using log4net; using log4net;
using Common;
using Common.Forms;
using Results.Output.Printers.Zapiska;
namespace TBF.Rig.Output.Printers.Zapiska namespace TBF.Rig.Output.Printers.Zapiska
{ {
@ -30,8 +32,12 @@ namespace TBF.Rig.Output.Printers.Zapiska
IList<Results.WMeterRsltItemSpec> testItems; IList<Results.WMeterRsltItemSpec> testItems;
string footer; string footer;
bool printingCompleted = false;
bool statusDlgShown = false;
ModelessForm modelessForm;
public Printer() { }
public Printer() { }
public Printer(Generic.IComponentCfg cfg) public Printer(Generic.IComponentCfg cfg)
: base(cfg) : base(cfg)
@ -42,10 +48,19 @@ namespace TBF.Rig.Output.Printers.Zapiska
public override void Initialize() public override void Initialize()
{ {
ApplyConfig(); 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); header = string.IsNullOrEmpty(printerCfg.Header) ? string.Empty : printerCfg.Header.Replace("~", Environment.NewLine);
commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1); commonItems = Results.WMeterRsltItemSpec.FromStrArray(printerCfg.CommonItems1);
@ -137,38 +152,67 @@ namespace TBF.Rig.Output.Printers.Zapiska
/// <summary>Start this operation</summary> /// <summary>Start this operation</summary>
public void Start() 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());
}
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;
} }
/// <summary>Run this operation</summary> /// <summary>Run this operation</summary>
/// <returns>Event.ResultsPrinted</returns> /// <returns>Event.ResultsPrinted</returns>
public Event Run() 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();
}
/// <summary>Stop this operation</summary> /// Printer is online => Print results now
public void Stop() 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;
}
}
/// <summary>Stop this operation</summary>
public void Stop()
{ {
} }
@ -215,7 +259,7 @@ namespace TBF.Rig.Output.Printers.Zapiska
Footer = printerCfg.Footer, 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; if (!string.IsNullOrEmpty(printerCfg.PrinterName)) pd.PrinterSettings.PrinterName = printerCfg.PrinterName;
pd.Print(); pd.Print();
} }

View File

@ -145,6 +145,7 @@
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Deployment" /> <Reference Include="System.Deployment" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Numerics" /> <Reference Include="System.Numerics" />
<Reference Include="System.Security" /> <Reference Include="System.Security" />
@ -1189,6 +1190,7 @@
<Compile Include="Rig\Output\Printers\OnePerMeter\PrinterCfgCtrl.designer.cs"> <Compile Include="Rig\Output\Printers\OnePerMeter\PrinterCfgCtrl.designer.cs">
<DependentUpon>PrinterCfgCtrl.cs</DependentUpon> <DependentUpon>PrinterCfgCtrl.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Rig\Output\Printers\PrinterStatus.cs" />
<Compile Include="Rig\Output\Printers\Zapiska\FactoryCompound.cs" /> <Compile Include="Rig\Output\Printers\Zapiska\FactoryCompound.cs" />
<Compile Include="Rig\Output\Printers\Zapiska\FactoryHeatMeters.cs" /> <Compile Include="Rig\Output\Printers\Zapiska\FactoryHeatMeters.cs" />
<Compile Include="Rig\Output\Printers\Zapiska\FactorySingle.cs" /> <Compile Include="Rig\Output\Printers\Zapiska\FactorySingle.cs" />