AgvClient_Line.cs 9.1 KB

using OnlineStore.Common;
using System;
using System.Collections.Generic;
using AsaPL;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OnlineStore.LoadCSVLibrary;

namespace OnlineStore.DeviceLibrary
{
    public class AgvClient_Line
    {
        private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.AgvServerIp);
        public AsaPL.AgvClient agvClient;
        private static bool isInit = false;
        public static Dictionary<string, AsaPL.ClientAction> actionMap = new Dictionary<string, AsaPL.ClientAction>();
         string Agv_In_Name = "A2";
        string Agv_Out_Name = "A1";
        public void Init(string agvIn,string agvOut)
        {
            try
            {
                if (!isInit)
                {
                    isInit = true;
                    agvClient = new AsaPL.AgvClient(ServerIp);
                    agvClient.CancelState = false;
                    agvClient.ReadyEnter += AgvClient_ReadyEnter;
                    agvClient.ReadyLeave += AgvClient_ReadyLeave;
                    agvClient.AGVFinishEnter += AgvClient_FinishEnter;
                    agvClient.AGVFinishLeave += AgvClient_FinishLeave;
                }
                actionMap = new Dictionary<string, AsaPL.ClientAction>();
                Agv_In_Name = agvIn;
                Agv_Out_Name = agvOut;
                agvClient.SetStatus(Agv_In_Name, "", ClientAction.None);
                agvClient.SetStatus(Agv_Out_Name, "", ClientAction.None);
                agvClient.Connect();
                LogUtil.info("初始化AGV服务");
            }
            catch (Exception ex)
            {
                LogUtil.error("初始化AGV服务 " + ServerIp + " 出错:", ex);
            }
        }

        /// <summary>
        /// 设置取消状态
        /// </summary>
        /// <param name="isCancel">true则ClientAction为None</param>
        public void SetCancelState(bool isCancel)
        {
            agvClient.CancelState = isCancel;
        }
        /// <summary>
        /// 料架准备进入线体
        /// </summary>
        /// <param name="name">节点名称</param>
        private void AgvClient_ReadyEnter(string name)
        {
            LogUtil.info("收到 AgvClient_ReadyEnter [" + name + "]");
            if (name.Equals(Agv_In_Name))
            {
                LineManager.Line.UpdateSleep(false);
                if (IOManager.IOValue(IO_Type.InCheck).Equals(IO_VALUE.LOW))
                {
                    Task.Factory.StartNew(delegate
                    {
                        SetStatus(name, "", ClientAction.MayEnter);
                        LogUtil.info("入口无料架,向服务端发送 MayEnter");
                        try
                        {
                            WaitUtil.Wait(60000, delegate
                            {
                                return IOManager.IOValue(IO_Type.InCheck).Equals(IO_VALUE.HIGH);
                            }, "等待InCheck=HIGH");
                        }
                        catch (Exception ex)
                        {
                            LogUtil.error("AgvClient_ReadyEnter:" + ex.ToString());
                        }

                        //两秒后改为离开状态
                        Thread.Sleep(3000);
                        LogUtil.info("调用  FinishEnter ");
                        SetStatus(name, "", ClientAction.FinishEnter);

                        Thread.Sleep(5000);
                        LogUtil.info( "处理结束  更新状态为None ");
                        SetStatus(name, "", ClientAction.None);

                    });
                }
                else
                {
                    LogUtil.error("InCheck 检测到有料架,AGV的料架无法进入");
                    SetStatus(name, "", ClientAction.None, ClientLevel.High);
                }
            }
        }
        /// <summary>
        ///料架准备离开线体
        /// </summary>
        /// <param name="name">节点名称</param>
        private void AgvClient_ReadyLeave(string name)
        {
            LogUtil.info("收到 AgvClient_ReadyLeave [" + name + "] ");
            if (name.Equals(Agv_Out_Name))
            {
                if (IOManager.IOValue(IO_Type.OutCheck).Equals(IO_VALUE.HIGH))
                {
                    SetStatus(name, "", ClientAction.MayLeave);
                    Thread.Sleep(3000);
                    LineManager.Line.UpdateSleep(false);
                    LineManager.Line.StopIOMove(IO_Type.OutStopDown, 1500);

                    LogUtil.info("下降 OutStopDown1.5秒");

                    Task.Factory.StartNew(delegate
                    {
                        //两秒后改为离开状态
                        Thread.Sleep(5000);
                        LogUtil.info("调用  FinishLeave ");
                        SetStatus(name, "", ClientAction.FinishLeave);

                        Thread.Sleep(5000);

                        LogUtil.info("处理结束  更新状态为None ");
                        SetStatus(name, "", ClientAction.None);
                    });
                }
                else
                {
                    LogUtil.error("OutCheck 未检测到料架,无法将料架进入AGV");
                    SetStatus(name, "", ClientAction.None, ClientLevel.High);
                }
            }

        }

        /// <summary>
        /// 料架离开小车完成
        /// </summary>
        /// <param name="name"></param>
        private void AgvClient_FinishLeave(string name)
        {
            LogUtil.info("收到 AgvClient_FinishLeave [" + name + "] ");
        }
        /// <summary>
        /// 料架进入小车完成
        /// </summary>
        /// <param name="name"></param>
        private void AgvClient_FinishEnter(string name)
        {
            LogUtil.info("收到 AgvClient_FinishEnter [" + name + "] ");
        }
        /// <summary>
        /// 左侧料架可以进入
        /// </summary>
        public bool A5_ReadyEnter { private set; get; }
        /// <summary>
        /// 右侧料架可以进入
        /// </summary>
        public bool A6_ReadyEnter { private set; get; }
        /// <summary>
        /// 右侧料架可以出去
        /// </summary>
        public bool A6_ReadyLeave { private set; get; }
        public void MayEnter(string name)
        {
            SetStatus(name, "", ClientAction.MayEnter);
            if (name.Equals("A5"))
            {
                A5_ReadyEnter = false;
            }
            else if (name.Equals("A6"))
            {
                A6_ReadyEnter = false;
            }
        }

        public void MayLeave(string name)
        {
            SetStatus(name, "", ClientAction.MayLeave);
            if (name.Equals("A6"))
            {
                A6_ReadyLeave = false;
            }
        }

        public void NeedEnter(string nodeName,string rfid)
        {
            SetStatus(nodeName, rfid,ClientAction.NeedEnter);
        }

        public void NeedLeave(string nodeName, string rfid)
        {
            SetStatus(nodeName, rfid,ClientAction.NeedLeave);
        }
        public void SetToNone(string nodeName)
        {
            SetStatus(nodeName);
        }
        private bool SetStatus(string name, string shelfId = "", ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.Low)
        {
            //    if (!agvClient.IsConn)
            //    {
            //        LogUtil.error("SetStatus AGV服务未连接!");
            //        return false;
            //    }

            if (actionMap.ContainsKey(name))
            {
                //ClientAction currA = actionMap[name]; //相同状态就设置一次
                //if (currA.Equals(action) && shelfId == "")
                //{
                //    return false;
                //}
                actionMap[name] = action;
                agvClient.SetStatus(name, shelfId, action, level);
                //LogUtil.info("设置 " + name + " [" + shelfId + "] " + action.ToString());
                return true;
            }
            agvClient.SetStatus(name, shelfId, action, level);
            actionMap.Add(name, action);
            LogUtil.debug("设置 " + name + " ["+ shelfId + "] "+ action.ToString());
            return true;
        }

        public bool ISConnected()
        {
            if (agvClient == null)
            {
                return false;
            }
            return agvClient.IsConn;
        }

        public void Dispose()
        {
            try
            {
                if (agvClient != null)
                {
                    agvClient.Close();
                    LogUtil.info("关闭AGV服务");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("关闭AGV服务 " + ServerIp + " 出错:", ex);
            }
        }
        public ClientAction GetAction(string NodeName)
        {
            if (actionMap.ContainsKey(NodeName))
            {
                return actionMap[NodeName];
            }
            return ClientAction.None;
        }
    }
}