yanjinhui
2 天以前 0a62180f002d8a601b6b3c46af3a6b4583f9acf6
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/HKTaskMethods.cs
@@ -10,6 +10,11 @@
using WIDESEAWCS_DTO.WMS;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO;
using WIDESEAWCS_DTO.AGV.HIKROBOT;
using System.Diagnostics.CodeAnalysis;
using System.Reflection.PortableExecutable;
using WIDESEAWCS_QuartzJob;
namespace WIDESEAWCS_TaskInfoService
{
@@ -25,12 +30,41 @@
        /// <returns></returns>
        public WebResponseContent CreateHKNewInTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int containerType = taskDTO.containerCode.Contains("LLM") ? LocationTypeEnum.LargePallet.ObjToInt() : LocationTypeEnum.SmallPallet.ObjToInt();//这里我不知道是什么类型
                Dt_HKLocationInfo? hKLocationInfo = null;
                if (taskType==(int)TaskTypeEnum.STURR)
                {
                    var reslut=_stationInfo.Repository.QueryFirst(x=>x.StationCode==taskDTO.fromLocationCode) ?? throw new Exception($"未找到起点货位【{taskDTO.fromLocationCode}】");
                }
                #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
                //获取货位信息
                Dt_HKLocationInfo? kLSLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.WarehouseId.ToString() == taskDTO.toAreaCode && x.EnableStatus == EnableStatusEnum.Normal.ObjToInt() && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && x.LocationType == containerType);
                if (kLSLocationInfo == null) throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!");
                else
                    hKLocationInfo = _hKLocationInfoService.GetFreeLocationInfo(taskDTO.toAreaCode) ?? throw new Exception($"未找到终点库区【{taskDTO.toAreaCode}】可用空货位!");
                if (taskType == (int)TaskTypeEnum.Q1TSJ4)
                {
                    var device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "TSJ") as OtherDevice;
                    if (device.IsConnected)
                    {
                        var reslu = device.GetValue<HoistEnum, string>(HoistEnum.Emptycontainernumber);
                        var result = reslu?.TrimEnd('\0')?.Substring(2);
                        if (reslu == taskDTO.containerCode && result != null)
                        {
                            throw new Exception($"当前的料箱号为{reslu}与{taskDTO.containerCode}料箱号不匹配");
                        }
                    }
                    else
                    {
                        throw new Exception("提升机信息设备连接失败");
                    }
                }
                Dt_Task dt_Task = new Dt_Task()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
@@ -38,27 +72,52 @@
                    //WMSId = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Grade = taskDTO.taskPriority,
                    PalletCode = taskDTO.containerCode,
                    Roadway = kLSLocationInfo.RoadwayNo,
                    Roadway = hKLocationInfo.RoadwayNo,
                    TaskState = TaskStatusEnum.New.ObjToInt(),
                    TaskType = taskType,
                    SourceAddress = taskDTO.fromLocationCode,
                    CurrentAddress = taskDTO.fromLocationCode,
                    NextAddress = kLSLocationInfo.LocationCode,
                    TargetAddress = kLSLocationInfo.LocationCode,
                    NextAddress = hKLocationInfo.LocationCode,
                    TargetAddress = hKLocationInfo.LocationCode,
                    Creater = "WMS",
                };
                kLSLocationInfo.LocationStatus = LocationStatusEnum.InLock.ObjToInt();
                Db.Ado.BeginTran();
                BaseDal.AddData(dt_Task);
                _hKLocationInfoService.Repository.UpdateData(kLSLocationInfo);
                Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK();
                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)
            {
                Db.Ado.RollbackTran();
                throw new Exception(ex.Message);
                content.Data = new
                {
                    taskCode = taskDTO.taskCode,
                    Message = $"失败!{ex.Message}"
                };
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// åˆ›å»ºæµ·åº·AGV出库任务
@@ -68,12 +127,23 @@
        /// <returns></returns>
        public WebResponseContent CreateHKNewOutTask(TaskDTO taskDTO, int taskType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_HKLocationInfo? hKLocationInfo = _hKLocationInfoService.Repository.QueryFirst(x => x.LocationCode == taskDTO.fromLocationCode);
                if (hKLocationInfo == null) throw new Exception($"未找到起点库位【{taskDTO.fromLocationCode}】!");
                //4楼库内到提升机
                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}】当前库位状态不可出库!");
                Dt_Task dt_Task = new Dt_Task()
                if (hKLocationInfo.PalletCode != taskDTO.containerCode) throw new Exception($"起点库位【{taskDTO.fromLocationCode}】绑定料箱号【{hKLocationInfo.PalletCode}】与任务料箱号【{taskDTO.containerCode}】不匹配!");
                //库内到现边的点到点任务,是否需要判断货位状态?
                if (taskType == (int)TaskTypeEnum.STU0003)
                {
                    var reslut = _stationInfo.Repository.QueryFirst(x => x.StationCode == taskDTO.toLocationCode) ?? throw new Exception($"未找到终点货位【{taskDTO.toLocationCode}】");
                    if (reslut.StationStatus != (int)LocationStatusEnum.Free)
                    {
                        throw new Exception($"终点站台【{taskDTO.toLocationCode}】状态不为空闲!");
                    }
                }
                    Dt_Task dt_Task = new Dt_Task()
                {
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    WMSTaskNum = taskDTO.taskCode,
@@ -90,18 +160,301 @@
                    Creater = "WMS",
                };
                hKLocationInfo.LocationStatus = LocationStatusEnum.OutLock.ObjToInt();
                Db.Ado.BeginTran();
                BaseDal.AddData(dt_Task);
                _hKLocationInfoService.Repository.UpdateData(hKLocationInfo);
                Db.Ado.CommitTran();
                return WebResponseContent.Instance.OK();
                #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)
            {
                Db.Ado.RollbackTran();
                throw new Exception(ex.Message);
                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任务实体转换失败");
                var json = hIKROBOTTaskSubmit.Serialize();
                #endregion
                // åˆ›å»ºHeaders字典
                var headers = new Dictionary<string, string>
                {
                    { "X-lr-request-id", DateTimeOffset.Now.ToUnixTimeSeconds().ToString()+task.TaskNum }
                    // å¦‚果需要其他Header,可以继续添加
                    // { "Content-Type", "application/json" }
                };
                // ä¼ é€’Headers参数
                string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskSubmit.Serialize(), headers: headers);
                //string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskSubmit.Serialize());
                hIKROBOTReturn = response.DeserializeObject<HIKROBOTReturn>();
                if (hIKROBOTReturn.code == "SUCCESS")
                {
                    var data = hIKROBOTReturn.data.ToString().DeserializeObject<HIKROBOTReturnData>();
                    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
        /// <summary>
        /// æµ·åº·ä»»åŠ¡å®žä½“ç±»è½¬æ¢
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public HIKROBOTTaskSubmit HIKROBOTTask(Dt_Task task)
        {
            HIKROBOTTaskSubmit hIKROBOTTaskSubmit = new HIKROBOTTaskSubmit();
            try
            {
                var tasktype = (TaskTypeEnum)Enum.GetValues(typeof(TaskTypeEnum)).GetValue(task.TaskType - 1);
                hIKROBOTTaskSubmit.initPriority = task.Grade;
                hIKROBOTTaskSubmit.robotTaskCode = task.WMSTaskNum;
                hIKROBOTTaskSubmit.taskType = tasktype.ToString(); ExtraDto extraDto = new ExtraDto();
                if (task.TaskType != (int)TaskTypeEnum.MOVE && task.TaskType != (int)TaskTypeEnum.Q3RK && task.TaskType != (int)TaskTypeEnum.Q3CK && task.TaskType != (int)TaskTypeEnum.F01)
                {
                    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.RK3F:
                    case TaskTypeEnum.CK3F:
                    case TaskTypeEnum.F02:
                    case TaskTypeEnum.F03:
                    case TaskTypeEnum.F04:
                        {
                            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.STURR:
                    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:
                    case TaskTypeEnum.Q3CK:
                    case TaskTypeEnum.MOVE:
                    case TaskTypeEnum.F01:
                        {
                            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.STUPTBY:
                        {
                            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 = "STORAGE",
                            };
                            hIKROBOTTaskSubmit.targetRoute.Add(target);
                            hIKROBOTTaskSubmit.targetRoute.Add(targetRoute);
                        }
                        break;
                    case TaskTypeEnum.CPMoveInventory:
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
            }
            return hIKROBOTTaskSubmit;
        }
        /// <summary>
        /// æµ·åº·AGV任务继续执行
        /// </summary>
        /// <param name="TaskCode">任务号</param>
        /// <returns></returns>
        public WebResponseContent Hikvisiontaskscontinue(string TaskCode, string Address = null)
        {
            WebResponseContent content = new WebResponseContent();
            HIKROBOTReturn hIKROBOTReturn = null;
            HIKROBOTTaskContinue hIKROBOTTaskContinue = null;
            try
            {
                // 1. å‚数验证
                if (string.IsNullOrWhiteSpace(TaskCode))
                {
                    throw new Exception("任务编码不能为空");
                }
                Dt_ApiInfo? apiInfo = _apiInfoService.Repository.QueryFirst(x => x.ApiCode == nameof(HIKROBOTTaskContinue)) ?? throw new Exception("未找到海康AGV继续执行接口配置信息!请检查接口配置");
                hIKROBOTTaskContinue = new HIKROBOTTaskContinue()
                {
                    triggerCode = TaskCode,
                    triggerType = "TASK",//固定值
                    targetRoute = string.IsNullOrEmpty(Address) ? null :
                    new TargetRouteDto()
                    {
                        code = Address,
                        type = "SITE",
                    }
                };
                var headers = new Dictionary<string, string>
                {
                    { "X-lr-request-id", DateTimeOffset.Now.ToUnixTimeSeconds().ToString()+TaskCode }
                };
                string response = HttpHelper.Post(apiInfo.ApiAddress, hIKROBOTTaskContinue.Serialize(), headers: headers);
                hIKROBOTReturn = response.DeserializeObject<HIKROBOTReturn>();
                if (hIKROBOTReturn.code == "SUCCESS")
                {
                    var data = hIKROBOTReturn.data.ToString().DeserializeObject<HIKROBOTReturnData>();
                    content.OK(data: data);
                }
                else
                {
                    throw new Exception(hIKROBOTReturn.message);
                }
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            finally
            {
                _trackloginfoService.AddTrackLog(hIKROBOTTaskContinue, content, "海康AGV继续执行任务", "", hIKROBOTReturn.message);
            }
            return content;
        }
    }
}