change - 优化AirportHttpClient类

master
SoulStar 4 months ago
parent 6abe7bf5c6
commit 38ed1f0a4d

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

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

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

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

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

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

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

Loading…
Cancel
Save