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; namespace SlnMesnac.TouchSocket { public class AirPorthttpClient { 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 { _logger.LogInformation("正在连接:" + IpHost); client.Connect(IpHost); _logger.LogInformation(IpHost + "连接成功"); return client; } catch (Exception ex) { _logger.LogError("ERROR: " + ex.Message); return null; } } public void init() { try { AirportAGVClient = CreateWebApiClient(_appConfig.AGVIpConfig); } catch (Exception ex) { _logger.LogError("ERROR: " + ex.Message); } } /// /// 获取到的JToken类型转换为实体类 /// /// /// /// public T JTokenToEntity(JToken value) where T : class { if (value == null) { return null; } string json = value.ToString(); if (string.IsNullOrEmpty(json)) { return null; } T ResponseEntity; ResponseEntity = JsonSerializer.Deserialize(json); return ResponseEntity; } /// /// AGV下发任务请求 /// /// /// public AGVResponseEntity AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue) { if (AirportAGVClient == null) { return null; } JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/addTask", null, requestValue); return JTokenToEntity>(responseValue); } /// /// 取消/终止任务请求 /// /// /// public AGVResponseEntity AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue) { if (AirportAGVClient == null) { return null; } JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/operationalTask", null, requestValue); return JTokenToEntity>(responseValue); } /// /// 查询任务状态请求 /// /// /// public AGVResponseEntity AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue) { if (AirportAGVClient == null) { return null; } JToken responseValue = AirportAGVClient.InvokeT("PUT:/api/task/getTaskStateDetail", null, requestValue); return JTokenToEntity>(responseValue); } /// /// 获取所有AGV信息请求 /// /// public AGVResponseEntity> AGVAllStateRequest() { if (AirportAGVClient == null) { return null; } JToken responseValue = AirportAGVClient.InvokeT("GET:/api/task/robot/getRobot", null); return JTokenToEntity>>(responseValue); } /// /// 获取当前激活地图信息请求 /// /// public AGVResponseEntity AGVMapActiveRequest() { if (AirportAGVClient == null) { return null; } JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Maps/mapActive", null); return JTokenToEntity>(responseValue); } /// /// 获取当前地图所有位置点请求 /// /// /// public AGVResponseEntity> AGVMapPositionRequest(string requestValue) { if (AirportAGVClient == null) { return null; } JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue); return JTokenToEntity>>(responseValue); } } }