MiR_API.cs 32.4 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 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using RestSharp;

namespace Model
{
    /// <summary>
    /// MiR小车API
    /// </summary>
    public class MiR_API
    {
        private readonly log4net.ILog LOG;
        private const int httpTimeout = 10000;

        /// <summary>
        /// Fleet的IP地址
        /// </summary>
        public string FleetIP { set; get; }

        /// <summary>
        /// 任务列表
        /// </summary>
        public Dictionary<string, string> MissionList { set; get; }

        /// <summary>
        /// MiR小车API
        /// </summary>
        public MiR_API()
        {
            LOG = log4net.LogManager.GetLogger("MiR_API");
        }







        /// <summary>
        /// 获取IO模块guid列表
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public bool Get_IO_Modules(string ip, string authorization, out string[] guid)
        {
            guid = null;

            try
            {
                string url = "http://" + ip + "/api/v2.0.0/io_modules";
                string json = HttpGet(url, ip, authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                object[] obj = (object[])serializer.DeserializeObject(json);
                if (obj == null) return false;
                guid = new string[obj.Length];

                for (int i = 0; i < obj.Length; i++)
                {
                    Dictionary<string, object> dic = (Dictionary<string, object>)obj[i];
                    guid[i] = dic["guid"].ToString();
                }
                return true;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }

        }

        /// <summary>
        /// 获取IO模块的状态
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <param name="guid"></param>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public bool Get_IO_Status(string ip, string authorization, string guid, out bool[] input, out bool[] output)
        {
            input = null;
            output = null;

            try
            {
                string url = "http://" + ip + "/api/v2.0.0/io_modules/" + guid + "/status";
                string json = HttpGet(url, ip, authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                object[] objInput = (object[])dic["input_state"];
                input = new bool[objInput.Length];
                for (int i = 0; i < input.Length; i++)
                    input[i] = Convert.ToBoolean(objInput[i]);

                object[] objOutput = (object[])dic["output_state"];
                output = new bool[objOutput.Length];
                for (int i = 0; i < output.Length; i++)
                    output[i] = Convert.ToBoolean(objOutput[i]);

                return true;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取任务状态
        /// </summary>
        /// <param name="authorization"></param>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool Get_MissionState_Fleet(string authorization, string id, out string state)
        {
            state = null;

            try
            {
                string url = "http://" + FleetIP + "/api/v2.0.0/mission_scheduler/" + id;
                string json = HttpGet(url, FleetIP, authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                state = dic["state"].ToString();
                return true;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取任务状态
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool Get_MissionState(string ip, string authorization, string id, out string state)
        {
            state = null;

            try
            {
                string url = "http://" + ip + "/api/v2.0.0/mission_queue/" + id;
                string json = HttpGet(url, FleetIP, authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                state = dic["state"].ToString();
                return true;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 获取PLC寄存器的内容
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <param name="reg"></param>
        /// <param name="regValue"></param>
        /// <returns></returns>
        public bool Get_Register(string ip, string authorization, int reg, out int regValue)
        {
            regValue = -1;

            try
            {
                string url = "http://" + ip + "/api/v2.0.0/registers/" + reg + "?whitelist=value";
                string json = HttpGet(url, ip, authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                string s = dic["value"].ToString();
                float f = Convert.ToSingle(s);
                regValue = Convert.ToInt32(f);
                return true;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 设置PLC寄存器的内容
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <param name="reg"></param>
        /// <param name="regValue"></param>
        /// <returns></returns>
        public bool Set_Register(string ip, string authorization, int reg, int regValue)
        {
            try
            {
                string url = "http://" + ip + "/api/v2.0.0/registers/" + reg;
                string body = "{\"value\": " + regValue + "}";
                string json = HttpPost(url, ip, authorization, body);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                string s = dic["value"].ToString();
                float f = Convert.ToSingle(s);
                if (regValue == Convert.ToInt32(f))
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 添加任务到小车自身任务队列
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <param name="missionName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Add_Mission(string ip, string authorization, string missionName, out string id)
        {
            id = null;

            try
            {
                string url = "http://" + ip + "/api/v2.0.0/mission_queue";
                string body = "{\"mission_id\":\"" + MissionList[missionName] + "\"}";
                LOG.Info(string.Format("URL:{0}   Body:{1}", url, body));

                string json = HttpPost(url, ip, authorization, body);
                LOG.Info("Return:" + json);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                id = dic["id"].ToString();
                string missionId = dic["mission_id"].ToString();
                if (missionId == MissionList[missionName])
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 添加任务到Fleet任务队列
        /// </summary>
        /// <param name="fleetID"></param>
        /// <param name="authorization"></param>
        /// <param name="missionName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Add_Mission_Fleet(string fleetID, string authorization, string missionName, out string id)
        {
            id = null;

            try
            {
                string url = "http://" + FleetIP + "/api/v2.0.0/mission_scheduler";
                string body = "{\"mission_id\":\"" + MissionList[missionName] + "\",\"robot_id\":" + fleetID + "}";
                LOG.Info(string.Format("URL:{0}   Body:{1}", url, body));

                string json = HttpPost(url, FleetIP, authorization, body);
                LOG.Info("Return:" + json);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;


                if (dic.TryGetValue("id", out object value))
                    id = value.ToString();
                else
                    return false;

                string missionId = dic["mission_id"].ToString();
                if (missionId == MissionList[missionName])
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        //        /// <summary>
        //        /// 获取所有任务列表
        //        /// </summary>
        //        /// <param name="info"></param>
        //        /// <param name="mission">任务(名称,guid)</param>
        //        /// <returns></returns>
        //        public bool Get_Mission(AgvInfo info, out Dictionary<string, string> mission)
        //        {
        //            try
        //            {
        //                mission = null;
        //                string url = "http://" + info.IP + "/api/v2.0.0/missions/search?whitelist=guid,name";
        //                string body = "{\"filters\" :[]}";

        //                string json = HttpPost(url, info.IP, info.Authorization, body);
        //                Common.log.Info(string.Format("URL: {0};  Body: {1}", url, body)+"\n"+"Return: " + json);
        //                if (string.IsNullOrWhiteSpace(json)) return false;

        //                JavaScriptSerializer serializer = new JavaScriptSerializer();
        //                object[] obj = (object[])serializer.DeserializeObject(json);
        //                if (obj == null) return false;
        //                mission = new Dictionary<string, string>();

        //                for (int i = 0; i < obj.Length; i++)
        //                {
        //                    Dictionary<string, object> dic = (Dictionary<string, object>)obj[i];
        //                    mission.Add(dic["name"].ToString(), dic["guid"].ToString());
        //                }
        //                return true;
        //            }
        //            catch (Exception ex)
        //            {
        //                mission = null;
        //                Common.log.Error(ex);
        //                return false;
        //            }
        //        }

        //        /// <summary>
        //        /// 获取当前任务队列guid
        //        /// </summary>
        //        /// <param name="info"></param>
        //        /// <param name="mission"></param>
        //        /// <returns></returns>
        //        public bool Get_Mission_Queue(AgvInfo info, out List<string> mission)
        //        {
        //            try
        //            {
        //                mission = null;
        //                string url = "http://" + info.IP + "/api/v2.0.0/mission_queue/search";
        //                string body = "{\"filters\" : [{\"fieldname\": \"state\", \"operator\": \"IN\", \"value\": [\"Pending\", \"Executing\"]}]}";

        //                string json = HttpPost(url, info.IP, info.Authorization, body);
        //                Common.log.Info(string.Format("URL: {0};  Body: {1}", url, body)+"\n"+"Return: " + json);
        //                if (string.IsNullOrWhiteSpace(json)) return false;

        //                JavaScriptSerializer serializer = new JavaScriptSerializer();
        //                object[] obj = (object[])serializer.DeserializeObject(json);
        //                if (obj == null) return false;

        //                mission = new List<string>();
        //                Dictionary<string, object> dic;
        //                for (int i = 0; i < obj.Length; i++)
        //                {
        //                    dic = (Dictionary<string, object>)obj[i];
        //                    mission.Add(dic["id"].ToString());
        //                }

        //                for (int i = 0; i < mission.Count; i++)
        //                {
        //                    url = "http://" + info.IP + "/api/v2.0.0/mission_queue/" + mission[i];
        //                    json = HttpGet(url, info.IP, info.Authorization);
        //                    serializer = new JavaScriptSerializer();
        //                    dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
        //                    mission[i] = dic["mission_id"].ToString();
        //                }

        //                return true;
        //            }
        //            catch (Exception ex)
        //            {
        //                mission = null;
        //                Common.log.Error(ex);
        //                return false;
        //            }
        //        }

        /// <summary>
        /// 删除小车所有任务
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        public void Del_Mission(string ip, string authorization)
        {
            try
            {
                string url = "http://" + ip + "/api/v2.0.0/mission_queue";
                HttpDel(url, ip, authorization);
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }

        /// <summary>
        /// 删除Fleet尽快中指定id的任务,已运行的无法删除
        /// </summary>
        /// <param name="authorization"></param>
        /// <param name="id"></param>
        public void Del_Mission_Fleet(string authorization, string id)
        {
            try
            {
                string url = "http://" + FleetIP + "/api/v2.0.0/mission_scheduler/" + id;
                HttpDel(url, FleetIP, authorization);
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
            }
        }

        /// <summary>
        /// 获取当前小车状态
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <param name="stateID"></param>
        /// <param name="stateText"></param>
        /// <param name="battery"></param>
        /// <param name="missionText"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public bool Get_State(string ip, string authorization, out int stateID, out string stateText, out int battery, out string missionText, out System.Drawing.PointF position)
        {
            stateID = -1;
            stateText = "";
            battery = 0;
            missionText = "";
            position = new System.Drawing.PointF();

            try
            {
                string url = "http://" + ip + "/api/v2.0.0/status?whitelist=state_id,state_text,battery_percentage,mission_text,position";
                string json = HttpGet(url, ip, authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                stateID = Convert.ToInt32(dic["state_id"].ToString());
                stateText = dic["state_text"].ToString();
                missionText = dic["mission_text"].ToString();
                Dictionary<string, object> tt = (Dictionary<string, object>)dic["position"];
                position.X = Convert.ToSingle(tt["x"]);
                position.Y = Convert.ToSingle(tt["y"]);
                string s = dic["battery_percentage"].ToString();
                float f = Convert.ToSingle(s);
                battery = Convert.ToInt32(f);
                return true;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 小车状态改为Ready
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <returns></returns>
        public bool State_Ready(string ip, string authorization)
        {
            try
            {
                string url = "http://" + ip + "/api/v2.0.0/status?whitelist=state_id,state_text";
                string body = "{\"state_id\": 3}";
                string json = HttpPut(url, ip, authorization, body);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                string s = dic["state_id"].ToString();
                if (s == "3")
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 小车状态改为Pause
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <returns></returns>
        public bool State_Pause(string ip, string authorization)
        {
            try
            {
                string url = "http://" + ip + "/api/v2.0.0/status?whitelist=state_id,state_text";
                string body = "{\"state_id\": 4}";
                string json = HttpPut(url, ip, authorization, body);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null) return false;

                string s = dic["state_id"].ToString();
                if (s == "4")
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 清除错误
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="authorization"></param>
        /// <returns></returns>
        public bool Clear_Error(string ip, string authorization)
        {
            try
            {
                string url = "http://" + ip + "/api/v2.0.0/status";
                string body = "{\"clear_error\": true}";
                string json = HttpPut(url, ip, authorization, body);
                if (string.IsNullOrWhiteSpace(json)) return false;

                return true;
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 检查IP地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public 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(ex);
                return false;
            }
        }

        //        public bool GetResourcePosition(AgvInfo info, out Dictionary<string, string> res)
        //        {
        //            res = new Dictionary<string, string>();
        //            string ip = Common.appConfig.AppSettings.Settings["FLEET"].Value;
        //            string url = "http://" + ip + "/api/v2.0.0/resources/positions";

        //            string json = HttpGet(url, info.IP, info.Authorization);
        //            Common.log.Info("URL: " + url + "\n" + "Return: " + json);
        //            if (string.IsNullOrWhiteSpace(json)) return false;

        //            JavaScriptSerializer serializer = new JavaScriptSerializer();
        //            object[] obj = (object[])serializer.DeserializeObject(json);
        //            if (obj == null) return false;

        //            for (int i = 0; i < obj.Length; i++)
        //            {
        //                string key;
        //                Dictionary<string, object> dic = (Dictionary<string, object>)obj[i];
        //                if (dic.TryGetValue("name", out object value))
        //                    key = value.ToString();
        //                else
        //                    continue;

        //                if (res.ContainsKey(key))
        //                    continue;
        //                else
        //                    res.Add(key, "");

        //                if (dic.TryGetValue("queue", out value))
        //                {
        //                    object[] temp = (object[])value;
        //                    if (temp.Length == 0) continue;
        //                    string[] robotName = new string[temp.Length];
        //                    for (int j = 0; j < temp.Length; j++)
        //                    {
        //                        if (GetRobot(info, temp[j].ToString(), out string name))
        //                            robotName[j] = name;
        //                    }
        //                    res[key] = string.Join(",", robotName);
        //                }


        //            }

        //            return true;

        //        }

        //        public bool GetResourceArea(AgvInfo info, out Dictionary<string, string> res)
        //        {
        //            res = new Dictionary<string, string>();
        //            string ip = Common.appConfig.AppSettings.Settings["FLEET"].Value;
        //            string url = "http://" + ip + "/api/v2.0.0/resources/areas";

        //            string json = HttpGet(url, info.IP, info.Authorization);
        //            Common.log.Info("URL: " + url + "\n" + "Return: " + json);
        //            if (string.IsNullOrWhiteSpace(json)) return false;

        //            JavaScriptSerializer serializer = new JavaScriptSerializer();
        //            object[] obj = (object[])serializer.DeserializeObject(json);
        //            if (obj == null) return false;

        //            for (int i = 0; i < obj.Length; i++)
        //            {
        //                string key;
        //                Dictionary<string, object> dic = (Dictionary<string, object>)obj[i];
        //                if (dic.TryGetValue("name", out object value))
        //                    key = value.ToString();
        //                else
        //                    continue;

        //                if (res.ContainsKey(key))
        //                    continue;
        //                else
        //                    res.Add(key, "");

        //                if (dic.TryGetValue("queue", out value))
        //                {
        //                    object[] temp = (object[])value;
        //                    if (temp.Length == 0) continue;
        //                    string[] robotName = new string[temp.Length];
        //                    for (int j = 0; j < temp.Length; j++)
        //                    {
        //                        if (GetRobot(info, temp[j].ToString(), out string name))
        //                            robotName[j] = name;
        //                    }
        //                    res[key] = string.Join(",", robotName);
        //                }


        //            }

        //            return true;

        //        }

        //        public bool GetRobot(AgvInfo info, string id, out string name)
        //        {
        //            name = "";
        //            string ip = Common.appConfig.AppSettings.Settings["FLEET"].Value;
        //            string url = "http://" + ip + "/api/v2.0.0/robots/" + id;

        //            string json = HttpGet(url, info.IP, info.Authorization);
        //            Common.log.Info("URL: " + url + "\n" + "Return: " + json);
        //            if (string.IsNullOrWhiteSpace(json)) return false;

        //            JavaScriptSerializer serializer = new JavaScriptSerializer();
        //            Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);

        //            if (dic.TryGetValue("status", out object value))
        //            {
        //                Dictionary<string, object> dic2 = (Dictionary<string, object>)value;
        //                if (dic2.TryGetValue("robot_name", out object robotName))
        //                {
        //                    name = robotName.ToString();
        //                    return true;
        //                }
        //                else
        //                {
        //                    return false;
        //                }
        //            }
        //            else
        //            {
        //                return false;
        //            }

        //        }

        private string HttpGet(string url, string ip, string authorization)
        {
            LOG.Info("[GET]URL:" + url);
            RestClient client = new RestClient(url) { Timeout = httpTimeout };
            RestRequest request = new RestRequest(Method.GET);

            request.AddHeader("Host", ip);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept-Language", "zh_CN");
            request.AddHeader("Authorization", authorization);

            IRestResponse response = client.Execute(request);
            string s = response.Content;
            if (string.IsNullOrWhiteSpace(s))
            {
                s = "";
            }
            else
            {
                s = s.Replace("\n", "");
                s = s.Replace("\r", "");
                while (s.IndexOf("  ") >= 0)
                    s = s.Replace("  ", " ");
            }
            LOG.Info("Return:" + s);
            return s;
        }

        private string HttpPost(string url, string ip, string authorization, string body)
        {
            LOG.Info("[POST]URL:" + url + "   Body:" + body);
            RestClient client = new RestClient(url) { Timeout = httpTimeout };
            RestRequest request = new RestRequest(Method.POST);

            request.AddHeader("Host", ip);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Authorization", authorization);
            request.AddParameter("application/json", body, ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);
            string s = response.Content;
            if (string.IsNullOrWhiteSpace(s))
            {
                s = "";
            }
            else
            {
                s = s.Replace("\n", "");
                s = s.Replace("\r", "");
                while (s.IndexOf("  ") >= 0)
                    s = s.Replace("  ", " ");
            }
            LOG.Info("Return:" + s);
            return s;
        }

        private string HttpPut(string url, string ip, string authorization, string body)
        {
            LOG.Info("[PUT]URL:" + url + "   Body:" + body);
            RestClient client = new RestClient(url) { Timeout = httpTimeout };
            RestRequest request = new RestRequest(Method.PUT);

            request.AddHeader("Host", ip);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Authorization", authorization);
            request.AddParameter("application/json", body, ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);
            string s = response.Content;
            if (string.IsNullOrWhiteSpace(s))
            {
                s = "";
            }
            else
            {
                s = s.Replace("\n", "");
                s = s.Replace("\r", "");
                while (s.IndexOf("  ") >= 0)
                    s = s.Replace("  ", " ");
            }
            LOG.Info("Return:" + s);
            return s;
        }

        private void HttpDel(string url, string ip, string authorization)
        {
            LOG.Info("[DELETE]URL:" + url);
            RestClient client = new RestClient(url) { Timeout = httpTimeout };
            RestRequest request = new RestRequest(Method.DELETE);

            request.AddHeader("Host", ip);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Authorization", authorization);
            request.AddParameter("application/json", "", ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);
            string s = response.Content;
            if (string.IsNullOrWhiteSpace(s))
            {
                s = "";
            }
            else
            {
                s = s.Replace("\n", "");
                s = s.Replace("\r", "");
                while (s.IndexOf("  ") >= 0)
                    s = s.Replace("  ", " ");
            }
            LOG.Info("Return:" + s);
        }

    }
}