LabelMachine.print.cs 7.5 KB
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
{
    public partial class LabelMachine
    {
        public void InitPrint()
        {
            RobotManage.PrintBean.PrintStatusChanged += Print_PrintStatusChanged;
            RobotManage.LoadPrintSetting();
        }

        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 (MoveInfo.IsStep(MoveStep.Lbl_WaitPrint))
                            {
                                MoveInfo.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, bool ReverseLabel=false)
        {
            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", "");

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

        public Task DoPrint(ReelParam labelParam, bool ReverseLabel = false)
        {
            LogUtil.info($"调用 PrintLabel 打印标签 WareCode:{labelParam.WareCode}, QTY:{labelParam.QTY}, ReverseLabel:{ReverseLabel}");
            StartPrintLabel(labelParam.WareCode, labelParam.QTY.ToString(), ReverseLabel);
            return Task.Run(() =>
            { });
        }

        /// <summary>
        /// 扫码线程
        /// </summary>
        Task<(List<CodeInfo>,Bitmap)> ScanTask;
        Task<(Point, double, bool)> ScanTask2;
        Task<(List<CodeInfo>, Bitmap)> ScanCode()
        {
            MoveInfo.log("开始贴标扫码线程");
            return Task.Run(new Func<(List<CodeInfo>, Bitmap)>(() =>
            {
                try
                {
                    Bitmap bitmap;
                    //IOMove(IO_Label_Type.Camera_Led, IO_VALUE.HIGH);
                    //Task.Delay(10).Wait();
                    List<CodeInfo> LastCodeList = CodeManager.CameraScan(new List<string> { Config.CameraName },out bitmap);

                    if (LastCodeList.Count <= 0)
                    {
                        Task.Delay(500).Wait();
                        LastCodeList = CodeManager.CameraScan(new List<string> { Config.CameraName });
                    }
                    //IOMove(IO_Label_Type.Camera_Led, IO_VALUE.LOW);

                    return (LastCodeList,bitmap);
                }
                catch {
                    return (new List<CodeInfo>(),null);
                }
            }));
        }

        Task<(Point, double, bool)> ScanCode2()
        {
            MoveInfo.log("开始贴标扫码线程");
            return Task.Run(new Func<(Point, double, bool)>(() =>
            {
                try
                {
                    Bitmap bitmap;
                    List<CodeInfo> LastCodeList = CodeManager.CameraScan(new List<string> { Config.CameraName }, out bitmap);
                    if (LastCodeList.Count <= 0)
                    {
                        Task.Delay(500).Wait();
                        LastCodeList = CodeManager.CameraScan(new List<string> { Config.CameraName });
                    }

                    if (LastCodeList.Count == 0)
                    {
                        MoveInfo.NextMoveStep(MoveStep.Lbl_WaitCheckLabel);
                        MoveInfo.log($"未识别到有效二维码");
                        WaitCheckLabeltype = WaitCheckLabeltypeE.Scan_QRCode_Fail;
                    }
                    else
                    {
                        if (MoveInfo.ReelParam.WareCode != LastCodeList[0].CodeStr)
                        {
                            MoveInfo.NextMoveStep(MoveStep.Lbl_WaitCheckLabel);
                            Msg.add($"扫描到的Reelid与发出打印的不一致,系统:{MoveInfo.ReelParam.WareCode},实际:{LastCodeList[0].CodeStr}", MsgLevel.warning);
                            MoveInfo.log($"扫描到的Reelid与发出打印的不一致,系统:{MoveInfo.ReelParam.WareCode},实际:{LastCodeList[0].CodeStr}");
                            WaitCheckLabeltype = WaitCheckLabeltypeE.Scan_Code_And_Print_are_different;
                            //MoveInfo.ReelParam.IsNg = true;
                            //MoveInfo.ReelParam.NgMsg = "扫描到的Reelid与发出打印的不一致";
                            //MoveInfo.ReelParam.logresult();

                        }
                        else
                        {
                            //MoveInfo.NextMoveStep(MoveStep.Lbl_Printted);
                            MoveInfo.ReelParam.codeInfos = LastCodeList;
                            MoveInfo.log($"已完成扫码, Count={LastCodeList.Count}");
                            MoveInfo.ReelParam.logresult();
                        }

                        MoveInfo.ReelParam.logresult();
                        //标签坐标
                        Point Label_Pix_Point = new Point(MoveInfo.ReelParam.codeInfos[0].X, MoveInfo.ReelParam.codeInfos[0].Y);
                        //照片坐标反转180度
                        var x = 5472 - Label_Pix_Point.X;
                        var y = 3648 - Label_Pix_Point.Y;
                        Label_Pix_Point.X = x;
                        Label_Pix_Point.Y = y;
                        //图像也旋转180度
                        bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);

                        (p1, labelRAxisPos, ReverseLabel) = ClacLabel2(Label_Pix_Point, bitmap, MoveInfo.ReelParam);

                        return (p1, labelRAxisPos, ReverseLabel);
                    }
                    return (Point.Empty, 0, false);
                }
                catch
                {
                    return (Point.Empty, 0, false);
                }
            }));
        }
    }
}