WorkInfo.cs 16.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
using PUSICANLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;
using TSA_V.Common; 
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 LabelInfo PreLabel = null ;
        public List<int> PreRgbLed = new List<int>();
        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 static bool isContinue = false;
        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;
            if (isContinue)
            {
                LogUtil.info("开始程序【" + boardInfo.boardName + "】的插件 , 继续之前的工作,索引号="+currIndex);
                currIndex = Convert.ToInt32(Setting_NInit.Work_TagNumber);
                isContinue = false;
            }
            else
            { 
                LogUtil.info("开始程序【" + boardInfo.boardName + "】的插件,重置手势统计");
                HandRecordManager.Reset();
                //LedLabelController.CloseAll();
            } 
            IsShowAOI = false;
            currPoint = null;
            IsWorking = true;
            endWorkTime = DateTime.Now;
            IsWaitMove = false;
            BoardCount = 0;
            PTipSoundProcess.PlayFile(Setting_NInit.Set_NewBoardSound);

        }
        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 (PreLabel != null)
                    {
                        LedLabelController.CloseLed(PreLabel.ip, PreLabel.mac);
                        PreLabel = null;
                    }
                    if(PreRgbLed.Count>0)
                    {
                        RgbLedController.CloseAll();
                        PreRgbLed = new List<int>();
                    }
                    //把上次的旋转轴转回待机点
                    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;
                    PTipSoundProcess.PlayFile(Setting_NInit.Set_BoardEndSound);
                }
            }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 + "】所有位置已走完 ");
                    ClosePrePos();
                    //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,true);
                TSAVPosition position = null;
                if (com != null)
                {
                    #region  2023-10-31添加                                    
                    position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum); 
                    Setting_NInit.Work_ProcedureName = currBoard.boardName;
                    Setting_NInit.Work_TagNumber = currIndex ;
                    if (currPoint.pointNum == 1)
                    {
                        Setting_NInit.Work_ProcedureName=currBoard.boardName;
                        Setting_NInit.Work_TagNumber = currIndex; 
                    }
                    #endregion
                    useComponets.Add(com);
                }
                else
                {
                    position = CSVPositionReader<TSAVPosition>.GetPositonByNum(currPoint.PositionNum);
                }
                if (position == null)
                {
                    LogUtil.info(" 程序【" + currBoard.boardName + "】插件【" + currPoint.PN + "】未找到对应的位置["+currPoint.PositionNum+"]");
                }
                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))
                    {
                        PTipSoundProcess.PlayFile(Setting_NInit.Set_PointChangeSound);
                        if (isNext && (currIndex == 0))
                        {
                            BoardCount++;
                        } 
                        int temp = currPoint.WeldTemp;
                        LogUtil.info(" 程序【" + currBoard.boardName + "】焊接【" + currPoint.PN + "】,温度【" + temp + "】");
                         
                    }
                    else if (WorkType.Equals(3))
                    {
                        PTipSoundProcess.PlayFile(Setting_NInit.Set_PointChangeSound);
                        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.PolaritiesType, currPoint.PointSizeX, currPoint.PointSizeY, currPoint.PenWidth, currPoint.ShowText, currBoard.PointColor);
                waitList.Add(WaitResultInfo.WaitTime(1000));
            }
            else
            {
                LogUtil.info(" 程序【" + currBoard.boardName + "】插件【" + currPoint.PN + "】,X轴【" + currPoint.NodePositionX + "】,Y轴【" + currPoint.NodePositionY + "】坐标无效,不移动光标");

            }
        }
        public void ClosePrePos()
        {
            if (PreNodeId > 0)
            {
                //上一个节点返回原点
                PUSICANControl.AbsMove(PreNodeId, TSAVBean.RotateNode_DefaultPosition);
            }
            if (PreLabel != null)
            {
                LedLabelController.CloseLed(PreLabel.ip, PreLabel.mac);
                Thread.Sleep(300);
            }
            if (PreRgbLed.Count > 0)
            {
                RgbLedController.CloseAll();
                PreRgbLed = new List<int>();
            }
        }
        public void MoveToBag(TSAVPosition position)
        { 
            if (PreLabel != null)
            {
                LedLabelController.CloseLed(PreLabel.ip, PreLabel.mac);
                Thread.Sleep(300);
            }
            if (PreRgbLed.Count > 0)
            {
                RgbLedController.CloseAll();
                PreRgbLed = new List<int>();
            }
            if (position.IsNodePos())
            {
                //转盘转动 
                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,1));
                    PreNodeId = moveNode.NodeId;
                }
                else
                {
                    LogUtil.error("positionNum=" + position.PositionNum + ",未找到对应的运动轴!");
                }
            }
            #region 2023-09-22 添加 控制便签功能
            else if (position.IsLedLabel())
            {
                if (PreNodeId > 0)
                {
                    PUSICANControl.AbsMove(PreNodeId, TSAVBean.RotateNode_DefaultPosition);
                }
                var smtPoint=TSAVBean.Work.currPoint;
                ComponetInfo com = CSVBomManager.GetCom(BoardManager.CurrBoard.bomName, smtPoint);
                LabelInfo label = LedLabelController.GetLabel(position, com, true); 
                LedLabelController.UpdateScreen(label);
                PreLabel = label;
                PTipSoundProcess.PlayFile(Setting_NInit.Set_PointChangeSound);
            }
            #endregion
            else if (position.IsRgbLed())
            {
                if (PreNodeId > 0)
                {
                    PUSICANControl.AbsMove(PreNodeId, TSAVBean.RotateNode_DefaultPosition);
                }
                RgbLedController.OpenPosLed(position);
                PreRgbLed = position.getLedList();
            }
            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,1));
            }
        }
        public bool IsLastP()
        {
            if (IsWorking)
            {
                if (currIndex >= GetSmtList().Count)
                {
                    return true;
                }
            }
            return false;
        }
    } 
}