AgvClient-20200117-back.cs 7.9 KB

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

namespace OnlineStore.DeviceLibrary
{
    public class AgvClient
    {
        private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.AgvServerIp);
        private static Asa.Client agvClient;
        public static Dictionary<string, string> actionMap = new Dictionary<string, string>();
        public static List<string> NodeList = new List<string>();
        private static bool isInit = false;       
        public static void Init()
        {
            try
            {
                if (!isInit)
                {
                    isInit = true;
                    agvClient = new Asa.Client(ServerIp);
                    agvClient.Log += AgvClient_Log;
                    agvClient.Arrive += AgvClient_Arrive;
                    agvClient.CanEnter += AgvClient_CanEnter;
                    agvClient.Ready += AgvClient_Ready;
                }
                lastNeedLeave = "";
                lastNeedEnter = "";
                actionMap = new Dictionary<string, string>();
                foreach (string key in NodeList)
                {
                    actionMap.Add(key, AGVAction.None);
                }

                agvClient.Connect(NodeList.ToArray());

            }
            catch (Exception ex)
            {
                LogUtil.error("初始化agvClient " + ServerIp + " 出错:", ex);
            }
        }

        private static void AgvClient_Ready(string id, byte[] content)
        {

            UpdateAction(id, AGVAction.Ready);
            RFIDData data = new RFIDData(content);
            LogUtil.info("收到 AgvClient_Ready [" + id + "] [" + data.ToData() + "]   ");

            if (id.Equals(LineManager.Config.OutL_AgvName))
            {
                //    MayEnter("C2");
                if (IOManager.IOValue(IO_Type.OutL_InCheck).Equals(IO_VALUE.LOW))
                {
                    agvClient.MayEnter(id);
                    LogUtil.info("收到 AgvClient_Ready [" + id + "] [" + data.ToData() + "]  调用 MayEnter ");
                }
            }

            else if (id.Equals(LineManager.Config.InL_AgvName))
            {
                if (IOManager.IOValue(IO_Type.InL_OutStopCheck).Equals(IO_VALUE.HIGH))
                {
                    LineManager.VMILine.StopIOMove(IO_Type.InL_OutStopDown);
                    agvClient.MayLeave(id);
                    LogUtil.info("收到 AgvClient_Ready [" + id + "] [" + data.ToData() + "]  ,下降 InL_OutStopDown 600ms,调用 MayLeave ");
                }
            }
        }
        private static void AgvClient_CanEnter(string id, byte[] content)
        {
            UpdateAction(id, AGVAction.CanEnter);
               RFIDData data = new RFIDData(content);

            LogUtil.info("收到 AgvClient_CanEnter [" + id + "] [" + data.ToData() + "]   ");
            if (id.Equals(LineManager.Config.OutL_AgvName))
            {
                bool result = IOManager.IOValue(IO_Type.OutL_InCheck).Equals(IO_VALUE.LOW);
                if (IOManager.IOValue(IO_Type.OutL_InCheck).Equals(IO_VALUE.LOW))
                {
                    agvClient.IsEnter(id);
                }
            }
            else if (id.Equals(LineManager.Config.InL_AgvName))
            {

            }
        }

        private static void AgvClient_Arrive(string id, byte[] content)
        {

            UpdateAction(id, AGVAction.Arrive);
            RFIDData data = new RFIDData(content);

            LogUtil.info("收到 AgvClient_Arrive [" + id + "] [" + data.ToData() + "]   ");

        }


        public static bool ISConnected()
        {
            if (agvClient == null)
            {
                return false;
            }
            return agvClient.IsConn;
        }
        public static void SendRFID(string NodeName, string rfid)
        {
            agvClient.SendRFID(NodeName, rfid);

        }
        private static string lastNeedEnter = "";
        public static void NeedEnter(string NodeName, string shelfID, bool NeedClearLast = false)
        {
            if (NeedClearLast)
            {
                lastNeedEnter = "";
            }
            if (lastNeedEnter.Equals(NodeName + shelfID))
            {
                //return;
            }
            bool result = agvClient.NeedEnter(NodeName);
            if (result)
            {
                lastNeedEnter = NodeName + shelfID;
                LogUtil.info(" agvClient 料架需要进入:NeedEnter[" + NodeName + shelfID + "],呼叫成功");
            }
            else
            {
                LogUtil.info(" agvClient 料架需要进入:NeedEnter[" + NodeName + shelfID + "],呼叫失败,等待重新呼叫");
            }
        }
        private static string lastNeedLeave = "";
        public static void NeedLeave(string NodeName, string shelfID, bool NeedClearLast = false)
        {
            if (NeedClearLast)
            {
                lastNeedLeave = "";
            }
            if (lastNeedLeave.Equals(NodeName + shelfID))
            {
            //    return;
            }
            bool result = agvClient.NeedLeave(NodeName);
            if (result)
            {
                lastNeedLeave = NodeName + shelfID;
                LogUtil.info(" agvClient 料架需要离开:NeedLeave[" + NodeName + shelfID + "],呼叫成功");
            }
            else
            {

                LogUtil.info(" agvClient 料架需要离开:NeedLeave[" + NodeName + shelfID + "],呼叫失败,等待重新呼叫");
            }
        }
        public static void MayEnter(string NodeName)
        {
            agvClient.MayEnter(NodeName);
        }
        public static void FinishEnter(string NodeName)
        {
            agvClient.FinishEnter(NodeName);
        }

        public static void MayLeave(string NodeName)
        {
            agvClient.MayLeave(NodeName);
        }
        public static void IsEnter(string NodeName, bool result)
        {
            if (result)
            {

                agvClient.IsEnter(NodeName);
            }
        }
        private static void AgvClient_Log(string s)
        {
            try
            {
                bool isLog = ConfigAppSettings.GetIntValue(Setting_Init.Agv_Log_Open).Equals(1);
                if (isLog)
                {
                    LogUtil.info(" AGV " + ServerIp + " Log : " + s);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("AgvClient_Log 出错:" + ex.ToString());
            }
        }

        public static string GetAction(string NodeName)
        {
            if (actionMap.ContainsKey(NodeName))
            {
                return actionMap[NodeName];
            }
            return AGVAction.None;
        }
        public static void UpdateAction(string name, string action)
        {
            if (actionMap.ContainsKey(name))
            {
                actionMap[name] = action;
            }
            else
            {
                actionMap.Add(name, action);
            }
        }
        public static void Dispose()
        {
            try
            {
                lastNeedLeave = "";
                lastNeedEnter = "";
                if (agvClient != null)
                {
                    agvClient.Close();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("agvClient.Close " + ServerIp + " 出错:", ex);
            }
        }
    }
    public class AGVAction
    {
        public static string None = "None";
        public static string Arrive = "Arrive";
        public static string CanEnter = "CanEnter";
        public static string GetRFID = "GetRFID";
        public static string Ready = "Ready";
    }
}