MainMachine_PrinterProcess.cs 4.6 KB
using OnlineStore;
using CodeLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Threading.Tasks;

namespace DeviceLibrary
{
    partial class MainMachine
    {
        void SetPrintJob(MoveInfo reel) {
            reel.log("设置打印任务:"+reel.MoveParam.ToDetailStr());
            PrintJob = reel.MoveParam.clone();

            var cc = PrintJob.codeInfos.Select((a) => a.CodeType + ":" + a.CodeStr);
            CountMoveInfo.log("打印时 Code:" + String.Join("##", cc));
        }

        readonly MoveInfo PrintMoveInfo = new MoveInfo("打印机", false);
        ReelParam PrintJob = null;
        ReelParam CurrentPrintJob = null;
        Task<(bool, string)> PrintTask = null;

        bool IsLabelPrinted = false;
        string printerrormsg = "";
        void PrinterProcess()
        {
            if (CheckWait(PrintMoveInfo))
                return;
            switch (PrintMoveInfo.MoveStep)
            {
                case MoveStep.Wait:
                    if (PrintJob != null)
                    {
                        CurrentPrintJob = PrintJob.clone();
                        PrintJob = null;
                        PrintMoveInfo.NextMoveStep(MoveStep.Print_01);
                    }
                    break;
                case MoveStep.Print_01:
                    PrintMoveInfo.NextMoveStep(MoveStep.Print_02);
                    IsLabelPrinted = false;
                    PrintTask = RobotManage.printerHelper.PrintLabel(CurrentPrintJob);                    
                    PrintMoveInfo.log("检测到打印任务开始打印:" + CurrentPrintJob.ToDetailStr());
                    break;
                case MoveStep.Print_02:
                    if (PrintTask.IsCompleted)
                    {
                        var (result, msg) = PrintTask.Result;
                        if (result)
                        {
                            PrintMoveInfo.NextMoveStep(MoveStep.Print_04);
                            PrintMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
                            PrintMoveInfo.log("打印成功,等待标签被取走");
                            printerrormsg = "";
                        }
                        else
                        {
                            Msg.add("打印机反馈:" + msg, MsgLevel.alarm);
                            printerrormsg = msg;
                            PrintMoveInfo.NextMoveStep(MoveStep.Print_01);
                            PrintMoveInfo.log("打印失败,再次打印:" + msg);
                        }
                    }
                    else if (!string.IsNullOrEmpty(printerrormsg))
                    {
                        Msg.add("打印失败,再次打印.印机反馈:" + printerrormsg, MsgLevel.alarm);

                    }
                    else if (PrintMoveInfo.IsTimeOut(30)) {
                        Msg.add("打印标签超时请检查打印机状态", MsgLevel.alarm);
                    }
                    break;
                case MoveStep.Print_04:
                    PrintMoveInfo.NextMoveStep(MoveStep.Print_WaitTakeLabel);
                    //PrintTask = RobotManage.printerHelper.IsLabelOnPeeler();
                    PrintMoveInfo.log("查询剥离器状态");
                    break;
                case MoveStep.Print_WaitTakeLabel:
                    //if (PrintTask.IsCompleted)
                    //{
                    //    if (PrintTask.Result.Item1)
                    //    {
                    //        PrintMoveInfo.log("等待标签被取走");
                    //        PrintMoveInfo.NextMoveStep(MoveStep.Print_04);
                    //        IsLabelPrinted = true;
                    //    }
                    //    else if (IsLabelPrinted)
                    //    {
                    //        PrintMoveInfo.log("标签已被取走");
                    //        PrintMoveInfo.NextMoveStep(MoveStep.Print_06);
                    //    }
                    //}
                    break;
                case MoveStep.Print_LabelTaked:
                    PrintMoveInfo.log("标签已取走");
                    CurrentPrintJob = null;
                    RobotManage.printerHelper.Close();
                    PrintMoveInfo.NextMoveStep(MoveStep.Wait);
                    PrintMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                    break;
                default:
                    LabelMoveInfo.log($"未找到对应步骤:{LabelMoveInfo.MoveStep}");
                    break;
            }
        }

    }
}