238 lines
8.7 KiB
C#
238 lines
8.7 KiB
C#
using System;
|
||
using System.Diagnostics;
|
||
using System.Threading.Tasks;
|
||
using HHWMS.Web.Models;
|
||
using HHWMS.Web.Models.Agv;
|
||
using Microsoft.Extensions.Logging;
|
||
using Microsoft.Extensions.Options;
|
||
using Newtonsoft.Json;
|
||
using WalkingTec.Mvvm.Core;
|
||
|
||
namespace HHWMS.Web.Services.Agv
|
||
{
|
||
/// <summary>
|
||
/// AGV 工具类:封装与 AGV 调度系统的全部 HTTP 通信(方案 11.5)
|
||
/// 职责:显式超时、报文留痕(AgvTaskLog)、响应统一解析、失败抛业务异常
|
||
/// </summary>
|
||
public class AgvApiClient
|
||
{
|
||
private readonly AgvOption _option;
|
||
private readonly WTMContext _wtm;
|
||
private readonly IUnitWork _unitWork;
|
||
private readonly ILogger<AgvApiClient> _logger;
|
||
|
||
public AgvApiClient(IOptions<AgvOption> option, WTMContext wtm, IUnitWork unitWork, ILogger<AgvApiClient> logger)
|
||
{
|
||
_option = option.Value;
|
||
_wtm = wtm;
|
||
_unitWork = unitWork;
|
||
_logger = logger;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下发:创建运单(方案 11.2)。成功返回 orderId(=TaskNo);失败抛异常
|
||
/// </summary>
|
||
public async Task<AgvCreateOrderResult> CreateOrderAsync(AgvCreateOrderRequest request, string taskNo)
|
||
{
|
||
// Mock 模式:不联网,直接返回约定成功报文
|
||
if (_option.MockEnabled)
|
||
{
|
||
WriteLog(taskNo, "down", "create", request, new AgvApiResponse<AgvCreateOrderResult>
|
||
{
|
||
Code = "0",
|
||
Msg = "mock success",
|
||
Data = new AgvCreateOrderResult { OrderId = taskNo, ExternalId = request.ExternalId }
|
||
}, 0, null);
|
||
return new AgvCreateOrderResult { OrderId = taskNo, ExternalId = request.ExternalId };
|
||
}
|
||
|
||
var sw = Stopwatch.StartNew();
|
||
try
|
||
{
|
||
var result = await _wtm.CallAPI<AgvApiResponse<AgvCreateOrderResult>>(
|
||
"AGV", "api/fleet/orders/create", HttpMethodEnum.POST, request, _option.CallTimeoutSeconds);
|
||
sw.Stop();
|
||
WriteLog(taskNo, "down", "create", request, result, sw.ElapsedMilliseconds, null);
|
||
if (result?.Data == null || result.Data.Code != "0" || result.Data.Data == null)
|
||
{
|
||
throw new Exception($"AGV下发失败: {result?.Data?.Msg ?? result?.ErrorMsg ?? "无响应"}");
|
||
}
|
||
return result.Data.Data;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "AGV下发任务[{taskNo}]异常", taskNo);
|
||
throw;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取消运单(方案 11.6)。sceneId 与 orderId 均必填,orderId 即 TaskNo;厂商侧幂等
|
||
/// </summary>
|
||
public async Task CancelOrderAsync(string sceneId, string orderId)
|
||
{
|
||
var request = new AgvCancelOrderRequest { SceneId = sceneId, OrderId = orderId };
|
||
if (_option.MockEnabled)
|
||
{
|
||
WriteLog(orderId, "down", "cancel", request, new AgvApiResponse<object>
|
||
{
|
||
Code = "0",
|
||
Msg = "mock success"
|
||
}, 0, null);
|
||
return;
|
||
}
|
||
|
||
var sw = Stopwatch.StartNew();
|
||
try
|
||
{
|
||
var result = await _wtm.CallAPI<AgvApiResponse<object>>(
|
||
"AGV", "api/fleet/orders/cancel", HttpMethodEnum.POST, request, _option.CallTimeoutSeconds);
|
||
sw.Stop();
|
||
WriteLog(orderId, "down", "cancel", request, result, sw.ElapsedMilliseconds, null);
|
||
if (result?.Data == null || result.Data.Code != "0")
|
||
{
|
||
throw new Exception($"AGV取消失败: {result?.Data?.Msg ?? result?.ErrorMsg ?? "无响应"}");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "AGV取消任务[{orderId}]异常", orderId);
|
||
throw;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询运单状态(11.3 查询式兜底用;接口待厂商确认,Mock 直接返回执行中)
|
||
/// </summary>
|
||
public async Task<AgvOrderStatusResult> QueryOrderAsync(string sceneId, string orderId)
|
||
{
|
||
if (_option.MockEnabled)
|
||
{
|
||
return new AgvOrderStatusResult { OrderId = orderId, Status = "Running" };
|
||
}
|
||
|
||
var request = new AgvCancelOrderRequest { SceneId = sceneId, OrderId = orderId };
|
||
var sw = Stopwatch.StartNew();
|
||
try
|
||
{
|
||
var result = await _wtm.CallAPI<AgvApiResponse<AgvOrderStatusResult>>(
|
||
"AGV", "api/fleet/orders/status", HttpMethodEnum.POST, request, _option.CallTimeoutSeconds);
|
||
sw.Stop();
|
||
WriteLog(orderId, "down", "query", request, result, sw.ElapsedMilliseconds, null);
|
||
if (result?.Data == null || result.Data.Code != "0" || result.Data.Data == null)
|
||
{
|
||
throw new Exception($"AGV查询失败: {result?.Data?.Msg ?? result?.ErrorMsg ?? "无响应"}");
|
||
}
|
||
return result.Data.Data;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "AGV查询任务[{orderId}]异常", orderId);
|
||
throw;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 报文留痕:原始请求/响应 JSON + 耗时统一写入 AgvTaskLog
|
||
/// </summary>
|
||
private void WriteLog(string taskNo, string direction, string @event,
|
||
object request, object response, long durationMs, string remark)
|
||
{
|
||
try
|
||
{
|
||
var log = new AgvTaskLog
|
||
{
|
||
TaskNo = taskNo,
|
||
OrderId = taskNo,
|
||
Direction = direction,
|
||
Event = @event,
|
||
DurationMs = (int)durationMs,
|
||
RequestJson = request == null ? null : JsonConvert.SerializeObject(request),
|
||
ResponseJson = response == null ? null : JsonConvert.SerializeObject(response),
|
||
Operator = "AGV",
|
||
Remark = remark,
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = "AGV"
|
||
};
|
||
_unitWork.Add(log);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// 留痕失败不能阻断主流程,仅记录日志
|
||
_logger.LogWarning(ex, "AGV报文留痕失败 taskNo={taskNo}", taskNo);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// AGV 接口统一响应包装(code 非 "0" 即失败,msg 为失败原因)
|
||
/// </summary>
|
||
public class AgvApiResponse<T>
|
||
{
|
||
public string Code { get; set; }
|
||
public string Msg { get; set; }
|
||
public T Data { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下发创建运单请求(方案 11.2 报文)
|
||
/// </summary>
|
||
public class AgvCreateOrderRequest
|
||
{
|
||
public string Id { get; set; }
|
||
public string SceneId { get; set; }
|
||
public string SceneName { get; set; }
|
||
public string ExternalId { get; set; }
|
||
public int? Priority { get; set; }
|
||
public string ContainerId { get; set; }
|
||
public bool StepFixed { get; set; } = true;
|
||
public System.Collections.Generic.List<AgvOrderStep> Steps { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下发步骤(起点取货 Load / 终点放货 Unload)
|
||
/// </summary>
|
||
public class AgvOrderStep
|
||
{
|
||
public string Location { get; set; }
|
||
public AgvStepRbkArgs RbkArgs { get; set; }
|
||
public bool ForLoad { get; set; }
|
||
public bool ForUnload { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 步骤动作参数(binTask 取货/放货动作名)
|
||
/// </summary>
|
||
public class AgvStepRbkArgs
|
||
{
|
||
public string BinTask { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下发成功响应(orderId = TaskNo,无需落库,仅日志留痕)
|
||
/// </summary>
|
||
public class AgvCreateOrderResult
|
||
{
|
||
public string OrderId { get; set; }
|
||
public string ExternalId { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取消/查询请求(sceneId 与 orderId 均必填)
|
||
/// </summary>
|
||
public class AgvCancelOrderRequest
|
||
{
|
||
public string SceneId { get; set; }
|
||
public string OrderId { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 运单状态查询结果(11.3 查询式兜底用;字段待厂商确认)
|
||
/// </summary>
|
||
public class AgvOrderStatusResult
|
||
{
|
||
public string OrderId { get; set; }
|
||
public string Status { get; set; }
|
||
}
|
||
}
|