diff --git a/SlnMesnac.LabelPrint.Timer/PrintTimer.cs b/SlnMesnac.LabelPrint.Timer/PrintTimer.cs new file mode 100644 index 0000000..a8653bc --- /dev/null +++ b/SlnMesnac.LabelPrint.Timer/PrintTimer.cs @@ -0,0 +1,145 @@ +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(2000); + + /// + /// 日志刷新 + /// + /// + public delegate void LogRefresh(string massage); + public event LogRefresh LogRefreshEvent; + + #region 单例实现 + private static readonly Lazy lazy = new Lazy(() => new PrintTimer()); + + public static PrintTimer Instance + { + get + { + return lazy.Value; + } + } + #endregion + + private PrintTimer() { } + + /// + /// 开启自动打印 + /// + /// + 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}"); + } + } + + /// + /// 停止自动打印 + /// + /// + public bool StopPrint() + { + bool result = false; + + try + { + this.PrintMessageToListBox("停止自动打印,并释放定时任务"); + if (timer.Enabled) + { + timer.Stop(); + timer.Close(); + timer.Dispose(); + timer = new System.Timers.Timer(2000); + } + result = true; + } + catch (Exception ex) + { + logHelper.Error("自动打印定时任务停止异常", ex); + this.PrintMessageToListBox($"自动打印定时任务停止异常:{ex.Message}"); + } + return result; + } + + /// + /// 日志打印 + /// + /// + private void PrintMessageToListBox(string message) + { + LogRefreshEvent?.Invoke(message); + } + } +} diff --git a/SlnMesnac.LabelPrint.Timer/Properties/AssemblyInfo.cs b/SlnMesnac.LabelPrint.Timer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0c335c9 --- /dev/null +++ b/SlnMesnac.LabelPrint.Timer/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("SlnMesnac.LabelPrint.Timer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SlnMesnac.LabelPrint.Timer")] +[assembly: AssemblyCopyright("Copyright © 2023")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("7e94f635-c22c-4b38-94ec-6f21842e1333")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SlnMesnac.LabelPrint.Timer/SlnMesnac.LabelPrint.Timer.csproj b/SlnMesnac.LabelPrint.Timer/SlnMesnac.LabelPrint.Timer.csproj new file mode 100644 index 0000000..c67d4a4 --- /dev/null +++ b/SlnMesnac.LabelPrint.Timer/SlnMesnac.LabelPrint.Timer.csproj @@ -0,0 +1,74 @@ + + + + + Debug + AnyCPU + {7E94F635-C22C-4B38-94EC-6F21842E1333} + Library + Properties + SlnMesnac.LabelPrint.Timer + SlnMesnac.LabelPrint.Timer + v4.7.2 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + x64 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + {6a110611-3f4c-46c2-a981-23a0145b1e9f} + SlnMesnac.LabelPrint.BarTender + + + {7232269d-60af-45f0-b614-8f9c4061c529} + SlnMesnac.LabelPrint.Common + + + {fd6aa76e-79b1-4a0c-9cea-6ce09ccbdf9d} + SlnMesnac.LabelPrint.Log4net + + + {B68E5B08-F3E3-4AC0-A6ED-C31646956A0C} + SlnMesnac.LabelPrint.Model + + + {44b60510-5d95-4c48-949b-973b9b29dba6} + SlnMesnac.LabelPrint.TaskQueue + + + + \ No newline at end of file diff --git a/SlnMesnac.LabelPrint.sln b/SlnMesnac.LabelPrint.sln index 1f0d433..c377b4d 100644 --- a/SlnMesnac.LabelPrint.sln +++ b/SlnMesnac.LabelPrint.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnMesnac.LabelPrint.Log4ne EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnMesnac.LabelPrint.TaskQueue", "SlnMesnac.LabelPrint.TaskQueue\SlnMesnac.LabelPrint.TaskQueue.csproj", "{44B60510-5D95-4C48-949B-973B9B29DBA6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnMesnac.LabelPrint.Timer", "SlnMesnac.LabelPrint.Timer\SlnMesnac.LabelPrint.Timer.csproj", "{7E94F635-C22C-4B38-94EC-6F21842E1333}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,10 @@ Global {44B60510-5D95-4C48-949B-973B9B29DBA6}.Debug|Any CPU.Build.0 = Debug|Any CPU {44B60510-5D95-4C48-949B-973B9B29DBA6}.Release|Any CPU.ActiveCfg = Release|Any CPU {44B60510-5D95-4C48-949B-973B9B29DBA6}.Release|Any CPU.Build.0 = Release|Any CPU + {7E94F635-C22C-4B38-94EC-6F21842E1333}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7E94F635-C22C-4B38-94EC-6F21842E1333}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7E94F635-C22C-4B38-94EC-6F21842E1333}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7E94F635-C22C-4B38-94EC-6F21842E1333}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SlnMesnac.LabelPrint/MainWindow.xaml b/SlnMesnac.LabelPrint/MainWindow.xaml index 5ca83e3..69c76b5 100644 --- a/SlnMesnac.LabelPrint/MainWindow.xaml +++ b/SlnMesnac.LabelPrint/MainWindow.xaml @@ -22,9 +22,10 @@ -