change - 优化AirportHttpClient类

master
SoulStar 4 months ago
parent 6abe7bf5c6
commit 38ed1f0a4d

@ -13,16 +13,19 @@ namespace SlnMesnac.Business.@base
private IAGVStateService _AGVStateService;
private ILogger<BaseStateRefreshBusiness> _logger;
private static BaseStateRefreshBusiness instance;
public BaseStateRefreshBusiness(ILogger<BaseStateRefreshBusiness> logger, IAGVStateService agvService)
private AirPorthttpClient _airPorthttpClient;
public BaseStateRefreshBusiness(ILogger<BaseStateRefreshBusiness> logger, IAGVStateService agvService, AirPorthttpClient airPorthttpClient)
{
_logger = logger;
_AGVStateService = agvService;
_airPorthttpClient = airPorthttpClient;
}
public static BaseStateRefreshBusiness GetInstance(ILogger<BaseStateRefreshBusiness> logger, IAGVStateService agvService)
public static BaseStateRefreshBusiness GetInstance(ILogger<BaseStateRefreshBusiness> logger, IAGVStateService agvService, AirPorthttpClient airPorthttpClient)
{
if (instance == null)
{
instance = new BaseStateRefreshBusiness(logger, agvService);
instance = new BaseStateRefreshBusiness(logger, agvService, airPorthttpClient);
}
return instance;
}
@ -31,7 +34,11 @@ namespace SlnMesnac.Business.@base
{
try
{
var response = AirPorthttpClient.AGVAllStateRequest();
var response = _airPorthttpClient.AGVAllStateRequest();
if(response == null)
{
return false;
}
_AGVStateService.UpdateOrAddByResponse(response);
}
catch (Exception e)

@ -91,7 +91,7 @@ namespace SlnMesnac.Business.@base
//任务异常
else if (entity.DataBytes[0] == 0x01)
{
}
}
}

@ -15,11 +15,13 @@ using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi;
using JsonSerializer = System.Text.Json.JsonSerializer;
using Serilog;
using Serilog.Events;
using Microsoft.Extensions.Logging;
namespace SlnMesnac.TouchSocket
{
public class AirPorthttpClient : BackgroundService
public class AirPorthttpClient
{
private readonly AppConfig _appConfig;
private readonly ILogger<AirPorthttpClient> _logger;
@ -37,18 +39,19 @@ namespace SlnMesnac.TouchSocket
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;
}
return client;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
public void init()
{
try
{
@ -57,143 +60,119 @@ namespace SlnMesnac.TouchSocket
catch (Exception ex)
{
_logger.LogError("ERROR: " + ex.Message);
return Task.FromException(ex);
}
return Task.CompletedTask;
}
/// <summary>
/// AGV下发任务请求
/// 获取到的JToken类型转换为实体类
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static AGVResponseEntity<ResponseAddTaskDataEntity> AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue)
public T JTokenToEntity<T>(JToken value) where T : class
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/addTask", null, requestValue);
if (responseValue == null)
if (value == null)
{
return new AGVResponseEntity<ResponseAddTaskDataEntity>();
return null;
}
string json = responseValue.ToString();
string json = value.ToString();
if (string.IsNullOrEmpty(json))
{
return new AGVResponseEntity<ResponseAddTaskDataEntity>();
return null;
}
AGVResponseEntity<ResponseAddTaskDataEntity> responseEntity;
responseEntity = JsonSerializer.Deserialize<AGVResponseEntity<ResponseAddTaskDataEntity>>(json);
return responseEntity;
T ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<T>(json);
return ResponseEntity;
}
/// <summary>
/// 取消/终止任务请求
/// AGV下发任务请求
/// </summary>
/// <param name="requestValue"></param>
/// <returns></returns>
public static AGVResponseEntity<object> AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue)
public AGVResponseEntity<ResponseAddTaskDataEntity> AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue)
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/operationalTask", null, requestValue);
if (responseValue == null)
if (AirportAGVClient == null)
{
return new AGVResponseEntity<object>();
return null;
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/addTask", null, requestValue);
return JTokenToEntity<AGVResponseEntity<ResponseAddTaskDataEntity>>(responseValue);
}
/// <summary>
/// 取消/终止任务请求
/// </summary>
/// <param name="requestValue"></param>
/// <returns></returns>
public AGVResponseEntity<object> AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue)
{
if (AirportAGVClient == null)
{
return new AGVResponseEntity<object>();
return null;
}
AGVResponseEntity<object> responseEntity;
responseEntity = JsonSerializer.Deserialize<AGVResponseEntity<object>>(json);
return responseEntity;
JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/operationalTask", null, requestValue);
return JTokenToEntity<AGVResponseEntity<object>>(responseValue);
}
/// <summary>
/// 查询任务状态请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <param name="requestValue"></param>
/// <returns></returns>
public static AGVResponseEntity<ResponseTaskStateDetailDataEntity> AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue)
public AGVResponseEntity<ResponseTaskStateDetailDataEntity> AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue)
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("PUT:/api/task/getTaskStateDetail", null, requestValue);
if (responseValue == null)
{
return new AGVResponseEntity<ResponseTaskStateDetailDataEntity>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
if (AirportAGVClient == null)
{
return new AGVResponseEntity<ResponseTaskStateDetailDataEntity>();
return null;
}
AGVResponseEntity<ResponseTaskStateDetailDataEntity> ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<ResponseTaskStateDetailDataEntity>>(json);
return ResponseEntity;
JToken responseValue = AirportAGVClient.InvokeT<JToken>("PUT:/api/task/getTaskStateDetail", null, requestValue);
return JTokenToEntity<AGVResponseEntity<ResponseTaskStateDetailDataEntity>>(responseValue);
}
/// <summary>
/// 获取所有AGV信息请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <returns></returns>
public static AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>> AGVAllStateRequest()
public AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>> AGVAllStateRequest()
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/task/robot/getRobot", null);
if (responseValue == null)
{
return new AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
if (AirportAGVClient == null)
{
return new AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>>();
return null;
}
AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>> ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>>>(json);
return ResponseEntity;
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/task/robot/getRobot", null);
return JTokenToEntity<AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>>>(responseValue);
}
/// <summary>
/// 获取当前激活地图信息请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <returns></returns>
public static AGVResponseEntity<ResponseActiveMapDataEntity> AGVMapActiveRequest()
public AGVResponseEntity<ResponseActiveMapDataEntity> AGVMapActiveRequest()
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Maps/mapActive", null);
if (responseValue == null)
{
return new AGVResponseEntity<ResponseActiveMapDataEntity>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
if (AirportAGVClient == null)
{
return new AGVResponseEntity<ResponseActiveMapDataEntity>();
return null;
}
AGVResponseEntity<ResponseActiveMapDataEntity> ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<ResponseActiveMapDataEntity>>(json);
return ResponseEntity;
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Maps/mapActive", null);
return JTokenToEntity<AGVResponseEntity<ResponseActiveMapDataEntity>>(responseValue);
}
/// <summary>
/// 获取当前地图所有位置点请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <param name="requestValue"></param>
/// <returns></returns>
public static AGVResponseEntity<List<ResponseMapPositionDataEntity>> AGVMapPositionRequest(string requestValue)
public AGVResponseEntity<List<ResponseMapPositionDataEntity>> AGVMapPositionRequest(string requestValue)
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue);
if (responseValue == null)
{
return new AGVResponseEntity<List<ResponseMapPositionDataEntity>>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
if (AirportAGVClient == null)
{
return new AGVResponseEntity<List<ResponseMapPositionDataEntity>>();
return null;
}
AGVResponseEntity<List<ResponseMapPositionDataEntity>> ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<List<ResponseMapPositionDataEntity>>>(json);
return ResponseEntity;
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue);
return JTokenToEntity<AGVResponseEntity<List<ResponseMapPositionDataEntity>>>(responseValue);
}
}
}

@ -93,8 +93,6 @@ namespace SlnMesnac.TouchSocket
ReceiveStackWorkDoneEvent += StackResultSend;
ReceiveManualExceptionDealDoneEvent += ManualExceptionDealDone;
}
public void Init(int serverPort)

@ -5,6 +5,7 @@ using System.Text;
using Microsoft.AspNetCore.Builder;
using TouchSocket.Sockets;
using TouchSocket.WebApi;
using SlnMesnac.Config;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
@ -39,8 +40,10 @@ namespace SlnMesnac.TouchSocket
public static IApplicationBuilder UseTouchSocketExtensions(this IApplicationBuilder app)
{
var _server = app.ApplicationServices.GetService<TcpServer>();
_server.Init(6001);
var _httpclient = app.ApplicationServices.GetService<AirPorthttpClient>();
_server.Init(6001);
_httpclient.init();
// var _apiServer = app.ApplicationServices.GetService<WebApiServer>();
// _apiServer.Init();
return app;

@ -46,7 +46,7 @@ namespace SlnMesnac.WPF
services.AddPlcFactorySetup();
//注册httpClient
services.AddHostedService<AirPorthttpClient>();
//services.AddHostedService<AirPorthttpClient>();
//注册TCPServer
//services.AddHostedService<TcpServer>();

@ -109,7 +109,6 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage
#region 测试数据
LoadTaskInfo();
//Thread.Sleep(5000);
//_tcpServer.SendReplyGetManualException(

Loading…
Cancel
Save