From 38ed1f0a4d613b1bf77fd14aa603dc2058de4538 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Thu, 26 Sep 2024 16:28:51 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E4=BC=98=E5=8C=96AirportHttpClien?= =?UTF-8?q?t=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/BaseStateRefreshBusiness.cs | 15 +- .../base/BaseTaskInfoBusiness.cs | 2 +- SlnMesnac.TouchSocket/AirPorthttpClient.cs | 139 ++++++++---------- SlnMesnac.TouchSocket/TcpServer.cs | 2 - SlnMesnac.TouchSocket/TouchSocketSetup.cs | 5 +- SlnMesnac.WPF/Startup.cs | 2 +- .../IndexPage/IndexContentViewModel.cs | 1 - 7 files changed, 76 insertions(+), 90 deletions(-) diff --git a/SlnMesnac.Business/base/BaseStateRefreshBusiness.cs b/SlnMesnac.Business/base/BaseStateRefreshBusiness.cs index 721e974..e01a058 100644 --- a/SlnMesnac.Business/base/BaseStateRefreshBusiness.cs +++ b/SlnMesnac.Business/base/BaseStateRefreshBusiness.cs @@ -13,16 +13,19 @@ namespace SlnMesnac.Business.@base private IAGVStateService _AGVStateService; private ILogger _logger; private static BaseStateRefreshBusiness instance; - public BaseStateRefreshBusiness(ILogger logger, IAGVStateService agvService) + private AirPorthttpClient _airPorthttpClient; + + public BaseStateRefreshBusiness(ILogger logger, IAGVStateService agvService, AirPorthttpClient airPorthttpClient) { _logger = logger; _AGVStateService = agvService; + _airPorthttpClient = airPorthttpClient; } - public static BaseStateRefreshBusiness GetInstance(ILogger logger, IAGVStateService agvService) + public static BaseStateRefreshBusiness GetInstance(ILogger 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) diff --git a/SlnMesnac.Business/base/BaseTaskInfoBusiness.cs b/SlnMesnac.Business/base/BaseTaskInfoBusiness.cs index 88d229d..d36fad3 100644 --- a/SlnMesnac.Business/base/BaseTaskInfoBusiness.cs +++ b/SlnMesnac.Business/base/BaseTaskInfoBusiness.cs @@ -91,7 +91,7 @@ namespace SlnMesnac.Business.@base //任务异常 else if (entity.DataBytes[0] == 0x01) { - + } } } diff --git a/SlnMesnac.TouchSocket/AirPorthttpClient.cs b/SlnMesnac.TouchSocket/AirPorthttpClient.cs index 28177a6..fb8513d 100644 --- a/SlnMesnac.TouchSocket/AirPorthttpClient.cs +++ b/SlnMesnac.TouchSocket/AirPorthttpClient.cs @@ -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 _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; } /// - /// AGV下发任务请求 + /// 获取到的JToken类型转换为实体类 /// - /// + /// + /// /// - public static AGVResponseEntity AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue) + public T JTokenToEntity(JToken value) where T : class { - JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/addTask", null, requestValue); - if (responseValue == null) + if (value == null) { - return new AGVResponseEntity(); + return null; } - string json = responseValue.ToString(); + string json = value.ToString(); if (string.IsNullOrEmpty(json)) { - return new AGVResponseEntity(); + return null; } - AGVResponseEntity responseEntity; - responseEntity = JsonSerializer.Deserialize>(json); - return responseEntity; + T ResponseEntity; + ResponseEntity = JsonSerializer.Deserialize(json); + return ResponseEntity; } /// - /// 取消/终止任务请求 + /// AGV下发任务请求 /// + /// /// - public static AGVResponseEntity AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue) + public AGVResponseEntity AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue) { - JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/operationalTask", null, requestValue); - if (responseValue == null) + if (AirportAGVClient == null) { - return new AGVResponseEntity(); + return null; } - string json = responseValue.ToString(); - if (string.IsNullOrEmpty(json)) + JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/addTask", null, requestValue); + return JTokenToEntity>(responseValue); + } + + /// + /// 取消/终止任务请求 + /// + /// + /// + public AGVResponseEntity AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue) + { + if (AirportAGVClient == null) { - return new AGVResponseEntity(); + return null; } - AGVResponseEntity responseEntity; - responseEntity = JsonSerializer.Deserialize>(json); - return responseEntity; + JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/operationalTask", null, requestValue); + return JTokenToEntity>(responseValue); } /// /// 查询任务状态请求 /// - /// + /// /// - public static AGVResponseEntity AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue) + public AGVResponseEntity AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue) { - JToken responseValue = AirportAGVClient.InvokeT("PUT:/api/task/getTaskStateDetail", null, requestValue); - if (responseValue == null) - { - return new AGVResponseEntity(); - } - string json = responseValue.ToString(); - if (string.IsNullOrEmpty(json)) + if (AirportAGVClient == null) { - return new AGVResponseEntity(); + return null; } - AGVResponseEntity ResponseEntity; - ResponseEntity = JsonSerializer.Deserialize>(json); - return ResponseEntity; + JToken responseValue = AirportAGVClient.InvokeT("PUT:/api/task/getTaskStateDetail", null, requestValue); + return JTokenToEntity>(responseValue); } /// /// 获取所有AGV信息请求 /// - /// /// - public static AGVResponseEntity> AGVAllStateRequest() + public AGVResponseEntity> AGVAllStateRequest() { - JToken responseValue = AirportAGVClient.InvokeT("GET:/api/task/robot/getRobot", null); - if (responseValue == null) - { - return new AGVResponseEntity>(); - } - string json = responseValue.ToString(); - if (string.IsNullOrEmpty(json)) + if (AirportAGVClient == null) { - return new AGVResponseEntity>(); + return null; } - AGVResponseEntity> ResponseEntity; - ResponseEntity = JsonSerializer.Deserialize>>(json); - return ResponseEntity; + JToken responseValue = AirportAGVClient.InvokeT("GET:/api/task/robot/getRobot", null); + return JTokenToEntity>>(responseValue); } /// /// 获取当前激活地图信息请求 /// - /// /// - public static AGVResponseEntity AGVMapActiveRequest() + public AGVResponseEntity AGVMapActiveRequest() { - JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Maps/mapActive", null); - if (responseValue == null) - { - return new AGVResponseEntity(); - } - string json = responseValue.ToString(); - if (string.IsNullOrEmpty(json)) + if (AirportAGVClient == null) { - return new AGVResponseEntity(); + return null; } - AGVResponseEntity ResponseEntity; - ResponseEntity = JsonSerializer.Deserialize>(json); - return ResponseEntity; + JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Maps/mapActive", null); + return JTokenToEntity>(responseValue); } /// /// 获取当前地图所有位置点请求 /// - /// + /// /// - public static AGVResponseEntity> AGVMapPositionRequest(string requestValue) + public AGVResponseEntity> AGVMapPositionRequest(string requestValue) { - JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue); - if (responseValue == null) - { - return new AGVResponseEntity>(); - } - string json = responseValue.ToString(); - if (string.IsNullOrEmpty(json)) + if (AirportAGVClient == null) { - return new AGVResponseEntity>(); + return null; } - AGVResponseEntity> ResponseEntity; - ResponseEntity = JsonSerializer.Deserialize>>(json); - return ResponseEntity; + JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue); + return JTokenToEntity>>(responseValue); } - } } diff --git a/SlnMesnac.TouchSocket/TcpServer.cs b/SlnMesnac.TouchSocket/TcpServer.cs index 6b97724..2f9ad4b 100644 --- a/SlnMesnac.TouchSocket/TcpServer.cs +++ b/SlnMesnac.TouchSocket/TcpServer.cs @@ -93,8 +93,6 @@ namespace SlnMesnac.TouchSocket ReceiveStackWorkDoneEvent += StackResultSend; ReceiveManualExceptionDealDoneEvent += ManualExceptionDealDone; - - } public void Init(int serverPort) diff --git a/SlnMesnac.TouchSocket/TouchSocketSetup.cs b/SlnMesnac.TouchSocket/TouchSocketSetup.cs index 52b5f6b..65bcab6 100644 --- a/SlnMesnac.TouchSocket/TouchSocketSetup.cs +++ b/SlnMesnac.TouchSocket/TouchSocketSetup.cs @@ -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(); - _server.Init(6001); + var _httpclient = app.ApplicationServices.GetService(); + _server.Init(6001); + _httpclient.init(); // var _apiServer = app.ApplicationServices.GetService(); // _apiServer.Init(); return app; diff --git a/SlnMesnac.WPF/Startup.cs b/SlnMesnac.WPF/Startup.cs index cc88308..c7c727b 100644 --- a/SlnMesnac.WPF/Startup.cs +++ b/SlnMesnac.WPF/Startup.cs @@ -46,7 +46,7 @@ namespace SlnMesnac.WPF services.AddPlcFactorySetup(); //注册httpClient - services.AddHostedService(); + //services.AddHostedService(); //注册TCPServer //services.AddHostedService(); diff --git a/SlnMesnac.WPF/ViewModel/IndexPage/IndexContentViewModel.cs b/SlnMesnac.WPF/ViewModel/IndexPage/IndexContentViewModel.cs index 0c3822a..20732c6 100644 --- a/SlnMesnac.WPF/ViewModel/IndexPage/IndexContentViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/IndexPage/IndexContentViewModel.cs @@ -109,7 +109,6 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage #region 测试数据 LoadTaskInfo(); - //Thread.Sleep(5000); //_tcpServer.SendReplyGetManualException(