WorkInfo.cs 13.6 KB
using PUSICANLibrary;
using System;
using System.Collections.Generic;
using System.Threading;
using TSA_V.Common;
using TSA_V.DeviceLibrary.manager;
using TSA_V.LoadCSVLibrary;

namespace TSA_V.DeviceLibrary
{
    public class WorkInfo
    {
        public static bool IsCycleDebug = false;

        public static  int LastDBId = 0;

        /// <summary>
        /// 是否在工作中,在插件中,会接受信号移动位置
        /// </summary>
        public bool IsWorking = false;
        public BoardInfo currBoard = null;
        public int currIndex = -1;
        public SMTPointInfo currPoint = null;
        public TSAVPosition currPosition = null;
        /// <summary>
        /// 工作类型0=未在工作界面,1=插件,2=焊接,3=检测,默认不设置是1
        /// </summary>
        public int WorkType = 0;
        public bool IsWaitMove = false;
        public List<WaitResultInfo> waitList = new List<WaitResultInfo>();
        public DateTime endWorkTime = DateTime.Now;
        public DateTime LastSetpTime = DateTime.Now;
        public uint PreNodeId = 0; 
        public List<SMTPointInfo> needWorkSmtList = new List<SMTPointInfo>();
        //开始工作后共工作了几块电路板``````
        public int BoardCount = 0;
        public DateTime beginWorkTime = DateTime.Now;
        public bool IsShowAOI = false;
        private List<ComponetInfo> useComponets = new List<ComponetInfo>();
        public void StartWork(BoardInfo boardInfo)
        {
            //StatusClient.instance.SendNew(Color.Green, "扫码成功:" + BoardManager.CurrBoard.boardCode);
            beginWorkTime = DateTime.Now;
            //开始工作
            LogUtil.info("开始程序【" + boardInfo.boardName + "】的插件"); 
            currBoard = boardInfo; 
            needWorkSmtList = currBoard.GetSmtList();
            currIndex = -1; 
            IsShowAOI = false;
            currPoint = null;
            IsWorking = true;
            endWorkTime = DateTime.Now;
            IsWaitMove = false;
            BoardCount = 0;
        }
        public List<SMTPointInfo> GetSmtList()
        {
            if (currBoard == null)
            {
                return new List<SMTPointInfo>();
            }
            //if (WorkType.Equals(1))
            //{
            //    return currBoard.smtList;
            //}
            //else if (WorkType.Equals(2))
            //{
            //    return needSoldSmtList;
            //}
            //else if (WorkType.Equals(3))
            //{
            //    return needCheckSmtList;
            //}
            //return new List<SMTPointInfo>();
            return needWorkSmtList;
        }
        public void StopWork()
        {
            try
            {
                if (IsWorking)
                {
                    EndWait();
                    if (currIndex > 0 && currBoard != null)
                    {
                        //如果是下一个并且当前是最后一个,其实是切换了新程序,需要减去刚刚的数量 
                        CSVBomManager.DelUseCount(currBoard,  useComponets);
                        useComponets = new List<ComponetInfo>();
                    }
                    DeviceStatus deviceStatus = MesUtil.GetCurrStatus();
                    if (MesUtil.CodeISOk)
                    {
                        MesUtil.SendStatus(deviceStatus);
                    }
                    deviceStatus.workInfo.ID = LastDBId;
                    DB.db.AddHistory(deviceStatus.workInfo, out int id);
                    if (LastDBId < 0)
                    {
                        LastDBId = id;
                        LogUtil.info("保存LastDBId=" + LastDBId);
                    }
                    if (currBoard != null)
                    {
                        LogUtil.info("停止程序【" + currBoard.boardName + "】工作");
                    }
                    //把上次的旋转轴转回待机点
                    if (this.WorkType.Equals(1) && PreNodeId > 0)
                    {
                        PUSICANControl.StopMove(PreNodeId);
                        Thread.Sleep(10);
                        //上一个节点返回原点
                        PUSICANControl.AbsMove(PreNodeId, TSAVBean.RotateNode_DefaultPosition);
                        Thread.Sleep(10);
                    }
                    //Thread.Sleep(100);
                    endWorkTime = DateTime.Now;
                    IsShowAOI = false;
                    IsWorking = false;
                    IsWaitMove = false;
                }
            }catch(Exception ex)
            {
                LogUtil.error("StopWork出错:"+ex.ToString());
            }
        }
        public void EndWait()
        {
            endWorkTime = DateTime.Now;
            IsWaitMove = false;
            waitList = new List<WaitResultInfo>();
        }      
        public bool MoveToNextPoint(bool isNext)
        {
            return MoveToNextPoint(isNext, false);
        }
        public bool MoveToNextPoint(bool isNext, bool isReset)
        {
            waitList = new List<WaitResultInfo>();
            IsWaitMove = true;
            LastSetpTime = DateTime.Now;
            if (isNext)
            {
                currIndex++;
            }
            else
            {
                currIndex--;
            }
            if (currIndex > GetSmtList().Count) 
            {
                if (isReset)
                {
                    if (IsCycleDebug)
                    { 
                        LogUtil.info("程序【" + currBoard.boardName + "】已到达最后一个焊点,重新开始工作");
                        StartWork(BoardManager.CurrBoard);
                        return true;
                    }
                    else
                    { 
                        LogUtil.info("程序【" + currBoard.boardName + "】已到达最后一个焊点,停止工作");
                        StopWork();
                        return true;
                    }
                }
                else
                { 
                    return false;
                }
            }
            if (currIndex < 0)
            {
                currIndex = GetSmtList().Count - 1;
            }
            if (currIndex == GetSmtList().Count)
            {
                if (WorkType.Equals(1))
                {
                    LogUtil.info(" 程序【" + currBoard.boardName + "】所有位置已走完 "); 
                    if (PreNodeId > 0)
                    {
                        //上一个节点返回原点
                        PUSICANControl.AbsMove(PreNodeId, TSAVBean.RotateNode_DefaultPosition);
                    }
                    //if (TSAVBean.IsNeedSoldering)
                    //{
                    //    LogUtil.info(" 程序【" + currBoard.boardName + "】需要焊接,打开烙铁灯 ");
                    //    LedManager.LedOFFALL();
                    //    LedManager.OpenSoldering();
                    //    waitList.Add(WaitResultInfo.WaitTime(5000));
                    //} 
                }
            }
            else
            {
                currPoint = GetSmtList()[currIndex];

                //LogUtil.info(" 程序【" + currBoard.boardName + "】插件【" + currPoint.pointName + "】");
                ComponetInfo com = CSVBomManager.GetCom(currBoard.bomName, currPoint);
                TSAVPosition position = null;
                if (com != null)
                {
                    position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum);
                    useComponets.Add(com);
                }
                else
                {
                    position = CSVPositionReader<TSAVPosition>.GetPositonByNum(currPoint.PositionNum);
                }
                if (position == null)
                {
                    LogUtil.info(" 程序【" + currBoard.boardName + "】插件【" + currPoint.PN + "】未找到对应的元器件");
                }
                else
                {
                    this.currPosition = position;
                    XYMove();
                    //插件机工作
                    if (WorkType.Equals(1))
                    {
                        LogUtil.info(" 程序【" + currBoard.boardName + "】插件【" + currPoint.PN + "】 库位【" + position.PositionName + "】X轴【" + currPoint.NodePositionX + "】,Y轴【" + currPoint.NodePositionY + "】");

                        MoveToBag(position);

                        //如果是下一个并且当前是最后一个,其实是切换了新程序,需要减去刚刚的数量
                        if (isNext && (currIndex == 0))
                        {
                            BoardCount++;
                            CSVBomManager.DelUseCount(currBoard, useComponets);
                            useComponets = new List<ComponetInfo>();
                        }
                    }
                    //焊接工作
                    else if (WorkType.Equals(2))
                    {
                        if (isNext && (currIndex == 0))
                        {
                            BoardCount++;
                        } 
                        int temp = currPoint.WeldTemp;
                        LogUtil.info(" 程序【" + currBoard.boardName + "】焊接【" + currPoint.PN + "】,温度【" + temp + "】");
                         
                    }
                    else if (WorkType.Equals(3))
                    {
                        if (isNext && (currIndex == 0))
                        {
                            BoardCount++;
                        } 
                        int temp = currPoint.WeldTemp;
                        LogUtil.info(" 程序【" + currBoard.boardName + "】检测【" + currPoint.PN + "】");
                    }
                }
            }
            return true;
        }
        public void XYMove()
        {

            //xy轴运动到固定点
            if (TSAVBean.IsValidPosition(currPoint.NodePositionX, currPoint.NodePositionY))
            {
                //LogUtil.info(" 程序【" + currBoard.boardName + "】插件【" + currPoint.pointName + "】,X轴【" + currPoint.NodePositionX + "】,Y轴【" + currPoint.NodePositionY + "】");
                 TSAVBean.ShowPoint(currPoint.NodePositionX, currPoint.NodePositionY,currPoint.PointType,currPoint.PointSizeX,currPoint.PointSizeY,currPoint.PenWidth,currPoint.ShowText);
                waitList.Add(WaitResultInfo.WaitTime(1000)); 
            }
            else
            {
                LogUtil.info(" 程序【" + currBoard.boardName + "】插件【" + currPoint.PN + "】,X轴【" + currPoint.NodePositionX + "】,Y轴【" + currPoint.NodePositionY + "】坐标无效,不移动光标");

            }
        }
        public void MoveToBag(TSAVPosition position)
        {
            Dictionary<string,string> pairs = new Dictionary<string,string>();
            if (pairs.Count!=0)
            {
                //关灯
                LogUtil.info($"{pairs.ToString()}");
                ScanRequestLabel.Turnoffthelights(pairs);
                pairs.Clear();
            }
            //LedManager.LedOFFALL();
            if (position.PositionType.Equals(1))
            {
                //转盘转动 
                NodeInfo moveNode = position.GetNode(TSAVBean.RotateMap);
                if (moveNode != null)
                {
                    if (PreNodeId > 0 && !(moveNode.NodeId.Equals(PreNodeId)))
                    {
                        //上一个节点返回原点
                        PUSICANControl.AbsMove(PreNodeId, TSAVBean.RotateNode_DefaultPosition);
                        Thread.Sleep(50);
                    }
                    PUSICANControl.AbsMove(moveNode.NodeId, position.RotatePosition);
                    waitList.Add(WaitResultInfo.WaitNode(moveNode, position.RotatePosition));
                    PreNodeId = moveNode.NodeId;
                }
                else
                {
                    LogUtil.error("positionNum=" + position.PositionNum + ",未找到对应的运动轴!");
                }
            }
            #region 2023-09-22 添加 控制便签功能
            else if (position.PositionType.Equals(2))
            {
                LogUtil.info("电子屏闪烁!");
                var smtPoint=TSAVBean.Work.currPoint;
                ComponetInfo com = CSVBomManager.GetCom(BoardManager.CurrBoard.bomName, smtPoint);
                string count=null;
                string pnname = null;
                string ComponentDes=null;   
                if (com!=null)
                {
                    count = com.ComCount.ToString();
                    pnname=com.PN.ToString();
                    ComponentDes=com.ComponentDes.ToString();   
                }          
                ScanRequestLabel.RequestPost(position, count, pnname, ComponentDes  ,out pairs);
            }
            #endregion
            else
            {
                if (PreNodeId > 0)
                {
                    PUSICANControl.AbsMove(PreNodeId, TSAVBean.RotateNode_DefaultPosition);
                }
                PreNodeId = 0;
                LEDModule module = LedManager.GetLEDModule(position.DeviceIP);
                LedManager.LightOn(position.DeviceIP, position.getLedList());
                waitList.Add(WaitResultInfo.WaitTime(500));
            }
        }
        public bool IsLastP()
        {
            if (IsWorking)
            {
                if (currIndex >= GetSmtList().Count)
                {
                    return true;
                }
            }
            return false;
        }
    } 
}