AgvClient.cs 8.1 KB
using Agv;
using OnlineStore.Common;
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 int ServerPort = ConfigAppSettings.GetIntValue(Setting_Init.AgvServerPort);
        private static Agv.AgvClient agvClient;
        public static Dictionary<string, Agv.ClientAction> actionMap = new Dictionary<string, Agv.ClientAction>();
        public static Dictionary<string, Agv.ClientShelf> shelfMap = new Dictionary<string, Agv.ClientShelf>();
        public static List<string> NodeList = new List<string>();
        private static bool isInit = false;
        public static void Init()
        {
            try
            {
                if (!isInit)
                {
                    isInit = true;
                    agvClient = new Agv.AgvClient();
                    agvClient.Received += AgvClient_Received;
                    agvClient.Connected += AgvClient_Connected;
                    //NodeList.Add(StoreManager.Config.AgvInName);
                    //NodeList.Add(StoreManager.Config.AgvOutName);

                    actionMap = new Dictionary<string, Agv.ClientAction>();
                    shelfMap = new Dictionary<string, Agv.ClientShelf>();
                    foreach (string key in NodeList)
                    {
                        actionMap.Add(key, Agv.ClientAction.None);
                        shelfMap.Add(key, Agv.ClientShelf.None);
                    }
                    agvClient.Connect(ServerIp, ServerPort);
                    foreach (string id in NodeList)
                    {
                        //SetStatus(id, "");
                        agvClient.SetStatus(id);
                        LogUtil.info("agv init ,SetStatus[" + id + "]=none ");
                    }
                    SetCancelState(false);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("初始化agvClient " + ServerIp + " 出错:", ex);
            }
        }

        private static void AgvClient_Connected(bool status)
        {
            //LogUtil.info($"AgvClient_Connected:{status}");
        }

        private static void AgvClient_Received(Node node)
        {
            /*var nodename = node.Name;
            if (nodename == StoreManager.Config.AgvInName) {
                SetStatus(nodename, "", ClientAction.MayEnter);
                _ = StoreManager.Store.AgvSendShelfIn();
            }
            else if (nodename == StoreManager.Config.AgvOutName) {
                SetStatus(nodename, "", ClientAction.MayLeave);
                StoreManager.Store.OneShelfOutProcess();
            }*/
            AgvClient_Ready(node.Name, node.RFID);
        }

        public static void SetCancelState(bool cancel)
        {
            agvClient.CancelState = cancel;
        }
        public static void SetStatus(string id, string shelfId, ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.Low, ClientShelf clientShelf=ClientShelf.Empty)
        {
            if (agvClient == null)
                return;

            if (actionMap.ContainsKey(id) && shelfMap.ContainsKey(id))
            {
                Agv.ClientAction currA = actionMap[id]; //相同状态就设置一次
                Agv.ClientShelf clientShelf1 = shelfMap[id];
                if (currA.Equals(action) && clientShelf1.Equals(clientShelf))
                {
                    return;
                }
            }

            agvClient.SetStatus(id, "", shelfId, action, level, clientShelf);
            UpdateAction(id, action);
            UpdateShelf(id, clientShelf);
            LogUtil.info("AgvClient SetStatus id:" + id + ",shelfId:" + shelfId + ",action:" + action.ToString() + ",level:" + level.ToString() + ",clientShelf:" + clientShelf.ToString());

        }

        private static void AgvClient_Ready(string id, string rfid)
        {
            try
            {
                if (rfid!= "Heartbeat package")
                    LogUtil.info("收到 AgvClient_Ready [" + id + "] [" + rfid + "] " + GetAction(id).ToString());

                if (GetAction(id) == ClientAction.NeedEnter || id == StoreManager.Config.AgvInName)
                {
                    SetStatus(id, "", ClientAction.MayEnter);
                    _=StoreManager.Store.AgvSendShelfIn();
                }
                else if (GetAction(id) == ClientAction.NeedLeave || id == StoreManager.Config.AgvOutName)
                {
                    SetStatus(id, "", ClientAction.MayLeave);
                    StoreManager.Store.OneShelfOutProcess();
                }
                else
                {
                    //LogUtil.error("收到 AgvClient_Arrive [" + id + "] [" + rfid + "] 未找到对应的设备 ,暂不处理");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("AgvClient_Ready [" + id + "] [" + rfid + "]  处理出错:" + ex.ToString());
            }
        }

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

        public static bool SetToNone(string id, string shelfId = "")
        {
            Agv.ClientAction currA = GetAction(id);
            //if (currA.Equals(Agv.ClientAction.None) || currA.Equals(Agv.ClientAction.NeedLeave) || currA.Equals(Agv.ClientAction.NeedEnter))
            //{
                SetStatus(id, shelfId, Agv.ClientAction.None);
                return true;
            //}
           // return false;
        }
        public static bool NeedEnter(string id, string shelfId, ClientShelf clientShelf)
        {
            Agv.ClientAction currA = GetAction(id);
            if (currA.Equals(Agv.ClientAction.None) ||  currA.Equals(Agv.ClientAction.NeedEnter))
            {
                SetStatus(id, shelfId, Agv.ClientAction.NeedEnter,ClientLevel.High, clientShelf);
                return true;
            }
            return false;
        }
        public static bool NeedLeave(string id, string shelfId,ClientShelf clientShelf)
        {
            Agv.ClientAction currA = GetAction(id);
            if (currA.Equals(Agv.ClientAction.None) || currA.Equals(Agv.ClientAction.NeedLeave) || currA.Equals(Agv.ClientAction.NeedEnter))
            {
                SetStatus(id, shelfId, Agv.ClientAction.NeedLeave,ClientLevel.High, clientShelf);
                return true;
            }
            return false;
        }

        public static Agv.ClientAction GetAction(string NodeName)
        {
            if (actionMap.ContainsKey(NodeName))
            {
                return actionMap[NodeName];
            }
            return Agv.ClientAction.None;
        }        
        public static Agv.ClientShelf GetShelf(string NodeName)
        {
            if (shelfMap.ContainsKey(NodeName))
            {
                return shelfMap[NodeName];
            }
            return Agv.ClientShelf.None;
        }
        public static void UpdateAction(string name, Agv.ClientAction action)
        {
            if (actionMap.ContainsKey(name))
            {
                actionMap[name] = action;
            }
            else
            {
                actionMap.Add(name, action);
            }
        }
        public static void UpdateShelf(string name, Agv.ClientShelf action)
        {
            if (shelfMap.ContainsKey(name))
            {
                shelfMap[name] = action;
            }
            else
            {
                shelfMap.Add(name, action);
            }
        }
        public static void Dispose()
        {
            try
            {
                if (agvClient != null)
                {
                    SetCancelState(true);
                    agvClient.Close();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("agvClient.Close " + ServerIp + " 出错:", ex);
            }
        }
    }
}