PrinterHelper.cs 2.4 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeviceLibrary
{
    public class PrinterHelper
    {
        public Asa.PrintLabel print;
        ZebraPrinterHelper.ZebraManger zebraManger;
        string Port;
        public PrinterHelper()
        {
            print = new Asa.PrintLabel(Application.StartupPath + "\\Label", 300);
        }
        public static CustPrinterStatus LastPrintStatus = CustPrinterStatus.Unknown;
        public bool Connection(string port)
        {
            Port = port;
            //zebraManger = new ZebraPrinterHelper.ZebraManger(Port, ZebraPrinterHelper.ConnectionType.Network);
            zebraManger = new ZebraPrinterHelper.ZebraManger("", ZebraPrinterHelper.ConnectionType.UsbDirect);
            if (!zebraManger.Connection(out string msg))
            {
                LogUtil.error(msg);
                return false;
            }
            else
            {
                print = new Asa.PrintLabel(Application.StartupPath + "\\Label", zebraManger.PrinterDPI);
                return true;
            }
        }
        public void Close()
        {
            zebraManger.Close();
        }
        public void EditLabel()
        {
            print.EditLabel();
        }
        public string[] GetLabelList()
        {
            return print.GetLabelName();
        }

        public bool Print(string labelname, Dictionary<string, string> data, out string msg)
        {

            print.LoadLabel(labelname);
            var bmp = print.PrintPreview(data);
            if (!zebraManger.PrintImage(bmp, out msg))
            {
                LogUtil.error(msg);
                return false;
            }
            bmp.Dispose();
            return true;
        }

        public Task<(bool, string)> IsLabelOnPeeler()
        {
            return Task.Run(() =>
            {
                bool rtn = zebraManger.IsLabelOnPeeler;
                if(rtn)
                {
                    LastPrintStatus = CustPrinterStatus.HasLabel;
                }
                else
                {
                    LastPrintStatus = CustPrinterStatus.NoLabel;
                }
                return (rtn, "");
            });
        }

    }

    public enum CustPrinterStatus
    {
        Unknown,
        NoLabel,
        HasLabel,
    }
}