zoukankan      html  css  js  c++  java
  • 三、openTCS4.12 Http接口创建运输订单(可用于二次开发)

    openTCS官方提供关了Web API的文档
    http://www.opentcs.org/docs/4.12/developer/web-api/index.html
    在这里插入图片描述

    1.运输订单
      Get方法(可以在浏览器地址栏直接输入测试)
         http://127.0.0.1:55200/v1/transportOrders
       返回所有的运输订单:
         [ {
          “name” : “TOrder-0fa56142-3e0a-4702-8eef-39136c82b8f1”,
          “category” : “-”,
          “state” : “FINISHED”,
          “intendedVehicle” : null,
          “processingVehicle” : “AGV2”,
          “destinations” : [ {
          “locationName” : “WorkPlace”,
          “operation” : “Work”,
           “state” : “FINISHED”,
          “properties” : []
          }]
         },{
          “name” : “Move-24d8eb37-1251-426b-af41-4604511ff4c9”,
          “category” : “-”,
          “state” : “BEING_PROCESSED”,
          “intendedVehicle” : “AGV2”,
          “processingVehicle” : “AGV2”,
          “destinations” : [{
          “locationName” : “Point1”,
          “operation” : “MOVE”,
          “state” : “TRAVELLING”,
          “properties” : []
          }]
         }]
    2.根据订单号获取运输订单信息
      GET方法:
        http://127.0.0.1:55200/v1/transportOrders/{orderNum}
        例如
        http://127.0.0.1:55200/v1/transportOrders/TOrder-0fa56142-3e0a-4702-8eef-39136c82b8f1
      返回:
      {
       “name” : “TOrder-0fa56142-3e0a-4702-8eef-39136c82b8f1”,
       “category” : “-”,
        “state” : “FINISHED”,
        “intendedVehicle” : null,
        “processingVehicle” : “AGV2”,
        “destinations” : [ {
        “locationName” : “WorkPlace”,
        “operation” : “Work”,
        “state” : “FINISHED”,
        “properties” : []
        }]
      }

    3.创建订单
        Post方法:
            http://127.0.0.1:55200/v1/transportOrders/{orderNum}
            orderNum随机生成(建议用UUID)
            contentType为:application/json
            Post数据如下:
            创建运输订单
            {
                “intendedVehicle” : null,
                “destinations” : [ {
                    “locationName” : “Load1”,
                    “operation” : “Load”,
                    “properties” : [ ]
                }, {
                    “locationName” : “Unload1”,
                    “operation” : “Unload”,
                    “properties” : [ ]
                } ]
            }
            或者移动订单
            {
                “intendedVehicle” : “AGV1”,
                “destinations” : [ {
                “locationName” : “Point5”,
                “operation” : “MOVE”,
                “properties” : [ ]
            } ]
        }
        返回:
            Successful operation

    4.取消订单
      POST方法
        http://127.0.0.1:55200/v1/transportOrders/{orderNum}/withdrawal
      返回
        Successful operation

    5.获取小车信息
        GET方法
            http://127.0.0.1:55200/v1/vehicles
        返回:
        [ {
            “name” : “AGV1”,
            “properties” : {
                “tcs:preferredAdapterClass” : “org.opentcs.virtualvehicle.LoopbackCommunicationAdapterFactory”,
                “IP” :“192.168.1.100”,
                “Port” : “4001”
                },
                “length” : 1000,
                “energyLevelGood” : 90,
                “energyLevelCritical” : 30,
                “energyLevel” : 100,
                “integrationLevel” : “TO_BE_UTILIZED”,
                “procState” : “IDLE”,
                “transportOrder” : null,
                “currentPosition” : “0,5”,
                “state” : “IDLE”
                }, {
                    “name” : “AGV2”,
                “properties” : {
                    “tcs:preferredAdapterClass” : “org.opentcs.virtualvehicle.LoopbackCommunicationAdapterFactory”,
                    “IP” : “192.168.1.101”,
                    “Port” : “4001”
                },
                “length” : 1000,
                “energyLevelGood” : 90,
                “energyLevelCritical” : 30,
                “energyLevel” : 100,
                “integrationLevel” : “TO_BE_UTILIZED”,
                “procState” : “PROCESSING_ORDER”,
                “transportOrder” : “Move-24d8eb37-1251-426b-af41-4604511ff4c9”,
                “currentPosition” : “0,0”,
                “state” : “IDLE”
            }]

    6.根据小车编号获得小车信息
      GET方法
        http://127.0.0.1:55200/v1/vehicles/{vehicleName}
        例如
        http://127.0.0.1:55200/v1/vehicles/AGV1
      返回:
        {
            “name” : “AGV1”,
            “properties” : {
            “tcs:preferredAdapterClass” :         “org.opentcs.virtualvehicle.LoopbackCommunicationAdapterFactory”,
            “IP” : “192.168.1.100”,
            “Port” : “4001”
        },
        “length” : 1000,
        “energyLevelGood” : 90,
        “energyLevelCritical” : 30,
        “energyLevel” : 100,
        “integrationLevel” : “TO_BE_UTILIZED”,
        “procState” : “IDLE”,
        “transportOrder” : null,
        “currentPosition” : “0,5”,
        “state” : “IDLE”
        }

    7.取消正在执行任务的小车
        POST方法:
            http://127.0.0.1:55200/v1/vehicles/{vehicleName}/withdrawal
            例如
            http://127.0.0.1:55200/v1/vehicles/AGV1/withdrawal
        返回:
            Successful operation

    特别注意:
    在创建订单的时候,系统会报错
    在这里插入图片描述
    将OrderHandler第165行修改成如下即可
    在这里插入图片描述

    附C# Http操作工具类

    public class HttpMethods
        {
            private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            /// <summary>
            /// get方法请求服务器
            /// </summary>
            /// <param name="url"></param>
            /// <param name="parametersDict"></param>
            /// <param name="timeout"></param>
            /// <returns></returns>
            public static string DoGet(string url, Dictionary<string, string> parametersDict, int? timeout)
            {
                if (parametersDict != null && parametersDict.Count != 0)
                {
                    if (!url.Contains("?"))
                        url = url + "?";
                    foreach (string key in parametersDict.Keys)
                    {
                        url = url + key + "=" + parametersDict[key] + "&";
                    }
                    url = url.Substring(0, url.Length - 1);
                }
                try
                {
                    HttpWebResponse response = CreateGetHttpResponse(url, timeout, null, null);
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader sReader = new StreamReader(responseStream))
                        {
                            return sReader.ReadToEnd();
                        }
                    }
                }
                catch (Exception exp)
                {
                    return null;
                }
            }
    
    
            /// <summary>  
            /// 创建GET方式的HTTP请求  
            /// </summary>  
            /// <param name="url">请求的URL</param>  
            /// <param name="timeout">请求的超时时间</param>  
            /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>  
            /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>  
            /// <returns></returns>  
            public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies)
            {
                if (string.IsNullOrEmpty(url))
                {
                    throw new ArgumentNullException("url");
                }
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                request.UserAgent = DefaultUserAgent;
                if (!string.IsNullOrEmpty(userAgent))
                {
                    request.UserAgent = userAgent;
                }
                if (timeout.HasValue)
                {
                    request.Timeout = timeout.Value;
                }
                if (cookies != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                }
                return request.GetResponse() as HttpWebResponse;
            }
    
            /// <summary>  
            /// 创建POST方式的HTTP请求  
            /// </summary>  
            /// <param name="url">请求的URL</param>  
            /// <param name="parameters">随同请求POST的参数名称及参数值字典</param>  
            /// <param name="timeout">请求的超时时间</param>  
            /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>  
            /// <param name="requestEncoding">发送HTTP请求时所用的编码</param>  
            /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>  
            /// <returns></returns>  
            public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies, string contentType)
            {
                if (string.IsNullOrEmpty(url))
                {
                    throw new ArgumentNullException("url");
                }
                if (requestEncoding == null)
                {
                    throw new ArgumentNullException("requestEncoding");
                }
                HttpWebRequest request = null;
                //如果是发送HTTPS请求  
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = WebRequest.Create(url) as HttpWebRequest;
                    request.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    request = WebRequest.Create(url) as HttpWebRequest;
                }
                request.Method = "POST";
                request.ContentType = contentType;
    
                if (!string.IsNullOrEmpty(userAgent))
                {
                    request.UserAgent = userAgent;
                }
                else
                {
                    request.UserAgent = DefaultUserAgent;
                }
    
                if (timeout.HasValue)
                {
                    request.Timeout = timeout.Value;
                }
                if (cookies != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                }
                //如果需要POST数据  
                if (!(parameters == null || parameters.Count == 0))
                {
                    StringBuilder buffer = new StringBuilder();
                    int i = 0;
                    foreach (string key in parameters.Keys)
                    {
                        if (i > 0)
                        {
                            buffer.AppendFormat("&{0}={1}", key, parameters[key]);
                        }
                        else
                        {
                            buffer.AppendFormat("{0}={1}", key, parameters[key]);
                        }
                        i++;
                    }
                    byte[] data = requestEncoding.GetBytes(buffer.ToString());
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                return request.GetResponse() as HttpWebResponse;
            }
    
    
            /// <summary>  
            /// 创建POST方式的HTTP请求  
            /// </summary>  
            /// <param name="url">请求的URL</param>  
            /// <param name="parameters">随同请求POST的参数名称及参数值字典</param>  
            /// <param name="timeout">请求的超时时间</param>  
            /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>  
            /// <param name="requestEncoding">发送HTTP请求时所用的编码</param>  
            /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>  
            /// <returns></returns>  
            public static HttpWebResponse CreatePostHttpResponse(string url, string data, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies, string contentType)
            {
                if (string.IsNullOrEmpty(url))
                {
                    throw new ArgumentNullException("url");
                }
                if (requestEncoding == null)
                {
                    throw new ArgumentNullException("requestEncoding");
                }
                HttpWebRequest request = null;
                //如果是发送HTTPS请求  
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = WebRequest.Create(url) as HttpWebRequest;
                    request.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    request = WebRequest.Create(url) as HttpWebRequest;
                }
                request.Method = "POST";
                request.ContentType = contentType;
    
                if (!string.IsNullOrEmpty(userAgent))
                {
                    request.UserAgent = userAgent;
                }
                else
                {
                    request.UserAgent = DefaultUserAgent;
                }
    
                if (timeout.HasValue)
                {
                    request.Timeout = timeout.Value;
                }
                if (cookies != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                }
                //如果需要POST数据  
                if (!data.IsNullOrEmpty())
                {
                    byte[] datas = requestEncoding.GetBytes(data);
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(datas, 0, data.Length);
                    }
    
                }
                return request.GetResponse() as HttpWebResponse;
            }
    
            private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true; //总是接受  
            }
    
    
            /// <summary>
            /// Post方法请求服务器
            /// </summary>
            /// <param name="url"></param>
            /// <param name="parametersDict"></param>
            /// <param name="timeout"></param>
            /// <returns></returns>
            public static string DoPost(string url, Dictionary<string, string> parametersDict, int? timeout)
            {
                try
                {
                    HttpWebResponse response = CreatePostHttpResponse(url, parametersDict, timeout, null, Encoding.UTF8, null, "application/x-www-form-urlencoded");
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader sReader = new StreamReader(responseStream))
                        {
                            return sReader.ReadToEnd();
                        }
                    }
                }
                catch (Exception exp)
                {
                    return null;
                }
            }
    
            /// <summary>
            /// Post方法请求服务器
            /// </summary>
            /// <param name="url"></param>
            /// <param name="parametersDict"></param>
            /// <param name="timeout"></param>
            /// <returns></returns>
            public static string DoPostJson(string url, string data, int? timeout)
            {
                try
                {
                    HttpWebResponse response = CreatePostHttpResponse(url, data, timeout, null, Encoding.UTF8, null, "application/json");
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader sReader = new StreamReader(responseStream))
                        {
                            return sReader.ReadToEnd();
                        }
                    }
                }
                catch (Exception exp)
                {
                    return null;
                }
            }
        }
    

    调用方法如下:
    获得AGV信息

     string url = "http://127.0.0.1:55200/v1/vehicles";
     string retValue = HttpMethods.DoGet(url, null, 1000);
     //json解析
    

    创建订单如下:

    string data ="{"intendedVehicle":"AGV1","destinations":[{"locationName":"Point5","operation":"MOVE","properties":[ ]}]}";;
    string orderName = "TOrder-" + DateTime.Today.ToString("yyyyMMdd") + Guid.NewGuid().ToString().Replace("-", "").ToUpper().Substring(0, 10);
    string url = "http://127.0.0.1:55200/v1/transportOrders/" + orderName;
    string str = HttpMethods.DoPostJson(url, data, 1000);
    

    预知后事如何,请听下回分解

  • 相关阅读:
    Yii常用路径说明
    PHP-redis中文文档
    PHP 判断客户端是IOS还是Android
    yiii 框架登录 判断是否是游客模式及未登录状态
    php实现数字格式化,数字每三位加逗号的功能函数
    php array_udiff_uassoc比较数组的键值与值
    php--数组函数array
    安装Postman
    vue指令
    vue 错误记录
  • 原文地址:https://www.cnblogs.com/zjwno1/p/13097328.html
Copyright © 2011-2022 走看看