using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using arDev;
using AR;
using AR;
namespace Project
{
    public partial class FMain
    {
        void Set_InterLock()
        {
            if (PUB.sm.Step < eSMStep.IDLE) return;
            intlockcnt += 1;

            var CurrentSaftyDoor = DIO.isSaftyDoorF();//도어안전
            var CurrentSaftyArea = true; //안전영역
            var bEmg = DIO.IsEmergencyOn(); //비상정지
            var bHSet = PUB.mot.IsInit && PUB.mot.HasHomeSetOff; //홈 설정이 완료되었는지 확인한다            

            PUB.flag.set(eVarBool.FG_DOORSAFTY, CurrentSaftyDoor, "COMMINTERLOCK");
            PUB.flag.set(eVarBool.FG_AREASAFTY, CurrentSaftyArea, "COMMINTERLOCK");

            for (int i = 0; i < PUB.iLock.Length; i++)
            {
                PUB.iLock[i].set((int)eILock.EMG, bEmg, "COMMINTERLOCK");
                PUB.iLock[i].set((int)eILock.DOOR, !CurrentSaftyDoor, "COMMINTERLOCK");
                if (i < 7)
                {
                    PUB.iLock[i].set((int)eILock.HOMESET, bHSet, "COMMINTERLOCK");
                }
            }

            //피커의 Z축이 움직이는 동안에는 Y축을 움직이지 못하게 한다
            PUB.iLock[(int)eAxis.PX_PICK].set((int)eILock.ZMOVE, PUB.mot.IsMotion((int)eAxis.PZ_PICK), "COMMINTERLOCK");
            PUB.iLock[(int)eAxis.PL_MOVE].set((int)eILock.ZMOVE, PUB.mot.IsMotion((int)eAxis.PL_UPDN), "COMMINTERLOCK");
            PUB.iLock[(int)eAxis.PR_MOVE].set((int)eILock.ZMOVE, PUB.mot.IsMotion((int)eAxis.PR_UPDN), "COMMINTERLOCK");

            //피커의 Y축이 움직이는 동안에는 Z축을 움직이지 못하게 한다
            PUB.iLock[(int)eAxis.PZ_PICK].set((int)eILock.XMOVE, PUB.mot.IsMotion((int)eAxis.PX_PICK), "COMMINTERLOCK");

            //외부컨베어 신호 인터락설정 (출구쪽센서가 인식되지 않았다면 멈추지 않는다)
            var cvLBusy = DIO.GetIOInput(eDIName.L_CONV4) && DIO.GetIOInput(eDIName.L_EXT_READY) == false && VAR.BOOL[eVarBool.FG_AUTOOUTCONVL] == false;
            var cvRBusy = DIO.GetIOInput(eDIName.R_CONV4) && DIO.GetIOInput(eDIName.R_EXT_READY) == false && VAR.BOOL[eVarBool.FG_AUTOOUTCONVR] == false;
            PUB.iLockCVL.set((int)eILockCV.EXTBUSY, cvLBusy, "COMMINTERLOCK");
            PUB.iLockCVR.set((int)eILockCV.EXTBUSY, cvRBusy, "COMMINTERLOCK");

            //축을 사용하지 않는경우
            PUB.iLockCVL.set((int)eILockCV.DISABLE, SETTING.Data.Disable_Left, "COMMINTERLOCK");
            PUB.iLockCVR.set((int)eILockCV.DISABLE, SETTING.Data.Disable_Right, "COMMINTERLOCK");

            //카트모드에서
            PUB.iLockCVL.set((int)eILockCV.CARTMODE, !VAR.BOOL[eVarBool.Use_Conveyor], "COMMINTERLOCK");
            PUB.iLockCVR.set((int)eILockCV.CARTMODE, !VAR.BOOL[eVarBool.Use_Conveyor], "COMMINTERLOCK");


            //포트가동상태에따라서 비젼인터락
            PUB.iLockVS0.set((int)eILockVS0.PORTRDY, !PUB.flag.get(eVarBool.FG_RDY_PORT_PL), "COMMINTERLOCK");
            if (PUB.Result.DryRun == true)
                PUB.iLockVS1.set((int)eILockVS1.PORTRDY, false, "COMMINTERLOCK");
            else
                PUB.iLockVS1.set((int)eILockVS1.PORTRDY, !PUB.flag.get(eVarBool.FG_RDY_PORT_PC), "COMMINTERLOCK");
            PUB.iLockVS2.set((int)eILockVS2.PORTRDY, !PUB.flag.get(eVarBool.FG_RDY_PORT_PR), "COMMINTERLOCK");

            //위치관련 인터락은 MODEL이 설정 되어있어야하며, MOTION도 정상이어야 한다
            if (PUB.mot.IsInit && PUB.Result.mModel.isSet)
            {
                //if (PUB.LockModel.WaitOne(1) == true)
                {
                    //PUB.LockModel.Reset();

                    //피커의 X축에 따라서 비젼의 촬영가능여부가 결정된다
                    PUB.iLockVS0.set((int)eILockVS0.PKXPOS, DIO.GetIOInput(eDIName.PICKER_SAFE) == false && MOT.getPositionOffset(MOT.GetPXPos(ePXLoc.PICKON)) < -2, "COMMINTERLOCK");
                    if (MOT.getPositionOffset(MOT.GetPXPos(ePXLoc.READYL)) <= 2 || MOT.getPositionOffset(MOT.GetPXPos(ePXLoc.READYR)) >= -2)
                        PUB.iLockVS1.set((int)eILockVS1.PKXPOS, false, "COMMINTERLOCK");
                    else
                        PUB.iLockVS1.set((int)eILockVS1.PKXPOS, true, "COMMINTERLOCK");
                    PUB.iLockVS2.set((int)eILockVS2.PKXPOS, DIO.GetIOInput(eDIName.PICKER_SAFE) == false && MOT.getPositionOffset(MOT.GetPXPos(ePXLoc.PICKON)) > 2, "COMMINTERLOCK");

                    //프린터Y축 위치에 따른 비젼촬영여부(기준위치보다 낮게 있다면 촬영이 안된다)
                    var PLMCmdPos = MOT.GetLMPos(eLMLoc.READY);
                    var PRMCmdPos = MOT.GetRMPos(eRMLoc.READY);
                    PUB.iLockVS0.set((int)eILockVS0.PRNYPOS, (MOT.getPositionOffset(PLMCmdPos) < -5), "COMMINTERLOCK");
                    PUB.iLockVS2.set((int)eILockVS2.PRNYPOS, (MOT.getPositionOffset(PRMCmdPos) < -5), "COMMINTERLOCK");

                    //피커의Z축이 안전위치가 아니면 Y축을 움직이지 않는다
                    var PKZ_POS_ERR = MOT.getPositionOffset(MOT.GetPZPos(ePZLoc.READY)) > 2;
                    PUB.iLock[(int)eAxis.PX_PICK].set((int)eILock.Z_POS, PKZ_POS_ERR, "COMMINTERLOCK");

                    PKZ_POS_ERR = (DIO.GetIOInput(eDIName.PICKER_SAFE) == false && MOT.getPositionOffset(MOT.GetPXPos(ePXLoc.PICKON)) <= 2);
                    PUB.iLock[(int)eAxis.PL_MOVE].set((int)eILock.X_POS, PKZ_POS_ERR, "COMMINTERLOCK"); ;

                    PKZ_POS_ERR = (DIO.GetIOInput(eDIName.PICKER_SAFE) == false && MOT.getPositionOffset(MOT.GetPXPos(ePXLoc.PICKON)) >= 2);
                    PUB.iLock[(int)eAxis.PR_MOVE].set((int)eILock.X_POS, PKZ_POS_ERR, "COMMINTERLOCK");

                    //프린터Z축 안전위치가 아니면 Y축을 멈춘다
                    var PLZ_POS_ERR = MOT.getPositionOffset(MOT.GetLZPos(eLZLoc.READY)) > 2.0;
                    var PRZ_POS_ERR = MOT.getPositionOffset(MOT.GetRZPos(eRZLoc.READY)) > 2.0;
                    PUB.iLock[(int)eAxis.PL_MOVE].set((int)eILock.PZ_POS, PLZ_POS_ERR, "COMMINTERLOCK");
                    PUB.iLock[(int)eAxis.PR_MOVE].set((int)eILock.PZ_POS, PRZ_POS_ERR, "COMMINTERLOCK");

                    //실린더가 전진해 잇다면 이동하지 않아야 한다 - 210207
                    var PLZ_CYL_FW = !DIO.GetIOInput(eDIName.L_PICK_BW);
                    var PRZ_CYL_FW = !DIO.GetIOInput(eDIName.R_PICK_BW);
                    PUB.iLock[(int)eAxis.PL_MOVE].set((int)eILock.CYL_FORWARD, PLZ_CYL_FW, "COMMINTERLOCK");
                    PUB.iLock[(int)eAxis.PR_MOVE].set((int)eILock.CYL_FORWARD, PRZ_CYL_FW, "COMMINTERLOCK");

                    //프린터Y축 안전위치가 아니면 Z축을 멈춘다
                    var PLM_YPOS_READY = MOT.getPositionMatch(MOT.GetLMPos(eLMLoc.READY)); //MOT.GetPLM_PosName();
                    var PRM_YPOS_READY = MOT.getPositionMatch(MOT.GetRMPos(eRMLoc.READY)); //MOT.GetPRM_PosName();

                    var PLM_YPOS_H07 = MOT.getPositionMatch(MOT.GetLMPos(eLMLoc.PRINTH07));
                    var PLM_YPOS_M07 = MOT.getPositionMatch(MOT.GetLMPos(eLMLoc.PRINTM07));
                    var PLM_YPOS_L07 = MOT.getPositionMatch(MOT.GetLMPos(eLMLoc.PRINTL07));
                    var PLM_YPOS_H13 = MOT.getPositionMatch(MOT.GetLMPos(eLMLoc.PRINTH13));
                    var PLM_YPOS_M13 = MOT.getPositionMatch(MOT.GetLMPos(eLMLoc.PRINTM13));
                    var PLM_YPOS_L13 = MOT.getPositionMatch(MOT.GetLMPos(eLMLoc.PRINTL13));

                    var PRM_YPOS_H07 = MOT.getPositionMatch(MOT.GetRMPos(eRMLoc.PRINTH07));
                    var PRM_YPOS_M07 = MOT.getPositionMatch(MOT.GetRMPos(eRMLoc.PRINTM07));
                    var PRM_YPOS_L07 = MOT.getPositionMatch(MOT.GetRMPos(eRMLoc.PRINTL07));
                    var PRM_YPOS_H13 = MOT.getPositionMatch(MOT.GetRMPos(eRMLoc.PRINTH13));
                    var PRM_YPOS_M13 = MOT.getPositionMatch(MOT.GetRMPos(eRMLoc.PRINTM13));
                    var PRM_YPOS_L13 = MOT.getPositionMatch(MOT.GetRMPos(eRMLoc.PRINTL13));


                    var 좌측대기옵셋 = MOT.getPositionOffset(MOT.GetLMPos(eLMLoc.READY));
                    var 우측대기옵셋 = MOT.getPositionOffset(MOT.GetRMPos(eRMLoc.READY));
                    var 좌측대기인정 = (좌측대기옵셋 >= (AR.SETTING.Data.MoveYForPaperVaccumeValue - 0.5) && 좌측대기옵셋 <= 0.5);
                    var 우측대기인정 = (우측대기옵셋 >= (AR.SETTING.Data.MoveYForPaperVaccumeValue - 0.5) && 좌측대기옵셋 <= 0.5);

                    var PLM_POS_ERR = !(좌측대기인정 || PLM_YPOS_READY || PLM_YPOS_H07 || PLM_YPOS_M07 || PLM_YPOS_L07 || PLM_YPOS_H13 || PLM_YPOS_M13 || PLM_YPOS_L13);
                    var PRM_POS_ERR = !(우측대기인정 || PRM_YPOS_READY || PRM_YPOS_H07 || PRM_YPOS_M07 || PRM_YPOS_L07 || PRM_YPOS_H13 || PRM_YPOS_M13 || PRM_YPOS_L13);

                    PUB.iLock[(int)eAxis.PL_UPDN].set((int)eILock.PY_POS, PLM_POS_ERR, "COMMINTERLOCK");
                    PUB.iLock[(int)eAxis.PR_UPDN].set((int)eILock.PY_POS, PRM_POS_ERR, "COMMINTERLOCK");

                    //PUB.LockModel.Set();
                }
            }
            else
            {
                //모션사용불능은 그냥 해제한다
                //Pub.LockPRL.set((int)eILockPRL.PYPOS, false, "COMMINTERLOCK");
                //Pub.LockPRL.set((int)eILockPRR.PYPOS, false, "COMMINTERLOCK");
            }

        }

    }
}
