using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Common.LocationEnum; using WIDESEAWCS_Common.TaskEnum; using WIDESEAWCS_Common; using WIDESEAWCS_Core; using WIDESEAWCS_DTO.WMS; using WIDESEAWCS_Model.Models; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_DTO; using WIDESEAWCS_DTO.AGV.HIKROBOT; using System.Diagnostics.CodeAnalysis; namespace WIDESEAWCS_TaskInfoService { public partial class TaskService { #region 海康AGV任务创建 /// /// 创建海康AGV入库任务 /// /// /// /// public WebResponseContent CreateHKNewInTask(TaskDTO taskDTO, int taskType) { WebResponseContent content = new WebResponseContent(); try { Dt_HKLocationInfo? hKLocationInfo = null; #region 点到点 if (!string.IsNullOrEmpty(taskDTO.toLocationCode)) { hKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.toLocationCode) ?? throw new Exception($"未找到终点货位【{taskDTO.toLocationCode}】"); if (hKLocationInfo.LocationStatus != (int)LocationStatusEnum.Free) throw new Exception($"终点货位【{taskDTO.toLocationCode}】货位状态不为空货位"); } #endregion //获取货位信息 else hKLocationInfo = _hKLocationInfoService.GetFreeLocationInfo(taskDTO.toAreaCode) ?? throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!"); Dt_Task dt_Task = new Dt_Task() { TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)), WMSTaskNum = taskDTO.taskCode, //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)), Grade = taskDTO.taskPriority, PalletCode = taskDTO.containerCode, Roadway = hKLocationInfo.RoadwayNo, TaskState = TaskStatusEnum.New.ObjToInt(), TaskType = taskType, SourceAddress = taskDTO.fromLocationCode, CurrentAddress = taskDTO.fromLocationCode, NextAddress = hKLocationInfo.LocationCode, TargetAddress = hKLocationInfo.LocationCode, Creater = "WMS", }; hKLocationInfo.LocationStatus = LocationStatusEnum.InLock.ObjToInt(); #region 下发海康AGV任务 content = SendHIKROBOTTask(dt_Task); if (!content.Status) throw new Exception(content.Message); dt_Task.TaskState = (int)TaskStatusEnum.Execut; dt_Task.Dispatchertime = DateTime.Now; #endregion try { Db.Ado.BeginTran(); BaseDal.AddData(dt_Task); _hKLocationInfoService.Repository.UpdateData(hKLocationInfo); Db.Ado.CommitTran(); } catch (Exception ex) { Db.Ado.RollbackTran(); throw new Exception(ex.Message); } content.OK(data: new { taskCode = taskDTO.taskCode, Message = "成功!" }); } catch (Exception ex) { content.Data = new { taskCode = taskDTO.taskCode, Message = $"失败!{ex.Message}" }; content.Error(ex.Message); } return content; } /// /// 创建海康AGV出库任务 /// /// /// /// public WebResponseContent CreateHKNewOutTask(TaskDTO taskDTO, int taskType) { WebResponseContent content = new WebResponseContent(); try { Dt_HKLocationInfo? hKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!"); if (hKLocationInfo.LocationStatus != LocationStatusEnum.InStock.ObjToInt()) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】当前库位状态不可出库!"); if (hKLocationInfo.PalletCode != taskDTO.containerCode) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】绑定料箱号【{hKLocationInfo.PalletCode}】与任务料箱号【{taskDTO.containerCode}】不匹配!"); Dt_Task dt_Task = new Dt_Task() { TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)), WMSTaskNum = taskDTO.taskCode, //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)), Grade = taskDTO.taskPriority, PalletCode = taskDTO.containerCode, Roadway = hKLocationInfo.RoadwayNo, TaskState = TaskStatusEnum.New.ObjToInt(), TaskType = taskType, SourceAddress = taskDTO.fromLocationCode, CurrentAddress = taskDTO.fromLocationCode, NextAddress = taskDTO.toLocationCode, TargetAddress = taskDTO.toLocationCode, Creater = "WMS", }; hKLocationInfo.LocationStatus = LocationStatusEnum.OutLock.ObjToInt(); #region 下发海康AGV任务 content = SendHIKROBOTTask(dt_Task); if (!content.Status) throw new Exception(content.Message); dt_Task.TaskState = (int)TaskStatusEnum.Execut; dt_Task.Dispatchertime = DateTime.Now; #endregion try { Db.Ado.BeginTran(); BaseDal.AddData(dt_Task); _hKLocationInfoService.Repository.UpdateData(hKLocationInfo); Db.Ado.CommitTran(); } catch (Exception ex) { Db.Ado.RollbackTran(); throw new Exception(ex.Message); } return content.OK(data: new { taskCode = taskDTO.taskCode, Message = "成功!" }); } catch (Exception ex) { content.Data = new { taskCode = taskDTO.taskCode, Message = $"失败!{ex.Message}" }; return content.Error(ex.Message); } } #endregion #region 下发海康AGV任务 public WebResponseContent SendHIKROBOTTask([NotNull] Dt_Task task) { WebResponseContent content = new WebResponseContent(); HIKROBOTReturn hIKROBOTReturn = null; HIKROBOTTaskSubmit hIKROBOTTaskSubmit = null; try { Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(HIKROBOTTaskSubmit)) ?? throw new Exception("未找到海康AGV任务下发接口配置信息!请检查接口配置"); #region 实体类转换 hIKROBOTTaskSubmit = HIKROBOTTask(task); if (hIKROBOTTaskSubmit == null) throw new Exception("海康AGV任务实体转换失败"); #endregion string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskSubmit.Serialize()); hIKROBOTReturn = response.DeserializeObject(); if (hIKROBOTReturn.code == "SUCCESS") { var data = hIKROBOTReturn.data.ToString().DeserializeObject(); content.OK(data: data); } else { throw new Exception(hIKROBOTReturn.message); } } catch (Exception ex) { content.Error(ex.Message); } finally { _trackloginfoService.AddTrackLog(hIKROBOTTaskSubmit, content, "下发海康AGV任务", "", hIKROBOTReturn.message); } return content; } #endregion /// /// 海康任务实体类转换 /// /// /// public HIKROBOTTaskSubmit HIKROBOTTask(Dt_Task task) { HIKROBOTTaskSubmit hIKROBOTTaskSubmit = new HIKROBOTTaskSubmit(); try { var tasktype = (TaskTypeEnum)Enum.GetValues(typeof(TaskTypeEnum)).GetValue(task.TaskType); hIKROBOTTaskSubmit.initPriority = task.Grade; hIKROBOTTaskSubmit.robotTaskCode = task.WMSTaskNum; hIKROBOTTaskSubmit.taskType = tasktype.ToString(); ExtraDto extraDto = new ExtraDto(); CarrierInfoDto carrierInfoDto = new CarrierInfoDto() { carrierCode = task.PalletCode, carrierType = task.PalletCode.Contains("LXM") ? "DX" : "SX" }; extraDto.carrierInfo.Add(carrierInfoDto); hIKROBOTTaskSubmit.extra = extraDto; switch (tasktype) { case TaskTypeEnum.CPInbound: case TaskTypeEnum.CPOutbound: case TaskTypeEnum.MLInbound: case TaskTypeEnum.MLOutbound: break; case TaskTypeEnum.F01: break; case TaskTypeEnum.RK3F: case TaskTypeEnum.CK3F: case TaskTypeEnum.F02: { TargetRouteDto target = new TargetRouteDto() { code = task.CurrentAddress, operation = "DELIVERY",//取货 seq = 0, type = "SITE", }; TargetRouteDto targetRoute = new TargetRouteDto() { code = task.NextAddress, operation = "DELIVERY",//送货 seq = 1, type = "SITE" }; hIKROBOTTaskSubmit.targetRoute.Add(target); hIKROBOTTaskSubmit.targetRoute.Add(targetRoute); } break; case TaskTypeEnum.Q1TSJ4: { TargetRouteDto target = new TargetRouteDto() { code = task.CurrentAddress, operation = "DELIVERY",//取货 seq = 0, type = "SITE", }; TargetRouteDto targetRoute = new TargetRouteDto() { code = task.NextAddress, operation = "DELIVERY",//送货 seq = 1, type = "STORAGE" }; hIKROBOTTaskSubmit.targetRoute.Add(target); hIKROBOTTaskSubmit.targetRoute.Add(targetRoute); } break; case TaskTypeEnum.STU0003: case TaskTypeEnum.CHUKU1: { TargetRouteDto target = new TargetRouteDto() { code = task.CurrentAddress, operation = "DELIVERY",//取货 seq = 0, type = "STORAGE", }; TargetRouteDto targetRoute = new TargetRouteDto() { code = task.NextAddress, operation = "DELIVERY",//送货 seq = 1, type = "SITE", }; hIKROBOTTaskSubmit.targetRoute.Add(target); hIKROBOTTaskSubmit.targetRoute.Add(targetRoute); } break; case TaskTypeEnum.Q3RK: break; case TaskTypeEnum.Q3CK: break; case TaskTypeEnum.Move: break; case TaskTypeEnum.CPMoveInventory: break; default: break; } } catch (Exception ex) { } return hIKROBOTTaskSubmit; } } }