206 lines
6.2 KiB
C#
Raw Permalink Normal View History

2025-03-16 03:17:36 +08:00
using System.Net.Http.Json;
using Microsoft.Extensions.Logging;
using Seyounth.Hyosung.Core.Agv.HikModels;
2025-03-23 13:09:36 +08:00
using Seyounth.Hyosung.Data.Entities;
2025-03-16 03:17:36 +08:00
namespace Seyounth.Hyosung.Core.Agv;
public class HikAgv(ILogger<HikAgv> logger)
{
private const string Url = "http://192.168.30.161:8181/rcms/services/rest/hikRpcService/";
private const string CancelTaskUri = "cancelTask";
private const string SchedulingTaskUri = "genAgvSchedulingTask";
private string GetRequestCode()
{
return $"Syc_{DateTimeOffset.Now.Ticks}";
}
/// <summary>
/// 取消任务
/// </summary>
/// <param name="taskCode"></param>
public async Task CancelTaskAsync(string taskCode)
{
var url = Url + CancelTaskUri;
var input = new
{
reqCode = GetRequestCode(),
taskCode
};
var result = await PostAsync<string>(url, input);
if (result is null)
{
logger.LogWarning($"task {taskCode} cancel error: result is null");
return;
}
if (result.Code != "0")
{
logger.LogWarning($"task {taskCode} cancel error: code is {result.Code}");
return;
}
logger.LogInformation($"task {taskCode} cancel succeed.");
}
2025-03-23 13:55:15 +08:00
public async Task<string> CarryToAsync(AgvPosition start, AgvPosition end, string ctnrType, int priority,
string taskType)
2025-03-16 03:17:36 +08:00
{
var url = Url + SchedulingTaskUri;
var input = new
{
reqCode = GetRequestCode(),
2025-03-19 20:29:11 +08:00
ctnrTyp = ctnrType,
taskTyp = taskType,
2025-03-16 03:17:36 +08:00
priority,
agvCode = "4083",
positionCodePath = new object[]
{
new { positionCode = start.PositionCode, type = start.Type },
new { positionCode = end.PositionCode, type = end.Type }
}
};
var rs = await PostAsync<string>(url, input);
if (rs is null)
{
2025-03-23 13:55:15 +08:00
throw new NullReferenceException($"{start.PositionCode} carry to {end.PositionCode} error: result is null");
2025-03-16 03:17:36 +08:00
}
if (rs.Code != "0")
{
2025-03-23 13:55:15 +08:00
throw new Exception(
$"{start.PositionCode} carry to {end.PositionCode} error: code is {rs.Code} message is {rs.Message}");
2025-03-16 03:17:36 +08:00
}
2025-03-23 13:55:15 +08:00
return rs.Data;
2025-03-16 03:17:36 +08:00
}
2025-03-23 13:55:15 +08:00
public async Task WaitingForTaskCompletedAsync(string taskCode)
{
while (true)
{
var input = new
{
reqCode = GetRequestCode(),
taskCodes = new List<string> { taskCode }
};
var rs = await PostAsync<List<TaskStatusModel>>(Url + "queryTaskStatus", input);
if (rs is null)
{
throw new NullReferenceException($"task {taskCode} wait completed error: result is null");
}
if (rs.Code != "0")
{
throw new Exception($"task {taskCode} wait completed error: code is {rs.Code} message is {rs.Message}");
}
// 检查是否有符合条件的任务状态为 9
var task = rs.Data.FirstOrDefault(t => t.taskCode == taskCode);
if (task is { taskStatus: "9" })
{
logger.LogInformation($"task {taskCode} wait completed succeed");
break; // 退出循环
}
// 可以添加适当的延迟,避免频繁请求
await Task.Delay(1000);
}
}
2025-03-23 13:09:36 +08:00
public async Task UnBin(AgvBinEntity bin)
{
var url = Url + "bindCtnrAndBin";
var input = new
{
reqCode = GetRequestCode(),
stgBinCode = bin.CtnrCode,
ctnrTyp = bin.HeightCode,
indBind = "0"
};
var rs = await PostAsync<string>(url, input);
if (rs is null)
{
logger.LogWarning($"agv unbin {bin.CtnrCode} error: result is null");
return;
}
if (rs.Code != "0")
{
logger.LogWarning($"agv unbin {bin.CtnrCode} error: code is {rs.Code} message:{rs.Message}");
return;
}
logger.LogInformation($"agv unbin {bin.CtnrCode} succeed");
}
2025-03-16 03:17:36 +08:00
public async Task MovingToAsync(AgvPosition des, string taskType)
{
var url = Url + SchedulingTaskUri;
var input = new
{
reqCode = GetRequestCode(),
taskType,
priority = 1,
positionCodePath =
new object[]
{
new { positionCode = des.PositionCode, type = des.Type }
}
};
var rs = await PostAsync<string>(url, input);
if (rs is null)
{
logger.LogWarning($"agv move to {des.PositionCode} error: result is null");
return;
}
if (rs.Code != "0")
{
logger.LogWarning($"agv move to {des.PositionCode} error: code is {rs.Code}");
return;
}
logger.LogInformation($"agv move to {des.PositionCode} succeed");
}
public async Task<AgvStatusInfo?> GetAgvStatus(string mapCode, string agvCode)
{
var url = "http://192.168.30.161:8181/rcms-dps/rest/queryAgvStatus";
var q = new
{
reqCode = GetRequestCode(),
mapCode
};
var rs = await PostAsync<List<AgvStatusInfo>>(url, q);
if (rs is null)
{
logger.LogWarning($"get agv {agvCode} from map {mapCode} error: result is null");
return null;
}
if (rs.Code == "0") return rs.Data.First(t => t.RobotCode == agvCode);
logger.LogWarning($"get agv {agvCode} from map {mapCode} error: code is {rs.Code}");
return null;
}
private async Task<HikApiResult<T>?> PostAsync<T>(string url, object content)
{
try
{
using var client = new HttpClient();
var json = JsonContent.Create(content);
var response = await client.PostAsync(url, json);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<HikApiResult<T>>();
}
catch (Exception e)
{
logger.LogError(e, $"post error");
return null;
}
}
}