AcsWebApiController.cs 15.4 KB
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.Model.PLC;
using RCS.Model.WebApi.Request;
using RCS.Model.WebApi.Respose;
using RCS.WinClient.Common;
using RCS.WinClient.Service;


namespace RCS.WinClient.WebApi.Service
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class AcsWebApiController : ControllerBase
    {
        /// <summary>
        /// 上层系统下发
        /// </summary>
        /// <param name="fTask"></param>
        /// <returns></returns>
        [HttpPost]
        public BllResult TaskAssign([FromBody] ReqTaskAssign fTask)
        {
            try
            {
                if (fTask != null)
                {
                    string json = JsonConvert.SerializeObject(fTask);
                    App.ExFile.MessageWebApi("TaskAssign", "接口(TaskAssign)," + json + "\r");
                    if (string.IsNullOrEmpty(fTask.TaskNo))
                    {
                        return BllResult.Error("任务号字段不允许为空!");
                    }

                    Base_Station? startStation;
                    Base_Station? endStation;


                    startStation = App.StationList.Find(t => t.Code == fTask.fromPort);
                    endStation = App.StationList.Find(t => t.Code == fTask.toPort);

                    var fromPoint = App.PointList.FirstOrDefault(x => x.Barcode == startStation?.Barcode);
                    var toPoint = App.PointList.FirstOrDefault(x => x.Barcode == endStation?.Barcode);
                    if (startStation == null)
                    {
                        return BllResult.Error("任务起点站台编码不存在!");
                    }
                    if (endStation == null)
                    {
                        var endBarcode = App.LoctionList.Find(x => x.LoctionCode == fTask.toPort)?.LoctionBarcode;
                        if (string.IsNullOrEmpty(endBarcode))
                            return BllResult.Error("任务终点站台编码不存在!");
                        toPoint = App.PointList.Find(x => x.Barcode == endBarcode);
                        if (toPoint == null)
                        {
                            return BllResult.Error("任务终点站台编码不存在!");
                        }
                        if (!ComnMethod.IsGroundPoint(toPoint))
                        {
                            return BllResult.Error("终点站台对应点位不是地面库!");
                        }
                    }


                    if (fromPoint == null || toPoint == null)
                    {
                        return BllResult.Error($"货架【{fTask.fromPort}】和【{fTask.toPort}】,配置的点位【{startStation.Barcode}】【{endStation?.Barcode}】在点位列表中不存在!");
                    }
                    var taskType = TaskManage.GetTaskType(fromPoint, toPoint);
                    if (taskType.IsNullOrEmpty())
                    {
                        return BllResult.Error("任务类型获取异常");
                    }
                    var taskGroup = EnumMsg.TaskGroup.WMS任务.ToString();
                    var result = TaskManage.TaskBuild(taskType, taskGroup, fTask.containerCode, fTask.priority, "", fromPoint.Barcode, toPoint.Barcode, fTask.TaskNo, startStation.Name, endStation?.Name ?? toPoint.Barcode);//startStation?.Name, endStation?.Name);
                    if (!result.IsSuccess)
                        return BllResult.Error($"创建{fTask.TaskNo}任务失败!,原因:{result.Message}");
                }
                else
                {
                    return BllResult.Error("未获取到TaskAssign函数的参数,请检查Json数据");
                }
            }
            catch (Exception ex)
            {
                var msg = $"接口【TaskAssign】发生异常,原因:{ex.Message},堆栈:{ex.StackTrace}";
                App.ExFile.MessageWebApi("AcsWebApiError", msg);
                return BllResult.Error(msg);
            }
            return BllResult.Success("建立成功");
        }

        /// <summary>
        /// 任务删除
        /// </summary>
        /// <param name="taskCancel"></param>
        /// <returns></returns>
        [HttpPost]
        public BllResult TaskCancel([FromBody] ReqTaskCancel taskCancel)
        {
            try
            {
                if (taskCancel != null)
                {
                    string json = JsonConvert.SerializeObject(taskCancel);
                    App.ExFile.MessageWebApi("TaskCancel", $"接口(TaskCancel),{json} \r");
                    var strResult = TaskManage.TaskDelete(taskCancel.TaskNo, -1);
                    return strResult;
                }
                else
                {
                    return BllResult.Error($"未获取到TaskCancel函数的数据,请检查Json数据");
                }
            }
            catch (Exception ex)
            {
                var msg = $"接口【TaskCancel】发生异常,原因:{ex.StackTrace},堆栈:{ex.StackTrace}";
                App.ExFile.MessageWebApi("AcsWebApiError", msg);
                return BllResult.Error(msg);
            }
        }

        /// <summary>
        /// 任务确认完成
        /// </summary>
        /// <param name="taskFinish"></param>
        /// <returns></returns>
        [HttpPost]
        public BllResult TaskFinish([FromBody] ReqTaskFinish taskFinish)
        {
            try
            {
                if (taskFinish != null)
                {
                    string json = JsonConvert.SerializeObject(taskFinish);
                    App.ExFile.MessageWebApi("TaskFinish", $"接口(TaskFinish),{json} \r");
                    var result = App.TaskList.FirstOrDefault(a => a.TaskPallet == taskFinish.PalletNo);
                    if (result == null)
                    {
                        return BllResult.Error("没有当前托盘号任务!");
                    }
                }
                else
                {
                    return BllResult.Error($"未获取到TaskFinish函数的数据,请检查Json数据");
                }
            }
            catch (Exception ex)
            {
                var msg = $"接口【TaskFinish】发生异常,原因:{ex.StackTrace},堆栈:{ex.StackTrace}";
                App.ExFile.MessageWebApi("AcsWebApiError", msg);
                return BllResult.Error(msg);
            }
            return BllResult.Success($"确认成功");
        }

        /// <summary>
        /// AGV信息查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public BllResult<IEnumerable<object>> AgvInfoList()
        {
            try
            {

                var agvInfoList = App.AgvList.Select(_agv => new
                {
                    AgvNumber = _agv.AgvNo,
                    TaskNo = _agv.AgvTask?.TaskNo,
                    Landmark = _agv.Barcode,
                    StartLandmark = _agv.AgvTask?.SubTaskList == null ? null : _agv.AgvTask?.SubTaskList.FirstOrDefault()?.StartBarcode,
                    EndLandmark = _agv.AgvTask?.SubTaskList == null ? null : _agv.AgvTask?.SubTaskList.FirstOrDefault()?.StartBarcode,
                    Status = _agv.AgvState.ToString(),
                    Speed = _agv.Speed,
                    Battery = _agv.Voltage


                });

                return BllResult<IEnumerable<object>>.Success(agvInfoList);
            }
            catch (Exception ex)
            {
                var msg = $"接口【AgvInfoList】发生异常,原因:{ex.StackTrace},堆栈:{ex.StackTrace}";
                App.ExFile.MessageWebApi("AgvInfoList", msg);
                return BllResult<IEnumerable<object>>.Error(msg);
            }
        }

        /// <summary>
        /// 继续任务
        /// </summary>
        /// <param name="taskFinish"></param>
        /// <returns></returns>
        [HttpPost]
        public BllResult ContinueTask([FromBody] ReqContinueTask continueTask)
        {
            try
            {
                if (continueTask != null)
                {
                    string json = JsonConvert.SerializeObject(continueTask);
                    App.ExFile.MessageWebApi("AcsWebApiMsg", $"接口(ContinueTask),{json} \r");
                    //获取任务号所属任务
                    var task = App.TaskList.FirstOrDefault(a => a.TaskNo == continueTask.TaskNo);
                    if (task == null) { return BllResult.Error($"未获取到任务"); }
                    var agv = App.AgvList.FirstOrDefault(a => a.AgvNo == task.TaskAgvNo);
                    var sonTask = task.SubTaskList[0];
                    if (continueTask.TaskStatus != (int)sonTask.RequestType)
                    {
                        return BllResult.Error($"中控返回任务状态和AGV当前状态不匹配");
                    }

                    //if (task.SubTaskList.Count() == 1 && sonTask.TaskRequestType == EnumMsg.TaskRequestType.等待MES回复已经出来终点光栅)
                    //{
                    //    sonTask.TaskRequestType = EnumMsg.TaskRequestType.任务完成信号;
                    //    sonTask.SubTaskState = EnumMsg.SubTaskState.动作完成;
                    //}
                    //else
                    //{
                    //    sonTask.TaskRequestType = EnumMsg.TaskRequestType.信号发送完成;
                    //}
                }
                else
                {
                    return BllResult.Error($"未获取到ContinueTask函数的数据,请检查Json数据");
                }
            }
            catch (Exception ex)
            {
                var msg = $"接口【ContinueTask】发生异常,原因:{ex.StackTrace},堆栈:{ex.StackTrace}";
                App.ExFile.MessageWebApi("AcsWebApiError", msg);
                return BllResult.Error(msg);
            }
            return BllResult.Success($"确认成功");
        }

        [HttpPost]
        public BllResult PlaceholderTask()
        {
            try
            {
                List<ResPlaceholderTask> Rpt = new List<ResPlaceholderTask>();

                var equipmentProps = App.EquipmentList.Where(t => t.Type == EnumPLC.EquipmentType.HuaHengStation.ToString())
                                                          //.SelectMany(t => t.EquipmentProps)
                                                          //.Where(t => t.Code == EnumPLC.PLCHuaHengProps.Placeholder.ToString())
                                                          .ToList();
                //1-true有货,0-false无货
                foreach (var equipmentProp in equipmentProps)
                {
                    ResPlaceholderTask content = new ResPlaceholderTask();
                    //content.StationCode = equipmentProp.Equipment.Code;
                    //content.Placeholder = equipmentProp.Value;
                    Rpt.Add(content);
                }
                return BllResult.Success(Rpt, "发送成功");
            }
            catch (Exception ex)
            {
                var msg = $"接口【PlaceholderTask】发生异常,原因:{ex.StackTrace},堆栈:{ex.StackTrace}";
                App.ExFile.MessageWebApi("AcsWebApiError", msg);
                return BllResult.Error(msg);
            }
        }



        #region 兼容JavaRcs接口
        /// <summary>
        /// 上层系统下发
        /// </summary>
        /// <param name="fTask"></param>
        /// <returns></returns>
        [HttpPost]
        public BllResult CreateAGVTask([FromBody] ReqTaskAssign fTask)
        {
            try
            {
                if (fTask != null)
                {
                    string json = JsonConvert.SerializeObject(fTask);
                    App.ExFile.MessageWebApi("TaskAssign", "接口(TaskAssign)," + json + "\r");
                    if (string.IsNullOrEmpty(fTask.TaskNo))
                    {
                        return BllResult.Error("任务号字段不允许为空!");
                    }

                    Base_Station? startStation;
                    Base_Station? endStation;


                    startStation = App.StationList.Find(t => t.Code == fTask.fromPort);
                    endStation = App.StationList.Find(t => t.Code == fTask.toPort);

                    var fromPoint = App.PointList.FirstOrDefault(x => x.Barcode == startStation?.Barcode);
                    var toPoint = App.PointList.FirstOrDefault(x => x.Barcode == endStation?.Barcode);
                    if (startStation == null)
                    {
                        return BllResult.Error("任务起点站台编码不存在!");
                    }
                    if (endStation == null)
                    {
                        return BllResult.Error("任务终点站台编码不存在!");
                    }


                    if (fromPoint == null || toPoint == null)
                    {
                        return BllResult.Error($"货架【{fTask.fromPort}】和【{fTask.toPort}】,配置的点位【{startStation.Barcode}】【{endStation.Barcode}】在点位列表中不存在!");
                    }
                    var taskType = TaskManage.GetTaskType(fromPoint, toPoint);
                    var taskGroup = EnumMsg.TaskGroup.WMS任务.ToString();
                    var result = TaskManage.TaskBuild(taskType, taskGroup, fTask.containerCode, fTask.priority, "", fromPoint.Barcode, toPoint.Barcode, fTask.TaskNo, startStation.Name, endStation?.Name ?? toPoint.Barcode);//startStation?.Name, endStation?.Name);
                    if (!result.IsSuccess)
                        return BllResult.Error($"创建{fTask.TaskNo}任务失败!,原因:{result.Message}");
                }
                else
                {
                    return BllResult.Error("未获取到TaskAssign函数的参数,请检查Json数据");
                }
            }
            catch (Exception ex)
            {
                var msg = $"接口【TaskAssign】发生异常,原因:{ex.Message},堆栈:{ex.StackTrace}";
                App.ExFile.MessageWebApi("AcsWebApiError", msg);
                return BllResult.Error(msg);
            }
            return BllResult.Success("建立成功");
        }

        /// <summary>
        /// 任务删除
        /// </summary>
        /// <param name="taskCancel"></param>
        /// <returns></returns>
        [HttpPost]
        public BllResult CancelAGVTask([FromBody] ReqTaskCancel taskCancel)
        {
            try
            {
                if (taskCancel != null)
                {
                    string json = JsonConvert.SerializeObject(taskCancel);
                    App.ExFile.MessageWebApi("TaskCancel", $"接口(TaskCancel),{json} \r");
                    var strResult = TaskManage.TaskDelete(taskCancel.TaskNo);
                    return strResult;
                }
                else
                {
                    return BllResult.Error($"未获取到TaskCancel函数的数据,请检查Json数据");
                }
            }
            catch (Exception ex)
            {
                var msg = $"接口【TaskCancel】发生异常,原因:{ex.StackTrace},堆栈:{ex.StackTrace}";
                App.ExFile.MessageWebApi("AcsWebApiError", msg);
                return BllResult.Error(msg);
            }
        }



        #endregion
    }
}