ABBRobotServer.cs 4.7 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 ClientKeepSecond = 10;
         
        private static int ServerPort = 21; 
        public static string ErrorInfo = "";
        private static string MoveOK = "point ok";
        private static string FreeOK = "free ok";
        private static string LockOK = "lock ok";

        private static string MoveCMD_1 = "move";
        private static string MoveCMD_2 = "move2";

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

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

        public static void StartServer(int port)
        {
            if (!IsStart)
            {
                ClientMap = new Dictionary<string, Client>();
                if (tcpserver == null)
                {
                    tcpserver = new TcpServer();
                }
                IsStart = true;
                tcpserver.Start(port);
                LogUtil.info( "ABBRobotServer在端口[" + port + "]监听!");
                tcpserver.ReviceMsgEvent += tcp_ReviceMsgEvent;
            }

        }

        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("处理料仓消息【" + 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.debug("收到【" + add + "】的消息【" + msg + "】");
            if (msg.ToUpper().Contains("ERROR"))
            {

            }else if(msg.ToUpper().Contains("MOVE")&& msg.ToUpper().Contains("OK"))
            {
                OnMoveEnd?.Invoke(msg);
            }
            else if (msg.ToUpper().Contains("MOVE2") && msg.ToUpper().Contains("OK"))
            { 
                OnMoveEnd?.Invoke(msg);
            } 
            return false;
        }

        public static void MoveTo(string robotIp, string PointName,   string moveType="L"  , double targetSpeed = 100, OpEnd AfterMove = null)
        { 
            ABBRobotServer.OnMoveEnd = AfterMove;
            SendMovePoint(robotIp, MoveCMD_1,PointName,moveType,targetSpeed.ToString());
        }
      
        public static void Move2To(string robotIp, string PointName, string moveType = "L", double targetSpeed = 100, OpEnd AfterMove = null)
        { 
            ABBRobotServer.OnMoveEnd = AfterMove;
            SendMovePoint(robotIp, MoveCMD_2, PointName, moveType, targetSpeed.ToString());
        }
        
        private static void SendMovePoint(string robotIp, string param1,string param2,string param3,string param4)
        {
            lock (LockObj)
            {
                if (ClientMap.ContainsKey(robotIp))
                {
                    Client client = ClientMap[robotIp];
                    string str = param1 + "," + param2 + "," + param3 + "," + param4+"\r\n";
                    LastSendPoint = param2;

                    SendStrToClient(client, str);
                }
            }
        }
        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;
        }
       
      
    }
}