AgvClient_Dbline.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;

namespace OnlineStore.DeviceLibrary
{
    public class AgvClient_Dbline
    {
        private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.Line_AGV);
        public AsaPL.AgvClient agvClient;
        private static bool isInit = false;
        public static Dictionary<string, AsaPL.ClientAction> actionMap = new Dictionary<string, AsaPL.ClientAction>();
        public bool A5_InProcess = false;
        public bool A6_InProcess = false;

        public string A6_RFID { get; set; } = "";
        #region A5 Action
        /// <summary>
        /// 上下满
        /// </summary>
        public void A5_None()
        {
            SetStatus("A5", "", ClientAction.None,ClientLevel.Low);
        }
        public void A5_NeedD()
        {
            SetStatus("A5", "", ClientAction.NeedD,ClientLevel.Low);
        }
        public void A5_NeedD_High()
        {
            SetStatus("A5", "", ClientAction.NeedD,ClientLevel.High);
        }
        public void A5_NeedC()
        {
            SetStatus("A5", "", ClientAction.NeedC,ClientLevel.Low);
        }
        /// <summary>
        /// 上下都未满(正常)
        /// </summary>
        public void A5_NeedEnter()
        {
            SetStatus("A5", "", ClientAction.NeedEnter,ClientLevel.Low);
        }
        public void A5_NeedEnter_High()
        {
            SetStatus("A5", "", ClientAction.NeedEnter, ClientLevel.High);
        }
        #endregion

        #region A6 Action
        /// <summary>
        /// 上ON,下OFF
        /// </summary>
        public void A6_None()
        {
            SetStatus("A6", "", ClientAction.None);
        }
        /// <summary>
        /// 上OFF,下OFF
        /// </summary>
        public void A6_NeedEnter()
        {
            SetStatus("A6", "", ClientAction.NeedEnter);
        }
        /// <summary>
        /// 上ON,下ON
        /// </summary>
        public void A6_NeedLeave()
        {
            SetStatus("A6", A6_RFID, ClientAction.NeedLeave);
        }
        /// <summary>
        /// 上OFF,下ON
        /// </summary>
        public void A6_NeedEnterLeave()
        {
            SetStatus("A6", A6_RFID, ClientAction.NeedEnterLeave);
        }
        #endregion

        public void Init()
        {
            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>();
                agvClient.SetStatus("A5", "", ClientAction.None);
                agvClient.SetStatus("A6", "", 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)
        {
            if (name.Equals("A5"))
            {
                A5_InProcess = true;
                A5_ReadyEnter = true;
            }
            else if (name.Equals("A6"))
            {
                A6_InProcess = true;
                A6_ReadyEnter = true;
            }
            LogUtil.info("收到 AgvClient_ReadyEnter [" + name + "]");
        }
        /// <summary>
        ///料架准备离开
        /// </summary>
        /// <param name="name">节点名称</param>
        private void AgvClient_ReadyLeave(string name)
        {

            //SetStatus(name, "", ClientAction.ReadyLeave);
            if (name.Equals("A6"))
            {
                A6_InProcess = true;
                A6_ReadyLeave = true;
            }
            LogUtil.info("收到 AgvClient_ReadyLeave [" + name + "] ");

        }

        /// <summary>
        /// 料架离开小车完成
        /// </summary>
        /// <param name="name"></param>
        private void AgvClient_FinishLeave(string name)
        {
            //if (name.Equals("A5"))
            //{
            //    A5_InProcess = false;
            //}
            //else if (name.Equals("A6"))
            //{
            //    A6_InProcess = false;
            //}
            LogUtil.info("收到 AgvClient_FinishLeave [" + name + "] ");
        }
        /// <summary>
        /// 料架进入小车完成
        /// </summary>
        /// <param name="name"></param>
        private void AgvClient_FinishEnter(string name)
        {
            //if (name.Equals("A5"))
            //{
            //    A5_InProcess = false;
            //}
            //else if (name.Equals("A6"))
            //{
            //    A6_InProcess = false;
            //}
            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 FinishEnter(string name)
        {
            Task.Factory.StartNew(delegate {
                SetStatus(name, "", ClientAction.FinishEnter);

                Thread.Sleep(15000);
                if (name.Equals("A5"))
                {
                    A5_InProcess = false;
                }
                else if (name.Equals("A6"))
                {
                    A6_InProcess = false;
                }
            });

        }

        public void FinishLeave(string name)
        {
            Task.Factory.StartNew(delegate
            {
                SetStatus(name, "", ClientAction.FinishLeave);
                Thread.Sleep(15000);
                if (name.Equals("A5"))
                {
                    A5_InProcess = false;
                }
                else if (name.Equals("A6"))
                {
                    A6_InProcess = false;

                }
            });

        }

        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.info("设置 " + 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);
            }
        }
    }
}