You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.7 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using SlnMesnac.LabelPrint.BarTender;
using SlnMesnac.LabelPrint.Common;
using SlnMesnac.LabelPrint.Log4net;
using SlnMesnac.LabelPrint.TaskQueue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SlnMesnac.LabelPrint.Timer
{
public class PrintTimer
{
private readonly LogHelper logHelper = LogHelper.Instance;
private readonly TaskHelper taskHelper = TaskHelper.Instance;
private readonly PrintManager printManager = PrintManager.Instance;
private readonly JsonChange jsonChange = JsonChange.Instance;
private System.Timers.Timer timer = new System.Timers.Timer(5000);
/// <summary>
/// 日志刷新
/// </summary>
/// <param name="massage"></param>
public delegate void LogRefresh(string massage);
public event LogRefresh LogRefreshEvent;
#region 单例实现
private static readonly Lazy<PrintTimer> lazy = new Lazy<PrintTimer>(() => new PrintTimer());
public static PrintTimer Instance
{
get
{
return lazy.Value;
}
}
#endregion
private PrintTimer() { }
/// <summary>
/// 开启自动打印
/// </summary>
/// <returns></returns>
public bool StartPrint()
{
bool result = false;
try
{
if (!timer.Enabled)
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(ReadRfidByTimer);
timer.AutoReset = true;
timer.Enabled = false;
timer.Start();
this.PrintMessageToListBox("开启定时任务自动执行标签打印");
}
result = true;
}catch(Exception ex)
{
logHelper.Error("自动打印定时任务启动异常", ex);
this.PrintMessageToListBox($"自动打印定时任务启动异常:{ex.Message}");
}
return result;
}
private void ReadRfidByTimer(object source, System.Timers.ElapsedEventArgs e)
{
try
{
var info = taskHelper.GetTask();
if (info == null)
{
this.PrintMessageToListBox("未获取到需要打印的任务");
}
else
{
this.PrintMessageToListBox($"开始打印标签信息:{jsonChange.ModeToJson(info)}");
Thread.Sleep(200);
if (info.column_A == null)
{
this.PrintMessageToListBox($"{info.productType}打印任务执行失败A列数据为空");
return;
}
var printResult = printManager.Print(info);
if (printResult)
{
this.PrintMessageToListBox($"{info.productType}打印{info.column_A.ToString()};打印成功");
}
else
{
this.PrintMessageToListBox($"{info.column_A.ToString()};打印失败");
}
taskHelper.RemoveTask(info);
}
}catch(Exception ex)
{
logHelper.Error("自动打印任务执行异常", ex);
this.PrintMessageToListBox($"自动打印任务执行异常:{ex.Message}");
}
}
/// <summary>
/// 停止自动打印
/// </summary>
/// <returns></returns>
public bool StopPrint()
{
bool result = false;
try
{
this.PrintMessageToListBox("停止自动打印,并释放定时任务");
if (timer.Enabled)
{
timer.Stop();
timer.Close();
timer.Dispose();
timer = new System.Timers.Timer(5000);
}
result = true;
}
catch (Exception ex)
{
logHelper.Error("自动打印定时任务停止异常", ex);
this.PrintMessageToListBox($"自动打印定时任务停止异常:{ex.Message}");
}
return result;
}
/// <summary>
/// 日志打印
/// </summary>
/// <param name="message"></param>
private void PrintMessageToListBox(string message)
{
LogRefreshEvent?.Invoke(message);
}
}
}