AgvClient.cs 5.7 KB

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

namespace OnlineStore.DeviceLibrary
{
    public class AgvClient
    {
        public static bool CurrCancelState = false;
        private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.AgvServerIp,"127.0.0.1");
        private static Client agvClient;
        public static List<Node> NodeList = new List<Node>();
        private static bool isInit = false;
        static int port = 12000;
        public static void Init()
        {
            try
            {
                LogUtil.info("开始  Init agvclient");
                if (!isInit)
                {
                    isInit = true;
                    agvClient = new Client();
                    agvClient.ReceivedEvent += AgvClient_ReceivedEvent;
                    agvClient.ConnectedEvent += AgvClient_ConnectedEvent;
                }
              
                LogUtil.info(" 开始 agvClient.Connect"); 
                agvClient.Connect(ServerIp, port);
            }
            catch (Exception ex)
            {
                LogUtil.error("初始化agvClient " + ServerIp + " 出错:", ex);
            }
        }
        static bool connectStr =false;
        private static void AgvClient_ConnectedEvent(bool status)
        {
            if(status!=connectStr)
            {
                connectStr= status;
                LogUtil.info($"连接AGV服务器结果:{ServerIp}:{port}" + (status ? "已连接" : "断开"));
            }
        }

        public delegate void ReceiveServerInfoHandlerInfo(Node node);
        public static event ReceiveServerInfoHandlerInfo RecvServerInfo;
        private static void AgvClient_ReceivedEvent(Node node)
        {
            RecvServerInfo?.Invoke(node);
        }

        public static void SetCancelState(bool isCancel)
        {
            if (agvClient == null)
            {
                return;
            }
            CurrCancelState = isCancel;
            agvClient.Shielded = isCancel;
        }
        public static void SetStatus(int id,NodeStatus nodeStatus,string shelfId="",NodeLevel level = NodeLevel.None,bool isMust=false)
        {
            if (agvClient == null)
            {
                return;
            }
           Node node= getNode(id);
            if (!isMust && level.Equals(NodeLevel.None))
            {
                if (node!=null)
                {
                   // NodeStatus currA = node.status; //相同状态就设置一次
                    if (node.status.Equals(nodeStatus) && node.shelf_id.Equals(shelfId))
                    {
                        return;
                    }
                }
            }
            //string mark = "";
            node.status = nodeStatus;
            node.shelf_id = shelfId;
            node.level = level;
            agvClient.SetStatus(node);
            UpdateAction(id, node.status);
        }
        static Node getNode(int id)
        {
           return NodeList.Find(s => s.id.Equals(id));
        }
        public static bool MayEnter(int id, string shelfId = "")
        {
            NodeStatus currA = GetAction(id);
            if (!currA.Equals(NodeStatus.MayEnter))
            {
                Node node = getNode(id);
                SetStatus(id, NodeStatus.MayEnter, shelfId);
                return true;
            }
            return false;
        }
        public static bool MayLeave(int id, string shelfId = "")
        {
            NodeStatus currA = GetAction(id);
            if (!currA.Equals(NodeStatus.MayLeave))
            {
                Node node = getNode(id);
                SetStatus(id, NodeStatus.MayLeave, shelfId);
                return true;
            }
            return false;
        }
        public static bool SetToNone(int id, string shelfId = "")
        {
            NodeStatus currA = GetAction(id);
            if (!currA.Equals(NodeStatus.None))
            {
               Node node = getNode(id);
                SetStatus(id, NodeStatus.None,shelfId);
                return true;
            }
            return false;
        }
        public static bool SetToComplete(int id, string shelfId = "")
        {
            NodeStatus currA = GetAction(id);
            if (!currA.Equals(NodeStatus.Complete))
            {
                Node node = getNode(id);
                SetStatus(id, NodeStatus.Complete,shelfId);
                return true;
            }
            return false;
        }
        public static bool NeedLeave(int id,string shelf_id="")
        {
            NodeStatus currA = GetAction(id);
            if (!currA.Equals(NodeStatus.NeedLeave))
            {
                SetStatus(id, NodeStatus.NeedLeave, shelf_id);
                return true;
            }
            return false;
        } 

        public static NodeStatus GetAction(int id)
        {
            Node node = getNode(id);
            if (node!=null)
            {
                return node.status;
            }
            return NodeStatus.None;
        }
        public static void UpdateAction(int id, NodeStatus action)
        {
            Node node=getNode(id);
            if (node!=null)
            {
                node.status = action;
            }
        }
        public static void Dispose()
        {
            try
            {
                if (agvClient != null)
                {
                    agvClient.Close();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("释放 agvClient " + ServerIp + " 出错:", ex);
            }
        }
    }
}