@ -20,6 +20,9 @@ using Serilog.Events;
using Microsoft.Extensions.Logging ;
using System.Net.Http ;
using static System . Net . WebRequestMethods ;
using TouchSocket.Http ;
using HttpClient = System . Net . Http . HttpClient ;
using HttpMethod = System . Net . Http . HttpMethod ;
namespace SlnMesnac.TouchSocket
{
@ -27,6 +30,8 @@ namespace SlnMesnac.TouchSocket
{
private readonly AppConfig _appConfig ;
private readonly ILogger < AirPorthttpClient > _logger ;
private System . Net . Http . HttpClient _httpClient ;
public string Url = "http://192.168.10.199:5102" ;
public AirPorthttpClient ( AppConfig appConfig , ILogger < AirPorthttpClient > logger )
{
@ -34,51 +39,67 @@ namespace SlnMesnac.TouchSocket
_logger = logger ;
}
public static WebApiClient AirportAGVClient ;
private WebApiClient CreateWebApiClient ( string IpHost )
{
var client = new WebApiClient ( ) ;
try
{
_logger . LogInformation ( "正在连接:" + IpHost ) ;
client . Connect ( IpHost ) ;
_logger . LogInformation ( IpHost + "连接成功" ) ;
return client ;
}
catch ( Exception ex )
{
_logger . LogError ( "ERROR: " + ex . Message ) ;
return null ;
}
}
//private WebApiClient CreateWebApiClient(string IpHost)
//{
// var client = new WebApiClient();
// try
// {
// _logger.LogInformation("正在连接:" + IpHost);
// client.Connect(IpHost);
// _logger.LogInformation(IpHost + "连接成功");
// return client;
// }
// catch (Exception ex)
// {
// _logger.LogError("ERROR: " + ex.Message);
// return null;
// }
//}
public void init ( )
{
try
{
AirportAGVClient = CreateWebApiClient ( _appConfig . AGVIpConfig ) ;
}
catch ( Exception ex )
{
_logger . LogError ( "ERROR: " + ex . Message ) ;
}
_httpClient = new HttpClient ( ) ;
}
/// <summary>
/// 获取到的JToken类型转换为实体类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public AGVResponseEntity < T > JTokenToEntity < T > ( JToken value ) where T : class
///// <summary>
///// 获取到的JToken类型转换为实体类
///// </summary>
///// <typeparam name="T"></typeparam>
///// <param name="value"></param>
///// <returns></returns>
//public AGVResponseEntity<T> JTokenToEntity<T>(JToken value) where T : class
//{
// if (value == null)
// {
// _logger.LogError("返回空数据!");
// return null;
// }
// string json = value.ToString();
// if (string.IsNullOrEmpty(json))
// {
// _logger.LogError("解析数据为空!");
// return null;
// }
// AGVResponseEntity<T> ResponseEntity;
// try
// {
// ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<T>>(json);
// return ResponseEntity;
// }
// catch (Exception ex)
// {
// _logger.LogError($"Json反序列化发生错误: {ex.Message}");
// return null;
// }
//}
public AGVResponseEntity < T > JsonStringToEntity < T > ( string json ) where T : class
{
if ( value = = null )
if ( json = = null )
{
_logger . LogError ( "返回空数据!" ) ;
return null ;
}
string json = value . ToString ( ) ;
if ( string . IsNullOrEmpty ( json ) )
{
_logger . LogError ( "解析数据为空!" ) ;
@ -87,7 +108,7 @@ namespace SlnMesnac.TouchSocket
AGVResponseEntity < T > ResponseEntity ;
try
{
ResponseEntity = Json Serializer. Deserialize < AGVResponseEntity < T > > ( json ) ;
ResponseEntity = Json Convert. DeserializeObject < AGVResponseEntity < T > > ( json ) ;
return ResponseEntity ;
}
catch ( Exception ex )
@ -96,6 +117,8 @@ namespace SlnMesnac.TouchSocket
return null ;
}
}
/// <summary>
/// AGV下发任务请求
/// </summary>
@ -104,12 +127,18 @@ namespace SlnMesnac.TouchSocket
///
public AGVResponseEntity < ResponseAddTaskDataEntity > AGVAddTaskRequest ( AGVRequestAddTaskEntity requestValue )
{
if ( AirportAGV Client = = null )
if ( _http Client = = null )
{
_logger . LogError ( "http服务为空" ) ;
return null ;
}
JToken responseValue = AirportAGVClient . InvokeT < JToken > ( "POST:/api/task/addTask" , null , requestValue ) ;
return JTokenToEntity < ResponseAddTaskDataEntity > ( responseValue ) ;
//序列化输入数据
string json = Newtonsoft . Json . JsonConvert . SerializeObject ( requestValue ) ;
var context = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
var httpContent = _httpClient . PostAsync ( $"{Url}/api/task/addTask" , context ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . Content ;
string result = httpContent . ReadAsStringAsync ( ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
//JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/addTask", null, requestValue);
return JsonStringToEntity < ResponseAddTaskDataEntity > ( result ) ;
}
/// <summary>
@ -119,12 +148,18 @@ namespace SlnMesnac.TouchSocket
/// <returns></returns>
public AGVResponseEntity < object > AGVOperationalTaskRequest ( AGVRequestOperationalTaskEntity requestValue )
{
if ( AirportAGV Client = = null )
if ( _http Client = = null )
{
_logger . LogError ( "http服务为空" ) ;
return null ;
}
JToken responseValue = AirportAGVClient . InvokeT < JToken > ( "POST:/api/task/operationalTask" , null , requestValue ) ;
return JTokenToEntity < object > ( responseValue ) ;
//序列化输入数据
string json = Newtonsoft . Json . JsonConvert . SerializeObject ( requestValue ) ;
var context = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
var httpContent = _httpClient . PostAsync ( $"{Url}/api/task/operationalTask" , context ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . Content ;
string result = httpContent . ReadAsStringAsync ( ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
//JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/operationalTask", null, requestValue);
return JsonStringToEntity < object > ( result ) ;
}
/// <summary>
@ -134,12 +169,18 @@ namespace SlnMesnac.TouchSocket
/// <returns></returns>
public AGVResponseEntity < ResponseTaskStateDetailDataEntity > AGVGetTaskStateDetailRequest ( AGVRequestTaskStateDetailEntity requestValue )
{
if ( AirportAGV Client = = null )
if ( _http Client = = null )
{
_logger . LogError ( "http服务为空" ) ;
return null ;
}
JToken responseValue = AirportAGVClient . InvokeT < JToken > ( "PUT:/api/task/getTaskStateDetail" , null , requestValue ) ;
return JTokenToEntity < ResponseTaskStateDetailDataEntity > ( responseValue ) ;
//序列化输入数据
string json = Newtonsoft . Json . JsonConvert . SerializeObject ( requestValue ) ;
var context = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
var httpContent = _httpClient . PutAsync ( $"{Url}/api/task/getTaskStateDetail" , context ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . Content ;
string result = httpContent . ReadAsStringAsync ( ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
//JToken responseValue = AirportAGVClient.InvokeT<JToken>("PUT:/api/task/getTaskStateDetail", null, requestValue);
return JsonStringToEntity < ResponseTaskStateDetailDataEntity > ( result ) ;
}
/// <summary>
@ -148,12 +189,16 @@ namespace SlnMesnac.TouchSocket
/// <returns></returns>
public AGVResponseEntity < List < ResponseRobotAtrributeDataEntity > > AGVAllStateRequest ( )
{
if ( AirportAGV Client = = null )
if ( _http Client = = null )
{
_logger . LogError ( "http服务为空" ) ;
return null ;
}
JToken responseValue = AirportAGVClient . InvokeT < JToken > ( "GET:/api/task/robot/getRobot" , null ) ;
return JTokenToEntity < List < ResponseRobotAtrributeDataEntity > > ( responseValue ) ;
//序列化输入数据
var httpContent = _httpClient . GetAsync ( $"{Url}/api/task/robot/getRobot" ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . Content ;
string result = httpContent . ReadAsStringAsync ( ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
//JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/task/robot/getRobot", null);
return JsonStringToEntity < List < ResponseRobotAtrributeDataEntity > > ( result ) ;
}
/// <summary>
@ -162,12 +207,15 @@ namespace SlnMesnac.TouchSocket
/// <returns></returns>
public AGVResponseEntity < ResponseActiveMapDataEntity > AGVMapActiveRequest ( )
{
if ( AirportAGV Client = = null )
if ( _http Client = = null )
{
_logger . LogError ( "http服务为空" ) ;
return null ;
}
JToken responseValue = AirportAGVClient . InvokeT < JToken > ( "GET:/api/v1.0.0/Maps/mapActive" , null ) ;
return JTokenToEntity < ResponseActiveMapDataEntity > ( responseValue ) ;
var httpContent = _httpClient . GetAsync ( $"{Url}/api/v1.0.0/Maps/mapActive" ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . Content ;
string result = httpContent . ReadAsStringAsync ( ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
//JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Maps/mapActive", null);
return JsonStringToEntity < ResponseActiveMapDataEntity > ( result ) ;
}
/// <summary>
@ -177,32 +225,68 @@ namespace SlnMesnac.TouchSocket
/// <returns></returns>
public AGVResponseEntity < List < ResponseMapPositionDataEntity > > AGVMapPositionRequest ( string requestValue )
{
if ( AirportAGV Client = = null )
if ( _http Client = = null )
{
_logger . LogError ( "http服务为空" ) ;
return null ;
}
JToken responseValue = AirportAGVClient . InvokeT < JToken > ( "GET:/api/v1.0.0/Positions?mapId={0}" , null , requestValue ) ;
return JTokenToEntity < List < ResponseMapPositionDataEntity > > ( responseValue ) ;
//序列化输入数据
string json = Newtonsoft . Json . JsonConvert . SerializeObject ( requestValue ) ;
var context = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
var httpContent = _httpClient . GetAsync ( $"{Url}/api/v1.0.0/Positions?mapId={requestValue}" ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) . Content ;
string result = httpContent . ReadAsStringAsync ( ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
//JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue);
return JsonStringToEntity < List < ResponseMapPositionDataEntity > > ( result ) ;
}
/// <summary>
/// 获取当前所有任务模板信息(目前不可用)
/// AGV异常获取清除
/// </summary>
/// <param name="agvIp"></param>
/// <returns></returns>
public AGVResponseEntity < object > AGV JobRequest( )
public AGVResponseEntity < object > AGV ErrorClear( string agvIp )
{
if ( AirportAGV Client = = null )
if ( _http Client = = null )
{
_logger . LogError ( "http服务为空" ) ;
return null ;
}
JToken responseValue = AirportAGVClient . InvokeT < JToken > ( "GET:/api/v1.0.0/Positions?mapId={0}" , null ) ;
return JTokenToEntity < object > ( responseValue ) ;
}
string token = "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJyb2xlIjoiMSIsImlzcyI6IlNpbmV2YVNlcnZlciIsImF1ZCI6IlNpbmV2YUNsaWVudCJ9.Ujn-JuQDzA_sloITF84z1pdQvvH8DQ0DSFlhz4zP44M" ;
var request = new HttpRequestMessage ( HttpMethod . Delete , $"http://{agvIp}/api/v1.0.0/ClearException/0" ) ;
// 单独为此请求添加 Authorization 头部
request . Headers . Add ( "Authorization" , token ) ;
// 发送请求
var response = _httpClient . SendAsync ( request ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
// 读取响应内容
var httpContent = response . Content ;
string result = httpContent . ReadAsStringAsync ( ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
return JsonStringToEntity < object > ( result ) ;
}
//HttpClient client1 = new HttpClient();
///// <summary>
///// 获取当前所有任务模板信息(目前不可用)
///// </summary>
///// <returns></returns>
//public AGVResponseEntity<object> AGVJobRequest()
//{
// if (_httpClient == null)
// {
// _logger.LogError("http服务为空");
// }
// //序列化输入数据
// string json = Newtonsoft.Json.JsonConvert.SerializeObject(requestValue);
// var context = new StringContent(json, Encoding.UTF8, "application/json");
// var httpContent = _httpClient.PostAsync($"{Url}/api/task/addTask", context).ConfigureAwait(false).GetAwaiter().GetResult().Content;
// string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
// AGVResponseEntity<ResponseAddTaskDataEntity> responseEntity = JsonConvert.DeserializeObject<AGVResponseEntity<ResponseAddTaskDataEntity>>(result);
// //JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Positions?mapId={0}", null);
// return JTokenToEntity<object>(responseValue);
//}
}
//string url = "http://192.168.10.199:5102/api/task/addTask";