PrinterHelper.cs 5.4 KB
using Asa;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.Common.util;
using System;
using System.Collections.Generic;
using System.Drawing;
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;

        int dpi = 100;
        public static bool istscprin = ConfigHelper.Config.Get("Hardwareversion", "V2")=="V2";
        public static bool newistscprin = ConfigHelper.Config.Get("HardwareversionofAGV", "V2") == "V2";
        //public PrintDevice printDevice = ConfigHelper.Config.Get("PrintDevice", istscprin ? PrintDevice.TSC : PrintDevice.Zebra);
        public PrintDevice printDevice = ConfigHelper.Config.Get("PrintDevice", istscprin ? PrintDevice.TSC : (newistscprin? PrintDevice.TSC : PrintDevice.Zebra));
        public enum PrintDevice { 
            TSC,
            Zebra,
            Windows
        }
        public PrinterHelper()
        {
            if (printDevice!= PrintDevice.Windows)
            {
                dpi = 300;
            }
            print = new Asa.PrintLabel(Application.StartupPath + "\\Label", dpi);
            print.PrintStatusChanged += Print_PrintStatusChanged;
        }

        private void Print_PrintStatusChanged(Asa.PrintLabel.PrinterStatus sta, string msg)
        {
            LogUtil.info($"打印机状态:{sta}, msg:{msg}");
        }

        public static CustPrinterStatus LastPrintStatus = CustPrinterStatus.Unknown;
        public bool Connection(string port)
        {
            Port = port;
            if (printDevice== PrintDevice.TSC || printDevice== PrintDevice.Windows)
            {
                print = new Asa.PrintLabel(Application.StartupPath + "\\Label", dpi);
                return true;
            }
            else if (printDevice == PrintDevice.Zebra)
            {               
                //默认使用老打印机
                zebraManger = new ZebraPrinterHelper.ZebraManger("", ZebraPrinterHelper.ConnectionType.UsbDirect);
                //zebraManger = new ZebraPrinterHelper.ZebraManger(Port, ZebraPrinterHelper.ConnectionType.Network);               
                if (!zebraManger.Connection(out string msg))
                {
                    LogUtil.error(msg);
                    return false;
                }
                return true;
            }
            return false;
        }
        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);
            if (printDevice == PrintDevice.TSC)
            {
                LogUtil.error($"TSC_打印数据:{JsonHelper.SerializeObject(data)}");
                TscConfig tscConfig = new TscConfig();
                tscConfig.Offset = ConfigHelper.Config.Get("Tsc_Offset", -17D);
                tscConfig.Speed = ConfigHelper.Config.Get("Tsc_Speed", 9);
                tscConfig.Density = ConfigHelper.Config.Get("Tsc_Density", 12);
                tscConfig.GapOffset = ConfigHelper.Config.Get("Tsc_GapOffset", 0);
                tscConfig.Gap = ConfigHelper.Config.Get("Tsc_Gap", 3);
                if (!print.PrintToTsc_New(data, tscConfig, out TscStauts tscStauts, out Bitmap bmp))
                {
                    msg = $"打印失败:{tscStauts.ToString()}";
                    LogUtil.error("打印失败原因:" + tscStauts.ToString());
                    return false;
                }
                if (bmp != null)
                {
                    string cid = ConfigHelper.Config.Get("CID");
                    _ = UnifiedDataHandler.PostSmfImageAsync(bmp, new Dictionary<string, string> { { "cid", cid + "_2" } }, bmp.Width, bmp.Height);
                }
                msg = $"打印成功";
                return true;
            }
            else if (printDevice == PrintDevice.Windows) {
                print.Print(data);
                msg = "";
                return true;
            }
            else
            {
                var bmp = print.PrintPreview(data);
                if (!zebraManger.PrintImage(bmp, out msg))
                {
                    if (msg == crc.GetString("Res0224","上一个标签尚未移走"))
                    {
                        msg = crc.GetString("Res0224", "上一个标签尚未移走");
                    }
                    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,
    }
}