1
hutongqing
2024-12-29 89051aef8a2c1a85d457914cf6317fe70e0e321c
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Controllers/AGVController.cs
@@ -1,8 +1,14 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WIDESEA_DTO.Agv;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_Tasks;
@@ -14,12 +20,129 @@
    public class AGVController : ControllerBase
    {
        private readonly IStationMangerRepository _stationMangerRepository;
        private readonly ITaskService _taskService;
        private readonly ITaskRepository _taskRepository;
        public AGVController(IStationMangerRepository stationMangerRepository)
        public AGVController(IStationMangerRepository stationMangerRepository, ITaskService taskService, ITaskRepository taskRepository)
        {
            _stationMangerRepository = stationMangerRepository;
            _taskService = taskService;
            _taskRepository = taskRepository;
        }
        /// <summary>
        /// å®‰å…¨ä¿¡å·ç”³è¯· AGV-WCS
        /// </summary>
        /// <param name="secureApplyModel"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("AgvSecureApply"), AllowAnonymous]
        public AgvResponseContent AgvSecureApply([FromBody] AgvSecureApplyDTO secureApplyModel)
        {
            AgvResponseContent agvResponseContent = new AgvResponseContent();
            agvResponseContent.ReqCode = secureApplyModel.ReqCode;
            try
            {
                var task = _taskRepository.QueryFirst(x => secureApplyModel.TaskCode.ObjToInt() == x.TaskNum);
                if (task == null) throw new Exception("未找到任务");
                if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt())
                {
                    var content = TakeRequest(task.CurrentAddress);
                    if (!content.Status)
                    {
                        throw new Exception(content.Message);
                    }
                }
                else
                {
                    var content = PutRequest(task.NextAddress, task.PalletType);
                    if (!content.Status)
                    {
                        throw new Exception(content.Message);
                    }
                }
                task.TaskState = TaskStatusEnum.AGV_WaitToExecute.ObjToInt();
                var up = _taskRepository.UpdateData(task);
                agvResponseContent.Code = up ? "0" : "1";
                agvResponseContent.Message = up ? "成功" : "失败";
            }
            catch (Exception ex)
            {
                agvResponseContent.Code = "1";
                agvResponseContent.Message = ex.Message;
            }
            return agvResponseContent;
            //return _taskService.AgvSecureApply(secureApplyModel);
        }
        /// <summary>
        /// AGV任务更新/完成
        /// </summary>
        /// <param name="agvUpdateModel"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("AgvCallback"), AllowAnonymous]
        public AgvResponseContent AgvUpdateTask([FromBody] AgvUpdateDTO agvUpdateModel)
        {
            AgvResponseContent agvResponseContent = new AgvResponseContent();
            try
            {
                if (agvUpdateModel == null) throw new Exception("未获取到请求参数");
                agvResponseContent.ReqCode = agvUpdateModel.ReqCode;
                var task = _taskRepository.QueryFirst(x => agvUpdateModel.TaskCode.ObjToInt() == x.TaskNum);
                if (task == null) throw new Exception("未找到任务");
                switch (agvUpdateModel.Method)
                {
                    case "start":
                        break;
                    case "outbin"://出库根据这个信号判断取货完成
                        if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt())
                        {
                            var content = TakeFinish(task.CurrentAddress);
                            if (!content.Status) throw new Exception(content.Message);
                            task.TaskState = TaskStatusEnum.AGV_Finish.ObjToInt();
                            var up = _taskRepository.DeleteAndMoveIntoHty(task, OperateTypeEnum.自动完成);
                            _taskService.TaskCompleted(task.TaskNum);
                            agvResponseContent.Code = up ? "0" : "1";
                            agvResponseContent.Message = up ? "成功" : "失败";
                            return agvResponseContent;
                        }
                        break;
                    case "end"://入库根据这个信号判断放货完成
                        if (task.TaskType != TaskTypeEnum.Outbound.ObjToInt())
                        {
                            var content = PutFinish(task.NextAddress);
                            if (!content.Status) throw new Exception(content.Message);
                            task.CurrentAddress = task.NextAddress;
                            task.NextAddress = "";
                            task.TaskState = TaskStatusEnum.SC_Execute.ObjToInt();
                            var up = _taskRepository.UpdateData(task);
                            agvResponseContent.Code = up ? "0" : "1";
                            agvResponseContent.Message = up ? "成功" : "失败";
                            return agvResponseContent;
                        }
                        break;
                    case "cancel":
                        task.TaskState = TaskStatusEnum.Cancel.ObjToInt();
                        _taskRepository.UpdateData(task);
                        break;
                    default:
                        throw new Exception($"未定义方法名【{agvUpdateModel.Method}】");
                }
                agvResponseContent.Code = "0";
                agvResponseContent.Message = "成功";
            }
            catch (Exception ex)
            {
                agvResponseContent.Code = "1";
                agvResponseContent.Message = ex.Message;
            }
            return agvResponseContent;
            //return _taskService.AgvUpdateTask(agvUpdateModel);
        }
        /// <summary>
        /// æ”¾è´§è¯·æ±‚
        /// </summary>
        /// <param name="code"></param>
        /// <param name="palletType"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("PutRequest"), AllowAnonymous]
        public WebResponseContent PutRequest(string code, int palletType)
        {
@@ -46,7 +169,6 @@
                else
                {
                    otherDevice.SetValue(GroundStationDBName.W_PutRequest, true, stationManger.StationCode);
                    otherDevice.SetValue(GroundStationDBName.W_PutPalletType, (short)palletType, stationManger.StationCode);
                    Thread.Sleep(1000);
                    canPut = otherDevice.GetValue<GroundStationDBName, bool>(GroundStationDBName.R_IsCanPut, stationManger.StationCode);
                    if (canPut)
@@ -65,6 +187,12 @@
            }
        }
        //[HttpPost, HttpGet, Route("PutFinish"), AllowAnonymous]
        /// <summary>
        /// æ”¾è´§å®Œæˆ
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("PutFinish"), AllowAnonymous]
        public WebResponseContent PutFinish(string code)
        {
@@ -92,6 +220,12 @@
            }
        }
        //[HttpPost, HttpGet, Route("TakeRequest"), AllowAnonymous]
        /// <summary>
        /// å–货请求
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("TakeRequest"), AllowAnonymous]
        public WebResponseContent TakeRequest(string code)
        {
@@ -136,6 +270,12 @@
            }
        }
        //[HttpPost, HttpGet, Route("TakeFinish"), AllowAnonymous]
        /// <summary>
        /// å–货完成
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost, HttpGet, Route("TakeFinish"), AllowAnonymous]
        public WebResponseContent TakeFinish(string code)
        {