MainMachine _Common.cs 12.6 KB
using OnlineStore;
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 DeviceLibrary
{
    partial class MainMachine
    {
        /// <summary>
        /// 发送托盘需求
        /// </summary>
        /// <param name="TrayType"></param>
        public RemoteResult SendTrayRequest(string TrayType, ReelParam reelParam)
        {
            RemoteLoad remoteLoad = new RemoteLoad();
            remoteLoad.Action = "SetTrayRequest";
            remoteLoad.GroupName = CID;
            remoteLoad.RequestLoadInfo = new RequestLoadInfo();
            remoteLoad.RequestLoadInfo.TrayType = TrayType;
            remoteLoad.RequestLoadInfo.LoadParam = reelParam.clone();
            remoteLoad.RequestLoadInfo.DeviceGroupName = StoreCID.AMH_ML5_1;
            remoteLoad.RequestLoadInfo.IsEmpty = true;
            return Remote.SendAndWait(remoteLoad);
        }
        public void StopMove(bool ServoOff=false)
        {
            runStatus = RunStatus.Stop;
            MoveInfo.List.ForEach((m)=> { m.EndMove(); });
            AxisBean.StopMultiAxis(AxisBean.List);

            if (ServoOff)
            {
                AxisBean.CloseMultiAxis(AxisBean.List);
            }
        }
        public void OpenAllServo()
        {
            AxisBean.RunMultiAxis(true, out _, AxisBean.List);
        }
        int logType = 1000;
        string WarnMsg = "";
        public bool CheckWait(MoveInfo MoveInfo)
        {
            //当等待超过一分钟时,需要打印提示 
            TimeSpan span = DateTime.Now - MoveInfo.LastSetpTime;
            string NotOkMsg = "";
            if (MoveInfo.WaitList.Count <= 0)
            {
                MoveInfo.EndStepWait();
                return false;
            }
            bool isOk = true;
            if (MoveInfo.OneWaitCanEndStep)
            {
                isOk = false;
            }

            MoveInfo.WaitList.ForEach((w) => {
                if (w.WaitType.Equals(WaitEnum.W014_Msg)) {
                    w.IsEnd = true;
                    Msg.add(w.ActionMsg, w.Data);
                }
            });
            foreach (WaitResultInfo wait in MoveInfo.WaitList)
            {
                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, WarnMsg);
                        Msg.add(WarnMsg, MsgLevel.alarm);
                        break;
                    }
                }
                else if (wait.WaitType.Equals(WaitEnum.W002_IOValue))
                {
                    ConfigIO io = RobotManage.Config.getWaitIO(wait.IoType);
                    NotOkMsg = MoveInfo.Name + $" {crc.GetString(L.wait, "等待")}【" + io.ElectricalDefinition + " " + crc.GetString(io.ProName, io.Explain) + "】=【" + wait.IoValue + "】";
                    wait.IsEnd = IOManager.IOValue(wait.IoType).Equals(wait.IoValue);
                    if (!wait.IsEnd)
                    {
                        int timeOutMs = RobotManage.Config.IOSingle_TimerOut * 1000;
                        if (span.TotalMilliseconds > timeOutMs && NoAlarm())
                        {

                            WarnMsg = MoveInfo.Name + "[" + MoveInfo.MoveStep + $"] {crc.GetString(L.wait, "等待")}(" + io.ElectricalDefinition + " " + crc.GetString(io.ProName, io.Explain) + "=" + wait.IoValue + $") {crc.GetString(L.timeout, "超时")}";

                            Msg.add(WarnMsg, MsgLevel.alarm);
                            if (NoAlarm())
                            {
                                //Alarm(AlarmType.IoSingleTimeOut, WarnMsg);
                                LogUtil.error(WarnMsg, logType + 14);
                                //MoveInfo.errlog(WarnMsg);
                                if (!MoveInfo.OneWaitCanEndStep)
                                {
                                    isOk = false;
                                    break;
                                }
                            }
                        }
                    }
                }
                else if (wait.WaitType.Equals(WaitEnum.W003_Time))
                {
                    wait.IsEnd = (span.TotalMilliseconds >= wait.TimeMSeconds);
                }
                else if (wait.WaitType.Equals(WaitEnum.W013_Action))
                {
                    wait.IsEnd = wait.Action.Invoke(wait);
                    LogUtil.info($"{Name} 自定义等待 IsEnd={wait.IsEnd},Type={wait.Action.GetType()}");
                }
                else if (wait.WaitType.Equals(WaitEnum.W008_BatchAxis))
                {
                    AxisBean axisBean = null;

                    //等待信号亮或者走到绝对位置才停止
                    if (IOValue(axisBean.TargetIoType).Equals(axisBean.TargetIoValue))
                    {
                        LogUtil.info(Name + "CheckWaitResult 检测到" + axisBean.TargetIoType + "=" + axisBean.TargetIoValue + ",停止运行");
                        axisBean.StopAxisCheckMove();
                        if (AxisManager.GetBusyStatus(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue()).Equals(1))
                        {
                            axisBean.SuddenStop();
                        }
                        wait.IsEnd = true;
                    }
                    else
                    {
                        bool isbusy = AxisManager.GetBusyStatus(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue()).Equals(1);
                        if (!isbusy)
                        {
                            int outCount = AxisManager.GetActualtPosition(wait.AxisInfo.DeviceName, wait.AxisInfo.GetAxisValue());
                            LogUtil.info($"{wait.AxisInfo.Explain},当前位置:{outCount}");
                            int errorCount = Math.Abs(outCount - wait.TargetPosition);
                            if (errorCount <= wait.AxisInfo.CanErrorCountMax)
                            {
                                LogUtil.info($"{wait.AxisInfo.Explain},到达目标:errorCount:{errorCount},停止上升");
                                axisBean.StopAxisCheckMove();
                                wait.IsEnd = true;
                            }
                            else
                            {
                                LogUtil.info($"{wait.AxisInfo.DeviceName},继续上升:{wait.TargetPosition}");
                                axisBean.AbsMove(null, wait.TargetPosition, wait.TargetSpeed);
                                wait.IsEnd = false;
                            }
                        }
                    }
                }

                if (wait.IsEnd)
                {
                    if (MoveInfo.OneWaitCanEndStep)
                    {
                        isOk = true;
                        break;
                    }
                }
                else
                {
                    if (!MoveInfo.OneWaitCanEndStep)
                    {
                        isOk = false;
                        break;
                    }
                }
            }
            if (isOk)
            {
                MoveInfo.EndStepWait();
                return false;
            }
            else if (span.TotalSeconds > MoveInfo.TimeOutSeconds)
            {
                WarnMsg = MoveInfo.Name + "[" + MoveInfo.MoveStep + $"]{crc.GetString(L.wait, "等待")}" + NotOkMsg + $"{crc.GetString(L.timeout, "超时")}[" + Math.Round(span.TotalSeconds, 1) + "]秒";

                int second = (int)(MoveInfo.TimeOutSeconds / span.TotalSeconds) * 10;
                if (second > 120)
                {
                    second = 120;
                }
                else if (second < 10)
                {
                    second = 10;
                }
                LogUtil.error(WarnMsg, logType + 100, second);
                //MoveInfo.errlog(WarnMsg);
                //Alarm(AlarmType.IoSingleTimeOut,  WarnMsg);
                Msg.add(WarnMsg, MsgLevel.alarm);
            }
            return true;
        }
        /// <summary>
        /// 是否再报警中
        /// </summary>
        AlarmType alarmType = AlarmType.None;
        DateTime LastAlarmTime = DateTime.Now;
        public void Alarm(AlarmType _alarmType, string alarmMsg = "")
        {
            if (_alarmType.Equals(AlarmType.None).Equals(false))
            {
                LastAlarmTime = DateTime.Now;
            }
            //SaveAlarmInfo(alarmType, alarmDetial, alarmMsg, storeMoveType);

            if (this.alarmType.Equals(_alarmType))
            {
                return;
            }
            LogUtil.error(Name + " 报警,报警类型:" + _alarmType);


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

                StopMove(true);
            }
            else if (_alarmType == AlarmType.SuddenStop)
            {
                //isInSuddenDown = true;
                LogUtil.error(Name + "收到急停信号,停止运动, 打开报警灯 ");
                StopMove(true);
                //storeStatus = StoreStatus.SuddenStop;
            }
            //else if (_alarmType.Equals(AlarmType.NoAirCheck))
            //{
            //    //isNoAirCheck = true;
            //    LogUtil.error(Name + " 未检测到气压信号 ,停止运动, 打开报警灯 ");
            //    StopMove(true);
            //    //storeStatus = StoreStatus.SuddenStop;
            //}
        }
        internal bool NoAlarm()
        {
            if (alarmType.Equals(AlarmType.None))
            {
                return true;
            }
            return false;
        }
        public void CylinderMove(MoveInfo moveInfo, string IoLowType, string IoHighType, IO_VALUE iO_VALUE = IO_VALUE.HIGH)
        {
            if (iO_VALUE == IO_VALUE.LOW)
            {
                //if (IOManager.DOValue(IoLowType).Equals(IO_VALUE.HIGH))
                //    return;
                (IoLowType, IoHighType) = (IoHighType, IoLowType);
            }
            //if (IOManager.DOValue(IoHighType).Equals(IO_VALUE.HIGH))
            //    return;
            IOMove(IoLowType, IO_VALUE.LOW);
            IOMove(IoHighType, IO_VALUE.HIGH);
            if (moveInfo != null)
            {
                moveInfo.WaitList.Add(WaitResultInfo.WaitIO(IoLowType, IO_VALUE.LOW));
                moveInfo.WaitList.Add(WaitResultInfo.WaitIO(IoHighType, IO_VALUE.HIGH));
            }
        }
        public IO_VALUE IOValue(string ioType) => IOManager.IOValue(ioType);
        void IOMove(string IoType, IO_VALUE value, bool isCheck = false, int msTime = 0)
        {
            if (msTime <= 0)
            {
                if (isCheck && (IOValue(IoType).Equals(value)))
                {
                    return;
                }
                IOManager.IOMove(IoType, value);
            }
            else
            {
                Task.Run(()=>
                {
                    IOManager.IOMove(IoType, value);
                    Thread.Sleep(msTime);
                    IO_VALUE tValue = value.Equals(IO_VALUE.HIGH) ? IO_VALUE.LOW : IO_VALUE.HIGH;

                    LogUtil.info(Name + "定时回写IO:  [" + IoType + "]=[" + value + "],msTime=" + msTime);

                    IOManager.IOMove(IoType, tValue);
                });
            }
        }
        
        public enum StringStateE
        {
            None=0,
            InStore,
            OutStore,            
            Full,
            ManualOut
        }
        public enum StringTypeE
        {
            None=0,
            In,
            Out
        }
    }
}