AgvClient - 复制.cs 8.9 KB

//using Asa;
//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 = true ;
//        private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.AgvServerIp,"127.0.0.1");
//        private static Asa.AgvClient agvClient;
//        public static Dictionary<string, Asa.ClientAction> actionMap = new Dictionary<string, Asa.ClientAction>();
//        public static List<string> NodeList = new List<string>();
//        private static bool isInit = false;
//        public static void Init()
//        {
//            try
//            {
//                LogUtil.info("开始  Init agvclient");
//                if (!isInit)
//                {
//                    isInit = true;
//                    agvClient = new Asa.AgvClient(ServerIp);
//                    agvClient.CancelState = true;
//                    agvClient.Log += AgvClient_Log;
//                    agvClient.Arrive += AgvClient_Arrive;
//                    agvClient.Ready += AgvClient_Ready;
//                    agvClient.CloseDoor += AgvClient_CloseDoor;
//                }
//                actionMap = new Dictionary<string, Asa.ClientAction>();
              
//                LogUtil.info(" 开始 agvClient.Connect"); 
//                agvClient.Connect();
//                foreach (string str in NodeList)
//                {
//                    SetStatus(str);
//                } 
//            }
//            catch (Exception ex)
//            {
//                LogUtil.error("初始化agvClient " + ServerIp + " 出错:", ex);
//            }
//        }

//        public static void SetCancelState(bool isCancel)
//        {
//            if (agvClient == null)
//            {
//                return;
//            }
//            CurrCancelState = isCancel;
//            agvClient.CancelState = isCancel;
//        }
//        public static void SetStatus(string id, string shelfId = "", ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.Low,bool isMust=false)
//        {
//            if (agvClient == null)
//            {
//                return;
//            }
//            if (!isMust && level.Equals(ClientLevel.Low))
//            {
//                if (actionMap.ContainsKey(id))
//                {
//                    ClientAction currA = actionMap[id]; //相同状态就设置一次
//                    if (currA.Equals(action))
//                    {
//                        return;
//                    }
//                }
//            }
//            string mark = "";
//            if (shelfId != "")
//            {
//                int index = shelfId.IndexOf(',');
//                if (index > 0)
//                {
//                    //紧急出料模块,料架离开时,mark=紧急料或者分配料,rfid=料架号
//                    mark = shelfId.Substring(index + 1, shelfId.Length - index - 1);
//                    shelfId = shelfId.Substring(0, index);
//                }
//            }
//            agvClient.SetStatus(id,mark, shelfId, action, level);
//            UpdateAction(id, action);
//        }
//        private static void AgvClient_CloseDoor(string id, string rfid)
//        {
//            LogUtil.info("收到 AgvClient_CloseDoor [" + id + "] [" + rfid + "] 更新状态 ");
//            UpdateAction(id, ClientAction.CloseDoor);
//            Task.Factory.StartNew(delegate
//            {
//                Thread.Sleep(5000);
//                if (GetAction(id).Equals(ClientAction.CloseDoor))
//                {
//                    SetStatus(id);
//                    LogUtil.error("收到 AgvClient_CloseDoor [" + id + "] [" + rfid + "] 5秒后,更新状态为None ");
//                }
//            });
//        }
//        private static void AgvClient_Ready(string id, string rfid)
//        {
//            //UpdateAction(id, ClientAction.Ready);
//            ////    RFIDData data = new RFIDData(content);
//            //LogUtil.info("收到 AgvClient_Ready [" + id + "] [" + rfid + "]  ");
//            //FeedingEquip equip = getFeedEquip(id);

//            //if (equip == null)
//            //{
//            //    LogUtil.error("收到 AgvClient_Ready [" + id + "] [" + rfid + "] 未找到对应的设备 ,暂不处理");
//            //    return;
//            //}
//            //equip.AgvReady(id, rfid);
//        }

//        private static void AgvClient_Arrive(string id, string rfid)
//        {
//           // UpdateAction(id, ClientAction.Arrive);
//           //// RFIDData data = new RFIDData(content);
//           // LogUtil.info("收到 AgvClient_Arrive [" + id + "] [" + rfid + "]   ");
//           // FeedingEquip equip = getFeedEquip(id);
//           // if (equip == null)
//           // {
//           //     LogUtil.error("收到 AgvClient_Ready [" + id + "] [" + rfid + "] 未找到对应的设备 ,暂不处理 ");
//           //     return;
//           // }
//           // equip.AgvArrive(id, rfid);
//        }

//        //private static FeedingEquip getFeedEquip(string nodeId)
//        //{
//        //    foreach (FeedingEquip feed in StoreManager.XLRStore.FeedingEquipMap.Values)
//        //    {
//        //        if (feed.Config.AgvInName.Equals(nodeId) || feed.Config.AgvOutName.Equals(nodeId))
//        //        {
//        //            return feed;
//        //        }
//        //    }
//        //    return null;

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

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

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

//        public static Asa.ClientAction GetAction(string NodeName)
//        {
//            if (actionMap.ContainsKey(NodeName))
//            {
//                return actionMap[NodeName];
//            }
//            return Asa.ClientAction.None;
//        }
//        public static void UpdateAction(string name, Asa.ClientAction action)
//        {
//            if (actionMap.ContainsKey(name))
//            {
//                actionMap[name] = action;
//            }
//            else
//            {
//                actionMap.Add(name, action);
//            }
//        }
//        public static void Dispose()
//        {
//            try
//            {
//                if (agvClient != null)
//                {
//                    agvClient.Close();
//                }
//            }
//            catch (Exception ex)
//            {
//                LogUtil.error("释放 agvClient " + 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";
//    //}
//}