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 Microsoft.Extensions.Logging; namespace SlnMesnac.TouchSocket { public class AirPorthttpClient : BackgroundService { private readonly AppConfig _appConfig; private readonly ILogger _logger; public AirPorthttpClient(AppConfig appConfig, ILogger logger) { _appConfig = appConfig; _logger = logger; } public static WebApiClient AirportAGVClient; private WebApiClient CreateWebApiClient(string IpHost) { var client = new WebApiClient(); try { client.Connect(IpHost); _logger.LogInformation(IpHost + "连接成功"); } catch (Exception ex) { _logger.LogError("ERROR: " + ex.Message); return null; } return client; } protected override Task ExecuteAsync(CancellationToken stoppingToken) { try { AirportAGVClient = CreateWebApiClient(_appConfig.AGVIpConfig); } catch (Exception ex) { _logger.LogError("ERROR: " + ex.Message); return Task.FromException(ex); } return Task.CompletedTask; } /// /// AGV下发任务请求 /// /// /// public static AGVResponseEntity AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue) { JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/addTask", null, requestValue); if (responseValue == null) { return new AGVResponseEntity(); } string json = responseValue.ToString(); if (string.IsNullOrEmpty(json)) { return new AGVResponseEntity(); } AGVResponseEntity responseEntity; responseEntity = JsonSerializer.Deserialize>(json); return responseEntity; } /// /// 取消/终止任务请求 /// /// public static AGVResponseEntity AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue) { JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/operationalTask", null, requestValue); if (responseValue == null) { return new AGVResponseEntity(); } string json = responseValue.ToString(); if (string.IsNullOrEmpty(json)) { return new AGVResponseEntity(); } AGVResponseEntity responseEntity; responseEntity = JsonSerializer.Deserialize>(json); return responseEntity; } /// /// 查询任务状态请求 /// /// /// public static 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)) { return new AGVResponseEntity(); } AGVResponseEntity ResponseEntity; ResponseEntity = JsonSerializer.Deserialize>(json); return ResponseEntity; } /// /// 获取所有AGV信息请求 /// /// /// public static 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)) { return new AGVResponseEntity>(); } AGVResponseEntity> ResponseEntity; ResponseEntity = JsonSerializer.Deserialize>>(json); return ResponseEntity; } /// /// 获取当前激活地图信息请求 /// /// /// public static 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)) { return new AGVResponseEntity(); } AGVResponseEntity ResponseEntity; ResponseEntity = JsonSerializer.Deserialize>(json); return ResponseEntity; } /// /// 获取当前地图所有位置点请求 /// /// /// public static 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)) { return new AGVResponseEntity>(); } AGVResponseEntity> ResponseEntity; ResponseEntity = JsonSerializer.Deserialize>>(json); return ResponseEntity; } } }