AgvClient.cs 12.2 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;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    public class AgvClient
    {
        private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.AgvServerIp);
        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
            {
                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>();
                //foreach (string key in NodeList)
                //{
                //    actionMap.Add(key, Asa.ClientAction.None);
                //}
               
                agvClient.Connect();
                foreach (string str in NodeList)
                {
                    SetStatus(str);
                    LogUtil.info("agv init ,SetStatus[" + str + "]=none ");
                }

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

        public static void SetCancelState(bool isCancel)
        {
            agvClient.CancelState = isCancel;
        }
        private static void AgvClient_CloseDoor(string id, string rfid)
        {
            //RFIDData data = new RFIDData(content);
            LogUtil.info("收到 AgvClient_CloseDoor [" + id + "] [" + rfid + "]  ");
        }

        public static void SetStatus(string id, string shelfId = "", ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.Low)
        {
          //  ClientAction currA = GetAction(id);
            if (actionMap.ContainsKey(id))
            {
                ClientAction currA = actionMap[id]; //相同状态就设置一次
                if (currA.Equals(action))
                {
                    return;
                }
            }
           
            agvClient.SetStatus(id, "",shelfId, action, level);
            UpdateAction(id, action);
        }
        private static void AgvClient_Ready(string id, string rfid)
        {

         //   RFIDData data = new RFIDData(content);
            string logName = "收到 AgvClient_Ready [" + id + "] [" + rfid + "]   ";
            LogUtil.info(logName);

            UpdateAction(id, Asa.ClientAction.Ready);
            if (id.Equals(LineManager.Config.L1_AgvName))
            {
                if (IOManager.IOValue(IO_Type.L1_InCheck).Equals(IO_VALUE.LOW))
                {
                    IOManager.CIOMove(IO_Type.L1_Run, IO_VALUE.HIGH);
                    Task.Factory.StartNew(delegate
                    {
                        SetStatus(id, "", ClientAction.MayEnter);
                        LogUtil.info(logName + "  ,等待L1_InCheck");
                        try
                        {
                            WaitUtil.Wait(60000, delegate
                            {
                                return IOManager.IOValue(IO_Type.L1_InCheck).Equals(IO_VALUE.HIGH);
                            }, "等待L1_InCheck=HIGH");
                        }
                        catch (Exception ex)
                        {
                            LogUtil.error(logName + ":" + ex.ToString());
                        }
                         
                        //两秒后改为离开状态
                        Thread.Sleep(5000);
                        LogUtil.info(logName + "  ,调用  FinishEnter ");
                        SetStatus(id, "", ClientAction.FinishEnter);

                        Thread.Sleep(5000);
                        LogUtil.info(logName + "  ,处理结束  更新状态为None ");
                        SetStatus(id, "", ClientAction.None);
                        LineManager.DISTLine.UpdateSleep(false);
                    });
                }
                else
                {
                    LogUtil.error(logName + " OutL_InCheck 检测到有料架,AGV的料架无法进入");
                    SetStatus(id, "", ClientAction.None, ClientLevel.High);
                }
            }

            else if (id.Equals(LineManager.Config.L2_AgvName))
            {
                string shefId = LineManager.DISTLine.LastOutShelfId;
                if (IOManager.IOValue(IO_Type.L2_OutCheck).Equals(IO_VALUE.HIGH))
                {
                    IOManager.CIOMove(IO_Type.L2_Run, IO_VALUE.HIGH);
                    //agvClient.MayLeave(id);
                    SetStatus(id, shefId, ClientAction.MayLeave);
                    //LineManager.DISTLine.StopIOMove(IO_Type.L2_OutStopDown, 2000);
                    IOManager.CIOMove(IO_Type.L2_OutStopDown, IO_VALUE.HIGH);
                    LogUtil.info(logName + "下降 L2_OutStopDown ,  " + shefId);
                    Task.Factory.StartNew(delegate
                    {
                        try
                        {
                            WaitUtil.Wait(10000, delegate
                            {
                                IOManager.CIOMove(IO_Type.L2_OutStopDown, IOManager.IOValue(IO_Type.L2_OutCheck));
                                return IOManager.IOValue(IO_Type.L2_OutCheck).Equals(IO_VALUE.LOW);
                            }, "等待L2_OutCheck=Low");
                        }
                        catch (Exception ex)
                        {
                            LogUtil.error(logName + ":" + ex.ToString());
                        }
                        IOManager.CIOMove(IO_Type.L2_OutStopDown, IO_VALUE.LOW);
                        //两秒后改为离开状态
                        Thread.Sleep(5000);
                        LogUtil.info(logName + "  ,调用  FinishLeave " + shefId);
                        SetStatus(id, shefId, ClientAction.FinishLeave);

                        Thread.Sleep(5000);

                        LogUtil.info(logName + "  ,处理结束  更新状态为None ");
                        SetStatus(id, "", ClientAction.None);
                    });
                    LineManager.DISTLine.UpdateSleep(false);
                }
                else
                {
                    LogUtil.error(logName + "  L2_OutCheck 未检测到料架,无法将料架进入AGV");
                    SetStatus(id, "", ClientAction.None, ClientLevel.High);
                }
            }
            else if (id.Equals(LineManager.Config.L3_AgvName))
            {
                string shefId = LineManager.DISTLine.LastOutShelfId;
                if (IOManager.IOValue(IO_Type.L3_OutCheck).Equals(IO_VALUE.HIGH))
                {
                    SetStatus(id, shefId, ClientAction.MayLeave);
                    LogUtil.info(logName + "下降 L3_OutStopDown ,  " + shefId);
                    IOManager.CIOMove(IO_Type.L3_Run, IO_VALUE.HIGH);
                    IOManager.CIOMove(IO_Type.L3_OutStopDown, IO_VALUE.HIGH);
                    Task.Factory.StartNew(delegate
                    {
                        try
                        {
                            WaitUtil.Wait(10000, delegate
                            {
                                IOManager.CIOMove(IO_Type.L3_OutStopDown, IOManager.IOValue(IO_Type.L3_OutCheck));
                                return IOManager.IOValue(IO_Type.L3_OutCheck).Equals(IO_VALUE.LOW);
                            }, "等待L3_OutCheck=Low");
                        }
                        catch (Exception ex)
                        {
                            LogUtil.error(logName + ":" + ex.ToString());
                        }
                        IOManager.CIOMove(IO_Type.L3_OutStopDown, IO_VALUE.LOW);
                        //两秒后改为离开状态
                        Thread.Sleep(5000);
                        LogUtil.info(logName + "  ,调用  FinishLeave " + shefId);
                        SetStatus(id, shefId, ClientAction.FinishLeave);

                        Thread.Sleep(5000);

                        LogUtil.info(logName + "  ,处理结束  更新状态为None ");
                        SetStatus(id, "", ClientAction.None);
                    });
                    LineManager.DISTLine.UpdateSleep(false);
                }
                else
                {
                    LogUtil.error(logName + "  L3_OutCheck 未检测到料架,无法将料架进入AGV");
                    SetStatus(id, "", ClientAction.None, ClientLevel.High);
                }
            }
        } 
        private static void AgvClient_Arrive(string id, string rfid)
        { 
            UpdateAction(id, Asa.ClientAction.Arrive);
          //  RFIDData data = new RFIDData(content); 
            LogUtil.info("收到 AgvClient_Arrive [" + id + "] [" +rfid + "]   ");
          
        }
       
        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)
        {
            ClientAction currA = GetAction(id);
            if (currA.Equals(ClientAction.None) ||   currA.Equals(ClientAction.NeedLeave) || currA.Equals(ClientAction.NeedEnter))
            {
                SetStatus(id, shelfId, ClientAction.NeedEnter);
                return true;
            }
            return false;
        }
        public static bool NeedLeave(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.NeedLeave);
                return true;
            }
            return false;
        }
        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 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 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.Close " + ServerIp + " 出错:", ex);
            }
        }
    } 
}