AgvClient.cs 6.8 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 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;
                    NodeList.Add(StoreManager.Config.AgvInName);
                    NodeList.Add(StoreManager.Config.AgvOutName);
                }
                actionMap = new Dictionary<string, Agv.ClientAction>();
                foreach (string key in NodeList)
                {
                   actionMap.Add(key, Agv.ClientAction.None);
                }
                agvClient.Connect(ServerIp, ServerPort);
                foreach (string str in NodeList)
                {
                    SetStatus(str,"");
                    LogUtil.info("agv init ,SetStatus[" + str + "]=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, bool isMust = false)
        {
            if (agvClient == null)
                return;

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

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

        }

        private static void AgvClient_Ready(string id, string rfid)
        {
            try
            {
                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)
        {
            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.NeedEnter);
                return true;
            }
            return false;
        }
        public static bool NeedLeave(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.NeedLeave);
                return true;
            }
            return false;
        }

        public static Agv.ClientAction GetAction(string NodeName)
        {
            if (actionMap.ContainsKey(NodeName))
            {
                return actionMap[NodeName];
            }
            return Agv.ClientAction.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 Dispose()
        {
            try
            {
                if (agvClient != null)
                {
                    SetCancelState(true);
                    agvClient.Close();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("agvClient.Close " + ServerIp + " 出错:", ex);
            }
        }
    }
}