| | |
| | | using MapsterMapper; |
| | | using Microsoft.Extensions.Configuration; |
| | | using Newtonsoft.Json; |
| | | using Serilog; |
| | | using SqlSugar; |
| | | using System.Diagnostics; |
| | | using System.Diagnostics.CodeAnalysis; |
| | | using WIDESEA_Core; |
| | | using WIDESEAWCS_Common.HttpEnum; |
| | |
| | | using WIDESEAWCS_Model.Models; |
| | | using WIDESEAWCS_QuartzJob; |
| | | using WIDESEAWCS_QuartzJob.DTO; |
| | | using WIDESEAWCS_Tasks; |
| | | |
| | | namespace WIDESEAWCS_TaskInfoService |
| | | { |
| | |
| | | private readonly IMapper _mapper; |
| | | private readonly HttpClientHelper _httpClientHelper; |
| | | private readonly ITaskExecuteDetailService _taskExecuteDetailService; |
| | | private readonly ILogger _logger; |
| | | |
| | | private Dictionary<string, OrderByType> _taskOrderBy = new() |
| | | { |
| | |
| | | |
| | | public List<int> TaskRobotTypes => typeof(TaskOtherTypeEnum).GetEnumIndexList(); |
| | | |
| | | public RobotTaskService(IRobotTaskRepository BaseDal, IMapper mapper, HttpClientHelper httpClientHelper, ITaskExecuteDetailService taskExecuteDetailService) : base(BaseDal) |
| | | public RobotTaskService(IRobotTaskRepository BaseDal, IMapper mapper, HttpClientHelper httpClientHelper, ITaskExecuteDetailService taskExecuteDetailService, ILogger logger) : base(BaseDal) |
| | | { |
| | | _mapper = mapper; |
| | | _httpClientHelper = httpClientHelper; |
| | | _taskExecuteDetailService = taskExecuteDetailService; |
| | | _logger = logger; |
| | | } |
| | | |
| | | public WebResponseContent ReceiveWMSTask([NotNull] WMSTaskDTO taskDTO, StockDTO stockDTO) |
| | |
| | | Dt_RobotTask task = new Dt_RobotTask |
| | | { |
| | | RobotTaskNum = taskDTO.TaskNum, |
| | | RobotSourceAddressLineCode = stockDTO.SourceLineNo, |
| | | RobotTargetAddressLineCode = stockDTO.TargetLineNo, |
| | | RobotRoadway = stockDTO.Roadway, |
| | | RobotSourceAddressLineCode = stockDTO?.SourceLineNo ?? string.Empty, |
| | | RobotTargetAddressLineCode = stockDTO?.TargetLineNo ?? string.Empty, |
| | | RobotRoadway = stockDTO?.Roadway ?? string.Empty, |
| | | RobotSourceAddress = taskDTO.SourceAddress, |
| | | RobotTargetAddress = taskDTO.TargetAddress, |
| | | RobotSourceAddressPalletCode = stockDTO.SourcePalletNo, |
| | | RobotTargetAddressPalletCode = stockDTO.TargetPalletNo, |
| | | RobotSourceAddressPalletCode = stockDTO?.SourcePalletNo ?? string.Empty, |
| | | RobotTargetAddressPalletCode = stockDTO?.TargetPalletNo ?? string.Empty, |
| | | RobotTaskType = taskDTO.TaskType, |
| | | RobotTaskState = taskDTO.TaskStatus, |
| | | RobotGrade = taskDTO.Grade, |
| | |
| | | { |
| | | string configKey = ResolveRobotTaskConfigKey(task.TargetAddress); |
| | | StockDTO stock = BuildRobotTaskStock(task, configKey); |
| | | string requestParam = stock.ToJson(); |
| | | var stopwatch = Stopwatch.StartNew(); |
| | | |
| | | var result = _httpClientHelper.Post<WebResponseContent>(configKey, stock.ToJson()); |
| | | |
| | | var result = _httpClientHelper.Post<WebResponseContent>(configKey, requestParam); |
| | | stopwatch.Stop(); |
| | | if (!result.IsSuccess || !result.Data.Status) |
| | | { |
| | | QuartzLogHelper.LogError(_logger, $"调用WMS接口失败,接口:【{configKey}】,请求参数:【{requestParam}】,错误信息:【{result.Data?.Message}】", "RobotTaskService"); |
| | | return WebResponseContent.Instance.Error($"获取WMS系统机械手任务失败,任务号:【{task.TaskNum}】,托盘号:【{task.PalletCode}】,目标地址:【{task.TargetAddress}】,接口:【{configKey}】,错误信息:【{result.Data?.Message}】"); |
| | | } |
| | | |
| | | var wMSTask = JsonConvert.DeserializeObject<WMSTaskDTO>(result.Data.Data?.ToString() ?? string.Empty); |
| | | QuartzLogHelper.LogInfo(_logger, $"调用WMS接口成功,接口:【{configKey}】,响应数据:【{result.Data?.Data}】,耗时:{stopwatch.ElapsedMilliseconds}ms", "RobotTaskService"); |
| | | |
| | | var wMSTask = JsonConvert.DeserializeObject<WMSTaskDTO>(result?.Data?.Data?.ToString() ?? string.Empty); |
| | | if (wMSTask == null) |
| | | return WebResponseContent.Instance.Error($"获取WMS系统机械手任务失败,任务号:【{task.TaskNum}】,托盘号:【{task.PalletCode}】,错误信息:【WMS未返回有效任务数据】"); |
| | | |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 在本地直接创建机械手任务,不调用WMS接口。 |
| | | /// 根据目标地址解析任务类型,构建任务数据并写入数据库。 |
| | | /// </summary> |
| | | /// <param name="task">出库任务实体</param> |
| | | /// <returns>操作结果</returns> |
| | | public WebResponseContent CreateLocalRobotTask(Dt_Task task) |
| | | { |
| | | WebResponseContent content = new(); |
| | | try |
| | | { |
| | | // 根据目标地址解析任务类型配置键 |
| | | string configKey = ResolveRobotTaskConfigKey(task.TargetAddress); |
| | | |
| | | // 构建Stock数据 |
| | | StockDTO stock = BuildRobotTaskStock(task, configKey); |
| | | |
| | | // 获取抓取和放置地址的线体配置(如果有) |
| | | var section = App.Configuration.GetSection("RobotTaskAddressRules").GetSection(stock?.TargetLineNo ?? string.Empty).GetChildren().Select(c => c.Value).ToArray(); |
| | | if (section.Length < 2) |
| | | return WebResponseContent.Instance.Error($"未找到线体[{stock?.TargetLineNo}]的地址配置"); |
| | | |
| | | // 创建本地机器人任务 |
| | | Dt_RobotTask robotTask = new() |
| | | { |
| | | RobotTaskNum = Random.Shared.Next(), |
| | | RobotSourceAddress = section[0]!, |
| | | RobotTargetAddress = section[1]!, |
| | | RobotSourceAddressLineCode = stock?.SourceLineNo ?? string.Empty, |
| | | RobotTargetAddressLineCode = stock?.TargetLineNo ?? string.Empty, |
| | | RobotRoadway = stock?.Roadway ?? string.Empty, |
| | | RobotSourceAddressPalletCode = stock?.SourcePalletNo ?? string.Empty, |
| | | RobotTargetAddressPalletCode = stock?.TargetPalletNo ?? string.Empty, |
| | | RobotTaskType = MapConfigKeyToRobotTaskType(configKey), |
| | | RobotTaskState = (int)TaskRobotStatusEnum.RobotNew, |
| | | RobotGrade = task.Grade, |
| | | Creater = "WCS_Local", |
| | | RobotTaskTotalNum = 1, |
| | | CreateDate = DateTime.Now |
| | | }; |
| | | |
| | | BaseDal.AddData(robotTask); |
| | | |
| | | _taskExecuteDetailService.AddTaskExecuteDetail(new List<int> { robotTask.RobotTaskNum }, "本地创建机器人任务"); |
| | | |
| | | QuartzLogHelper.LogInfo(_logger, $"本地创建机器人任务成功,任务号:【{robotTask.RobotTaskNum}】,源地址:【{robotTask.RobotSourceAddress}】,目标地址:【{robotTask.RobotTargetAddress}】,任务类型:【{configKey}】", "RobotTaskService"); |
| | | |
| | | content = WebResponseContent.Instance.OK("本地创建机器人任务成功", robotTask); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | QuartzLogHelper.LogError(_logger, $"本地创建机器人任务失败,任务号:【{task.TaskNum}】,错误信息:{ex.Message}", "RobotTaskService"); |
| | | content = WebResponseContent.Instance.Error($"本地创建机器人任务失败,错误信息:{ex.Message}"); |
| | | } |
| | | return content; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 将配置键映射到机械手任务类型枚举值。 |
| | | /// </summary> |
| | | /// <param name="configKey">配置键名称</param> |
| | | /// <returns>任务类型枚举值</returns> |
| | | public int MapConfigKeyToRobotTaskType(string? configKey) |
| | | { |
| | | return configKey switch |
| | | { |
| | | nameof(ConfigKey.CreateRobotGroupPalletTask) => (int)RobotTaskTypeEnum.GroupPallet, |
| | | nameof(ConfigKey.CreateRobotSplitPalletTask) => (int)RobotTaskTypeEnum.SplitPallet, |
| | | _ => (int)RobotTaskTypeEnum.ChangePallet |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据输送线目标地址解析机械手任务接口。 |
| | | /// 规则: |
| | | /// 1. 从配置读取精确地址映射(AddressMap) |