BoxBean.cs 15.1 KB

using Asa;
using DeviceLib;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{ 
    public partial class BoxBean : EquipBase
    { 
        private   bool IsIntSlvBlock = false;
        public AutoInoutInfo AutoInout = new AutoInoutInfo();
        public string CID = "";
        public BOX_Config Config;
        public string lastPosId = "";
        public StoreStatus lastPosIdStatus = StoreStatus.StoreOnline;
        public List<ConfigMoveAxis> moveAxisList = new List<ConfigMoveAxis>();
 
        public List<string> PositionNumList = new List<string>();
      
        public HumitureBean humBean = null;
       

        public AxisBean UpdownAxis = null;
        public AxisBean MiddleAxis = null;
        public AxisBean ComAxis = null;
        public AxisBean InoutAxis = null;

        /// <summary>
        /// 出料口最后一盘料信息,0=暂无,1=单盘出库料亮绿灯,2=入库NG料亮红灯
        /// </summary>
        public int OutDoorReelType = 0;
         
        public BoxBean(BOX_Config config)
        {
            Init();
            baseConfig = config;
            CID = config.CID;

            IoCheckTimer.Elapsed += IoCheckTimer_Elapsed;
            serverConTimer.Elapsed += server_connect_timer_Tick;
            logType = 1000 * config.Id;
            //添加调试 
            IsDebug = config.IsInDebug.Equals(1);
            Name = (" BOX_" + config.Id + " ").ToUpper();
            MoveInfo = new StoreMoveInfo(ID, Name);
            this.ID = config.Id; ;
            this.Config = config;  
            //温湿度
            humBean = new HumitureBean(config.Humiture_Port, Name);
          
            MoveAxisConfig();
            List<ACBoxPosition> positionList = CSVPositionReader<ACBoxPosition>.getPositionList();
            PositionNumList = new List<string>();
            foreach (ACBoxPosition position in positionList)
            {
                if (position.StoreId.Equals(ID) && ACBoxPosition.CheckPosition(position, Config))
                {
                    PositionNumList.Add(position.PositionNum);
                }
            }
            InoutAxis = new AxisBean(config.InOut_Axis, Name);
            MiddleAxis = new AxisBean(config.Middle_Axis, Name);
            UpdownAxis = new AxisBean(config.UpDown_Axis, Name);
            ComAxis = new AxisBean(config.Comp_Axis, Name);
            mainTimer.Enabled = false;
         
        }

        private void IoCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //判断急停 
            if (runStatus >= StoreRunStatus.HomeMoving)
            {
                if (IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
                {
                    if (isInSuddenDown.Equals(false))
                    {
                        isInSuddenDown = true;
                        LogUtil.error(Name + "收到急停信号,报警急停");
                        WarnMsg = Name + "收到急停信号,报警急停";

                        //报警时会关闭所有轴
                        Alarm(AlarmType.SuddenStop, "1", WarnMsg, MoveType.None);
                    }
                }
            }
        } 
        public void MoveAxisConfig()
        {
            Config.UpDown_Axis.BreakOnDO = IO_Type.UpdownAxis_Break;
            moveAxisList = new List<ConfigMoveAxis>(); 
            moveAxisList.Add(Config.Middle_Axis);
            moveAxisList.Add(Config.UpDown_Axis);
            moveAxisList.Add(Config.InOut_Axis);
            moveAxisList.Add(Config.Comp_Axis);
        } 
        public override bool StartRun(bool isDebug = false)
        {
            if (!StoreManager.Store.canStart)
            {
                WarnMsg = "启动失败:设备未初始化完成";
                LogUtil.error(Name + "启动失败:设备未初始化完成");
                return false;
            }
            LogInfo("开始启动,启动时间:" + StartTime.ToString()); 
            AutoInout.StopAuto();
            mainTimer.Enabled = false;
            alarmType = AlarmType.None;

            //急停按钮和气压检测需要一起判断
            IO_VALUE suddenBtn = IOValue(IO_Type.SuddenStop_BTN);
            IO_VALUE airCheck = IOValue(IO_Type.Airpressure_Check);

            if (suddenBtn.Equals(IO_VALUE.HIGH) && (airCheck.Equals(IO_VALUE.HIGH)))
            {
                //lastAirValue = airCheck;
                lastAirCloseTime = DateTime.Now;
                if (!OpenAllAxis(true))
                {
                    return false;
                }

                //TODO 启动时先所有轴远点返回,测试暂时关闭 
                runStatus = StoreRunStatus.HomeMoving;
                storeStatus = StoreStatus.ResetMove;
                //启动温湿度服务器 
                HumitureController.Init(Config.Humiture_Port);

                ReturnHome();
                StartTime = DateTime.Now;
                mainTimer.Enabled = true;
                IoCheckTimer.Enabled = true;
                serverConTimer.Enabled = true;

                return true;
            }
            else
            {
                if (suddenBtn.Equals(IO_VALUE.LOW))
                {
                    WarnMsg = "启动失败:急停没开";
                    LogUtil.error(" (" + Name + ")启动失败:急停没开!");
                }
                else
                {
                    WarnMsg = "启动失败:没有气压信号";
                    LogUtil.error(" (" + Name + ")启动失败:没有气压信号!");
                }
                return false;
            }
        }

      

        #region 原点返回和复位处理
        private void ReturnHome()
        { 
            lastPosId = "";
            lastPosIdStatus = StoreStatus.ResetMove;
            AutoInout.ClearCount();
            runStatus = StoreRunStatus.HomeMoving;
            MoveInfo.NewMove(MoveType.ReturnHome);
            InoutStartReset();
        }

        public override void Reset(bool isNeedClearAuto = true)
        {
            AutoInout.ClearCount();
            //复位之前先停止运行
            if (isNeedClearAuto)
            {
                AutoInout.StopAuto();
            }
            runStatus = StoreRunStatus.Reset;
            storeStatus = StoreStatus.ResetMove;
            MoveInfo.NewMove(MoveType.Reset);

            if (!OpenAllAxis(true))
            {
                LogInfo("复位时打开轴失败,需要再次复位,直接报警停止复位");
                return;
            }
            InoutStartReset();
            isInPro = false;
        }
        private void InoutStartReset()
        {
            //清理报警
            WarnMsg = "";
            isInSuddenDown = false;
            isNoAirCheck = false;
            alarmType = AlarmType.None; 

            //开始复位动作
            MoveInfo.NextMoveStep(StoreMoveStep.BOX_H01_InOutBack);

            LogInfo(MoveInfo.MoveType + ":  进出轴回原点 ");
            MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000)); 
            InoutAxis.HomeMove(MoveInfo);
        }
      
        protected override void ResetProcess()
        {
            if (MoveInfo.IsInWait)
            {
                CheckWait();
            }
            if (MoveInfo.IsInWait)
            {
                return;
            }
            switch (MoveInfo.MoveStep)
            {
                case StoreMoveStep.BOX_H01_InOutBack: 
                    MoveInfo.NextMoveStep(StoreMoveStep.BOX_H02_InoutToP1);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
                    LogInfo(MoveInfo.MoveType + ":   进出轴到待机点P1,关闭仓门"); 
                    InoutAxis.AbsMove(MoveInfo, Config.InOutAxis_P1, Config.InOutAxis_P1_Speed); 
                    CylinderMove(MoveInfo, IO_Type.OutDoor_Up, IO_Type.OutDoor_Down); 
                    break;

                case StoreMoveStep.BOX_H02_InoutToP1: 
                    //如果此时轴三还在报警,需要提示错误并等待 
                    if (ACServerManager.GetAlarmStatus(Config.InOut_Axis.DeviceName, Config.InOut_Axis.GetAxisValue()) > 0)
                    {
                        LogUtil.error(Name + MoveInfo.MoveType + "复位失败:  " + Config.InOut_Axis.Explain + "报警");
                        WarnMsg = Name + "复位失败:  " + Config.InOut_Axis.Explain + "报警";
                        Alarm(AlarmType.AxisAlarm );
                        return;
                    }
                    //复位和回原点要等轴3进出轴ORG亮了以后才能返回其他轴
                    MoveInfo.NextMoveStep(StoreMoveStep.BOX_H03_OtherAxisHome);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
                    LogInfo(MoveInfo.MoveType + ":  压紧轴,旋转轴,上下轴开始 原点返回,关闭NG门");
                    CylinderMove(MoveInfo, IO_Type.OutDoor_Up, IO_Type.OutDoor_Down);
                    ComAxis.HomeMove(MoveInfo);
                    MiddleAxis.HomeMove(MoveInfo);
                    UpdownAxis.HomeMove(MoveInfo);

                    break;
                case StoreMoveStep.BOX_H03_OtherAxisHome:
                    MoveInfo.NextMoveStep(StoreMoveStep.BOX_H04_OtherAxisBack);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(2000));
                    LogInfo(MoveInfo.MoveType + ": 旋转轴运动到P1,上下轴走到P1,压紧轴到P1, ");
                    MiddleAxis.AbsMove(MoveInfo, Config.MiddleAxis_P1, Config.MiddleAxis_P1_Speed);
                    UpdownAxis.AbsMove(MoveInfo, Config.UpDownAxis_P1, Config.UpDownAxis_P1_Speed);
                    ComAxis.AbsMove(MoveInfo, Config.CompAxis_P1, Config.CompAxis_P1_Speed);
                    break;
                case StoreMoveStep.BOX_H04_OtherAxisBack:
                    LogInfo(MoveInfo.MoveType + ": 完成");
                    MoveEndP();
                    break; 
                default: break;
            }
        }


        #endregion
        
        public override void StopRun()
        { 
            lastPosId = "";
            lastPosIdStatus = StoreStatus.StoreOnline;
            WarnMsg = "";
            AutoInout.StopAuto();
            IoCheckTimer.Enabled = false;
            serverConTimer.Enabled = false;
            StopMove();

            runStatus = StoreRunStatus.Wait;
            mainTimer.Enabled = false;
            TimeSpan span = DateTime.Now - StartTime;
            LogInfo(",停止运行,总运行时间:" + span.ToString());
        }
        public override void Alarm(AlarmType alarmType, string alarmDetial = "", string alarmMsg = "", MoveType storeMoveType = MoveType.None)
        {
            base.Alarm(alarmType, alarmDetial, alarmMsg, storeMoveType);
            AutoInout.StopAuto(); 
        }
       

        internal override void TimerProcess()
        {
            try
            {
                DateTime time = DateTime.Now;
                if (MoveInfo.MoveType != MoveType.None)
                {
                    BusyMoveProcess();
                    ShowTimeLog("BusyMoveProcess");
                }
                else if (runStatus.Equals(StoreRunStatus.Runing))
                {
                    ShowTimeLog("判断是否需要出入库");
                    AutoResetProcess();
                    ShowTimeLog("AutoResetProcess");
                    IOTimeOutProcess();
                    ShowTimeLog("IOTimeOutProcess");

                }  //检查运动轴报警
                if (runStatus > StoreRunStatus.Wait && (!isInSuddenDown) && (!isNoAirCheck))
                {
                    ShowTimeLog("开始检测轴报警");
                    CheckAxisAlarm(new AxisBean[] { MiddleAxis, UpdownAxis, InoutAxis, ComAxis });
                    ShowTimeLog("轴报警检测完成");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "定时处理出错" + ex.ToString());
            }
        }


        private void AutoResetProcess()
        {
            try
            {
                bool noInStore = MoveInfo.MoveType.Equals(MoveType.None) && alarmType.Equals(AlarmType.None);
                if (AutoInout.CurrInOutACount >= StoreManager.Config.Box_ResetACount && noInStore)
                {
                    if (runStatus < StoreRunStatus.Runing || MoveInfo.MoveType == MoveType.InStore || MoveInfo.MoveType == MoveType.OutStore)
                    {
                        //    LogInfo("已经累计出入库" + AutoInout.CurrInOutACount + "次,当时当前正在忙碌中暂不复位");
                    }
                    else
                    {
                        LogInfo("已经累计出入库" + AutoInout.CurrInOutACount + "次,需要复位一下");
                        Reset();
                    }
                    return;
                }
                else if (waitInStoreParam != null && noInStore && IsDebug.Equals(false))
                {
                    if (!StartInStoreMove(waitInStoreParam))
                    {
                        LogInfo("执行排队的入库【" + waitInStoreParam.ToStr() + "】失败");
                    }
                    else
                    {
                        LogInfo("执行排队的入库【" + waitInStoreParam.ToStr() + "】成功,清理缓存");
                        waitInStoreParam = null;
                    }
                }
                else if (waitOutStoreList.Count > 0 && noInStore && IsDebug.Equals(false))
                {
                    InOutParam param = null;
                    bool result = waitOutStoreList.TryDequeue(out param);
                    if (result && param != null)
                    {
                        LogInfo("执行排队的出库【" + param.ToStr() + "】");
                        StartExecuctOut(param);
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "处理出入库排队列表出错:" + ex.ToString());
            }
        }
        

         
        public override void StopMove()
        {   
            MoveInfo.EndMove();
            IOMove(IO_Type.UpdownAxis_Break, IO_VALUE.LOW);
            //运动版停止 
            MiddleAxis.SuddenStop(true);
            UpdownAxis.SuddenStop(true);
            InoutAxis.SuddenStop(true);
            ComAxis.SuddenStop(true);
            CloseAllAxis();
            LogInfo("StopMove");
            isInPro = false;
        }
          
        public bool OpenAllAxis(bool isCheck)
        {
            return RunMultiAxis(true, IO_Type.Axis_Run, IO_Type.UpdownAxis_Break, new AxisBean[] { MiddleAxis, UpdownAxis, InoutAxis, ComAxis });
        }
        public void CloseAllAxis()
        {
            LogUtil.info(Name + "关闭刹车,关闭伺服");
            CloseMultiAxis(IO_Type.Axis_Run, IO_Type.UpdownAxis_Break, new AxisBean[] { MiddleAxis, UpdownAxis, InoutAxis, ComAxis });

        }
       
    }
}