You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

399 lines
15 KiB
C#

using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SlnMesnac.Config;
using SlnMesnac.Model.AirportApiEntity;
using SQLitePCL;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Text.Json;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi;
using JsonSerializer = System.Text.Json.JsonSerializer;
using Serilog;
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
{
public class AirPorthttpClient
{
private readonly AppConfig _appConfig;
private readonly ILogger<AirPorthttpClient> _logger;
private System.Net.Http.HttpClient _httpClient;
public string Url;
public AirPorthttpClient(AppConfig appConfig, ILogger<AirPorthttpClient> logger)
{
_appConfig = appConfig;
_logger = logger;
}
//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()
{
Url = _appConfig.AGVIpConfig;
_httpClient = new HttpClient()
{
Timeout = TimeSpan.FromSeconds(3),
};
}
///// <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 int GetConnectState()
{
if (_httpClient != null)
{
try
{
var res = _httpClient.GetAsync($"{Url}/test").ConfigureAwait(false).GetAwaiter().GetResult().Content;
string result = res.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
return 1;
}
catch
{
_logger.LogError("AGV服务器连接失败");
return 0;
}
}
else
{
return 2;
}
}
public AGVResponseEntity<T> JsonStringToEntity<T>(string json) where T : class
{
if (json == null)
{
_logger.LogError("返回空数据!");
return null;
}
if (string.IsNullOrEmpty(json))
{
_logger.LogError("解析数据为空!");
return null;
}
AGVResponseEntity<T> ResponseEntity;
try
{
ResponseEntity = JsonConvert.DeserializeObject<AGVResponseEntity<T>>(json);
return ResponseEntity;
}
catch (Exception ex)
{
_logger.LogError($"Json反序列化发生错误{ex.Message}");
return null;
}
}
/// <summary>
/// AGV下发任务请求
/// </summary>
/// <param name="requestValue"></param>
/// <returns></returns>
///
public AGVResponseEntity<ResponseAddTaskDataEntity> AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue)
{
try
{
if (_httpClient == null)
{
_logger.LogError("http服务为空");
return null;
}
//序列化输入数据
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);
}
catch (Exception ex)
{
_logger.LogError($"AGV下发任务请求接口异常 {ex.Message}");
return null;
}
}
/// <summary>
/// 取消/终止任务请求
/// </summary>
/// <param name="requestValue"></param>
/// <returns></returns>
public AGVResponseEntity<object> AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue)
{
try
{
if (_httpClient == null)
{
_logger.LogError("http服务为空");
return null;
}
//序列化输入数据
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);
}
catch (Exception ex)
{
_logger.LogError($"取消/终止任务请求接口异常 {ex.Message}");
return null;
}
}
/// <summary>
/// 查询任务状态请求
/// </summary>
/// <param name="requestValue"></param>
/// <returns></returns>
public AGVResponseEntity<ResponseTaskStateDetailDataEntity> AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue)
{
try
{
if (_httpClient == null)
{
_logger.LogError("http服务为空");
return null;
}
//序列化输入数据
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);
}
catch (Exception ex)
{
_logger.LogError($"查询任务状态接口异常 {ex.Message}");
return null;
}
}
/// <summary>
/// 获取所有AGV信息请求
/// </summary>
/// <returns></returns>
public AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>> AGVAllStateRequest()
{
try
{
if (_httpClient == null)
{
_logger.LogError("http服务为空");
return null;
}
//序列化输入数据
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);
}
catch (Exception ex)
{
_logger.LogError($"获取所有AGV信息接口异常 {ex.Message}");
return null;
}
}
/// <summary>
/// 获取当前激活地图信息请求
/// </summary>
/// <returns></returns>
public AGVResponseEntity<ResponseActiveMapDataEntity> AGVMapActiveRequest()
{
try
{
if (_httpClient == null)
{
_logger.LogError("http服务为空");
return null;
}
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);
}
catch (Exception ex)
{
_logger.LogError($"互殴当前激活地图接口异常 {ex.Message}");
return null;
}
}
/// <summary>
/// 获取当前地图所有位置点请求
/// </summary>
/// <param name="requestValue"></param>
/// <returns></returns>
public AGVResponseEntity<List<ResponseMapPositionDataEntity>> AGVMapPositionRequest(string requestValue)
{
try
{
if (_httpClient == null)
{
_logger.LogError("http服务为空");
return null;
}
//序列化输入数据
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);
}
catch (Exception ex)
{
_logger.LogError($"获取当前地图所有位置点接口异常 {ex.Message}");
return null;
}
}
/// <summary>
/// AGV异常获取清除
/// </summary>
/// <param name="agvIp"></param>
/// <returns></returns>
public AGVResponseEntity<object> AGVErrorClear(string agvIp)
{
try
{
if (_httpClient == null)
{
_logger.LogError("http服务为空");
return null;
}
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);
}
catch (Exception ex)
{
_logger.LogError($"AGV异常清楚接口异常 {ex.Message}");
return null;
}
}
///// <summary>
///// AGV入库接口
///// </summary>
///// <returns></returns>
//public bool AGVInStore(string agvGuid)
//{
//}
///// <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($"http://192.168.10.199/api/v1.0.0/Jobs", 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";
//string json =Newtonsoft.Json.JsonConvert.SerializeObject(requestValue);
//var context=new StringContent(json,Encoding.UTF8, "application/json");
//var httpContent = client1.PostAsync(url, context).ConfigureAwait(false).GetAwaiter().GetResult().Content;
//var result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
//_logger.LogInformation(result);
}