LineBean_Partial.cs 9.2 KB
using Asa;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    partial class LineBean
    {
        #region CheckWait处理

        private DateTime preCheckAxisTime = DateTime.Now;
        protected DateTime preRWTime = DateTime.Now;
        internal void CheckWait(LineMoveInfo moveInfo)
        {
            try
            {
                List<WaitResultInfo> list = moveInfo.WaitList;
                if (list.Count <= 0)
                {
                    moveInfo.EndStepWait();
                    return;
                }

                //当等待超过一分钟时,需要打印提示 
                TimeSpan span = DateTime.Now - moveInfo.LastSetpTime;
                string NotOkMsg = "";
                bool isOk = true;
                if (moveInfo.OneWaitCanEndStep)
                {
                    isOk = false;
                }
                foreach (WaitResultInfo wait in list)
                {
                    if (wait.IsEnd)
                    {
                        continue;
                    }
                    NotOkMsg = " [" + wait.ToStr() + "] ";
                    if (wait.WaitType.Equals(WaitEnum.W001_AxisMove))
                    {

                    }
                    else if (wait.WaitType.Equals(WaitEnum.W002_IOValue))
                    {
                        wait.IsEnd = IOManager.IOValue(wait.IoType).Equals(wait.IoValue);
                    }
                    else if (wait.WaitType.Equals(WaitEnum.W003_Time))
                    {
                        wait.IsEnd = (span.TotalMilliseconds >= wait.TimeMSeconds);
                    }

                    if (wait.IsEnd && moveInfo.OneWaitCanEndStep)
                    {
                        isOk = true;
                        break;
                    }
                    else if (!moveInfo.OneWaitCanEndStep)
                    {
                        isOk = false;
                        break;
                    }
                }
                if (isOk)
                {
                    moveInfo.EndStepWait();
                }
                else if (span.TotalSeconds > moveInfo.TimeOutSeconds)
                {
                    WarnMsg = Name + "[" + moveInfo.MoveType + "][" + moveInfo.MoveStep + "]等待" + NotOkMsg
                        + "超时[" + Math.Round(span.TotalSeconds, 1) + "]秒";
                    LogUtil.error(WarnMsg);
                    Alarm(AlarmType.IoSingleTimeOut);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + " [" + moveInfo.MoveStep + "] CheckWait 出错:", ex);
            }
        }

        public void StopIOMove(string ioType, int ms = 1000)
        {
            Task.Factory.StartNew(delegate
            {
                IOManager.IOMove(ioType, IO_VALUE.HIGH);
                Thread.Sleep(ms);
                IOManager.IOMove(ioType, IO_VALUE.LOW);
            });
        }
        public void SetWarnMsg(string msg = "")
        {
            if (String.IsNullOrEmpty(msg).Equals(false))
            {
                if (WarnMsg.Equals(msg))
                {
                    if (msg.StartsWith(Name))
                    {
                        LogUtil.error(msg, 105);
                    }
                    else
                    {
                        LogUtil.error(Name + msg, 105);
                    }
                }
                else
                {
                    if (msg.StartsWith(Name))
                    {
                        LogUtil.error(msg);
                    }
                    else
                    {
                        LogUtil.error(Name + msg);
                    }
                }
            }

            WarnMsg = msg;
        }

        public string GetRunInfo()
        {
            return "空料串线入口" + Config.L1_In_AgvName + ":" + AgvClient.GetAction(Config.L1_In_AgvName) + "; " +
                 "空料串线出口" + Config.L1_Out_AgvName + ":" + AgvClient.GetAction(Config.L1_Out_AgvName) + ",料架: " + LastL1OutShelfId + "\r\n" +
                "上料线入口" + Config.L2_In_AgvName + ":" + AgvClient.GetAction(Config.L2_In_AgvName) + "; " +
                "上料线出口" + Config.L2_Out_AgvName + ":" + AgvClient.GetAction(Config.L2_Out_AgvName) + ",料架: " + LastL2OutShelfId + "\r\n";

        }
        #endregion

        #region 休眠处理

        public bool IsSleep = false;
        private DateTime LastBusyTime = DateTime.Now;

        private Dictionary<string, IO_VALUE> lastDIValue = new Dictionary<string, IO_VALUE>();
        private List<string> needCheckList = new List<string>();
        private int GetDIV(string type)
        {
            if (lastDIValue.ContainsKey(type))
            {
                return (int)lastDIValue[type];
            }
            return -1;
        }
        private void SleepTimerProcess()
        {

            if (needCheckList.Count <= 0)
            {
                needCheckList.Add(IO_Type.L1_InCheck);
                needCheckList.Add(IO_Type.L1_StopCheck);
                needCheckList.Add(IO_Type.L1_OutCheck);
                needCheckList.Add(IO_Type.L1_StopDown);

                needCheckList.Add(IO_Type.L2_InCheck);
                needCheckList.Add(IO_Type.L2_StopCheck);
                needCheckList.Add(IO_Type.L2_OutCheck);
                needCheckList.Add(IO_Type.L2_LoadSig);
                needCheckList.Add(IO_Type.L2_StopDown);
            }
            bool agvBusy = false;
            ClientAction currA = AgvClient.GetAction(Config.L1_In_AgvName);
            if (currA.Equals(ClientAction.None) || currA.Equals(ClientAction.NeedLeave) || currA.Equals(ClientAction.NeedEnter))
            {
                ClientAction currB = AgvClient.GetAction(Config.L1_Out_AgvName);
                if (currB.Equals(ClientAction.None) || currB.Equals(ClientAction.NeedLeave) || currB.Equals(ClientAction.NeedEnter))
                {
                    agvBusy = false;
                }
                else
                {
                    LastBusyTime = DateTime.Now;
                    agvBusy = true;
                }
            }
            else
            {
                LastBusyTime = DateTime.Now;
                agvBusy = true;
            }
            if (!agvBusy)
            {
                currA = AgvClient.GetAction(Config.L2_In_AgvName);
                if (currA.Equals(ClientAction.None) || currA.Equals(ClientAction.NeedLeave) || currA.Equals(ClientAction.NeedEnter))
                {
                    ClientAction currB = AgvClient.GetAction(Config.L2_Out_AgvName);
                    if (currB.Equals(ClientAction.None) || currB.Equals(ClientAction.NeedLeave) || currB.Equals(ClientAction.NeedEnter))
                    {
                        agvBusy = false;
                    }
                    else
                    {
                        LastBusyTime = DateTime.Now;
                        agvBusy = true;
                    }
                }
                else
                {
                    LastBusyTime = DateTime.Now;
                    agvBusy = true;
                }
            }

            if (agvBusy && IsSleep)
            {
                UpdateSleep(false);
                return;
            }

            bool diIsBusy = false;
            foreach (string type in needCheckList)
            {
                IO_VALUE currValue = IOManager.IOValue(type);
                int lastValue = GetDIV(type);
                if (((int)currValue).Equals(lastValue))
                {
                    diIsBusy = false;
                }
                else
                {
                    if (lastDIValue.ContainsKey(type))
                    {
                        lastDIValue[type] = currValue;
                    }
                    else
                    {
                        lastDIValue.Add(type, currValue);
                    }
                    LastBusyTime = DateTime.Now;
                    diIsBusy = true;
                    UpdateSleep(false);
                    return;
                }
            }

            if (diIsBusy.Equals(false) && agvBusy.Equals(false) && IsSleep.Equals(false))
            {
                TimeSpan time = DateTime.Now - LastBusyTime;
                if (time.TotalSeconds > Config.SleepSeconds)
                {
                    LogUtil.info(Name + "已经空闲【" + Math.Abs(time.TotalSeconds) + "】秒,开始休眠");
                    UpdateSleep(true);
                }
            }
        }
        public void UpdateSleep(bool needSleep)
        {
            if (needSleep)
            {
                IsSleep = true;
                IOManager.IOMove(IO_Type.L1_Run, IO_VALUE.LOW);
                IOManager.IOMove(IO_Type.L2_Run, IO_VALUE.LOW);
            }
            else
            {
                IsSleep = false;
                LastBusyTime = DateTime.Now;
                IOManager.IOMove(IO_Type.L1_Run, IO_VALUE.HIGH);
                IOManager.IOMove(IO_Type.L2_Run, IO_VALUE.HIGH);
            };
        }
        #endregion


    }

}