XiangdianStockerExcute.cs 11.8 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HHECS.Bll;
using HHECS.Model;
using HHECS.OPC;
using S7.Net;

namespace HHECS.Common
{
    /// <summary>
    /// todo:湘电单叉双伸位堆垛机处理函数
    /// </summary>
    public class XiangdianStockerExcute : IStockerExcute
    {

        #region 接口实现 
        /// <summary>
        /// 标记这个堆垛机处理类所处理的设备类型
        /// </summary>
        public EquipmentType EquipmentType { get; set; }

        public BllResult Excute(List<Equipment> stockers, List<Plc> plcs)
        {
            try
            {
                foreach (var stocker in stockers)
                {
                    //一般根据堆垛机的位置来决定是否执行入库任务 hack:这里默认堆垛机在站台旁时列为0
                    if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "CurrentColumn").Value == "0")
                    {
                        //先处理入库
                        ExcuteTaskIn(stocker, plcs.Find(t => t.IP == stocker.IP));
                    }
                    //处理出库
                    ExcuteTaskOut(stocker, plcs.Find(t => t.IP == stocker.IP));
                    //先处理入库
                    ExcuteTaskIn(stocker, plcs.Find(t => t.IP == stocker.IP));
                    //处理巷道内移库
                    ExcuteTaskTransferInRoadway(stocker, plcs.Find(t => t.IP == stocker.IP));
                    //处理堆垛机任务完成
                    ExcuteTaskComplete(stocker, plcs.Find(t => t.IP == stocker.IP));
                    //任务下发错误处理
                    ExcuteTaskError(stocker, plcs.Find(t => t.IP == stocker.IP));
                }
                return BllResultFactory.Sucess();
            }
            catch (Exception ex)
            {
                Logger.Log($"堆垛机处理过程中出现异常:{ex.Message}", LogLevel.Exception);
                return BllResultFactory.Error();
            }
        }

        public BllResult ExcuteTaskError(Equipment stocker, Plc plc)
        {
            throw new NotImplementedException();
        }

        public BllResult ExcuteTaskComplete(Equipment stocker, Plc plc)
        {
            throw new NotImplementedException();
        }

        public BllResult ExcuteTaskIn(Equipment stocker, Plc plc)
        {
            try
            {
                //校验入库条件
                if (ValidateStockerIn(stocker).Success)
                {
                    //找出入库状态的任务
                    var tasksResult = AppSession.Bll.GetCommonModelByCondition<TaskEntity>($"where status = {(int)TaskEntityStatus.响应接入口位置到达}");
                    if (tasksResult.Success)
                    {
                        //找出同巷道的任务
                        var locationsResult = AppSession.Bll.GetAllLocations(null, null, null, null, stocker.RoadWay, null, null);
                        if (locationsResult.Success)
                        {
                            var task = tasksResult.Data.FirstOrDefault(t => locationsResult.Data.Count(a => a.Code == t.DestinationLocation) > 0);
                            if (task != null)
                            {
                                var location = locationsResult.Data.Find(t => t.Code == task.DestinationLocation);
                                //todo:说明找到了则下发
                                BllResult sendResult = SendTaskToStocker(stocker,plc,"1","3","0","0","0",task.Station.ToString(),task.Id.ToString());
                                if (sendResult.Success)
                                {
                                    //更改任务状态

                                    //手动标记堆垛机为不可用状态,以免被再次响应

                                }
                                else
                                {
                                    //记录日志
                                }
                            }
                        }
                    }
                }
                return BllResultFactory.Sucess();
            }
            catch (Exception ex)
            {
                Logger.Log($"堆垛机{stocker.Name}处理[入库任务]过程中出现异常:{ex.Message}", LogLevel.Exception);
                return BllResultFactory.Error();
            }
        }

        private BllResult SendTaskToStocker(Equipment stocker,Plc plc, string forkAction,string forkTaskFlag,string forkRow,string forkColumn,string forkLayer,string forkStation,string taskNo)
        {
            try
            {
                List<EquipmentProp> propsToWriter = new List<EquipmentProp>();
                var props = stocker.EquipmentProps;
                var action = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSForkAction");
                action.Value = forkAction;
                var taskFlag = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag");
                taskFlag.Value = forkTaskFlag;
                var row = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Row");
                row.Value = forkRow;
                var column = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Column");
                column.Value = forkColumn;
                var layer = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Layer");
                layer.Value = forkLayer;
                var station = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Station");
                station.Value = forkStation;
                var task = props.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1Task");
                task.Value = taskNo;
                propsToWriter.AddRange(new List<EquipmentProp>() { action, taskFlag,row,column,layer,station,task });
                return S7Helper.PlcSplitWrite(plc, propsToWriter, 20);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"下发任务出现异常:{ex.Message}");
            }
        }

        public BllResult ExcuteTaskOut(Equipment stocker, Plc plc)
        {
            throw new NotImplementedException();
        }

        public BllResult ExcuteTaskTransferInRoadway(Equipment stocker, Plc plc)
        {
            throw new NotImplementedException();
        }

        public BllResult ValidateStockerForCompleteTask(Equipment stocker)
        {
            throw new NotImplementedException();
        }

        public BllResult ValidateStockerIn(Equipment stocker)
        {
            try
            {
                //判断条件,这里没有判断货叉故障和堆垛机故障,这特么怎么判断,这么多字段
                //1.操作模式 = 5  联机;
                //2.双货叉原位,任务货叉上无货;
                //3.任务货叉无故障无任务;
                //4.堆垛机无故障
                //5.对应货叉任务类型:0 - 无任务
                //6.对应货叉任务标志:1待机
                //WCS交换区无任务
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == "5" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "False" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskType").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == "1"&& stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == "0")
                {
                    return BllResultFactory.Sucess();
                }
                else
                {
                    return BllResultFactory.Error();
                }
            }
            catch (Exception ex)
            {
                Logger.Log($"校验堆垛机{stocker.Name}入库条件异常:{ex.Message}", LogLevel.Exception);
                return BllResultFactory.Error();
            }
        }

        public BllResult ValidateStockerOut(Equipment stocker)
        {
            try
            {
                //判断条件,这里没有判断货叉故障和堆垛机故障,这特么怎么判断,这么多字段
                //1.操作模式:5  联机;
                //2.任务限制:不等于3; 
                //3.双货叉原位,任务货叉上有货;
                //4.任务货叉无故障无任务;
                //5.堆垛机无故障
                //5.对应货叉任务类型:0 - 无任务
                //6.对应货叉任务标志:1待机
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == "5" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "False" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskType").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == "1" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "TaskLimit").Value != "3"&& stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == "0")
                {
                    return BllResultFactory.Sucess();
                }
                else
                {
                    return BllResultFactory.Error();
                }

            }
            catch (Exception ex)
            {
                Logger.Log($"校验堆垛机{stocker.Name}出库条件异常:{ex.Message}", LogLevel.Exception);
                return BllResultFactory.Error();
            }
        }

        public BllResult ValidateStockerTransferInRoadway(Equipment stocker)
        {
            try
            {
                //这个同巷道内的移库任务,判断条件与出库一致
                //判断条件,这里没有判断货叉故障和堆垛机故障,这特么怎么判断,这么多字段
                //1.操作模式:5  联机;
                //2.任务限制:不等于3; 
                //3.双货叉原位,任务货叉上有货;
                //4.任务货叉无故障无任务;
                //5.堆垛机无故障
                //5.对应货叉任务类型:0 - 无任务
                //6.对应货叉任务标志:1待机
                if (stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "OperationModel").Value == "5" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1ForkHasPallet").Value == "False" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskNo").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskType").Value == "0" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "Fork1TaskExcuteStatus").Value == "1" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "TaskLimit").Value != "3" && stocker.EquipmentProps.Find(t => t.EquipmentTypePropTemplateCode == "WCSFork1TaskFlag").Value == "0")
                {
                    return BllResultFactory.Sucess();
                }
                else
                {
                    return BllResultFactory.Error();
                }

            }
            catch (Exception ex)
            {
                Logger.Log($"校验堆垛机{stocker.Name}移库条件异常:{ex.Message}", LogLevel.Exception);
                return BllResultFactory.Error();
            }
        } 
        #endregion
    }
}