RobotMoveBean.cs 13.5 KB
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class RobotMoveBean
    {
        internal string RobotIp = "";
        internal string Name = "";
        internal string DI_TrayCheck = "";
        internal LineMoveInfo MoveInfo = null;
        internal int robotNum = 1;
        public string WarnMsg = "";
        internal string NeedShelf = "";
        internal TrayInfo CurrTray = null;

        internal List<string> MyPointList = new List<string>();
        public string CurrPoint = "";

        public RobotMoveBean(int num, string ip, string name, string traycheck)
        {
            this.robotNum = num;
            MoveInfo = new LineMoveInfo();
            this.RobotIp = ip;
            this.Name = name;
            this.DI_TrayCheck = traycheck;
            int smallStart = 1;
            int smallEnd = 46;
            int bigStart = 1;
            int bigEnd = 6;
            if (num.Equals(1))
            {
                smallStart = 47;
                smallEnd = 92;
                bigStart = 7;
                bigEnd = 12;
            }
            //大料架=C, 小料架=D
            for (int s = smallStart; s <= smallEnd; s++)
            {
                MyPointList.Add("dp" + s);
            }

            for (int b = bigStart; b <= bigEnd; b++)
            {
                MyPointList.Add("cp" + b);
            }
            CurrTray = new TrayInfo();
        }

        public bool CanChange(string currShelf)
        {
            if (CurrTray != null)
            {
                TimeSpan span = DateTime.Now - CurrTray.updateTime;
                if (span.TotalSeconds > 5)
                {
                    return true;
                }
                else if (!CurrTray.realRFID.Equals(currShelf))
                {
                    return true;
                }
                else if (span.TotalSeconds > 2 && IOManager.IOValue(DI_TrayCheck).Equals(IO_VALUE.LOW))
                {
                    return true;
                }
            }
            else if (IOManager.IOValue(DI_TrayCheck).Equals(IO_VALUE.LOW))
            {
                return true;
            }
            return false;
        }

        public void Reset()
        {
            LogUtil.info(Name + "复位");
            MoveInfo.EndMove();
            //判断机器人是否连接,若未连接,直接连接
            if (!ABBControl.IsConnected(RobotIp))
            {
                ABBControl.StartABBControl(RobotIp);
            }
            WarnMsg = "";
            NeedShelf = "";
            CurrTray = new TrayInfo();
        }

        internal void StopMove()
        {
            LogUtil.info(Name + " Stop Move ");
            NeedShelf = "";
            MoveInfo.EndMove();
        }
        //     private Stopwatch DiStop = new Stopwatch();
        private Stopwatch abbWatch = new Stopwatch();
        private string lockTimer = "";
        internal void TimerProcess()
        {
            if (Monitor.TryEnter(lockTimer, 1))
            {
                try
                {
                    if (LineManager.checkWatch(abbWatch, 3000, true) || WarnMsg.Contains(RobotIp))
                    {
                        if (!ABBControl.IsConnected(RobotIp))
                        {
                            WarnMsg = Name + "[" + RobotIp + "]未正常连接";
                            return;
                        }
                        else if (WarnMsg.Contains(RobotIp))
                        {
                            WarnMsg = "";
                        }
                    }
                    if (!MoveInfo.MoveType.Equals(LineMoveType.None))
                    {
                        BusyProcess();
                        return;
                    }
                    else if (IOManager.IOValue(DI_TrayCheck).Equals(IO_VALUE.LOW))
                    {
                        CurrPoint = "";
                        NeedShelf = "";
                        //  DiStop.Stop();
                        return;
                    }

                    else if (LineManager.Line.S1IsReady())
                    {
                        string outMsg = "";
                        CurrTray = HttpServer.GetDiskInfo(robotNum, LineManager.Line.S1_CurrShelfId, out outMsg);
                        //需要取放料,且料架已准备好
                        if (String.IsNullOrEmpty(CurrTray.getP).Equals(false) && outMsg.Equals(""))
                        {
                            if (WarnMsg.Contains("getLocation"))
                            {
                                WarnMsg = "";
                            }
                            //   CurrTray = tray;
                            if (IsRightRfid(LineManager.Line.S1_CurrShelfId, out NeedShelf))
                            {
                                //DiStop.Stop();
                                string pname = "dp";
                                if (LineManager.Line.S1_CurrShelfId.StartsWith("C"))
                                {
                                    pname = "cp";
                                }
                                CurrPoint = pname + CurrTray.shelfP;

                                //判断另一个机器人是否干涉
                                if (LineManager.Line.ABBCanStart(robotNum, MyPointList.Contains(CurrPoint)))
                                {
                                    if (!CurrTray.realRFID.Equals(LineManager.Line.S1_CurrShelfId) || LineManager.Line.GetXNrfid(LineManager.Line.S1_CurrShelfId).Equals(""))
                                    {
                                        CurrTray.realRFID = LineManager.Line.S1_CurrShelfId;
                                        LineManager.Line.AddRealShelf(CurrTray.realRFID, CurrTray.rfid);
                                    }
                                    //获取成功,开始放料
                                    MoveInfo.NewMove(LineMoveType.PutTray, MoveStep.T01_GetTray);
                                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
                                    LogUtil.info(Name + MoveInfo.SLog + "开始取料【" + CurrPoint + "】" + CurrTray.ToStr() + "");
                                    ABBControl.SendCmd(RobotIp, ABBControl.Cmd_moveget, CurrTray.getP, "L", ABBControl.DefaultSpeed);
                                }

                            }
                        }
                        else
                        {
                            CurrPoint = "";
                            NeedShelf = "";
                            if (!outMsg.Equals(""))
                            {
                                WarnMsg = " getLocation " + outMsg;
                            }
                        }

                    }

                }
                catch (Exception ex)
                {
                    LogUtil.error(Name + "Timer Process Error :" + ex.ToString());
                }
                //机器人报警
                finally { Monitor.Exit(lockTimer); }

            }
            else
            {
                LogUtil.error(Name + "TimerProcess 未得到锁     ", robotNum * 100 + 13);
            }
        }

        private void BusyProcess()
        {
            if (MoveInfo.IsInWait)
            {
                LineManager.Line.CheckWait(MoveInfo);
            }

            if (MoveInfo.IsInWait)
            {
                return;
            }
            if (MoveInfo.MoveStep.Equals(MoveStep.Wait))
            {
                MoveInfo.NextMoveStep(MoveStep.T01_GetTray);
                MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
                ABBControl.SendCmd(RobotIp, ABBControl.Cmd_moveget, CurrTray.getP, "L", ABBControl.DefaultSpeed);
                //putFinTask = null;
            }
            else if (MoveInfo.MoveStep.Equals(MoveStep.T01_GetTray))
            {
                if (ABBControl.GetAbbCmd(RobotIp).Equals(""))
                {
                    MoveInfo.NextMoveStep(MoveStep.T02_PutTray);
                    LogUtil.debug(Name + MoveInfo.SLog + "【" + CurrPoint + "】取料完成,开始放料");
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(100));
                    ABBControl.SendCmd(RobotIp, ABBControl.Cmd_moveput, CurrPoint, "L", ABBControl.DefaultSpeed);
                }
                else if (MoveInfo.IsTimeOut(180))
                {
                    TimeOutAlarm("等待[" + CurrPoint + "]" + CurrTray.ToStr() + " 取料完成");
                }
            }
            else if (MoveInfo.MoveStep.Equals(MoveStep.T02_PutTray))
            {
                if (ABBControl.GetAbbCmd(RobotIp).Contains(ABBControl.FINISHED))
                {
                    MoveInfo.NextMoveStep(MoveStep.T03_PutFINISHED);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(10000));
                    LineManager.Line.S1_ShelfTask = HttpServer.ShelfFinish(CurrTray.realRFID, CurrTray.barcode, CurrTray.shelfP.ToString(), robotNum.ToString());
                    MoveInfo.EndStepWait();
                }
                else if (ABBControl.GetAbbCmd(RobotIp).Equals(""))
                {
                    MoveInfo.NextMoveStep(MoveStep.T04_WaitEnd);
                    MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(10000));
                    LineManager.Line.S1_ShelfTask = HttpServer.ShelfFinish(CurrTray.realRFID, CurrTray.barcode, CurrTray.shelfP.ToString(), robotNum.ToString());
                    EndMove();
                }
                else if (MoveInfo.IsTimeOut(180))
                {
                    TimeOutAlarm("等待[" + CurrPoint + "]" + CurrTray.ToStr() + " FINISHED");
                }
            }
            else if (MoveInfo.MoveStep.Equals(MoveStep.T03_PutFINISHED))
            {
                if (ABBControl.GetAbbCmd(RobotIp).Equals(""))
                {
                    EndMove();
                }
                else if (MoveInfo.IsTimeOut(180))
                {
                    TimeOutAlarm("等待[" + CurrPoint + "]" + CurrTray.ToStr() + " 放料完成");
                }
            }
            else if (MoveInfo.MoveStep.Equals(MoveStep.T04_WaitEnd))
            {
                EndMove();
            }
        }
        private void EndMove()
        {
            LogUtil.info(Name + MoveInfo.SLog + "【" + CurrPoint + "】结束放料 " + DateTime.Now.ToLongTimeString() + "");
            CurrTray.updateTime = DateTime.Now;
            MoveInfo.EndMove();
            NeedShelf = "";
        }
        private void TimeOutAlarm(string msg)
        {
            WarnMsg = Name + "[" + MoveInfo.MoveType + "][" + MoveInfo.SLog + "]" + msg + "" + CurrPoint + CurrTray.ToStr() + " [" + Math.Round(MoveInfo.StepSpan().TotalSeconds, 1) + "]秒";
            LogUtil.error(WarnMsg, robotNum * 100 + 12);
        }
        internal bool IsRightRfid(string currRfid, out string NeedRfid)
        {
            if (CurrTray == null)
            {
                NeedRfid = "";
                return false;
            }

            if (CurrTray.realRFID.Equals(currRfid))
            {
                NeedRfid = currRfid;
                return true;
            }
            else if (String.IsNullOrEmpty(CurrTray.realRFID))
            {
                NeedRfid = CurrTray.rfid;

                //料架号=000,不放料
                if (!currRfid.StartsWith("D")&& !currRfid.StartsWith("C"))
                {
                    return false;
                }

                //如果虚拟料架号一致,返回ture
                string xnrfid = LineManager.Line.GetXNrfid(currRfid);
                if ((!xnrfid.Equals("") && xnrfid.Equals(CurrTray.rfid)))
                {
                    NeedRfid = currRfid;
                    return true;
                }

                if (!String.IsNullOrEmpty(CurrTray.usedRfidList))
                {
                    string[] array = CurrTray.usedRfidList.Split(',');
                    //若料架已使用过,直接返回false
                    if (array.Contains(currRfid))
                    {
                        return false;
                    }
                }


                //当前是第一盘料,使用虚拟料架号
                string needType = CurrTray.rfid.Substring(CurrTray.rfid.Length - 1, 1);
                string currType = currRfid.Substring(0, 1);
                //类型相同,且此料架未绑定

                if (needType.Equals(currType) && xnrfid.Equals(""))
                {
                    NeedRfid = currRfid;
                    return true;
                }
                return false;
            }
            else
            {
                NeedRfid = CurrTray.realRFID;
                //真实料架对不上
                return false;
            }
        }


        internal string GetRunInfo()
        {
            string info = "" + Name + ":" + MoveInfo.MoveType + "  " + MoveInfo.MoveStep;

            if (CurrPoint != "")
            {
                info += " ,位置=" + CurrPoint + "";
            }
            if (NeedShelf != "")
            {
                info += ", 需要料架=" + NeedShelf + "";
            }
            if (String.IsNullOrEmpty(CurrTray.getP).Equals(false))
            {
                info += ", " + CurrTray.ToStr() + " ";
            }
            return info;
        }
    }

}