You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
7.6 KiB
C#

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<AirPorthttpClient> _logger;
public AirPorthttpClient(AppConfig appConfig, ILogger<AirPorthttpClient> 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;
}
/// <summary>
/// AGV下发任务请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <returns></returns>
public static AGVResponseEntity<ResponseAddTaskDataEntity> AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue)
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/addTask", null, requestValue);
if (responseValue == null)
{
return new AGVResponseEntity<ResponseAddTaskDataEntity>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
{
return new AGVResponseEntity<ResponseAddTaskDataEntity>();
}
AGVResponseEntity<ResponseAddTaskDataEntity> responseEntity;
responseEntity = JsonSerializer.Deserialize<AGVResponseEntity<ResponseAddTaskDataEntity>>(json);
return responseEntity;
}
/// <summary>
/// 取消/终止任务请求
/// </summary>
/// <returns></returns>
public static AGVResponseEntity<object> AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue)
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/operationalTask", null, requestValue);
if (responseValue == null)
{
return new AGVResponseEntity<object>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
{
return new AGVResponseEntity<object>();
}
AGVResponseEntity<object> responseEntity;
responseEntity = JsonSerializer.Deserialize<AGVResponseEntity<object>>(json);
return responseEntity;
}
/// <summary>
/// 查询任务状态请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <returns></returns>
public static AGVResponseEntity<ResponseTaskStateDetailDataEntity> AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue)
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("PUT:/api/task/getTaskStateDetail", null, requestValue);
if (responseValue == null)
{
return new AGVResponseEntity<ResponseTaskStateDetailDataEntity>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
{
return new AGVResponseEntity<ResponseTaskStateDetailDataEntity>();
}
AGVResponseEntity<ResponseTaskStateDetailDataEntity> ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<ResponseTaskStateDetailDataEntity>>(json);
return ResponseEntity;
}
/// <summary>
/// 获取所有AGV信息请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <returns></returns>
public static AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>> AGVAllStateRequest()
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/task/robot/getRobot", null);
if (responseValue == null)
{
return new AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
{
return new AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>>();
}
AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>> ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>>>(json);
return ResponseEntity;
}
/// <summary>
/// 获取当前激活地图信息请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <returns></returns>
public static AGVResponseEntity<ResponseActiveMapDataEntity> AGVMapActiveRequest()
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Maps/mapActive", null);
if (responseValue == null)
{
return new AGVResponseEntity<ResponseActiveMapDataEntity>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
{
return new AGVResponseEntity<ResponseActiveMapDataEntity>();
}
AGVResponseEntity<ResponseActiveMapDataEntity> ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<ResponseActiveMapDataEntity>>(json);
return ResponseEntity;
}
/// <summary>
/// 获取当前地图所有位置点请求
/// </summary>
/// <param name="aGVStateRequest"></param>
/// <returns></returns>
public static AGVResponseEntity<List<ResponseMapPositionDataEntity>> AGVMapPositionRequest(string requestValue)
{
JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue);
if (responseValue == null)
{
return new AGVResponseEntity<List<ResponseMapPositionDataEntity>>();
}
string json = responseValue.ToString();
if (string.IsNullOrEmpty(json))
{
return new AGVResponseEntity<List<ResponseMapPositionDataEntity>>();
}
AGVResponseEntity<List<ResponseMapPositionDataEntity>> ResponseEntity;
ResponseEntity = JsonSerializer.Deserialize<AGVResponseEntity<List<ResponseMapPositionDataEntity>>>(json);
return ResponseEntity;
}
}
}