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; using System.Net.Http; using static System.Net.WebRequestMethods; using TouchSocket.Http; using HttpClient = System.Net.Http.HttpClient; using HttpMethod = System.Net.Http.HttpMethod; namespace SlnMesnac.TouchSocket { public class AirPorthttpClient { private readonly AppConfig _appConfig; private readonly ILogger _logger; private System.Net.Http.HttpClient _httpClient; public string Url; public AirPorthttpClient(AppConfig appConfig, ILogger logger) { _appConfig = appConfig; _logger = logger; } //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() { Url = _appConfig.AGVIpConfig; _httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(3), }; } ///// ///// 获取到的JToken类型转换为实体类 ///// ///// ///// ///// //public AGVResponseEntity JTokenToEntity(JToken value) where T : class //{ // if (value == null) // { // _logger.LogError("返回空数据!"); // return null; // } // string json = value.ToString(); // if (string.IsNullOrEmpty(json)) // { // _logger.LogError("解析数据为空!"); // return null; // } // AGVResponseEntity ResponseEntity; // try // { // ResponseEntity = JsonSerializer.Deserialize>(json); // return ResponseEntity; // } // catch (Exception ex) // { // _logger.LogError($"Json反序列化发生错误:{ex.Message}"); // return null; // } //} public int GetConnectState() { if (_httpClient != null) { try { var res = _httpClient.GetAsync($"{Url}/test").ConfigureAwait(false).GetAwaiter().GetResult().Content; string result = res.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); return 1; } catch { _logger.LogError("AGV服务器连接失败"); return 0; } } else { return 2; } } public AGVResponseEntity JsonStringToEntity(string json) where T : class { if (json == null) { _logger.LogError("返回空数据!"); return null; } if (string.IsNullOrEmpty(json)) { _logger.LogError("解析数据为空!"); return null; } AGVResponseEntity ResponseEntity; try { ResponseEntity = JsonConvert.DeserializeObject>(json); return ResponseEntity; } catch (Exception ex) { _logger.LogError($"Json反序列化发生错误:{ex.Message}"); return null; } } /// /// AGV下发任务请求 /// /// /// /// public AGVResponseEntity AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue) { try { if (_httpClient == null) { _logger.LogError("http服务为空"); return null; } //序列化输入数据 string json = Newtonsoft.Json.JsonConvert.SerializeObject(requestValue); var context = new StringContent(json, Encoding.UTF8, "application/json"); var httpContent = _httpClient.PostAsync($"{Url}/api/task/addTask", context).ConfigureAwait(false).GetAwaiter().GetResult().Content; string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); //JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/addTask", null, requestValue); return JsonStringToEntity(result); } catch (Exception ex) { _logger.LogError($"AGV下发任务请求接口异常 {ex.Message}"); return null; } } /// /// 取消/终止任务请求 /// /// /// public AGVResponseEntity AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue) { try { if (_httpClient == null) { _logger.LogError("http服务为空"); return null; } //序列化输入数据 string json = Newtonsoft.Json.JsonConvert.SerializeObject(requestValue); var context = new StringContent(json, Encoding.UTF8, "application/json"); var httpContent = _httpClient.PostAsync($"{Url}/api/task/operationalTask", context).ConfigureAwait(false).GetAwaiter().GetResult().Content; string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); //JToken responseValue = AirportAGVClient.InvokeT("POST:/api/task/operationalTask", null, requestValue); return JsonStringToEntity(result); } catch (Exception ex) { _logger.LogError($"取消/终止任务请求接口异常 {ex.Message}"); return null; } } /// /// 查询任务状态请求 /// /// /// public AGVResponseEntity AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue) { try { if (_httpClient == null) { _logger.LogError("http服务为空"); return null; } //序列化输入数据 string json = Newtonsoft.Json.JsonConvert.SerializeObject(requestValue); var context = new StringContent(json, Encoding.UTF8, "application/json"); var httpContent = _httpClient.PutAsync($"{Url}/api/task/getTaskStateDetail", context).ConfigureAwait(false).GetAwaiter().GetResult().Content; string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); //JToken responseValue = AirportAGVClient.InvokeT("PUT:/api/task/getTaskStateDetail", null, requestValue); return JsonStringToEntity(result); } catch (Exception ex) { _logger.LogError($"查询任务状态接口异常 {ex.Message}"); return null; } } /// /// 获取所有AGV信息请求 /// /// public AGVResponseEntity> AGVAllStateRequest() { try { if (_httpClient == null) { _logger.LogError("http服务为空"); return null; } //序列化输入数据 var httpContent = _httpClient.GetAsync($"{Url}/api/task/robot/getRobot").ConfigureAwait(false).GetAwaiter().GetResult().Content; string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); //JToken responseValue = AirportAGVClient.InvokeT("GET:/api/task/robot/getRobot", null); return JsonStringToEntity>(result); } catch (Exception ex) { _logger.LogError($"获取所有AGV信息接口异常 {ex.Message}"); return null; } } /// /// 获取当前激活地图信息请求 /// /// public AGVResponseEntity AGVMapActiveRequest() { try { if (_httpClient == null) { _logger.LogError("http服务为空"); return null; } var httpContent = _httpClient.GetAsync($"{Url}/api/v1.0.0/Maps/mapActive").ConfigureAwait(false).GetAwaiter().GetResult().Content; string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); //JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Maps/mapActive", null); return JsonStringToEntity(result); } catch (Exception ex) { _logger.LogError($"互殴当前激活地图接口异常 {ex.Message}"); return null; } } /// /// 获取当前地图所有位置点请求 /// /// /// public AGVResponseEntity> AGVMapPositionRequest(string requestValue) { try { if (_httpClient == null) { _logger.LogError("http服务为空"); return null; } //序列化输入数据 string json = Newtonsoft.Json.JsonConvert.SerializeObject(requestValue); var context = new StringContent(json, Encoding.UTF8, "application/json"); var httpContent = _httpClient.GetAsync($"{Url}/api/v1.0.0/Positions?mapId={requestValue}").ConfigureAwait(false).GetAwaiter().GetResult().Content; string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); //JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue); return JsonStringToEntity>(result); } catch (Exception ex) { _logger.LogError($"获取当前地图所有位置点接口异常 {ex.Message}"); return null; } } /// /// AGV异常获取清除 /// /// /// public AGVResponseEntity AGVErrorClear(string agvIp) { try { if (_httpClient == null) { _logger.LogError("http服务为空"); return null; } string token = "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiYWRtaW4iLCJyb2xlIjoiMSIsImlzcyI6IlNpbmV2YVNlcnZlciIsImF1ZCI6IlNpbmV2YUNsaWVudCJ9.Ujn-JuQDzA_sloITF84z1pdQvvH8DQ0DSFlhz4zP44M"; var request = new HttpRequestMessage(HttpMethod.Delete, $"http://{agvIp}/api/v1.0.0/ClearException/0"); // 单独为此请求添加 Authorization 头部 request.Headers.Add("Authorization", token); // 发送请求 var response = _httpClient.SendAsync(request).ConfigureAwait(false).GetAwaiter().GetResult(); // 读取响应内容 var httpContent = response.Content; string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); return JsonStringToEntity(result); } catch (Exception ex) { _logger.LogError($"AGV异常清楚接口异常 {ex.Message}"); return null; } } ///// ///// AGV入库接口 ///// ///// //public bool AGVInStore(string agvGuid) //{ //} ///// ///// 获取当前所有任务模板信息(目前不可用) ///// ///// //public AGVResponseEntity AGVJobRequest() //{ // if (_httpClient == null) // { // _logger.LogError("http服务为空"); // } // 序列化输入数据 // string json = Newtonsoft.Json.JsonConvert.SerializeObject(requestValue); // var context = new StringContent(json, Encoding.UTF8, "application/json"); // var httpContent = _httpClient.PostAsync($"http://192.168.10.199/api/v1.0.0/Jobs", context).ConfigureAwait(false).GetAwaiter().GetResult().Content; // string result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); // AGVResponseEntity responseEntity = JsonConvert.DeserializeObject>(result); // JToken responseValue = AirportAGVClient.InvokeT("GET:/api/v1.0.0/Positions?mapId={0}", null); // return JTokenToEntity(responseValue); //} } //string url = "http://192.168.10.199:5102/api/task/addTask"; //string json =Newtonsoft.Json.JsonConvert.SerializeObject(requestValue); //var context=new StringContent(json,Encoding.UTF8, "application/json"); //var httpContent = client1.PostAsync(url, context).ConfigureAwait(false).GetAwaiter().GetResult().Content; //var result = httpContent.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); //_logger.LogInformation(result); }