MiR_API.cs 29.2 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
using Common;
using Microsoft.SqlServer.Server;
using RestSharp;

namespace DeviceLibrary
{
    public class MiR_API
    {
        private const int httpTimeout = 5000;
        private static log4net.ILog log = log4net.LogManager.GetLogger("MiR_API");
        public static bool IsDebug { get; set; } = false;
        /// <summary>
        /// 获取IO模块guid列表
        /// </summary>
        /// <param name="info"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static bool Get_IO_Modules(Agv_Info info, out string[] guid)
        {
            try
            {
                guid = null;
                string url = "http://" + info.IP + "/api/v2.0.0/status?whitelist=state_id,state_text";

                string json = HttpGet(url, info.IP, info.Authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                object[] obj = (object[])serializer.DeserializeObject(json);
                if (obj == null)
                {
                    log.Error("URL: " + url + "\n" + "Return: " + json);
                    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();
                }
                log.Info("URL: " + url + "\n" + "Return: " + json);
                return true;
            }
            catch (Exception ex)
            {
                guid = null;
                log.Error("Get_IO_Modules", ex);
                return false;
            }

        }

        /// <summary>
        /// 获取IO模块的状态
        /// </summary>
        /// <param name="info"></param>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public static bool Get_IO_Status(Agv_Info info, out bool[] input, out bool[] output)
        {
            try
            {
                input = null;
                output = null;
                string url = "http://" + info.IP + "/api/v2.0.0/io_modules/" + info.IOID + "/status";

                string json = HttpGet(url, info.IP, info.Authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null)
                {
                    log.Error("URL: " + url + "\n" + "Return: " + json);
                    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)
            {
                input = null;
                output = null;
                //log.Error("Get_IO_Status", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取PLC寄存器的内容
        /// </summary>
        /// <param name="info"></param>
        /// <param name="reg"></param>
        /// <param name="regValue"></param>
        /// <returns></returns>
        public static bool Get_Register(Agv_Info info, int reg, out int regValue)
        {
            try
            {
                regValue = -1;
                string url = "http://" + info.IP + "/api/v2.0.0/registers/" + reg + "?whitelist=value";
                string json = HttpGet(url, info.IP, info.Authorization);
                if (string.IsNullOrWhiteSpace(json)) return false;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
                if (dic == null)
                {
                    log.Error("URL: " + url + "\n" + "Return: " + json);
                    return false;
                }


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

        /// <summary>
        /// 设置PLC寄存器的内容
        /// </summary>
        /// <param name="info"></param>
        /// <param name="reg"></param>
        /// <param name="regValue"></param>
        /// <returns></returns>
        public static bool Set_Register(Agv_Info info, int reg, int regValue)
        {
            try
            {
                string url = "http://" + info.IP + "/api/v2.0.0/registers/" + reg;
                string body = "{\"value\": " + regValue + "}";
                string json = HttpPost(url, info.IP, info.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)
                {
                    log.Error("URL: " + url + "\n" + "Return: " + json);
                    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("Set_Register", ex);
                return false;
            }
        }

        /// <summary>
        /// 添加任务到任务队列
        /// </summary>
        /// <param name="info"></param>
        /// <param name="mission_id"></param>
        /// <returns></returns>
        public static bool Add_Mission(Agv_Info info, string mission_id, bool isRemovePreMission = true)
        {
            try
            {
                info.CurTaskID = -1;
                info.CurTaskGUID = "";
                info.CurTaskState = SettingString.Wait;
                var key = CommonVar.agvMission.Where(qq => qq.Value == mission_id).Select(qq => qq.Key);
                info.CurTaskName = key.ToList()[0];
                //删除上一任务
                Del_Mission(info);

                string url = "http://" + info.IP + "/api/v2.0.0/mission_queue";
                string body = "{\"mission_id\":\"" + mission_id + "\"}";
                string json = HttpPost(url, info.IP, info.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;
                }
                log.Info("URL: " + url + "\n" + "Return: " + json);

                string s = dic["mission_id"].ToString();
                string id = dic["id"].ToString();
                if (s == mission_id)
                {
                    try
                    {
                        info.CurTaskID = Convert.ToInt32(id);
                        info.CurTaskGUID = mission_id;

                        log.Info(string.Format("{0} Add_Mission [{1}]", info.Name, key.ToList()[0]));
                    }
                    catch
                    {
                        log.Error(string.Format("{0} Add_Mission [{1}]", info.Name, mission_id));
                    }

                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                log.Error("Add_Mission", ex);
                return false;
            }
        }

        /// <summary>
        /// 添加任务到FLEET任务队列
        /// </summary>
        /// <param name="info"></param>
        /// <param name="mission_id"></param>
        /// <returns></returns>
        public static bool Add_Mission_Fleet(Agv_Info info, string mission_id, bool isRemovePreMission = true)
        {
            try
            {

                info.CurTaskID = -1;
                info.CurTaskGUID = "";
                info.CurTaskState = SettingString.Wait;
                var key = CommonVar.agvMission.Where(qq => qq.Value == mission_id).Select(qq => qq.Key);
                info.CurTaskName = key.ToList()[0];
                //防止上一个任务已执行但返回失败时,删除任务
                Del_Mission(info);

                string ip = AppConfigHelper.GetValue(SettingString.FLEET);
                string url = "http://" + ip + "/api/v2.0.0/mission_scheduler";
                string body = "{\"mission_id\":\"" + mission_id + "\",\"robot_id\":" + info.Fleet_IP + "}";
                string json = HttpPost(url, info.IP, info.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;
                }

                log.Info("URL: " + url + "\n" + "Return: " + json);
                string s = dic["mission_id"].ToString();
                string id = dic["id"].ToString();
                if (s == mission_id)
                {
                    try
                    {
                        info.CurTaskID = Convert.ToInt32(id);
                        info.CurTaskGUID = mission_id;
                        log.Info(string.Format("{0} Add_Mission_Fleet [{1}]", info.Name, key.ToList()[0]));
                    }
                    catch
                    {
                        log.Info(string.Format("{0} Add_Mission_Fleet [{1}]", info.Name, mission_id));
                        info.CurTaskName = "";
                    }
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                log.Error("Add_Mission_Fleet", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取所有任务列表
        /// </summary>
        /// <param name="info"></param>
        /// <param name="mission">任务(名称,guid)</param>
        /// <returns></returns>
        public static bool Get_Mission(Agv_Info info, out Dictionary<string, string> mission)
        {
            mission = null;
            try
            {
                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);
                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)
            {
                log.Error("Get_Mission", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取当前任务队列guid
        /// </summary>
        /// <param name="info"></param>
        /// <param name="mission"></param>
        /// <returns></returns>
        public static bool Get_Mission_Queue(Agv_Info info, out List<string> mission)
        {
            mission = null;
            try
            {
                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);
                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)
            {
                log.Error("Get_Mission_Queue", ex);
                return false;
            }
        }

        /// <summary>
        /// 删除当前所有任务
        /// </summary>
        /// <param name="info"></param>
        public static void Del_Mission(Agv_Info info)
        {
            try
            {
                string url = "http://" + info.IP + "/api/v2.0.0/mission_queue";
                HttpDel(url, info.IP, info.Authorization);
            }
            catch (Exception ex)
            {
                log.Error("Del_Mission", ex);
            }
        }

        /// <summary>
        /// 删除Fleet尽快中指定id的任务,已运行的无法删除
        /// </summary>
        /// <param name="authorization"></param>
        /// <param name="id"></param>
        public static void Del_Mission_Fleet(Agv_Info agv, string id)
        {
            try
            {
                string ip = AppConfigHelper.GetValue(SettingString.FLEET);
                string url = "http://" + ip + "/api/v2.0.0/mission_scheduler/" + id;
                HttpDel(url, ip, agv.Authorization);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
        /// <summary>
        /// 获取当前小车状态
        /// </summary>
        /// <param name="info"></param>
        /// <param name="stateID"></param>
        /// <param name="stateText"></param>
        /// <param name="battery"></param>
        /// <param name="mission_text"></param>
        /// <returns></returns>
        public static bool Get_State(Agv_Info info, out eAGVState stateID, out string stateText, out int battery, out string mission_text, out Agv_Info.AgvPosition position)
        {
            stateID = eAGVState.UNKNOWN;
            stateText = "";
            battery = 0;
            mission_text = "";
            position = new Agv_Info.AgvPosition();
            try
            {
                string url = "http://" + info.IP + "/api/v2.0.0/status?whitelist=state_id,state_text,battery_percentage,mission_text,position";
                string json = HttpGet(url, info.IP, info.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_id不存在:{"args":{},"error_code":"service_unavailable","error_human":"\u670d\u52a1\u4e0d\u53ef\u7528\uff0c\u8bf7\u7a0d\u540e\u91cd\u8bd5"}
                try
                {
                    stateID = (eAGVState)(Convert.ToInt32(dic["state_id"].ToString()));
                }
                catch
                {
                    log.Error("Get_State URL:" + url + " Return" + json);
                    return false;
                }
                stateID = (eAGVState)(Convert.ToInt32(dic["state_id"].ToString()));
                stateText = dic["state_text"].ToString();
                mission_text = dic["mission_text"].ToString();
                string s = dic["battery_percentage"].ToString();
                float f = Convert.ToSingle(s);
                battery = Convert.ToInt32(f);
                Dictionary<string, object> posDic = (Dictionary<string, object>)dic["position"];
                if (posDic == null) return false;
                position.orientation = Convert.ToDouble(posDic["orientation"].ToString());
                position.x = Convert.ToDouble(posDic["x"]);
                position.y = Convert.ToDouble(posDic["y"]);
                return true;
            }
            catch (Exception ex)
            {
                log.Error("Get_State: ", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取小车位置
        /// </summary>
        /// <param name="ip">小车ip</param>
        /// <param name="author">授权码</param>
        /// <param name="position">输出位置</param>
        /// <returns></returns>
        public static bool Get_Position(string ip,string author, out Agv_Info.AgvPosition position)
        {
            position = new Agv_Info.AgvPosition();
            try
            {
                string url = "http://" + ip + "/api/v2.0.0/status?whitelist=position";
                string json = HttpGet(url, ip,author);
                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;

                Dictionary<string, object> posDic = (Dictionary<string, object>)dic["position"];
                if (posDic == null) return false;
                position.orientation = Convert.ToDouble(posDic["orientation"].ToString());
                position.x = Convert.ToDouble(posDic["x"]);
                position.y = Convert.ToDouble(posDic["y"]);
                return true;
            }
            catch (Exception ex)
            {
                log.Error("Get_Position: ", ex);
                return false;
            }
        }

        public static bool Get_Task_State(Agv_Info agv, out string stateStr)
        {
            stateStr = SettingString.Wait;
            try
            {
                if (agv.CurTaskID == -1)
                    return true;

                if(agv.Use_Fleet)
                {
                    string ip = AppConfigHelper.GetValue(SettingString.FLEET);
                    string url = "http://" + ip + "/api/v2.0.0/mission_scheduler/" + agv.CurTaskID;
                    string json = HttpGet(url, ip, CommonVar.agvInfo[0].Authorization);
                    log.Debug("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 == null) return false;
                    //    "mission_id": "2e433130-c045-11ea-9a66-94c691a7387d",
                    //    "id": 7178,
                    //    "state": "Done"

                    string s = dic["id"].ToString();
                    if (s.Equals(agv.CurTaskID.ToString()))
                    {
                        stateStr = dic["state"].ToString();
                        return true;
                    }
                }
                else
                {
                    string url = "http://"+agv.IP+"/api/v2.0.0/mission_queue/" + agv.CurTaskID;
                    string json = HttpGet(url, agv.IP, agv.Authorization);
                    log.Debug("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 == null) return false;
                    //    "mission_id": "2e433130-c045-11ea-9a66-94c691a7387d",
                    //    "id": 7178,
                    //    "state": "Done"

                    string s = dic["id"].ToString();
                    if (s.Equals(agv.CurTaskID.ToString()))
                    {
                        stateStr = dic["state"].ToString();
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                log.Error("Get_Task_State", ex);
                return false;
            }

        }

        /// <summary>
        /// 小车状态改为Ready
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool State_Ready(Agv_Info info)
        {
            try
            {
                string url = "http://" + info.IP + "/api/v2.0.0/status?whitelist=state_id,state_text";
                string body = "{\"state_id\": 3}";
                string json = HttpPut(url, info.IP, info.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("State_Ready", ex);
                return false;
            }
        }

        /// <summary>
        /// 小车状态改为Pause
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool State_Pause(Agv_Info info)
        {
            try
            {
                string url = "http://" + info.IP + "/api/v2.0.0/status?whitelist=state_id,state_text";
                string body = "{\"state_id\": 4}";
                string json = HttpPut(url, info.IP, info.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("State_Pause", ex);
                return false;
            }
        }

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

                return true;
            }
            catch (Exception ex)
            {
                log.Error("Clear_Error", ex);
                return false;
            }
        }

        /// <summary>
        /// 检查IP地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static 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.Error("非法的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.Error("Ping " + ip + " 请求没有响应");
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                log.Error("CheckIP", ex);
                return false;
            }
        }


        #region HTTP GET POST PUT DEL
        private static string HttpGet(string url, string ip, string authorization)
        {
            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;
            s = s.Replace("\n", "");
            s = s.Replace(" ", "");
            if (IsDebug)
                log.Debug("HttpGet URL: " + url + " Return: " + s);
            return s;
        }

        private static string HttpPost(string url, string ip, string authorization, string body)
        {
            var client = new RestClient(url) { Timeout = httpTimeout };
            var request = new RestRequest(Method.POST);

            request.AddHeader("Host", ip);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept-Language", "zh_CN");
            request.AddHeader("Authorization", authorization);
            request.AddParameter("application/json", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            string s = response.Content;
            s = s.Replace("\n", "");
            s = s.Replace(" ", "");
            if (IsDebug)
                log.Debug(string.Format("HttpPost URL: {0};  Body: {1}  Return: {2}", url, body, s));
            return s;
        }

        private static string HttpPut(string url, string ip, string authorization, string 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("Accept-Language", "zh_CN");
            request.AddHeader("Authorization", authorization);
            request.AddParameter("application/json", body, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            string s = response.Content;
            s = s.Replace("\n", "");
            s = s.Replace(" ", "");
            if (IsDebug)
                log.Debug(string.Format("HttpPut URL: {0};  Body: {1} Return: {2}", url, body, s));
            return s;
        }

        private static void HttpDel(string url, string ip, string authorization)
        {
            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.AddHeader("Accept-Language", "zh_CN");
            request.AddParameter("application/json", "", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            string s = response.Content;
            if (IsDebug)
                log.Debug("HttpDel URL: " + url);
        }
        #endregion
    }
}