ABBRobotServer.cs 7.5 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ABBRobotTest
{
    public class ABBRobotServer
    { 

        private static TcpServer tcpserver = null;
        public static bool IsStart = false; 

        private static int ABBServerPort = ConfigAppSettings.GetIntValue(Setting_Init.ABBServerPort ); 
        public static string ErrorInfo = "";
         
        internal static string Cmd_movep = "movep";
        internal static string Cmd_moveget = "moveget";
        internal static string Cmd_moveput = "moveput";
        internal static string Cmd_validateP = "validateP";

        public static Dictionary<string, Client> ClientMap = new Dictionary<string, Client>();
        
        //最后一次软件控制移动到的位置
        public static string LastSendPoint = "";

        public delegate void OpEnd(string result);
        private static event OpEnd OnCmdEnd;
         
        private static object LockObj = new object();

        public static void StartServer( )
        {
            try
            {
                if (ABBServerPort <= 0)
                {
                    ABBServerPort = 21;
                }
                if (!IsStart)
                {
                    ClientMap = new Dictionary<string, Client>();
                    if (tcpserver == null)
                    {
                        tcpserver = new TcpServer();
                    }
                    IsStart = true;
                    tcpserver.Start(ABBServerPort);
                    tcpserver.AcceptClientEvent += Tcpserver_AcceptClientEvent;
                    LogUtil.info("ABBRobotServer在端口[" + ABBServerPort + "]监听!");
                    tcpserver.ReviceMsgEvent += tcp_ReviceMsgEvent;
                }
            }catch(Exception ex)
            {
                LogUtil.error("ABBRobotServer启动[" + ABBServerPort + "]监听错误:" + ex.ToString());
            }
        }

        private static void Tcpserver_AcceptClientEvent(Client client)
        {
            SaveRobotClient(client);
        }
        private static object MapLock = "";
        private static void SaveRobotClient(  Client client)
        {
            lock (MapLock)
            {
                IPEndPoint clientipe = (IPEndPoint)client.ClientSocket.RemoteEndPoint;
                string ip = clientipe.Address.ToString();
                if (ClientMap.ContainsKey(ip))
                {
                    ClientMap.Remove(ip);
                }
                ClientMap.Add(ip, client);
            }
        }
        public static void StopServer()
        {
            try
            {
                if (IsStart)
                {
                    IsStart = false;
                    tcpserver.Stop();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("关闭 监听出错:" + ex.ToString());
            }
        }
        private static void tcp_ReviceMsgEvent(Client client, string msg)
        {
            try
            {
                if (String.IsNullOrEmpty(msg))
                {
                    return;
                }
                ProcessMsg(client, msg);

            }
            catch (Exception ex)
            {
                LogUtil.error("处理ABB消息【" + msg + "】出错:" + ex.ToString());
            }
        }

        private static bool ProcessMsg(Client client, string msg)
        {
            IPEndPoint clientipe = (IPEndPoint)client.ClientSocket.RemoteEndPoint;
            string add = clientipe.Address.ToString();
            //  string[] msgArray = msg.Split(cmd_spilt);
            msg = msg.Replace("\r", "") ;
            LogUtil.info("Revice[" + add + "]:[" + msg + "]");
            msg = msg.Replace(";", "");
            string OkStr = "OK";
            if (msg.Contains("ERROR"))
            {
                ErrorInfo = msg;
            }
            else if(msg.Contains(Cmd_movep)&& msg.Contains(OkStr))
            {
                OnCmdEnd?.Invoke(msg);
            }
            else if (msg.Contains(Cmd_moveput) && msg.Contains(OkStr))
            { 
                OnCmdEnd?.Invoke(msg);
            } else if (msg.Contains(Cmd_moveget) && msg.Contains(OkStr))
            {
                OnCmdEnd?.Invoke(msg);
            }else if (msg.Contains(OkStr))
            {
                OnCmdEnd?.Invoke(msg);
            }
            return false;
        }

        public static bool MoveToP(string robotIp, string PointName,   string moveType="L"  , double targetSpeed = 100, OpEnd AfterCmd = null)
        { 
            ABBRobotServer.OnCmdEnd = AfterCmd;
            return SendMovePoint(robotIp, Cmd_movep, PointName,moveType,targetSpeed.ToString());
        }
      
        public static bool MoveToPut(string robotIp, string PointName, string moveType = "L", double targetSpeed = 100, OpEnd AfterCmd = null)
        { 
            ABBRobotServer.OnCmdEnd = AfterCmd;
            return SendMovePoint(robotIp, Cmd_moveput, PointName, moveType, targetSpeed.ToString());
        }
        public static bool MoveToGet(string robotIp, string PointName, string moveType = "L", double targetSpeed = 100, OpEnd AfterCmd = null)
        {
            ABBRobotServer.OnCmdEnd = AfterCmd;
            return SendMovePoint(robotIp, Cmd_moveget, PointName, moveType, targetSpeed.ToString());
        }
        public static bool SendCmd(string robotIp, string moveCmd,string PointName, string moveType = "L", double targetSpeed = 100, OpEnd AfterCmd = null)
        {
            ABBRobotServer.OnCmdEnd = AfterCmd;
            if (moveCmd.Equals(Cmd_moveget) || moveCmd.Equals(Cmd_movep) || moveCmd.Equals(Cmd_moveput)||moveCmd.Equals(Cmd_validateP))
            {
                return SendMovePoint(robotIp, moveCmd, PointName, moveType, targetSpeed.ToString());
            }
            return false;
        }
        public static bool ValidateP(string robotIp, string pointName, OpEnd AfterCmd = null)
        {
            ABBRobotServer.OnCmdEnd = AfterCmd; 
            return SendMovePoint(robotIp, Cmd_validateP, pointName); 
        }
        private static bool SendMovePoint(string robotIp, string param1, string param2, string param3 = "L", string param4 = "10")
        {
            Client client = null;
            lock (LockObj)
            {
                if (ClientMap.ContainsKey(robotIp))
                {
                    client = ClientMap[robotIp];
                }
            }
            if (client != null)
            {
                string str = param1 + "," + param2 + "," + param3 + "," + param4 + "";
                LastSendPoint = param2;
                LogUtil.info("Send [" + robotIp + "] : [" + str + "]");
                SendStrToClient(client, str);
                return true;
            }
            return false;
        }
        private static bool SendStrToClient(Client client, string sendMsg)
        {
            if (client != null && client.ClientSocket.Connected)
            {
                byte[] sendBuffer = new byte[1024];
                sendBuffer = Encoding.UTF8.GetBytes(sendMsg);
                client.ClientSocket.Send(sendBuffer);
                return true;
            }

            return false;
        }
        internal static bool RobotIsConnect(string ip)
        {
            if (ClientMap.ContainsKey(ip))
            {
                return true;
            }
            return false;
        }
    }
}