AgvClient.cs 21.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
//using System;
//using System.Collections.Generic;
//using System.Net;
//using System.Net.Sockets;
//using System.Threading;

//namespace Asa
//{
//    /// <summary>
//    /// AGV客户端
//    /// </summary>
//    public class AgvClient
//    {
//        private bool _loopClient;
//        private int _countRecon;
//        private bool _loopRecon;
//        private Socket _clientSocket;        //客户端
//        private List<ClientNode> _node;
//        private Thread tSend;          //发送
//        private Thread tListen;        //监听网络
//        private Thread tRecon;

//        private readonly log4net.ILog LOG;
//        private readonly string SERVER_IP;        //远程IP地址
//        private const int SERVER_PORT = 12000;    //端口

//        /// <summary>
//        /// 小车接收事件
//        /// </summary>
//        /// <param name="name">名称</param>
//        /// <param name="realRfid">真实RFID</param>
//        /// <param name="virtualRfid">虚拟RFID</param>
//        /// <param name="action">动作</param>
//        /// <param name="level">优先级</param>
//        /// <param name="shelf">料架</param>
//        /// <param name="type">放置地点</param>
//        public delegate void ReceiveEvent(ClientNode node);
//        /// <summary>
//        /// 服务器连接事件
//        /// </summary>
//        /// <param name="status"></param>
//        public delegate void ConnectEvent(bool status);
//        /// <summary>
//        /// 小车接收事件
//        /// </summary>
//        public event ReceiveEvent Received;
//        /// <summary>
//        /// 服务器连接事件
//        /// </summary>
//        public event ConnectEvent Connected;



//        /// <summary>
//        /// AGV客户端
//        /// </summary>
//        /// <param name="serverIP">服务器IP地址</param>
//        /// <param name="logName"></param>
//        public AgvClient(string serverIP, string logName = "AgvClient")
//        {
//            SERVER_IP = serverIP;
//            _node = new List<ClientNode>();
//            LOG = log4net.LogManager.GetLogger(logName);
//        }

//        /// <summary>
//        /// 是否连接服务器
//        /// </summary>
//        public bool IsConn { private set; get; } = false;

//        /// <summary>
//        /// 发送命令的时间间隔,不能大于10s(单位:秒)
//        /// </summary>
//        public int SendSleep { set; get; } = 3;



//        /// <summary>
//        /// 连接
//        /// </summary>
//        public void Connect()
//        {
//            _countRecon = 0;
//            _loopRecon = true;
//            IsConn = false;
//            _loopClient = true;

//            tListen = new Thread(new ThreadStart(ListenServerNet));
//            tSend = new Thread(new ThreadStart(SendStatus));
//            tRecon = new Thread(new ThreadStart(Reconnect));
//            tRecon.Start();
//            tListen.Start();
//            tSend.Start();
//            LOG.Info("连接服务器");
//        }

//        /// <summary>
//        /// 关闭
//        /// </summary>
//        public void Close()
//        {
//            _loopClient = false;
//            _loopRecon = false;

//            if (_clientSocket != null)
//            {
//                _clientSocket.Close();
//                _clientSocket = null;
//            }
//            LOG.Info("客户端关闭");
//        }

//        /// <summary>
//        /// 设置状态
//        /// </summary>
//        /// <param name="nodeName">节点名称</param>
//        /// <param name="realRfid">真实RFID</param>
//        /// <param name="virtualRfid">虚拟RFID</param>
//        /// <param name="action">动作</param>
//        /// <param name="level">优先级</param>
//        /// <param name="shelf">料架</param>
//        /// <param name="type">类型</param>
//        public void SetStatus(string nodeName, string realRfid = "", string virtualRfid = "", ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.None, ClientShelf shelf = ClientShelf.None, ClientType type = ClientType.None)
//        {
//            int index = _node.FindIndex(s => s.Name == nodeName);
//            if (index == -1)  //没有找到
//            {
//                ClientNode node = new ClientNode
//                {
//                    Name = nodeName,
//                    RealRfid = realRfid,
//                    VirtualRfid = virtualRfid,
//                    Action = action,
//                    Level = level,
//                    Shelf = shelf,
//                    Type = type
//                };
//                _node.Add(node);
//                LOG.Info("SetStatus " + node.ToText());
//            }
//            else
//            {
//                _node[index].RealRfid = realRfid;
//                _node[index].VirtualRfid = virtualRfid;
//                _node[index].Action = action;
//                _node[index].Level = level;
//                _node[index].Shelf = shelf;
//                _node[index].Type = type;
//                LOG.Info("SetStatus " + _node[index].ToText());
//            }
//        }

//        /// <summary>
//        /// 设置状态
//        /// </summary>
//        /// <param name="nodeName">节点名称</param>
//        /// <param name="action">动作</param>
//        public void SetStatus(string nodeName, ClientAction action)
//        {
//            int index = _node.FindIndex(s => s.Name == nodeName);
//            if (index == -1)  //没有找到
//            {
//                LOG.Info("SetStatus 没有找到" + nodeName);
//            }
//            else
//            {
//                _node[index].Action = action;
//                LOG.Info("SetStatus " + _node[index].ToText());
//            }
//        }







//        /// <summary>
//        /// 重连线程
//        /// </summary>
//        private void Reconnect()
//        {
//            while (_loopRecon)
//            {
//                if (!IsConn)
//                {
//                    Open();
//                    if (IsConn)
//                    {
//                        _countRecon = 0;
//                        LOG.Info("连接服务器成功");
//                        Connected?.Invoke(IsConn);
//                    }
//                    else
//                    {
//                        LOG.Info("连接服务器失败" + ++_countRecon + "次");
//                    }
//                }
//                Thread.Sleep(2000);
//            }
//            LOG.Info("Quit Thread Reconnect");
//        }

//        /// <summary>
//        /// 打开,连接到服务器
//        /// </summary>
//        private void Open()
//        {
//            try
//            {
//                if (CheckIP(SERVER_IP))
//                {
//                    _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//                    _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2000);
//                    _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000);
//                    _clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
//                    _clientSocket.Connect(IPAddress.Parse(SERVER_IP), SERVER_PORT);
//                    if (!_loopRecon) return;
//                    IsConn = true;
//                }
//            }
//            catch (Exception ex)
//            {
//                IsConn = false;
//                LOG.Error("Open Error", ex);
//            }
//        }

//        /// <summary>
//        /// 监听服务端发送的内容
//        /// </summary>
//        private void ListenServerNet()
//        {
//            byte[] temp = new byte[200];
//            int timeout = 100;

//            while (_loopClient)
//            {
//                Thread.Sleep(timeout);
//                if (!IsConn) continue;
//                if (_clientSocket == null) continue;
//                if (!_loopClient) break;

//                try
//                {
//                    if (_clientSocket.Available > 0)
//                    {
//                        int count = _clientSocket.Receive(temp);
//                        byte[] buff = new byte[count];
//                        Array.Copy(temp, 0, buff, 0, count);

//                        ClientNode node = Decode(buff);
//                        if (node == null)
//                        {
//                            LOG.Info("命令解析失败");
//                        }
//                        else
//                        {
//                            LOG.Info("触发Received事件," + node.ToText());
//                            Received?.Invoke(node);
//                        }
//                    }
//                }
//                catch (Exception ex)
//                {
//                    IsConn = false;
//                    LOG.Error("ListenNet Error", ex);
//                    Connected?.Invoke(IsConn);
//                }
//            }
//            LOG.Info("Quit Thread ListenNet");
//        }

//        /// <summary>
//        /// 连续发送状态线程
//        /// </summary>
//        private void SendStatus()
//        {
//            while (_loopClient)
//            {
//                Thread.Sleep(SendSleep * 1000);
//                if (!IsConn) continue;
//                if (_clientSocket == null) continue;

//                for (int i = 0; i < _node.Count; i++)
//                {
//                    if (!_loopClient) break;
//                    Thread.Sleep(100);
//                    byte[] buff = Encode(_node[i]);
//                    bool bln = Send(buff);
//                    if (!bln) continue;
//                }
//            }
//            LOG.Info("Quit Thread SendStatus");
//        }

//        /// <summary>
//        /// 编码
//        /// </summary>
//        /// <param name="node"></param>
//        /// <returns></returns>
//        private byte[] Encode(ClientNode node)
//        {
//            try
//            {
//                System.Reflection.PropertyInfo[] info = node.GetType().GetProperties();
//                string[] arr = new string[info.Length];
//                for (int i = 0; i < info.Length; i++)
//                    arr[i] = string.Format("\"{0}\":\"{1}\"", info[i].Name, info[i].GetValue(node));
//                string json = "{" + string.Join(",", arr) + "}";
//                LOG.Info("编码json " + json);
//                byte[] buff = System.Text.Encoding.UTF8.GetBytes(json);
//                return buff;
//            }
//            catch (Exception ex)
//            {
//                LOG.Error("Encode Error", ex);
//                return null;
//            }
//        }

//        /// <summary>
//        /// 解码
//        /// </summary>
//        /// <param name="buff"></param>
//        /// <returns></returns>
//        private ClientNode Decode(byte[] buff)
//        {
//            try
//            {
//                string json = System.Text.Encoding.UTF8.GetString(buff);
//                LOG.Info("解码json " + json);
//                string[] arr = json.Trim(new char[] { '{', '}' }).Split(',');

//                ClientNode node = new ClientNode();
//                System.Reflection.PropertyInfo[] info = node.GetType().GetProperties();
//                for (int i = 0; i < info.Length; i++)
//                {
//                    string name = string.Format("\"{0}\":", info[i].Name);
//                    int index = Array.FindIndex(arr, s => s.StartsWith(name));
//                    if (index == -1) continue;

//                    string value = arr[index].Replace(name, "").Trim('\"');
//                    switch (info[i].Name)
//                    {
//                        case "Action":
//                            Enum.TryParse(value, out ClientAction action);
//                            node.Action = action;
//                            break;
//                        case "Level":
//                            Enum.TryParse(value, out ClientLevel level);
//                            node.Level = level;
//                            break;
//                        case "Type":
//                            Enum.TryParse(value, out ClientType type);
//                            node.Type = type;
//                            break;
//                        case "Shelf":
//                            Enum.TryParse(value, out ClientShelf shelf);
//                            node.Shelf = shelf;
//                            break;
//                        default:
//                            info[i].SetValue(node, value);
//                            break;
//                    }
//                }
//                return node;
//            }
//            catch (Exception ex)
//            {
//                LOG.Error("Decode Error", ex);
//                return null;
//            }
//        }

//        /// <summary>
//        /// 发送命令
//        /// </summary>
//        /// <param name="buff"></param>
//        /// <returns></returns>
//        private bool Send(byte[] buff)
//        {
//            if (!IsConn)
//            {
//                LOG.Info("Send 服务器没有连接");
//                return false;
//            }

//            try
//            {
//                if (!_loopRecon) return false;
//                if (buff == null) return false;
//                _clientSocket.Send(buff);
//                LOG.Info("发送到服务器");
//                return true;
//            }
//            catch (Exception ex)
//            {
//                LOG.Error("Send Error", ex);
//                IsConn = false;
//                Connected?.Invoke(IsConn);
//                return false;
//            }

//        }

//        /// <summary>
//        /// 16进制
//        /// </summary>
//        /// <param name="buff"></param>
//        /// <returns></returns>
//        private string HexBuff(byte[] buff)
//        {
//            string s = "";
//            if (buff == null) return s;

//            for (int i = 0; i < buff.Length; i++)
//                s += buff[i].ToString("X2") + " ";
//            return s;
//        }

//        /// <summary>
//        /// 检查IP地址
//        /// </summary>
//        /// <param name="ip"></param>
//        /// <returns></returns>
//        private bool CheckIP(string ip)
//        {
//            //IP合法
//            string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
//            bool rtn = System.Text.RegularExpressions.Regex.IsMatch(ip, pattern);
//            if (!rtn)
//            {
//                LOG.Info("非法的IP地址" + ip);
//                return false;
//            }

//            //Ping服务端
//            try
//            {
//                System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
//                System.Net.NetworkInformation.PingReply result = ping.Send(ip, 2000);
//                ping.Dispose();
//                if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
//                {
//                    LOG.Info("Ping " + ip + " 请求没有响应");
//                    return false;
//                }
//                return true;
//            }
//            catch (Exception ex)
//            {
//                LOG.Error("CheckIP Error", ex);
//                return false;
//            }
//        }

//    }

//    /// <summary>
//    /// 客户端的节点
//    /// </summary>
//    public class ClientNode
//    {
//        /// <summary>
//        /// 节点名称
//        /// </summary>
//        public string Name { set; get; } = "";
//        /// <summary>
//        /// 真实的料架号
//        /// </summary>
//        public string RealRfid { set; get; } = "";
//        /// <summary>
//        /// 虚拟的料架号
//        /// </summary>
//        public string VirtualRfid { set; get; } = "";
//        /// <summary>
//        /// 动作
//        /// </summary>
//        public ClientAction Action { set; get; } = ClientAction.None;
//        /// <summary>
//        /// 优先级
//        /// </summary>
//        public ClientLevel Level { set; get; } = ClientLevel.None;
//        /// <summary>
//        /// 类型
//        /// </summary>
//        public ClientType Type { set; get; } = ClientType.None;
//        /// <summary>
//        /// 料架
//        /// </summary>
//        public ClientShelf Shelf { set; get; } = ClientShelf.None;
//        /// <summary>
//        /// 来自上一个节点名称
//        /// </summary>
//        public string From { set; get; } = "";

//        /// <summary>
//        /// 客户端的节点
//        /// </summary>
//        public ClientNode()
//        {
//        }

//        /// <summary>
//        /// 所有属性的文本形式
//        /// </summary>
//        /// <returns></returns>
//        public string ToText()
//        {
//            System.Reflection.PropertyInfo[] info = this.GetType().GetProperties();
//            string[] arr = new string[info.Length];
//            for (int i = 0; i < info.Length; i++)
//                arr[i] = info[i].Name + "=" + info[i].GetValue(this).ToString();
//            return string.Join(",", arr);
//        }

//        /// <summary>
//        /// 拷贝一个新的实例
//        /// </summary>
//        /// <returns></returns>
//        public ClientNode ToCopy()
//        {
//            ClientNode node = new ClientNode();
//            System.Reflection.PropertyInfo[] info1 = node.GetType().GetProperties();
//            System.Reflection.PropertyInfo[] info2 = this.GetType().GetProperties();
//            for (int i = 0; i < info1.Length; i++)
//                info1[i].SetValue(node, info2[i].GetValue(this));
//            return node;

//        }
//    }

//    /// <summary>
//    /// 客户端的动作
//    /// </summary>
//    public enum ClientAction
//    {
//        /// <summary>
//        /// 没有动作
//        /// </summary>
//        None,
//        /// <summary>
//        /// 需要进入A料架
//        /// </summary>
//        NeedEnterA,
//        /// <summary>
//        /// 需要进入B料架
//        /// </summary>
//        NeedEnterB,
//        /// <summary>
//        /// 需要进入C料架
//        /// </summary>
//        NeedEnterC,
//        /// <summary>
//        /// 需要进入D料架
//        /// </summary>
//        NeedEnterD,
//        /// <summary>
//        /// 需要进入料架
//        /// </summary>
//        NeedEnter,
//        /// <summary>
//        /// 需要流出料架
//        /// </summary>
//        NeedLeave,
//        /// <summary>
//        /// 需要进入流出料架
//        /// </summary>
//        NeedEnterLeave,
//        /// <summary>
//        /// 可以进入料架
//        /// </summary>
//        MayEnter,
//        /// <summary>
//        /// 可以流出料架
//        /// </summary>
//        MayLeave,
//        /// <summary>
//        /// 可以停靠
//        /// </summary>
//        MayDock,
//        /// <summary>
//        /// 不可以进入料架
//        /// </summary>
//        MayNotEnter,
//        /// <summary>
//        /// 不可以流出料架
//        /// </summary>
//        MayNotLeave,
//        /// <summary>
//        /// 不可以停靠
//        /// </summary>
//        MayNotDock,
//        /// <summary>
//        /// 完成进入料架
//        /// </summary>
//        FinishEnter,
//        /// <summary>
//        /// 完成流出料架
//        /// </summary>
//        FinishLeave,
//        /// <summary>
//        /// 小车准备好(服务器发送)
//        /// </summary>
//        ReadyEnter,
//        /// <summary>
//        /// 小车准备好(服务器发送)
//        /// </summary>
//        ReadyLeave,
//        /// <summary>
//        /// 到达包装料仓门口(服务器发送)
//        /// </summary>
//        Arrive,
//        /// <summary>
//        /// 完成(服务器发送)
//        /// </summary>
//        Complete
//    }

//    /// <summary>
//    /// 客户端的优先级
//    /// </summary>
//    public enum ClientLevel
//    {
//        None,
//        /// <summary>
//        /// 低
//        /// </summary>
//        Low,
//        /// <summary>
//        /// 中等
//        /// </summary>
//        Middle,
//        /// <summary>
//        /// 高
//        /// </summary>
//        High
//    }

//    /// <summary>
//    /// 客户端的料架
//    /// </summary>
//    public enum ClientShelf
//    {
//        /// <summary>
//        /// 没有架子
//        /// </summary>
//        None,
//        /// <summary>
//        /// 空架子
//        /// </summary>
//        Empty,
//        /// <summary>
//        /// 满料架子
//        /// </summary>
//        Full
//    }

//    /// <summary>
//    /// 类型
//    /// </summary>
//    public enum ClientType
//    {
//        /// <summary>
//        /// 无
//        /// </summary>
//        None,
//        /// <summary>
//        /// 紧急料
//        /// </summary>
//        Urgent,
//        /// <summary>
//        /// 包装料
//        /// </summary>
//        Pack,
//        /// <summary>
//        /// 分盘料
//        /// </summary>
//        Cut
//    }



//}