EquipBase.cs 12.1 KB
using DeviceLib;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public abstract class EquipBase : KTK_Store
    {
        internal int logType = 1000;
        public DateTime LastAlarmTime = DateTime.Now;
        public override void Alarm(AlarmType alarmType, string alarmDetial = "", string alarmMsg = "", MoveType storeMoveType = MoveType.None)
        {
            if (alarmType.Equals(AlarmType.None).Equals(false))
            {
                LastAlarmTime = DateTime.Now;
            }
            SaveAlarmInfo(alarmType, alarmDetial, alarmMsg, storeMoveType);

            if (this.alarmType.Equals(alarmType) && alarmType != AlarmType.SuddenStop && alarmType != AlarmType.NoAirCheck)
            {
                return;
            }
            LogUtil.error(Name + " 报警,报警类型:" + alarmType);

            this.alarmType = alarmType;
            if (alarmType.Equals(AlarmType.AxisAlarm) | alarmType.Equals(AlarmType.AxisMoveError))
            {
                LogUtil.error(Name + "轴报警, 停止运动, 打开报警灯");

                StopMove();
            }
            else if (alarmType == AlarmType.SuddenStop)
            {
                isInSuddenDown = true;
                LogUtil.error(Name + "收到急停信号,停止运动, 打开报警灯 ");
                StopMove();
                storeStatus = StoreStatus.SuddenStop;
            }
            else if (alarmType.Equals(AlarmType.NoAirCheck))
            {
                isNoAirCheck = true;
                LogUtil.error(Name + " 未检测到气压信号 ,停止运动, 打开报警灯 ");
                StopMove();
                storeStatus = StoreStatus.SuddenStop;
            }
        }
        private bool InProcess = false;
        private bool IsChongfu = false;
        private Stopwatch stopwatch = new Stopwatch();
        protected override void timersTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (InProcess)
            {
                if (stopwatch.Elapsed.TotalSeconds < 10)
                {
                    return;
                }
                else
                {
                    LogUtil.error("主定时器:InProcess已等待" + stopwatch.Elapsed.ToString() + "重新处理");
                    IsChongfu = true;
                }
            }
            try
            {
                InProcess = true; 
                stopwatch.Restart(); 
                TimerProcess();
                ShowTimeLog("TimerProcess"); 
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "定时处理出错:" + ex.ToString());
            }
            IsChongfu = false;
            InProcess = false;
        }
        internal virtual void  TimerProcess()
        {

        }
        internal  void ShowTimeLog(string info)
        {
            if (IsChongfu)
            {
                LogUtil.info(Name + "【" + info + "】 处理完成,耗时:" + stopwatch.Elapsed.ToString());
            }
        }
        internal DateTime preIoTimerOutTime = DateTime.Now;
        internal void IOTimeOutProcess()
        {
            try
            {
                TimeSpan span = DateTime.Now - preIoTimerOutTime;
                if (span.TotalSeconds > 1)
                {
                    preIoTimerOutTime = DateTime.Now;
                    if (!alarmType.Equals(AlarmType.IoSingleTimeOut))
                    {
                        return;
                    }
                    if (runStatus < StoreRunStatus.Runing || isInSuddenDown || isNoAirCheck)
                    {
                        return;
                    }
                    //若BOX和移栽都没有在等待Io的过程中则此Io超时异常可能已经处理过
                    if (MoveInfo.IsInWait == false)
                    {
                        LogUtil.info(Name + "之前有IO超时异常【" + alarmInfo.alarmDetail + "】,但是当前已经没有在等待中,清理信号超时异常!");
                        alarmType = AlarmType.None;
                        WarnMsg = "";
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error(Name + "IOTimeOutProcess出错:" + ex.ToString());
            }
        }
        /// <summary>
        /// 判断是否报警,返回 true表示报警  报警检测2秒钟检测一次
        /// </summary>
        /// <returns></returns>
        internal DateTime checkAlarmTime = DateTime.Now;
        internal bool CheckAxisAlarm(AxisBean[] axisList)
        {
            if (alarmType.Equals(AlarmType.AxisAlarm) || alarmType.Equals(AlarmType.AxisMoveError))
            {
                return true;
            }
            TimeSpan span = DateTime.Now - checkAlarmTime;
            //在回原点,复位,出入库时,检测报警间隔减小
            if ((runStatus.Equals(StoreRunStatus.Runing) && span.TotalSeconds < 3) || span.TotalSeconds < 1)
            {
                return false;
            }
            checkAlarmTime = DateTime.Now;
            bool isInAlarm = false;

            foreach (AxisBean axisInfo in axisList)
            {
                short axis = axisInfo.Config.GetAxisValue();
                string deviceName = axisInfo.Config.GetNameStr();

                if (ACServerManager.GetAlarmStatus(deviceName, axis) == 1)
                {
                    WarnMsg = Name + " 运动轴" + axisInfo.Config.Explain + "报警";
                    Alarm(AlarmType.AxisAlarm, GetAlarmCodeByAxis(axisInfo.Config).ToString(), WarnMsg, MoveType.None);
                    isInAlarm = true;
                }
            }
            //判断报警状态
            return isInAlarm;
        }
        internal  bool IsDoValue(string ioType, IO_VALUE ioValue)
        {
            return IOValue(ioType).Equals(ioValue);
        }
        internal void  MoveEndP()
        {
            MoveInfo.EndMove();
            runStatus = StoreRunStatus.Runing;
            storeStatus = StoreStatus.StoreOnline;
            if (alarmType.Equals(AlarmType.None))
            {
                WarnMsg = "";
            }
        }

        #region 出入库结果验证 
        internal void CheckWait()
        {
            List<WaitResultInfo> list = MoveInfo.WaitList;
            //当等待超过一分钟时,需要打印提示 
            TimeSpan span = DateTime.Now - MoveInfo.LastSetpTime;
            string NotOkMsg = "";
            if (list.Count <= 0)
            {
                MoveInfo.EndStepWait();
                return;
            }
            bool isOk = true;
            if (MoveInfo.OneWaitCanEndStep)
            {
                isOk = false;
            }
            foreach (WaitResultInfo wait in list)
            {
                if (wait.IsEnd)
                {
                    if (!wait.WaitType.Equals(WaitEnum.W002_IOValue))
                    {
                        continue;
                    }
                }
                NotOkMsg = wait.ToStr();
                if (wait.WaitType.Equals(WaitEnum.W001_AxisMove))
                {
                    string msg = "";
                    if (wait.IsHomeMove)
                    {
                        wait.IsEnd = AxisBean.HomeMoveIsEnd(MoveInfo, wait.AxisInfo, out msg);
                    }
                    else
                    {
                        wait.IsEnd = AxisBean.ACAxisMoveIsEnd(MoveInfo, wait.AxisInfo, wait.TargetPosition, wait.TargetSpeed, out msg);
                    }
                    if (!msg.Equals(""))
                    {
                        isOk = false;
                        WarnMsg = msg;
                        Alarm(AlarmType.AxisMoveError, GetAlarmCodeByAxis(wait.AxisInfo).ToString(), WarnMsg, MoveInfo.MoveType);
                        break;
                    }
                }
                else if (wait.WaitType.Equals(WaitEnum.W002_IOValue))
                {
                    ConfigIO io = baseConfig.getWaitIO(wait.IoType);
                    NotOkMsg = " 等待【" + io.DisplayStr + "】=【" + wait.IoValue + "】";
                    wait.IsEnd = IOValue(wait.IoType).Equals(wait.IoValue);
                    int timeOutMs = StoreManager.Config.IOSingle_TimerOut;
                    if (!wait.IsEnd)
                    {
                        if (wait.IoType.Equals(IO_Type.Line_BackRun) || wait.IoType.Equals(IO_Type.Line_Run))
                        {
                            IOMove(wait.IoType, wait.IoValue);
                        } 
                        else if (span.TotalMilliseconds > timeOutMs && NoAlarm())
                        {
                          
                            WarnMsg = Name + "[" + MoveInfo.MoveType + "][" + MoveInfo.MoveStep + "] 等待(" + io.DisplayStr + "=" + wait.IoValue + ") 超时";
                            Alarm(AlarmType.IoSingleTimeOut, io.ElectricalDefinition, WarnMsg, MoveInfo.MoveType);
                            LogUtil.error(Name + "[" + MoveInfo.MoveType + "][" + MoveInfo.MoveStep + "] 等待(" + io.DisplayStr + "=" + wait.IoValue + ") 超时", logType + 14);
                            if (!MoveInfo.OneWaitCanEndStep)
                            {
                                isOk = false;
                                break;
                            }
                        }
                    }
                }
                else if (wait.WaitType.Equals(WaitEnum.W003_Time))
                {
                    wait.IsEnd = (span.TotalMilliseconds >= wait.TimeMSeconds);
                }
                else
                {
                    wait.IsEnd = CheckWaitResult(MoveInfo, wait);
                }
                if (wait.IsEnd)
                {
                    if (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) + "]秒";
                int second = 10;

                second = (int)(MoveInfo.TimeOutSeconds / span.TotalSeconds) * 10;
                if (second > 120)
                {
                    second = 120;
                }
                else if (second < 10)
                {
                    second = 10;
                }
                LogUtil.error(WarnMsg, logType + 100, second);
                Alarm(AlarmType.IoSingleTimeOut, "", WarnMsg, MoveInfo.MoveType);
            }
        }
        protected virtual bool CheckWaitResult(StoreMoveInfo moveInfo, WaitResultInfo wait)
        {
            return false;
        }

        #endregion

        public string GetMoveStr()
        {
            string msg = "";
            msg += "状态: " + runStatus + "   " + storeStatus + "\n";
            //  msg += "lineS: " + storeStatus + "\n";
            msg += "报警:  " + alarmType + "\n";
            msg += MoveInfo.MoveType + " " + MoveInfo.SLog + "\n";
            return msg;
        }
        protected void ClearTimeoutAlarm(string msg)
        {
            if (isInSuddenDown || isNoAirCheck)
            {
                return;
            }
            if (WarnMsg.Contains(msg) && alarmType.Equals(AlarmType.IoSingleTimeOut))
            {
                LogUtil.info(Name + "清理信号超时报警【" + WarnMsg + "】 ");
                alarmType = AlarmType.None;
                SetWarnMsg("");
            }
        }
    }
}