MainMachine _Printer.cs 3.5 KB
using CodeLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeviceLibrary
{
    partial class MainMachine
    {

        
        //Asa.PrintLabel PrintBean = null;
        public void InitPrint()
        {
            //RobotManage.PrintBean = new Asa.PrintLabel(Application.StartupPath + "\\Label");
            RobotManage.PrintBean.PrintStatusChanged += Print_PrintStatusChanged;
            /*
            System.Drawing.Printing.PrintDocument print = new System.Drawing.Printing.PrintDocument();
            string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
            LogUtil.info("PrintLabel 本机默认打印机:" + sDefault);
            int index = 1;
            foreach (string sPrint in System.Drawing.Printing.PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                LogUtil.info("PrintLabel 打印机_" + index + "_名称:" + sPrint);
                index++;
            }*/
        }

        public Asa.PrintLabel.PrinterStatus LastPrintStatus = Asa.PrintLabel.PrinterStatus.Unknown;
        void Print_PrintStatusChanged(Asa.PrintLabel.PrinterStatus sta, string msg)
        {
            if (sta.Equals(LastPrintStatus).Equals(false))
            {
                LogUtil.info("PrintLabel 收到打印机新状态:【" + sta + "】【" + msg + "】,替换原来的状态【" + LastPrintStatus + "】 ");
            }
            else
            {
                if (sta == Asa.PrintLabel.PrinterStatus.Idle) {
                    Task.Run(()=> {
                        while (true)
                        {
                            if (LabelMoveInfo.IsStep(MoveStep.Lbl_WaitPrint))
                            {
                                LabelMoveInfo.NextMoveStep(MoveStep.Lbl_Printted);
                                break;
                            }
                            Task.Delay(500).Wait();
                        }
                    });
                }
                LogUtil.info(" PrintLabel 收到打印机新状态:【" + sta + "】【" + msg + "】,与之前状态一样 ");
            }
            LastPrintStatus = sta;
        }
        public string LastPrintLabel="";
        void StartPrintLabel(string wareCode, string wareCount, string FactoryCode = "")
        {
            try
            {
                LastPrintStatus= Asa.PrintLabel.PrinterStatus.Unknown;
                LogUtil.info("调用 PrintLabel 打印标签 StartPrintLabel  ,[" + wareCode + "] [" + wareCount + "]  开始");
                Dictionary<string, string> text = new Dictionary<string, string>();
                text.Add("Code", wareCode);
                text.Add("Count", wareCount);
                text.Add("DateTime", System.DateTime.Now.ToString("F"));
                text.Add("FactoryCode", FactoryCode);

                LastPrintLabel = wareCode;
                RobotManage.PrintBean.Print(text);
                LogUtil.info("PrintLabel 打印标签 StartPrintLabel 结束  ");
            }
            catch (Exception ex)
            {
                LogUtil.error("PrintLabel 打印标签 StartPrintLabel 错误:" + ex.ToString());
            }
        }

        public Task DoPrint(string aaa) {

            return Task.Run(() =>
            {
                StartPrintLabel(aaa, aaa, aaa);
            });
        }

    }
}