using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.Redis;
using ServiceCenter.WebApi;
using WCS.WorkEngineering.WebApi.Models.AGV;
using WCS.WorkEngineering.WebApi.Models.AGV.Request;
using WCS.WorkEngineering.WebApi.Models.AGV.Response;
using WCS.WorkEngineering.WebApi.Models.WCS.Response;
using WCS.WorkEngineering.WebApi.Models.WMS.Response;
namespace WCS.WorkEngineering.WebApi.Controllers
{
///
/// AGV接口
///
public static class AgvApi
{
private static string _AgvUrl = null!;
///
/// AGV地址
///
public static string AgvUrl
{
get
{
_AgvUrl ??= RedisHub.Default.Check("AgvUrl")!;
if (string.IsNullOrEmpty(_AgvUrl))
{
throw new KnownException($"请在Redis配置AgvUrl", LogLevelEnum.High);
}
return _AgvUrl;
}
}
#region 任务单生成
///
/// 测试路径
///
/// 取货机台
/// WMS任务号
///
public static GenAgvSchedulingTaskResponse 测试路径(string position, string taskCode)
{
return GenAgvSchedulingTask("iwms_third", "", "4", new List()
{
new positionCodeClass(){ //取货机台
positionCode=position,
type="00"
},
new positionCodeClass(){ //巷道分配点
positionCode="LX002",
type="00"
},
new positionCodeClass(){ //预分配放货点
positionCode="2501",
type="00"
}
}, "1", taskCode, "LX03", "1");
}
///
/// 机台叫料
///
/// 起始点
/// 目标点
/// WMS任务号
///
public static GenAgvSchedulingTaskResponse 机台叫料(string SPosition, string EPosition, string taskCode)
{
return GenAgvSchedulingTask("iwms_third", "", "4", new List()
{
new positionCodeClass(){ //取货机台
positionCode=SPosition,
type="00"
},
new positionCodeClass(){ //巷道分配点
positionCode=EPosition,
type="00"
}
}, "1", taskCode, "ZTGT35", "1");
}
///
/// 托盘回库
///
/// 取货机台
/// WMS任务号
/// 站台
///
public static GenAgvSchedulingTaskResponse 托盘回库(string position, string taskCode, string station)
{
var position1 = "";
var position2 = "";
switch (station)
{
case "2501": //分拣一北
position1 = "LX002";
position2 = "2501";
break;
case "2701": //分拣一南
position1 = "LX004";
position2 = "2701";
break;
case "2901": //分拣二北
position1 = "LX006";
position2 = "2901";
break;
case "3101": //分拣二南
position1 = "LX008";
position2 = "3101";
break;
case "3301": //分拣三北
position1 = "LX010";
position2 = "3301";
break;
case "3501": //分拣三南
position1 = "LX012";
position2 = "3501";
break;
}
return GenAgvSchedulingTask("iwms_third", "", "4", new List()
{
new positionCodeClass(){ //取货机台
positionCode=position,
type="00"
},
new positionCodeClass(){ //巷道分配点
positionCode=position1,
type="00"
},
new positionCodeClass(){ //预分配放货点
positionCode=position2,
type="00"
}
}, "1", taskCode, "ZTGT31", "1");
}
///
/// 满托入库
///
/// 取货站台
/// WMS任务号
/// 放货站台
///
public static GenAgvSchedulingTaskResponse 满托入库(string startPosition, string taskCode, string station)
{
var position = "";
var position1 = "";
position = startPosition;
position1 = station;
return GenAgvSchedulingTask("iwms_third", "", "4", new List()
{
new positionCodeClass(){ //取货机台
positionCode=position,
type="00"
},
new positionCodeClass(){ //预分配放货点
positionCode=position1,
type="00"
}
}, "1", taskCode, "ZTGT69", "1");
}
///
/// Agv任务单生成接口
///
/// 客户端编号
/// 容器编号
/// 容器类型
/// 路径
/// 优先级
/// 任务单号
/// 任务类型
/// 合金任务模板
///
///
public static GenAgvSchedulingTaskResponse GenAgvSchedulingTask(string clienCode, string ctnrCode, string ctnrTyp, List positionCodePath, string priority, string taskCode, string taskType, string hjTaskTy)
{
var res = APICaller.CallApi2(AgvUrl + "/rcms/services/rest/hikRpcService/genAgvSchedulingTask", new GenAgvSchedulingTaskRequest
{
clientCode = clienCode,
ctnrCode = ctnrCode,
ctnrTyp = ctnrTyp,
interfaceName = "genAgvSchedulingTask",
positionCodePath = positionCodePath,
priority = priority,
reqCode = Guid.NewGuid().ToString().Replace("-", ""),
taskCode = taskCode,
taskTyp = taskType,
hjTaskTy = hjTaskTy,
tokenCode = "56898661ea976b748f328cefa6960434",
});
if (res.code != AgvResponseCode.Success)
{
if (res.message.Contains("未知的呼叫站点"))
{
if (taskType == "ZTGT35")
{
res.message = res.message + positionCodePath[1].positionCode;
}
else if (taskType == "ZTGT31")
{
res.message = res.message + positionCodePath[0].positionCode;
}
}
throw new KnownException($"AGV返回错误:{res.message}", LogLevelEnum.High);
}
return res;
}
#endregion 任务单生成
#region 继续执行任务
///
/// 继续执行任务
///
/// AGV任务号
/// 下一个地址
/// 接口返回结果
///
public static ContinueTaskResponse ContinueTask(string taskCode, string nextPositionCode)
{
var res = APICaller.CallApi2(AgvUrl + "/rcms/services/rest/hikRpcService/continueTask", new ContinueTaskRequest
{
reqCode = Guid.NewGuid().ToString().Replace("-", ""),
taskCode = taskCode,
nextPositionCode = new positionCodeClass()
{
positionCode = nextPositionCode,
type = "00"
}
});
if (res.message.Contains("状态出错"))
{
res.message = "任务" + taskCode + res.message + "请检查对应是否有误";
}
if (res.code != AgvResponseCode.Success)
{
throw new KnownException(res.message, LogLevelEnum.High);
}
return res;
}
#endregion 继续执行任务
///
/// 取消任务
///
/// AGV任务号
///
///
public static CancelTaskResponse CancelAgvTask(string AGVtaskCode)
{
var res = APICaller.CallApi2(AgvUrl + "/rcms/services/rest/hikRpcService/cancelTask", new CancelTaskRequest
{
reqCode = Guid.NewGuid().ToString().Replace("-", ""),
taskCode = AGVtaskCode,
forceCancel = "0",
});
if (res.code != AgvResponseCode.Success)
{
throw new KnownException(res.message, LogLevelEnum.High);
}
return res;
}
///
/// AGV取消任务验证
///
///
///
/// 需要取消任务的AGV任务号
///
public static CancelTaskResponse? CancelAgvTask(SRes sRes, int id, string agvTask)
{
try
{
var res = AgvApi.CancelAgvTask(agvTask);
return res;
}
catch (Exception ex)
{
sRes.ResDataList.Add(new HandleTaskResponse()
{
IsSuccess = false,
TaskNo = id,
Message = $"AGV错误:{ex.Message}",
});
return null;
}
}
}
}