PrinterHelper.cs 2.6 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
    {
        private Asa.PrintLabel print;
        ZebraPrinterHelper.ZebraManger zebraManger;
        string Port;
        public PrinterHelper() {
            print = new Asa.PrintLabel(Application.StartupPath + "\\Label", 300);
        }

        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 Task<(bool,string)> PrintLabel(ReelParam reel)
        {
            return Task.Run(() =>
            {
                string labelname = Setting_Init.Printer_Labelname;
                Dictionary<string, string> data = new Dictionary<string, string>();
                data.Add("RI", reel.ReeID);
                data.Add("PN", reel.PN);
                data.Add("QTY", reel.QTY.ToString());
                data.Add("datetime", DateTime.Now.ToString());
                LogUtil.info($"打印标签:{reel.ToDetailStr()}");
                var result = Print(labelname, data, out string msg);
                Task.Delay(500).Wait();
                return (result,msg);
            });
        }
        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(() =>
            {
                return (zebraManger.IsLabelOnPeeler,"");
            });
        }

    }
}